-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprim.js
More file actions
62 lines (54 loc) · 1.33 KB
/
prim.js
File metadata and controls
62 lines (54 loc) · 1.33 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
// prim algorithm
class Prim {
constructor(graph) {
this.graph = graph;
this.visited = [];
this.dist = [];
this.prev = [];
this.mst = [];
this.mstWeight = 0;
}
// get the minimum distance vertex
getMinVertex() {
let minVertex = -1;
for (let i = 0; i < this.graph.length; i++) {
if (
!this.visited[i] &&
(minVertex === -1 || this.dist[i] < this.dist[minVertex])
) {
minVertex = i;
}
}
return minVertex;
}
// prim algorithm
prim() {
for (let i = 0; i < this.graph.length; i++) {
this.dist[i] = Infinity;
this.visited[i] = false;
}
this.dist[0] = 0;
for (let i = 0; i < this.graph.length - 1; i++) {
let u = this.getMinVertex();
this.visited[u] = true;
for (let v = 0; v < this.graph.length; v++) {
if (this.graph[u][v] !== 0 && !this.visited[v]) {
if (this.graph[u][v] < this.dist[v]) {
this.dist[v] = this.graph[u][v];
this.prev[v] = u;
}
}
}
}
for (let i = 1; i < this.graph.length; i++) {
this.mst.push([this.prev[i], i]);
this.mstWeight += this.graph[i][this.prev[i]];
}
}
}
var graph = new Graph();
graph.addVertex("A");
graph.addVertex("B");
graph.addEdge("A", "B", 1);
var prim = new Prim(graph);
prim.run("A");