当前位置:网站首页>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

边栏推荐
猜你喜欢

Abnova anti GBA monoclonal antibody solution

shell学习记录(一)

深度好文:什么是超网 Supernetting?

Qtvtkvs2015 test code

【js】免费api判断节假日、工作日和周六日
![[untitled] vsbiji ESP thirty-two](/img/08/c479031c80d4dfdd8a05d530ae30ba.png)
[untitled] vsbiji ESP thirty-two

论文阅读 Exploring Temporal Information for Dynamic Network Embedding

SDRAM Controller - add read / write FIFO

A lost note for konjaku beginner

Getting to know OpenGL
随机推荐
Differences and functions of export set env in makefile
regular expression
CVPR2022 | 长期行动预期的Future Transformer
Redis linked list
Gun make (5) variables in makefile
Gun make (7) execute make
Snake game
螺旋矩阵
Abnova actn4 DNA probe solution
SQL column value to row value (unpivot)
Jenkins localization and its invalid solution
基于邻接表的广度优先遍历
跨域问题的一种解决方案
LeetCode 31 ~ 40
Raspberry pie + AWS IOT introductory experiment
初识Opengl
【js】免费api判断节假日、工作日和周六日
SDRAM控制器——添加读写FIFO
SDRAM Controller - add read / write FIFO
vscode调试时提示更新到最新调试版本