-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathGraph.java
More file actions
89 lines (78 loc) · 1.6 KB
/
Graph.java
File metadata and controls
89 lines (78 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package ch19;
import ch03.MyStack;
/**
* 图
* @author Administrator
*
*/
public class Graph {
//顶点数组
private Vertex[] vertexList;
//邻接矩阵
private int[][] adjMat;
//顶点的最大数目
private int maxSize = 20;
//当前顶点
private int nVertex;
//栈
private MyStack stack;
public Graph() {
vertexList = new Vertex[maxSize];
adjMat = new int[maxSize][maxSize];
for(int i = 0; i < maxSize; i++) {
for(int j = 0; j < maxSize; j++) {
adjMat[i][j] = 0;
}
}
nVertex = 0;
stack = new MyStack();
}
/**
* 添加顶点
*/
public void addVertex(char label) {
vertexList[nVertex++] = new Vertex(label);
}
/**
* 添加边
*/
public void addEdge(int start,int end) {
adjMat[start][end] = 1;
adjMat[end][start] = 1;
}
public int getadjUnvisitedVertex(int v) {
for(int i = 0; i < nVertex; i++) {
if(adjMat[v][i] == 1 && vertexList[i].wasVisited == false) {
return i;
}
}
return -1;
}
public void dfs() {
//首先访问0号顶点
vertexList[0].wasVisited = true;
//显示该顶点
displayVertex(0);
//压入栈中
stack.push(0);
while(!stack.isEmpty()) {
//获得一个未访问过的邻接点
int v = getadjUnvisitedVertex((int)stack.peek());
if(v == -1) {
//弹出一个顶点
stack.pop();
} else {
vertexList[v].wasVisited = true;
displayVertex(v);
stack.push(v);
}
}
//搜索完以后,要将访问信息修改
for(int i = 0; i < nVertex; i++) {
vertexList[i].wasVisited = false;
}
}
public void displayVertex(int v) {
System.out.print(vertexList[v].label);
}
}