/
Graph.java
249 lines (213 loc) · 6.13 KB
/
Graph.java
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*
* Copyright (c) 2018. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
* Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
* Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
* Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
* Vestibulum commodo. Ut rhoncus gravida arcu.
*/
package com.example.god.androidmore.datastructure;
import java.util.LinkedList;
/**
* 图-临接矩阵实现
* 图的两种遍历:深度优先遍历,广度优先遍历
* 图的最小生成树的两种算法:普利姆算法,克鲁斯卡尔算法
* 最短路径:迪杰斯特拉算法
*/
public class Graph {
private int length;
private int[] labels;
private int[][] tables;
private boolean[] isVisited;
static int MAX = 8000;
public Graph(int length) {
this.length = length;
labels = new int[length];
tables = new int[length][length];
isVisited = new boolean[length];
for (int i = 0; i < length; i++) {
labels[i] = i;
}
}
public int[] getVertexs() {
return labels;
}
public void setVertexs(int[] vertexs) {
this.labels = vertexs;
}
//获取某个节点的度
public int getOutDegree(int m) {
int result = 0;
for (int i = 0; i < length; i++) {
if (tables[m][length] != 0 && tables[m][length] != MAX) {
result++;
}
}
return result;
}
//获取某个节点的第一个临接节点
public int getFirstNeighbor(int m) {
for (int i = 0; i < length; i++) {
if (tables[m][length] != 0 && tables[m][length] != MAX) {
return tables[m][length];
}
}
return -1;
}
//根据前一个邻接点的下标来取得下一个邻接点
public int getNextNeighbor(int m, int x) {
for (int i = x; i < length; i++) {
if (tables[m][length] != 0 && tables[m][length] != MAX) {
return tables[m][length];
}
}
return -1;
}
// 图的深度优先遍历算法:深度优先遍历一个节点
private void depthFirstSearch(int m) {
isVisited[m] = true;
int x = getFirstNeighbor(m);
while (x != -1) {
if (!isVisited[x]) {
System.out.print("遍历到" + x);
depthFirstSearch(x);
}
x = getNextNeighbor(m, x);
}
}
// 遍历所有节点,防止出现断掉的顶点未遍历,
// 然后重置isVisited数组以备下次遍历
public void depthFirstSearch() {
isVisited = new boolean[length];
for (int i = 0; i < length; i++) {
if (!isVisited[i]) {
System.out.print("遍历到" + i);
depthFirstSearch(i);
}
}
isVisited = new boolean[length];
}
//实现广度优先遍历:
public void broadFirstSearchT(int m) {
LinkedList<Object> myQueue = new LinkedList<>();
isVisited[m] = true;
myQueue.add(m);
while (!myQueue.isEmpty()) {
int p = (int) myQueue.removeFirst();
int x = getFirstNeighbor(p);
while (x != -1) {
if (!isVisited[x]) {
isVisited[x] = true;
myQueue.add(x);
x = getNextNeighbor(x, p);
}
}
}
}
/**
* 最小生成树:
* 普利姆算法:
* 获取一个顶点A和它连接的顶点数组X,然后连接权值最小的顶点B,并将B连接的顶点数组合并到X,
* 仍从X中找出权值最小的顶点连接,以此类推。
*/
public static void prim(int m, int length, int[][] tables) {
int[] repleData = tables[m];
for (int i = 0; i < length; i++) {
int small = MAX;
for (int j = 0; j < length; j++) {
if (repleData[j] != 0 && repleData[j] != MAX && repleData[j] < small) {
small = tables[m][j];
repleData[j] = 0;
}
}
System.out.print("节点:" + i + " 最小权重:" + small);
for (int j = 0; j < length; j++) {
if (tables[small][j] != 0 && tables[small][j] != MAX && tables[small][j] < repleData[j]) {
repleData[j] = tables[small][j];
}
}
}
}
/**
* 最小生成树
* 克鲁斯卡尔算法:
* Edge edge0 = new Edge(int begin,int end, int weight);
* begin为边的起始顶点,end为边的结束顶点,weight为边的权重
* 通过构建边的数组来进行计算。
* 思想:按权重从小到大遍历,构成回环的边舍弃
*
* @param dataSize
* @param datas Edge的集合,按权重从小到大排列
*/
int[] results;
public void Kruskal(int dataSize, Edge[] datas) {
results = new int[dataSize];
for (int i = 0; i < datas.length; i++) {
int m = find(datas[i].begin);
int n = find(datas[i].end);
if (n != m) {
results[m] = n;
}
}
}
//找到最终节点
int find(int m) {
while (results[m] != 0) {
m = results[m];
}
return m;
}
class Edge {
int begin;
int end;
int weight;
public int getBegin() {
return begin;
}
public void setBegin(int begin) {
this.begin = begin;
}
public int getEnd() {
return end;
}
public void setEnd(int end) {
this.end = end;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
public Edge(int begin, int end, int weight) {
this.begin = begin;
this.end = end;
this.weight = weight;
}
}
/**
* 最短路径:
* 迪杰斯特拉算法:
* 获取一个顶点A和它连接的顶点数组X,然后连接最小权值(M)的顶点B,然后将B顶点的数组P整合到数组X(数组P相加M和数组X取最小值)
*/
public void Dijkstra(int length, int[][] tables) {
results = new int[length];
results[0] = 0;
isVisited[0] = true;
results = tables[0];
for (int i = 1; i < length; i++) {
int min = MAX;
int k = 0;
for (int j = 0; j < tables[i].length; j++) {
if (!isVisited[j] && tables[i][j] < min) {
min = tables[i][j];
k = j;
}
}
for (int j = 0; j < length; j++) {
if (!isVisited[j] && tables[k][j] + min < results[j]) {
results[j] = min + tables[k][j];
}
}
}
}
}