-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathGraphPath.ts
More file actions
126 lines (106 loc) · 3.57 KB
/
GraphPath.ts
File metadata and controls
126 lines (106 loc) · 3.57 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
/**
* Copyright © https://github.com/microwind All rights reserved.
* @author: jarryli@gmail.com
* @version: 1.0
* @description:
* 带权图及最短路径算法实现(TypeScript)
* 功能:使用邻接表存储带权图,实现Dijkstra最短路径算法
* 用途:学习图的最短路径算法,理解优先队列在图算法中的应用
*/
// 边类 - 表示图中的边(目标节点,边的权重)
class Edge {
private end: string;
private distance: number;
constructor(end: string, distance: number) {
this.end = end;
this.distance = distance;
}
getEnd(): string {
return this.end;
}
getDistance(): number {
return this.distance;
}
}
// 带权图类 - 支持Dijkstra最短路径算法
class GraphPath {
private adjacencyList: Map<string, Edge[]>;
// 构造函数,初始化图
constructor() {
this.adjacencyList = new Map();
}
// 添加边 (起点, 终点, 距离)
addEdge(start: string, end: string, distance: number): void {
if (!this.adjacencyList.has(start)) {
this.adjacencyList.set(start, []);
}
this.adjacencyList.get(start)!.push(new Edge(end, distance));
// 保证终点也在图中(处理孤立节点情况)
if (!this.adjacencyList.has(end)) {
this.adjacencyList.set(end, []);
}
}
// Dijkstra算法寻找最短路径
dijkstra(start: string): Map<string, number> {
// 存储最短路径的距离
const distances = new Map<string, number>();
for (const vertex of this.adjacencyList.keys()) {
distances.set(vertex, Infinity); // 初始设置为无穷大
}
distances.set(start, 0);
// 优先队列按距离排序(使用数组模拟最小堆)
const priorityQueue: Edge[] = [];
priorityQueue.push(new Edge(start, 0));
while (priorityQueue.length > 0) {
// 找出距离最小的节点
priorityQueue.sort((a, b) => a.getDistance() - b.getDistance());
const current = priorityQueue.shift()!;
const currentVertex = current.getEnd();
const currentDistance = current.getDistance();
// 如果当前距离大于已知最短距离,跳过
if (currentDistance > distances.get(currentVertex)!) {
continue;
}
// 遍历相邻节点
const neighbors = this.adjacencyList.get(currentVertex) || [];
for (const edge of neighbors) {
const newDist = currentDistance + edge.getDistance();
if (newDist < distances.get(edge.getEnd())!) {
distances.set(edge.getEnd(), newDist);
priorityQueue.push(new Edge(edge.getEnd(), newDist));
}
}
}
return distances;
}
// 输出最短路径及其距离
printShortestPath(start: string, end: string): void {
const distances = this.dijkstra(start);
const distance = distances.get(end);
if (distance === Infinity) {
console.log(`从 ${start} 到 ${end} 没有路径`);
} else {
console.log(`从 ${start} 到 ${end} 的最短距离是: ${distance} 公里`);
}
}
// 获取邻接表
getAdjacencyList(): Map<string, Edge[]> {
return this.adjacencyList;
}
}
// 测试代码
const graphPath = new GraphPath();
graphPath.addEdge("A", "B", 4);
graphPath.addEdge("A", "C", 2);
graphPath.addEdge("B", "C", 1);
graphPath.addEdge("B", "D", 5);
graphPath.addEdge("C", "D", 8);
graphPath.addEdge("C", "E", 10);
graphPath.addEdge("D", "E", 2);
console.log("各节点最短距离:");
const distances = graphPath.dijkstra("A");
distances.forEach((dist, vertex) => {
console.log(`A -> ${vertex}: ${dist}`);
});
graphPath.printShortestPath("A", "E");
export { GraphPath, Edge };