当前位置:网站首页>基于邻接矩阵的深度优先遍历实现
基于邻接矩阵的深度优先遍历实现
2022-06-26 00:36:00 【chengqiuming】
一 要创建的图

二 代码
package graph;
import java.util.Scanner;
public class DFSAM {
static final int MaxVnum = 100; // 顶点数最大值
// 访问标志数组,其初值为"false"
static Boolean visited[] = new Boolean[MaxVnum];
static {
for (int i = 0; i < visited.length; i++) {
visited[i] = false;
}
}
static int locatevex(DFSAM.AMGraph G, char x) {
for (int i = 0; i < G.vexnum; i++) // 查找顶点信息的下标
if (x == G.Vex[i])
return i;
return -1; // 没找到
}
static void CreateAMGraph(DFSAM.AMGraph G) {
Scanner scanner = new Scanner(System.in);
int i, j;
char u, v;
System.out.println("请输入顶点数:");
G.vexnum = scanner.nextInt();
System.out.println("请输入边数:");
G.edgenum = scanner.nextInt();
System.out.println("请输入顶点信息:");
// 输入顶点信息,存入顶点信息数组
for (int k = 0; k < G.vexnum; k++) {
G.Vex[k] = scanner.next().charAt(0);
}
// 初始化邻接矩阵所有值为0,如果是网,则初始化邻接矩阵为无穷大
for (int m = 0; m < G.vexnum; m++)
for (int n = 0; n < G.vexnum; n++)
G.Edge[m][n] = 0;
System.out.println("请输入每条边依附的两个顶点:");
while (G.edgenum-- > 0) {
u = scanner.next().charAt(0);
v = scanner.next().charAt(0);
i = locatevex(G, u);// 查找顶点 u 的存储下标
j = locatevex(G, v);// 查找顶点 v 的存储下标
if (i != -1 && j != -1)
G.Edge[i][j] = 1; //邻接矩阵储置1
else {
System.out.println("输入顶点信息错!请重新输入!");
G.edgenum++; // 本次输入不算
}
}
}
static void print(DFSAM.AMGraph G) { // 输出邻接矩阵
System.out.println("图的邻接矩阵为:");
for (int i = 0; i < G.vexnum; i++) {
for (int j = 0; j < G.vexnum; j++)
System.out.print(G.Edge[i][j] + "\t");
System.out.println();
}
}
static void dfsAm(AMGraph G, int v) {//基于邻接矩阵的深度优先遍历
int w;
System.out.println(G.Vex[v] + "\t");
visited[v] = true;
for (w = 0; w < G.vexnum; w++) // 依次检查v的所有邻接点
if (G.Edge[v][w] == 1 && !visited[w]) // v、w 邻接而且 w 未被访问
dfsAm(G, w); // 从 w 顶点开始递归深度优先遍历
}
public static void main(String[] args) {
int v;
char c;
AMGraph G = new DFSAM.AMGraph();
CreateAMGraph(G);
print(G);
System.out.println("请输入遍历连通图的起始点:");
Scanner scanner = new Scanner(System.in);
c = scanner.next().charAt(0);
v = locatevex(G, c); // 查找顶点u的存储下标
if (v != -1) {
System.out.println("深度优先搜索遍历连通图结果:");
dfsAm(G, v);
} else
System.out.println("输入顶点信息错!请重新输入!");
}
static class AMGraph {
char Vex[] = new char[CreateAMGraph.MaxVnum];
int Edge[][] = new int[CreateAMGraph.MaxVnum][CreateAMGraph.MaxVnum];
int vexnum; // 顶点数
int edgenum; // 边数
}
}三 测试
绿色为输入,白色为输出

边栏推荐
猜你喜欢

General introduction to gun make (2)

Disruptor(一)Sequence

recvmsg & sendmsg

Tarte aux framboises + AWS IOT Greengrass

Interface test case design
![[untitled] vsbiji ESP thirty-two](/img/08/c479031c80d4dfdd8a05d530ae30ba.png)
[untitled] vsbiji ESP thirty-two

Prompt to update to the latest debug version during vscode debugging

Abnova anti GBA monoclonal antibody solution

NDK20b FFmpeg4.2.2 编译和集成

Redis7.0 installation steps
随机推荐
Interface test case design
Multi type study of Worthington collagen protease
论文阅读 Exploring Temporal Information for Dynamic Network Embedding
Disruptor (I) sequence
Connecting the projector
其他代码,,vt,,,k
ROS2+DDS+RTPS
vscode调试时提示更新到最新调试版本
leetcode 300. Longest Increasing Subsequence 最长递增子序列 (中等)
[untitled] vsbiji ESP thirty-two
连接投影仪
jenkins汉化及汉化无效解决方案
LeetCode 41 ~ 50
LeetCode 41 ~ 50
Differences and functions of export set env in makefile
将weishi相机图片进行转换
Mot - clé C facile à comprendre statique
Computer shortcut keys commonly used by experts
Output Lua print to the cocos2d console output window
Is the securities account recommended by qiniu safe?