-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGraph.ts
65 lines (56 loc) · 1.61 KB
/
Graph.ts
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
class Graph {
// 鄰接表
adjacencyList: Map<string, string[]>;
constructor(initialAdjList?: { [key: string]: string[] }) {
this.adjacencyList = new Map();
// 初始化
if (initialAdjList) {
for (const vertex in initialAdjList) {
this.adjacencyList.set(vertex, initialAdjList[vertex]);
}
}
}
// 新增頂點
addVertex(vertex: string): void {
if (!this.adjacencyList.has(vertex)) {
this.adjacencyList.set(vertex, []);
}
}
// 移除頂點
removeVertex(vertex: string): void {
if (this.adjacencyList.has(vertex)) {
for (const adjacentVertex of this.adjacencyList.get(vertex) || []) {
this.removeEdge(vertex, adjacentVertex);
}
this.adjacencyList.delete(vertex);
}
}
// 新增邊
addEdge(vertex1: string, vertex2: string): void {
if (this.adjacencyList.has(vertex1) && this.adjacencyList.has(vertex2)) {
this.adjacencyList.get(vertex1)?.push(vertex2);
this.adjacencyList.get(vertex2)?.push(vertex1);
}
}
// 移除邊
removeEdge(vertex1: string, vertex2: string): void {
if (this.adjacencyList.has(vertex1)) {
this.adjacencyList.set(
vertex1,
this.adjacencyList.get(vertex1)?.filter((v) => v !== vertex2) || [],
);
}
if (this.adjacencyList.has(vertex2)) {
this.adjacencyList.set(
vertex2,
this.adjacencyList.get(vertex2)?.filter((v) => v !== vertex1) || [],
);
}
}
// 印出圖的鄰接表
print(): void {
for (const [vertex, edges] of this.adjacencyList) {
console.log(`${vertex} -> ${edges.join(', ')}`);
}
}
}