-
Notifications
You must be signed in to change notification settings - Fork 4
/
minimum-spanning-tree.js
82 lines (72 loc) · 1.73 KB
/
minimum-spanning-tree.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
class vertex_mst {
constructor(id, visited) {
this.id = id;
this.visited = visited;
}
}
class edge_mst {
constructor(weight, visited, src, dest) {
this.weight = weight;
this.visited = visited;
this.src = src;
this.dest = dest;
}
}
class graph_mst {
constructor(g, e) {
this.g = g;
this.e = e;
}
vertex_exists(id) {
for (let i = 0; i < this.g.length; i++) {
if (this.g[i].id === id) {
return this.g[i];
}
}
return null;
}
generate_graph(vertices, edges) {
for (let i = 0; i < vertices; i++) {
let v = new vertex_mst(i + 1, false);
this.g.push(v);
}
for (let i = 0; i < edges.length; i++) {
let src = this.vertex_exists(edges[i][1]);
let dest = this.vertex_exists(edges[i][2]);
let e = new edge_mst(edges[i][0], false, src, dest);
this.e.push(e);
}
}
find_min_spanning_tree() {
let weight = -1;
return weight;
}
print_graph() {
for (let i = 0; i < this.g.length; i++) {
console.log(this.g[i].id + " " + this.g[i].visited);
}
for (let i = 0; i < this.e.length; i++) {
console.log(this.e[i].src.id + "->" +
this.e[i].dest.id + "[" +
this.e[i].weight + ", " +
this.e[i].visited + "] ");
}
}
get_graph() {
res = ""
for (let i = 0; i < this.e.length; i++) {
res += "[" + this.e[i].src.id + "->" +
this.e[i].dest.id + "], ";
}
return res;
}
print_mst(w) {
console.log("MST");
for (let i = 0; i < this.e.length; i++) {
if (this.e[i].visited === true) {
console.log(this.e[i].src.id + "->" + this.e[i].dest.id);
}
}
console.log("weight: " + w);
}
}