当前位置:网站首页>基於鄰接矩陣的廣度優先遍曆
基於鄰接矩陣的廣度優先遍曆
2022-06-26 02:10:00 【chengqiuming】
一 需要創建的圖

二 代碼
package graph;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class BFSAM {
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(BFSAM.AMGraph G, char x) {
for (int i = 0; i < G.vexnum; i++) // 查找頂點信息的下標
if (x == G.Vex[i])
return i;
return -1; // 沒找到
}
static void CreateAMGraph(BFSAM.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(BFSAM.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 BFS_AM(AMGraph G, int v) {
int u, w;
// 創建一個普通隊列(先進先出),裏面存放int類型
Queue<Integer> Q = new LinkedList<>();
System.out.println(G.Vex[v] + "\t");
visited[v] = true;
Q.add(v); // 源點 v 入隊
while (!Q.isEmpty()) { //如果隊列不空
u = Q.peek(); // 取出隊頭元素賦值給u
Q.poll(); // 隊頭元素出隊
for (w = 0; w < G.vexnum; w++) {// 依次檢查 u 的所有鄰接點
if (G.Edge[u][w] == 1 && !visited[w]) { // u、w 鄰接而且 w 未被訪問
System.out.println(G.Vex[w] + "\t");
visited[w] = true;
Q.add(w);
}
}
}
}
public static void main(String[] args) {
int v;
char c;
AMGraph G = new BFSAM.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("廣度優先搜索遍曆圖結果:");
BFS_AM(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; // 邊數
}
}
三 測試

边栏推荐
猜你喜欢

Abnova anti GBA monoclonal antibody solution

Analytic hierarchy process

Eureka注册信息配置备忘

Jenkins localization and its invalid solution

安装了Visual Studio 2013 Redistributable,mysql还是安装失败

vs2015+PCL1.8.1+qt5.12-----(1)

树莓派 + AWS IoT 入门实验

CVPR2022 | 长期行动预期的Future Transformer

NDK20b FFmpeg4.2.2 编译和集成

V4l2+qt video optimization strategy
随机推荐
将lua print输出到cocos2d控制台输出窗口中
How to set an achievable annual goal?
其他代码,,vt,,,k
keda 2.7.1 scaledJob 代码简要分析
Redis7.0 installation steps
如何使用命令将文件夹中的文件名(包括路径)写入到txt文件中
论文阅读 Exploring Temporal Information for Dynamic Network Embedding
Characteristics and related specificity of Papain
Shell learning record (I)
Connectez Le projecteur
Abnova CSV monoclonal antibody solution
Find the multiplication order of n
SDRAM控制器——仲裁模块的实现
shell学习记录(三)
Ardiuno智能电蚊拍
Ndk20b ffmpeg4.2.2 compilation and integration
[untitled] vsbiji ESP thirty-two
Chrome浏览器开发者工具使用
vs2015+PCL1.8.1+qt5.12-----(1)
Shell learning record (III)