forked from TheAlgorithms/TypeScript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathedmondkarp_test.ts
84 lines (77 loc) · 2.5 KB
/
edmondkarp_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
import { edmondsKarp } from '../edmondsKarp'
describe('edmondsKarp', () => {
const init_flow_network = (N: number): number[][] => {
const graph = Array.from({ length: N }, () => Array(N).fill(0));
return graph;
}
const add_capacity = (
graph: number[][],
u: number,
v: number,
capacity: number
) => {
graph[u][v] = capacity;
}
it('should return the correct maximum flow value for basic graph', () => {
const graph = init_flow_network(6);
add_capacity(graph, 0, 1, 16);
add_capacity(graph, 0, 2, 13);
add_capacity(graph, 1, 2, 10);
add_capacity(graph, 1, 3, 12);
add_capacity(graph, 2, 1, 4);
add_capacity(graph, 2, 4, 14);
add_capacity(graph, 3, 2, 9);
add_capacity(graph, 3, 5, 20);
add_capacity(graph, 4, 3, 7);
add_capacity(graph, 4, 5, 4);
expect(edmondsKarp(graph, 0, 5)).toBe(23);
});
it('should return the correct maximum flow value for single element graph', () => {
const graph = init_flow_network(1);
expect(edmondsKarp(graph, 0, 0)).toBe(0);
});
const linear_flow_network = init_flow_network(4);
add_capacity(linear_flow_network, 0, 1, 10);
add_capacity(linear_flow_network, 1, 2, 5);
add_capacity(linear_flow_network, 2, 3, 15);
test.each([
[0, 3, 5],
[0, 2, 5],
[1, 3, 5],
[1, 2, 5],
])(
'correct result for linear flow network with source node %i and sink node %i',
(source, sink, maxFlow) => {
expect(edmondsKarp(linear_flow_network, source, sink)).toBe(maxFlow);
}
);
const disconnected_flow_network = init_flow_network(4);
add_capacity(disconnected_flow_network, 0, 1, 10);
add_capacity(disconnected_flow_network, 2, 3, 5);
test.each([
[0, 3, 0],
[1, 2, 0],
[2, 3, 5],
])(
'correct result for disconnected flow network with source node %i and sink node %i',
(source, sink, maxFlow) => {
expect(edmondsKarp(disconnected_flow_network, source, sink)).toBe(maxFlow);
}
);
const cyclic_flow_network = init_flow_network(5);
add_capacity(cyclic_flow_network, 0, 1, 10);
add_capacity(cyclic_flow_network, 1, 2, 5);
add_capacity(cyclic_flow_network, 2, 0, 7);
add_capacity(cyclic_flow_network, 2, 3, 10);
add_capacity(cyclic_flow_network, 3, 4, 10);
test.each([
[0, 4, 10],
[1, 4, 10],
[2, 4, 10],
])(
'correct result for cyclic flow network with source node %i and sink node %i',
(source, sink, maxFlow) => {
expect(edmondsKarp(cyclic_flow_network, source, sink)).toBe(maxFlow);
}
);
});