-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathkruskal.js
82 lines (71 loc) · 1.39 KB
/
kruskal.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
const INF = Number.MAX_SAFE_INTEGER
const find = (i, parent) => {
while (parent[i]) {
i = parent[i]
}
return i
}
const union = (i, j, parent) => {
if (i !== j) {
parent[j] = i
return true
}
return false
}
const initializeCost = graph => {
const cost = []
const { length } = graph
for (let i = 0; i < length; i++) {
cost[i] = []
for (let j = 0; j < length; j++) {
if (graph[i][j] === 0) {
cost[i][j] = INF
} else {
cost[i][j] = graph[i][j]
}
}
}
return cost
}
const kruskal = graph => {
const { length } = graph
const parent = []
let ne = 0
let a
let b
let u
let v
const cost = initializeCost(graph)
while (ne < length - 1) {
for (let i = 0, min = INF; i < length; i++) {
for (let j = 0; j < length; j++) {
if (cost[i][j] < min) {
min = cost[i][j]
a = u = i
b = v = j
}
}
}
u = find(u, parent)
v = find(v, parent)
if (union(u, v, parent)) {
ne++
}
cost[a][b] = cost[b][a] = INF
}
return parent
}
// test
const graph = [
[0, 2, 4, 0, 0, 0],
[2, 0, 2, 4, 2, 0],
[4, 2, 0, 0, 3, 0],
[0, 4, 0, 0, 3, 2],
[0, 2, 3, 3, 0, 2],
[0, 0, 0, 2, 2, 0]
]
const parent = kruskal(graph)
console.log('Edge Weight')
for (i = 1; i < graph.length; i++) {
console.log(parent[i] + ' - ' + i + ' ' + graph[i][parent[i]])
}