当前位置:网站首页>Implementation of depth first traversal based on adjacency matrix
Implementation of depth first traversal based on adjacency matrix
2022-06-26 02:10:00 【chengqiuming】
One Diagram to create

Two Code
package graph;
import java.util.Scanner;
public class DFSAM {
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(DFSAM.AMGraph G, char x) {
for (int i = 0; i < G.vexnum; i++) // Find the subscript of vertex information
if (x == G.Vex[i])
return i;
return -1; // Did not find
}
static void CreateAMGraph(DFSAM.AMGraph G) {
Scanner scanner = new Scanner(System.in);
int i, j;
char u, v;
System.out.println(" Please enter the number of vertices :");
G.vexnum = scanner.nextInt();
System.out.println(" Please enter the number of sides :");
G.edgenum = scanner.nextInt();
System.out.println(" Please enter vertex information :");
// Enter vertex information , Store vertex information array
for (int k = 0; k < G.vexnum; k++) {
G.Vex[k] = scanner.next().charAt(0);
}
// Initialize all values of adjacency matrix as 0, If it's a net , Then the initial adjacency matrix is infinite
for (int m = 0; m < G.vexnum; m++)
for (int n = 0; n < G.vexnum; n++)
G.Edge[m][n] = 0;
System.out.println(" Please enter two vertices attached to each edge :");
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)
G.Edge[i][j] = 1; // Adjacency matrix storage 1
else {
System.out.println(" Input vertex information error ! Please re-enter !");
G.edgenum++; // This input does not count
}
}
}
static void print(DFSAM.AMGraph G) { // Output adjacency matrix
System.out.println(" The adjacency matrix of the graph is :");
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) {// Depth first traversal based on adjacency matrix
int w;
System.out.println(G.Vex[v] + "\t");
visited[v] = true;
for (w = 0; w < G.vexnum; w++) // Check in turn v All the adjacency points of
if (G.Edge[v][w] == 1 && !visited[w]) // v、w Adjacency and w Not visited
dfsAm(G, w); // from w Vertex start recursive depth first traversal
}
public static void main(String[] args) {
int v;
char c;
AMGraph G = new DFSAM.AMGraph();
CreateAMGraph(G);
print(G);
System.out.println(" Please enter the starting point of traversing the connected graph :");
Scanner scanner = new Scanner(System.in);
c = scanner.next().charAt(0);
v = locatevex(G, c); // Find vertices u Storage subscript for
if (v != -1) {
System.out.println(" Depth first search traversal connected graph results :");
dfsAm(G, v);
} else
System.out.println(" Input vertex information error ! Please re-enter !");
}
static class AMGraph {
char Vex[] = new char[CreateAMGraph.MaxVnum];
int Edge[][] = new int[CreateAMGraph.MaxVnum][CreateAMGraph.MaxVnum];
int vexnum; // Number of vertices
int edgenum; // Number of edges
}
}3、 ... and test
Green is the input , White is the output

边栏推荐
猜你喜欢
随机推荐
如何高效的完成每日的任务?
【无标题】vsbiji esp....32
General introduction to gun make (2)
regular expression
Markov decision process (MDP): gambler problem
shell学习记录(一)
Computer shortcut keys commonly used by experts
Abnova anti GBA monoclonal antibody solution
树莓派 + AWS IoT 入门实验
Create OpenGL window
win32
Jenkins localization and its invalid solution
【js】免费api判断节假日、工作日和周六日
Codecraft-17 and Codeforces Round #391 (Div. 1 + Div. 2, combined) C. Felicity is Coming!
shell学习记录(二)
跨域问题的一种解决方案
高手常用的电脑快捷键
Interface test case design
安装了Visual Studio 2013 Redistributable,mysql还是安装失败
标定。。。








