当前位置:网站首页>Breadth first traversal based on adjacency table
Breadth first traversal based on adjacency table
2022-06-26 02:10:00 【chengqiuming】
One Diagram to be created

Two Code
package graph;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class BFSAL {
static final int MaxVnum = 100; // Maximum number of vertices
// Access flag array , Its initial value is "false"
static Boolean visited[] = new Boolean[MaxVnum];
static {
for (int i = 0; i < visited.length; i++) {
visited[i] = false;
}
}
static int locatevex(BFSAL.ALGraph G, char x) {
for (int i = 0; i < G.vexnum; i++) // Find the subscript of vertex information
if (x == G.Vex[i].data)
return i;
return -1; // Did not find
}
// Insert an edge
static void insertedge(BFSAL.ALGraph G, int i, int j) {
graph.AdjNode s = new graph.AdjNode();
s.v = j;
s.next = G.Vex[i].first;
G.Vex[i].first = s;
}
// Create a directed graph adjacency table
static void CreateALGraph(BFSAL.ALGraph G) {
int i, j;
char u, v;
System.out.println(" Please enter the number of vertices and edges :");
Scanner scanner = new Scanner(System.in);
G.vexnum = scanner.nextInt();
G.edgenum = scanner.nextInt();
System.out.println(" Please enter vertex information :");
for (i = 0; i < G.vexnum; i++)// Enter vertex information , Store vertex information array
G.Vex[i].data = scanner.next().charAt(0);
for (i = 0; i < G.vexnum; i++)
G.Vex[i].first = null;
System.out.println(" Please enter two vertices of each edge in turn u,v");
while (G.edgenum-- > 0) {
u = scanner.next().charAt(0);
v = scanner.next().charAt(0);
i = locatevex(G, u); // Find vertices u Storage subscript for
j = locatevex(G, v); // Find vertices v Storage subscript for
if (i != -1 && j != -1)
insertedge(G, i, j);
else {
System.out.println(" Input vertex information error ! Please re-enter !");
G.edgenum++; // This input does not count
}
}
}
// Output adjacency table
static void printg(BFSAL.ALGraph G) {
System.out.println("---------- The adjacency table is as follows :----------");
for (int i = 0; i < G.vexnum; i++) {
graph.AdjNode t = G.Vex[i].first;
System.out.print(G.Vex[i].data + ": ");
while (t != null) {
System.out.print("[" + t.v + "]\t");
t = t.next;
}
System.out.println();
}
}
// Breadth first traversal based on adjacency table
static void BFS_AL(ALGraph G, int v) {
int u, w;
graph.AdjNode p;
// Create a normal queue ( fifo ), Inside the store int type
Queue<Integer> Q = new LinkedList<>();
System.out.println(G.Vex[v].data + "\t");
visited[v] = true;
Q.add(v); // Source point v The team
// If the queue is not empty
while (!Q.isEmpty()) {
u = Q.peek(); // Take out the team head element and assign it to u
Q.poll(); // Team leader element out of the team
p = G.Vex[u].first;
while (p != null) { // Check in turn u All the adjacency points of
w = p.v; // w by u The adjacency point
if (!visited[w]) { // w Not visited
System.out.println(G.Vex[w].data + "\t");
visited[w] = true;
Q.add(w);
}
p = p.next;
}
}
}
public static void main(String[] args) {
BFSAL.ALGraph G = new BFSAL.ALGraph();
for (int i = 0; i < G.Vex.length; i++) {
G.Vex[i] = new graph.VexNode();
}
CreateALGraph(G); // Create a directed graph adjacency table
printg(G); // Output adjacency table
System.out.println(" Please enter the starting point of the traversal graph :");
Scanner scanner = new Scanner(System.in);
char c = scanner.next().charAt(0);
int v = locatevex(G, c);// Find vertices u Storage subscript for
if (v != -1) {
System.out.println(" Breadth first search traversal graph results :");
BFS_AL(G, v);
} else
System.out.println(" Input vertex information error ! Please re-enter !");
}
// Define the adjacent contact type
static class AdjNode {
int v; // Adjacency subscript
graph.AdjNode next; // Point to the next adjacent point
}
// Define vertex types
static class VexNode {
char data; // VexType Is the data type of the vertex , Defined as needed
graph.AdjNode first; // Point to the first adjacent contact
}
// Define adjacency table types
static class ALGraph {
graph.VexNode Vex[] = new graph.VexNode[CreateALGraph.MaxVnum];
int vexnum; // Number of vertices
int edgenum; // Number of edges
}
}3、 ... and test
Green is the input , White is the output

边栏推荐
猜你喜欢

Abnova actn4 DNA probe solution

Implementation of image binary morphological filtering based on FPGA -- Corrosion swelling

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

Differences and functions of TOS cos DSCP

Abnova anti GBA monoclonal antibody solution

Interface test case design

一分钟了解同步、异步、阻塞和非阻塞的区别

Disruptor (I) sequence

基于邻接矩阵的广度优先遍历

OA process editing
随机推荐
Ardiuno智能电蚊拍
V4L2+QT视频优化策略
vscode调试时提示更新到最新调试版本
Meaning of each state in TCP network communication
Connecting the projector
regular expression
深度好文:什么是超网 Supernetting?
记录一个诡异的图片上传问题
How to efficiently complete daily tasks?
[JS] free API to judge holidays, working days, Saturdays and Sundays
Abnova CMV CISH probe solution
将weishi相机图片进行转换
工作一年闲记
shell学习记录(二)
标定。。。
购买了fastadmin小程序助手但是问题工单中无法发布工单
Record a weird picture upload problem
vs2015+PCL1.8.1+qt5.12-----(1)
Introduction to gun make (1)
Weishi camera display