当前位置:网站首页>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
边栏推荐
猜你喜欢
General introduction to gun make (2)
shell学习记录(二)
基于邻接表的广度优先遍历
Abnova actn4 DNA probe solution
[untitled] vsbiji ESP thirty-two
Chrome browser developer tool usage
Abnova CSV monoclonal antibody solution
一分钟了解同步、异步、阻塞和非阻塞的区别
Implementation of image binary morphological filtering based on FPGA -- Corrosion swelling
Eureka注册信息配置备忘
随机推荐
基于邻接表的广度优先遍历
How to use commands to write file names (including paths) in folders to txt files
Disruptor (I) sequence
cv==biaoding---open----cv001
如何制定可实现中长期目标?
NDK20b FFmpeg4.2.2 编译和集成
Markov decision process (MDP): Blackjack problem (mc-es)
Codecraft-17 and Codeforces Round #391 (Div. 1 + Div. 2, combined) D. Felicity‘s Big Secret Revealed
如何制定一个可实现的年度目标?
Chrome browser developer tool usage
标定。。。
Redis linked list
Exploring temporary information for dynamic network embedding
Codecraft-17 and Codeforces Round #391 (Div. 1 + Div. 2, combined) C. Felicity is Coming!
Getting to know OpenGL
Ardiuno智能电蚊拍
高手常用的电脑快捷键
Weishi camera display
A lost note for konjaku beginner
Output Lua print to the cocos2d console output window