-
-
Notifications
You must be signed in to change notification settings - Fork 408
/
Copy pathprim.test.ts
94 lines (82 loc) · 2.3 KB
/
prim.test.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import { Edge, prim } from '../prim'
const edge_equal = (x: Edge, y: Edge): boolean => {
return (
(x.a == y.a && x.b == y.b) ||
(x.a == y.b && x.b == y.a && x.weight == y.weight)
)
}
const test_graph = (
expected_tree_edges: Edge[],
other_edges: Edge[],
num_vertices: number,
expected_cost: number
) => {
// First make sure the graph is undirected
const graph: [number, number][][] = []
for (let _ = 0; _ < num_vertices; ++_) {
graph.push([])
}
for (const edge of expected_tree_edges) {
graph[edge.a].push([edge.b, edge.weight])
graph[edge.b].push([edge.a, edge.weight])
}
for (const edge of other_edges) {
graph[edge.a].push([edge.b, edge.weight])
graph[edge.b].push([edge.a, edge.weight])
}
const [tree_edges, cost] = prim(graph)
expect(cost).toStrictEqual(expected_cost)
for (const expected_edge of expected_tree_edges) {
expect(
tree_edges.find((edge) => edge_equal(edge, expected_edge))
).toBeTruthy()
}
for (const unexpected_edge of other_edges) {
expect(
tree_edges.find((edge) => edge_equal(edge, unexpected_edge))
).toBeFalsy()
}
}
describe('prim', () => {
it('should return empty tree for empty graph', () => {
expect(prim([])).toStrictEqual([[], 0])
})
it('should return empty tree for single element graph', () => {
expect(prim([])).toStrictEqual([[], 0])
})
it('should return correct value for two element graph', () => {
expect(prim([[[1, 5]], []])).toStrictEqual([[new Edge(0, 1, 5)], 5])
})
it('should return the correct value', () => {
const expected_tree_edges = [
new Edge(0, 1, 1),
new Edge(1, 3, 2),
new Edge(3, 2, 3)
]
const other_edges = [
new Edge(0, 2, 4),
new Edge(0, 3, 5),
new Edge(1, 2, 6)
]
test_graph(expected_tree_edges, other_edges, 4, 6)
})
it('should return the correct value', () => {
const expected_tree_edges = [
new Edge(0, 2, 2),
new Edge(1, 3, 9),
new Edge(2, 6, 74),
new Edge(2, 7, 8),
new Edge(3, 4, 3),
new Edge(4, 9, 9),
new Edge(5, 7, 5),
new Edge(7, 9, 4),
new Edge(8, 9, 2)
]
const other_edges = [
new Edge(0, 1, 10),
new Edge(2, 4, 47),
new Edge(4, 5, 42)
]
test_graph(expected_tree_edges, other_edges, 10, 116)
})
})