Repository navigation
Expand file tree
/
Copy pathGraph.java
More file actions
331 lines (294 loc) · 13 KB
/
Copy pathGraph.java
File metadata and controls
331 lines (294 loc) · 13 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
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
package demo.graph;
import java.util.*;
//GraphNode
record GraphNode(int v, double weight) {
}
//Edge
record GraphEdge(int s, int d, double weight) {
}
public class Graph {
protected static final int NODE_COUNT = 26;
private final List<List<GraphNode>> adjacencyList = new ArrayList<>();
private final double[][] adjacencyMatrix;
public Graph() {
for (int i = 0; i < NODE_COUNT; i++) {
adjacencyList.addLast(new ArrayList<>()); //初始化邻接链表
}
adjacencyMatrix = new double[NODE_COUNT][NODE_COUNT];
for (int i = 0; i < NODE_COUNT; i++) {
Arrays.fill(adjacencyMatrix[i], Double.MAX_VALUE);
adjacencyMatrix[i][i] = 0; // Distance to self is 0
}
}
//添加边
public void addEdge(int start, int end, double weight) {
adjacencyList.get(start).add(new GraphNode(end, weight));
adjacencyList.get(end).add(new GraphNode(start, weight));
adjacencyMatrix[start][end] = weight;
adjacencyMatrix[end][start] = weight;
}
//Dijkstra算法 单源最短路径
public List<String> findShortestPathDijkstra(char s, char s2) {
int start = s - 'A', end = s2 - 'A';
double[] d = new double[NODE_COUNT]; //距离
ArrayList<ArrayList<Integer>> lists = new ArrayList<>(); //记录前一个节点
for (int i = 0; i < NODE_COUNT; i++) {
ArrayList<Integer> list = new ArrayList<>();
list.add(-1); // 初始化
lists.add(list);
}
boolean[] visited = new boolean[NODE_COUNT]; //记录是否已经访问过了
Arrays.fill(d, Integer.MAX_VALUE);
d[start] = 0;
PriorityQueue<Integer> heap = new PriorityQueue<>(Comparator.comparingDouble(a -> d[a])); //最小堆
heap.add(start);
while (!heap.isEmpty()) {
int index = heap.poll();
if (visited[index]) continue;
visited[index] = true;
int l = adjacencyList.get(index).size();
for (int i = 0; i < l; i++) {
int ver = adjacencyList.get(index).get(i).v(); //提取相邻的节点信息
double weight = adjacencyList.get(index).get(i).weight();
if (visited[ver]) continue;
if (d[index] + weight < d[ver]) {
d[ver] = d[index] + weight;
lists.remove(ver);
ArrayList<Integer> al = new ArrayList<>();
al.add(index);
lists.add(ver, al);
heap.add(ver);
} else if (!visited[ver] && d[index] + weight == d[ver]) {
if (lists.get(ver).getFirst() == -1) {
lists.get(ver).removeFirst();
lists.get(ver).add(index);
} else {
lists.get(ver).add(index);
}
}
}
}
Deque<Integer> stack = new LinkedList<>();
stack.add(end);
List<String> paths = new ArrayList<>();
collectPaths(lists, end, end, start, stack, 0, paths);
return paths;
}
//Bellman-Ford算法
public List<String> findShortestPathBellmanFord(char s, char s2) {
int x = s - 'A', end = s2 - 'A';
double[] d = new double[NODE_COUNT]; //初始化距离数组
Arrays.fill(d, Integer.MAX_VALUE);
d[x] = 0;
ArrayList<ArrayList<Integer>> lists = new ArrayList<>(); //记录前一个节点是哪个
for (int i = 0; i < NODE_COUNT; i++) {
ArrayList<Integer> list = new ArrayList<>();
list.add(-1);
lists.add(list);
}
for (int i = 0; i < NODE_COUNT - 1; i++) { //循环次数为顶点数减1
boolean[] visited = new boolean[NODE_COUNT]; //记录每次循环中节点是否被访问过
for (int j = 0; j < NODE_COUNT; j++) { //遍历所有边
int m = adjacencyList.get(j).size();
for (int k = 0; k < m; k++) {
int ver = adjacencyList.get(j).get(k).v();
double weight = adjacencyList.get(j).get(k).weight();
if (d[j] != Integer.MAX_VALUE && d[j] + weight < d[ver]) { //成功松弛
d[ver] = d[j] + weight;
lists.remove(ver);
ArrayList<Integer> al = new ArrayList<>();
al.add(j);
lists.add(ver, al);
visited[ver] = true;
} else if (d[j] != Integer.MAX_VALUE && d[j] + weight == d[ver]) {
if (!visited[ver]) {
lists.remove(ver);
ArrayList<Integer> al = new ArrayList<>();
al.add(j);
lists.add(ver, al);
} else {
lists.get(ver).add(j);
}
visited[ver] = true;
}
}
}
}
Deque<Integer> stack = new LinkedList<>();
stack.add(end);
List<String> paths = new ArrayList<>();
collectPaths(lists, end, end, x, stack, 0, paths);
return paths;
}
//路径信息
private void collectPaths(ArrayList<ArrayList<Integer>> record, int temp, int next, int x, Deque<Integer> stack, double weight, List<String> paths) {
//向前寻找路径信息
while (record.get(temp).getFirst() != x) {
if (temp < 0 || temp >= NODE_COUNT) {
return;
}
int m = record.get(temp).size();
if (m > 1) { //如果在同一级中有多个节点,就复制相关信息进入递归
for (int i = 1; i < m; i++) {
int v = record.get(temp).get(i);
Deque<Integer> newStack = new LinkedList<>(stack);
newStack.push(v);
collectPaths(record, v, v, x, newStack, weight + adjacencyMatrix[v][next], paths);
}
}
int v = record.get(temp).getFirst();
if (v < 0 || v >= NODE_COUNT) {
break;
}
stack.push(v);
temp = v;
weight += adjacencyMatrix[temp][next];
next = temp;
}
weight += adjacencyMatrix[x][next];
StringBuilder path = new StringBuilder();
path.append((char) (x + 'A'));
while (!stack.isEmpty()) {
int index = stack.pop();
path.append("->").append((char) ('A' + index));
}
path.append("\n最短距离为 ").append(String.format("%.2f", weight)).append(" km.\n");
paths.add(path.toString());
}
private int find(int[] father, int x) {
if (father[x] != x) {
father[x] = find(father, father[x]);
}
return father[x];
}
private void union(int[] father, int x, int y) {
int fx = find(father, x);
int fy = find(father, y);
father[fx] = fy;
}
//Kruskal算法
public List<String> Kruskal_subway() {
int[] father = new int[NODE_COUNT]; // 并查集初始化
for (int i = 0; i < NODE_COUNT; i++) father[i] = i;
ArrayList<GraphEdge> edges = new ArrayList<>();
for (int i = 0; i < NODE_COUNT; i++) {
for (int j = i + 1; j < NODE_COUNT; j++) {
if (adjacencyMatrix[i][j] != Double.MAX_VALUE)
edges.add(new GraphEdge(i, j, adjacencyMatrix[i][j]));
}
}
// 按边权重从小到大排序
edges.sort(Comparator.comparingDouble(GraphEdge::weight));
List<String> result = new ArrayList<>();
double totalWeight = 0;
// 选择边构建最小生成树
for (GraphEdge edge : edges) {
if (find(father, edge.s()) != find(father, edge.d())) {
union(father, edge.s(), edge.d());
result.add("( " + (char) (edge.s() + 'A') + " , " + (char) (edge.d() + 'A') + " , " + String.format("%.2f", edge.weight()) + " km)\n");
totalWeight += edge.weight();
}
}
// 检查是否所有节点都被连接
int root = find(father, 0);
for (int i = 1; i < NODE_COUNT; i++) {
if (find(father, i) != root) {
return List.of("无法构建连通图,无法满足条件!");
}
}
result.add("最短路径总长度为: " + String.format("%.2f", totalWeight) + " km.\n");
return result;
}
//Prim算法
public List<String> Prim_subway() {
boolean[] visited = new boolean[NODE_COUNT];
List<String> result = new ArrayList<>();
PriorityQueue<GraphEdge> heap = new PriorityQueue<>(Comparator.comparingDouble(GraphEdge::weight));
for (GraphNode node : adjacencyList.getFirst()) {
heap.add(new GraphEdge(0, node.v(), node.weight()));
}
visited[0] = true;
double weight = 0;
while (!heap.isEmpty()) {
GraphEdge edge = heap.poll();
int v = edge.d();
if (visited[v]) continue;
weight += edge.weight();
result.add("( " + (char) (edge.s() + 'A') + " , " + (char) (edge.d() + 'A') + " , " + String.format("%.2f", edge.weight()) + " km)\n");
visited[v] = true;
for (GraphNode node : adjacencyList.get(v)) {
heap.add(new GraphEdge(v, node.v(), node.weight()));
}
}
result.add("最短距离为 " + String.format("%.2f", weight) + " km.\n");
return result;
}
//实现满足功能的busRoutes函数,利用Dijkstra算法找出从给定起点出发的公交路线相关信息
public List<String> bus_Dijkstra(char s) {
int x = s - 'A'; // 起点
double[] distance = new double[NODE_COUNT]; // 起点到其他各点的距离
Arrays.fill(distance, Double.MAX_VALUE);
distance[x] = 0; // 起点到自身的距离为 0
ArrayList<ArrayList<Integer>> record = new ArrayList<>(); // 记录前一个节点
for (int i = 0; i < NODE_COUNT; i++) {
ArrayList<Integer> al = new ArrayList<>();
al.add(-1);
record.add(al);
}
boolean[] visited = new boolean[NODE_COUNT]; // 是否访问过
PriorityQueue<Integer> heap = new PriorityQueue<>(Comparator.comparingDouble(a -> distance[a])); // 最小堆
heap.add(x);
// 使用 Dijkstra 算法计算从起点到所有点的最短路径
while (!heap.isEmpty()) {
int index = heap.poll();
if (visited[index]) continue;
visited[index] = true;
for (GraphNode neighbor : adjacencyList.get(index)) {
int ver = neighbor.v();
double weight = neighbor.weight();
if (visited[ver]) continue;
if (distance[index] + weight < distance[ver]) { // 松弛操作
distance[ver] = distance[index] + weight;
record.set(ver, new ArrayList<>(List.of(index)));
heap.add(ver);
} else if (distance[index] + weight == distance[ver]) { // 记录等长路径
record.get(ver).add(index);
}
}
}
// 构建公交路线图
Graph busGraph = new Graph();
for (int y = 0; y < NODE_COUNT; y++) {
if (y == x) continue;
Deque<Integer> stack = new LinkedList<>();
stack.add(y);
bus_Recursive(record, y, y, x, stack, busGraph);
}
// 使用 Prim 算法生成最小生成树
return busGraph.Prim_subway();
}
private void bus_Recursive(ArrayList<ArrayList<Integer>> record, int temp, int next, int x, Deque<Integer> stack, Graph g) {
// 向前追踪路径信息
while (record.get(temp).get(0) != x) {
int m = record.get(temp).size();
if (m > 1) { // 如果同一级有多个节点,递归处理
for (int i = 1; i < m; i++) {
int v = record.get(temp).get(i);
Deque<Integer> newStack = new LinkedList<>(stack);
newStack.push(v);
bus_Recursive(record, v, v, x, newStack, g);
}
}
int v = record.get(temp).get(0);
stack.push(v);
temp = v;
next = temp;
}
// 添加边到公交路线图
g.addEdge(x, stack.peek(), adjacencyMatrix[x][stack.peek()]);
while (!stack.isEmpty()) {
int index = stack.pop();
if (!stack.isEmpty()) g.addEdge(index, stack.peek(), adjacencyMatrix[index][stack.peek()]);
}
}
}