|
| 1 | +import edmondsKarp from '../edmonds_karp' |
| 2 | + |
| 3 | +describe('Edmonds-Karp Algorithm', () => { |
| 4 | + it('should find the maximum flow in a simple graph', () => { |
| 5 | + const graph: [number, number][][] = [ |
| 6 | + [ |
| 7 | + [1, 3], |
| 8 | + [2, 2] |
| 9 | + ], // Node 0: Edges to node 1 (capacity 3), and node 2 (capacity 2) |
| 10 | + [[3, 2]], // Node 1: Edge to node 3 (capacity 2) |
| 11 | + [[3, 3]], // Node 2: Edge to node 3 (capacity 3) |
| 12 | + [] // Node 3: No outgoing edges |
| 13 | + ] |
| 14 | + const source = 0 |
| 15 | + const sink = 3 |
| 16 | + const maxFlow = edmondsKarp(graph, source, sink) |
| 17 | + expect(maxFlow).toBe(4) |
| 18 | + }) |
| 19 | + |
| 20 | + it('should find the maximum flow in a more complex graph', () => { |
| 21 | + const graph: [number, number][][] = [ |
| 22 | + [ |
| 23 | + [1, 10], |
| 24 | + [2, 10] |
| 25 | + ], // Node 0: Edges to node 1 and node 2 (both capacity 10) |
| 26 | + [ |
| 27 | + [3, 4], |
| 28 | + [4, 8] |
| 29 | + ], // Node 1: Edges to node 3 (capacity 4), and node 4 (capacity 8) |
| 30 | + [[4, 9]], // Node 2: Edge to node 4 (capacity 9) |
| 31 | + [[5, 10]], // Node 3: Edge to node 5 (capacity 10) |
| 32 | + [[5, 10]], // Node 4: Edge to node 5 (capacity 10) |
| 33 | + [] // Node 5: No outgoing edges (sink) |
| 34 | + ] |
| 35 | + const source = 0 |
| 36 | + const sink = 5 |
| 37 | + const maxFlow = edmondsKarp(graph, source, sink) |
| 38 | + expect(maxFlow).toBe(14) |
| 39 | + }) |
| 40 | + |
| 41 | + it('should return 0 when there is no path from source to sink', () => { |
| 42 | + const graph: [number, number][][] = [ |
| 43 | + [], // Node 0: No outgoing edges |
| 44 | + [], // Node 1: No outgoing edges |
| 45 | + [] // Node 2: No outgoing edges (sink) |
| 46 | + ] |
| 47 | + const source = 0 |
| 48 | + const sink = 2 |
| 49 | + const maxFlow = edmondsKarp(graph, source, sink) |
| 50 | + expect(maxFlow).toBe(0) |
| 51 | + }) |
| 52 | + |
| 53 | + it('should handle graphs with no edges', () => { |
| 54 | + const graph: [number, number][][] = [ |
| 55 | + [], // Node 0: No outgoing edges |
| 56 | + [], // Node 1: No outgoing edges |
| 57 | + [] // Node 2: No outgoing edges |
| 58 | + ] |
| 59 | + const source = 0 |
| 60 | + const sink = 2 |
| 61 | + const maxFlow = edmondsKarp(graph, source, sink) |
| 62 | + expect(maxFlow).toBe(0) |
| 63 | + }) |
| 64 | + |
| 65 | + it('should handle graphs with self-loops', () => { |
| 66 | + const graph: [number, number][][] = [ |
| 67 | + [ |
| 68 | + [0, 10], |
| 69 | + [1, 10] |
| 70 | + ], // Node 0: Self-loop with capacity 10, and edge to node 1 (capacity 10) |
| 71 | + [ |
| 72 | + [1, 10], |
| 73 | + [2, 10] |
| 74 | + ], // Node 1: Self-loop and edge to node 2 |
| 75 | + [] // Node 2: No outgoing edges (sink) |
| 76 | + ] |
| 77 | + const source = 0 |
| 78 | + const sink = 2 |
| 79 | + const maxFlow = edmondsKarp(graph, source, sink) |
| 80 | + expect(maxFlow).toBe(10) |
| 81 | + }) |
| 82 | +}) |
0 commit comments