|
| 1 | +import { edmondsKarp } from '../edmondsKarp' |
| 2 | + |
| 3 | +describe('edmondsKarp', () => { |
| 4 | + const init_flow_network = (N: number): number[][] => { |
| 5 | + const graph = Array.from({ length: N }, () => Array(N).fill(0)); |
| 6 | + return graph; |
| 7 | + } |
| 8 | + |
| 9 | + const add_capacity = ( |
| 10 | + graph: number[][], |
| 11 | + u: number, |
| 12 | + v: number, |
| 13 | + capacity: number |
| 14 | + ) => { |
| 15 | + graph[u][v] = capacity; |
| 16 | + } |
| 17 | + |
| 18 | + it('should return the correct maximum flow value for basic graph', () => { |
| 19 | + const graph = init_flow_network(6); |
| 20 | + add_capacity(graph, 0, 1, 16); |
| 21 | + add_capacity(graph, 0, 2, 13); |
| 22 | + add_capacity(graph, 1, 2, 10); |
| 23 | + add_capacity(graph, 1, 3, 12); |
| 24 | + add_capacity(graph, 2, 1, 4); |
| 25 | + add_capacity(graph, 2, 4, 14); |
| 26 | + add_capacity(graph, 3, 2, 9); |
| 27 | + add_capacity(graph, 3, 5, 20); |
| 28 | + add_capacity(graph, 4, 3, 7); |
| 29 | + add_capacity(graph, 4, 5, 4); |
| 30 | + expect(edmondsKarp(graph, 0, 5)).toBe(23); |
| 31 | + }); |
| 32 | + |
| 33 | + it('should return the correct maximum flow value for single element graph', () => { |
| 34 | + const graph = init_flow_network(1); |
| 35 | + expect(edmondsKarp(graph, 0, 0)).toBe(0); |
| 36 | + }); |
| 37 | + |
| 38 | + const linear_flow_network = init_flow_network(4); |
| 39 | + add_capacity(linear_flow_network, 0, 1, 10); |
| 40 | + add_capacity(linear_flow_network, 1, 2, 5); |
| 41 | + add_capacity(linear_flow_network, 2, 3, 15); |
| 42 | + test.each([ |
| 43 | + [0, 3, 5], |
| 44 | + [0, 2, 5], |
| 45 | + [1, 3, 5], |
| 46 | + [1, 2, 5], |
| 47 | + ])( |
| 48 | + 'correct result for linear flow network with source node %i and sink node %i', |
| 49 | + (source, sink, maxFlow) => { |
| 50 | + expect(edmondsKarp(linear_flow_network, source, sink)).toBe(maxFlow); |
| 51 | + } |
| 52 | + ); |
| 53 | + |
| 54 | + const disconnected_flow_network = init_flow_network(4); |
| 55 | + add_capacity(disconnected_flow_network, 0, 1, 10); |
| 56 | + add_capacity(disconnected_flow_network, 2, 3, 5); |
| 57 | + test.each([ |
| 58 | + [0, 3, 0], |
| 59 | + [1, 2, 0], |
| 60 | + [2, 3, 5], |
| 61 | + ])( |
| 62 | + 'correct result for disconnected flow network with source node %i and sink node %i', |
| 63 | + (source, sink, maxFlow) => { |
| 64 | + expect(edmondsKarp(disconnected_flow_network, source, sink)).toBe(maxFlow); |
| 65 | + } |
| 66 | + ); |
| 67 | + |
| 68 | + const cyclic_flow_network = init_flow_network(5); |
| 69 | + add_capacity(cyclic_flow_network, 0, 1, 10); |
| 70 | + add_capacity(cyclic_flow_network, 1, 2, 5); |
| 71 | + add_capacity(cyclic_flow_network, 2, 0, 7); |
| 72 | + add_capacity(cyclic_flow_network, 2, 3, 10); |
| 73 | + add_capacity(cyclic_flow_network, 3, 4, 10); |
| 74 | + test.each([ |
| 75 | + [0, 4, 10], |
| 76 | + [1, 4, 10], |
| 77 | + [2, 4, 10], |
| 78 | + ])( |
| 79 | + 'correct result for cyclic flow network with source node %i and sink node %i', |
| 80 | + (source, sink, maxFlow) => { |
| 81 | + expect(edmondsKarp(cyclic_flow_network, source, sink)).toBe(maxFlow); |
| 82 | + } |
| 83 | + ); |
| 84 | +}); |
0 commit comments