当前位置:网站首页>基於鄰接矩陣的廣度優先遍曆
基於鄰接矩陣的廣度優先遍曆
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; // 邊數
}
}
三 測試
边栏推荐
- Qtvtkvs2015 test code
- Interface test case design
- shell学习记录(二)
- cv==biaoding---open----cv001
- Cross server SQL connection configuration
- Weishi camera display
- Prompt to update to the latest debug version during vscode debugging
- Analytic hierarchy process
- Record a weird picture upload problem
- Is the securities account recommended by qiniu safe?
猜你喜欢
随机推荐
Prompt to update to the latest debug version during vscode debugging
Other codes,, VT,,, K
深度好文:什么是超网 Supernetting?
Redis-SDS
Record a weird picture upload problem
@Query 疑难杂症
初识Opengl
连接投影仪
SDRAM controller -- implementation of arbitration module
Cross server SQL connection configuration
螺旋矩阵
Characteristics and related specificity of Papain
Connecting the projector
Disruptor (I) sequence
记录一个诡异的图片上传问题
keda 2.7.1 scaledJob 代码简要分析
跨平台应用开发进阶(二十三) :一文走近 testflight 上架
ARM流水线如何提高代码执行效率
Codecraft-17 and Codeforces Round #391 (Div. 1 + Div. 2, combined) D. Felicity‘s Big Secret Revealed
Input 3 integers and output them from large to small