-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprim.js
63 lines (52 loc) · 1.84 KB
/
prim.js
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
class Graph {
constructor() {
this.edges = new Map();
this.vertices = new Set();
}
addEdge(vertex1, vertex2, weight) {
this.vertices.add(vertex1);
this.vertices.add(vertex2);
if (!this.edges.has(vertex1)) {
this.edges.set(vertex1, []);
}
if (!this.edges.has(vertex2)) {
this.edges.set(vertex2, []);
}
this.edges.get(vertex1).push({ vertex: vertex2, weight });
this.edges.get(vertex2).push({ vertex: vertex1, weight });
}
prim() {
const priorityQueue = [];
const minimumSpanningTree = [];
const startingVertex = Array.from(this.vertices)[0];
const visited = new Set([startingVertex]);
this.edges.get(startingVertex).forEach(edge => {
priorityQueue.push({ vertex: edge.vertex, weight: edge.weight });
});
while (priorityQueue.length > 0) {
priorityQueue.sort((a, b) => a.weight - b.weight);
const { vertex, weight } = priorityQueue.shift();
if (!visited.has(vertex)) {
visited.add(vertex);
minimumSpanningTree.push({ source: vertex, destination: vertex, weight });
this.edges.get(vertex).forEach(edge => {
if (!visited.has(edge.vertex)) {
priorityQueue.push({ vertex: edge.vertex, weight: edge.weight });
}
});
}
}
return minimumSpanningTree;
}
}
const graph = new Graph();
graph.addEdge('A', 'B', 2);
graph.addEdge('A', 'C', 3);
graph.addEdge('B', 'C', 5);
graph.addEdge('B', 'D', 7);
graph.addEdge('C', 'D', 1);
graph.addEdge('C', 'E', 6);
graph.addEdge('D', 'E', 4);
const minimumSpanningTree = graph.prim();
console.log("Minimum Spanning Tree:");
console.log(minimumSpanningTree);