当前位置:网站首页>基于邻接矩阵的深度优先遍历实现
基于邻接矩阵的深度优先遍历实现
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; // 边数
}
}三 测试
绿色为输入,白色为输出

边栏推荐
- SDRAM控制器——添加读写FIFO
- NDK20b FFmpeg4.2.2 编译和集成
- Visual studio 2013 redistributable is installed, but MySQL installation fails
- 深度好文:什么是超网 Supernetting?
- Differences and functions of export set env in makefile
- qtvtkvs2015测试代码
- vs2015+PCL1.8.1+qt5.12-----(1)
- Raspberry pie + AWS IOT introductory experiment
- SDRAM controller -- implementation of arbitration module
- Abnova anti GBA monoclonal antibody solution
猜你喜欢

A lost note for konjaku beginner

cv==biaoding---open----cv001

About vs scanf, 'scanf' appears: this function or variable may be unsafe Solutions to the problem of consumer usi

SDRAM控制器——仲裁模块的实现

Abnova anti GBA monoclonal antibody solution

shell学习记录(二)

Abnova actn4 DNA probe solution

shell学习记录(一)

cv==biaoding---open----cv001

Raspberry pie + AWS IOT Greengrass
随机推荐
深度好文:什么是超网 Supernetting?
轻轻松松理解指针
连接投影仪
Ardiuno智能电蚊拍
socket demo01
Theoretical speed calculation method of WiFi
Tcp网络通信中各个状态的含义
Input 3 integers and output them from large to small
Command of gun make (4) rule
Prometeus 2.33.0 新特性
Application and chemical properties of elastase
Codecraft-17 and Codeforces Round #391 (Div. 1 + Div. 2, combined) D. Felicity‘s Big Secret Revealed
Redis7.0 installation steps
shell学习记录(三)
V4L2+QT视频优化策略
Disruptor(一)Sequence
Abnova actn4 DNA probe solution
SDRAM控制器——添加读写FIFO
Finding the sum of N multiplications
Use of static library and dynamic library