|
| 1 | +import GraphVertex from '../../../../data-structures/graph/GraphVertex'; |
| 2 | +import GraphEdge from '../../../../data-structures/graph/GraphEdge'; |
| 3 | +import Graph from '../../../../data-structures/graph/Graph'; |
| 4 | +import graphBridges from '../graphBridges'; |
| 5 | + |
| 6 | +describe('graphBridges', () => { |
| 7 | + it('should find bridges in simple graph', () => { |
| 8 | + const vertexA = new GraphVertex('A'); |
| 9 | + const vertexB = new GraphVertex('B'); |
| 10 | + const vertexC = new GraphVertex('C'); |
| 11 | + const vertexD = new GraphVertex('D'); |
| 12 | + |
| 13 | + const edgeAB = new GraphEdge(vertexA, vertexB); |
| 14 | + const edgeBC = new GraphEdge(vertexB, vertexC); |
| 15 | + const edgeCD = new GraphEdge(vertexC, vertexD); |
| 16 | + |
| 17 | + const graph = new Graph(); |
| 18 | + |
| 19 | + graph |
| 20 | + .addEdge(edgeAB) |
| 21 | + .addEdge(edgeBC) |
| 22 | + .addEdge(edgeCD); |
| 23 | + |
| 24 | + const bridges = graphBridges(graph); |
| 25 | + |
| 26 | + expect(bridges.length).toBe(3); |
| 27 | + expect(bridges[0].getKey()).toBe(edgeCD.getKey()); |
| 28 | + expect(bridges[1].getKey()).toBe(edgeBC.getKey()); |
| 29 | + expect(bridges[2].getKey()).toBe(edgeAB.getKey()); |
| 30 | + }); |
| 31 | + |
| 32 | + it('should find bridges in simple graph with back edge', () => { |
| 33 | + const vertexA = new GraphVertex('A'); |
| 34 | + const vertexB = new GraphVertex('B'); |
| 35 | + const vertexC = new GraphVertex('C'); |
| 36 | + const vertexD = new GraphVertex('D'); |
| 37 | + |
| 38 | + const edgeAB = new GraphEdge(vertexA, vertexB); |
| 39 | + const edgeBC = new GraphEdge(vertexB, vertexC); |
| 40 | + const edgeCD = new GraphEdge(vertexC, vertexD); |
| 41 | + const edgeAC = new GraphEdge(vertexA, vertexC); |
| 42 | + |
| 43 | + const graph = new Graph(); |
| 44 | + |
| 45 | + graph |
| 46 | + .addEdge(edgeAB) |
| 47 | + .addEdge(edgeAC) |
| 48 | + .addEdge(edgeBC) |
| 49 | + .addEdge(edgeCD); |
| 50 | + |
| 51 | + const bridges = graphBridges(graph); |
| 52 | + |
| 53 | + expect(bridges.length).toBe(1); |
| 54 | + expect(bridges[0].getKey()).toBe(edgeCD.getKey()); |
| 55 | + }); |
| 56 | + |
| 57 | + it('should find bridges in graph', () => { |
| 58 | + const vertexA = new GraphVertex('A'); |
| 59 | + const vertexB = new GraphVertex('B'); |
| 60 | + const vertexC = new GraphVertex('C'); |
| 61 | + const vertexD = new GraphVertex('D'); |
| 62 | + const vertexE = new GraphVertex('E'); |
| 63 | + const vertexF = new GraphVertex('F'); |
| 64 | + const vertexG = new GraphVertex('G'); |
| 65 | + const vertexH = new GraphVertex('H'); |
| 66 | + |
| 67 | + const edgeAB = new GraphEdge(vertexA, vertexB); |
| 68 | + const edgeBC = new GraphEdge(vertexB, vertexC); |
| 69 | + const edgeAC = new GraphEdge(vertexA, vertexC); |
| 70 | + const edgeCD = new GraphEdge(vertexC, vertexD); |
| 71 | + const edgeDE = new GraphEdge(vertexD, vertexE); |
| 72 | + const edgeEG = new GraphEdge(vertexE, vertexG); |
| 73 | + const edgeEF = new GraphEdge(vertexE, vertexF); |
| 74 | + const edgeGF = new GraphEdge(vertexG, vertexF); |
| 75 | + const edgeFH = new GraphEdge(vertexF, vertexH); |
| 76 | + |
| 77 | + const graph = new Graph(); |
| 78 | + |
| 79 | + graph |
| 80 | + .addEdge(edgeAB) |
| 81 | + .addEdge(edgeBC) |
| 82 | + .addEdge(edgeAC) |
| 83 | + .addEdge(edgeCD) |
| 84 | + .addEdge(edgeDE) |
| 85 | + .addEdge(edgeEG) |
| 86 | + .addEdge(edgeEF) |
| 87 | + .addEdge(edgeGF) |
| 88 | + .addEdge(edgeFH); |
| 89 | + |
| 90 | + const bridges = graphBridges(graph); |
| 91 | + |
| 92 | + expect(bridges.length).toBe(3); |
| 93 | + expect(bridges[0].getKey()).toBe(edgeFH.getKey()); |
| 94 | + expect(bridges[1].getKey()).toBe(edgeDE.getKey()); |
| 95 | + expect(bridges[2].getKey()).toBe(edgeCD.getKey()); |
| 96 | + }); |
| 97 | + |
| 98 | + it('should find bridges in graph starting with different root vertex', () => { |
| 99 | + const vertexA = new GraphVertex('A'); |
| 100 | + const vertexB = new GraphVertex('B'); |
| 101 | + const vertexC = new GraphVertex('C'); |
| 102 | + const vertexD = new GraphVertex('D'); |
| 103 | + const vertexE = new GraphVertex('E'); |
| 104 | + const vertexF = new GraphVertex('F'); |
| 105 | + const vertexG = new GraphVertex('G'); |
| 106 | + const vertexH = new GraphVertex('H'); |
| 107 | + |
| 108 | + const edgeAB = new GraphEdge(vertexA, vertexB); |
| 109 | + const edgeBC = new GraphEdge(vertexB, vertexC); |
| 110 | + const edgeAC = new GraphEdge(vertexA, vertexC); |
| 111 | + const edgeCD = new GraphEdge(vertexC, vertexD); |
| 112 | + const edgeDE = new GraphEdge(vertexD, vertexE); |
| 113 | + const edgeEG = new GraphEdge(vertexE, vertexG); |
| 114 | + const edgeEF = new GraphEdge(vertexE, vertexF); |
| 115 | + const edgeGF = new GraphEdge(vertexG, vertexF); |
| 116 | + const edgeFH = new GraphEdge(vertexF, vertexH); |
| 117 | + |
| 118 | + const graph = new Graph(); |
| 119 | + |
| 120 | + graph |
| 121 | + .addEdge(edgeDE) |
| 122 | + .addEdge(edgeAB) |
| 123 | + .addEdge(edgeBC) |
| 124 | + .addEdge(edgeAC) |
| 125 | + .addEdge(edgeCD) |
| 126 | + .addEdge(edgeEG) |
| 127 | + .addEdge(edgeEF) |
| 128 | + .addEdge(edgeGF) |
| 129 | + .addEdge(edgeFH); |
| 130 | + |
| 131 | + const bridges = graphBridges(graph); |
| 132 | + |
| 133 | + expect(bridges.length).toBe(3); |
| 134 | + expect(bridges[0].getKey()).toBe(edgeFH.getKey()); |
| 135 | + expect(bridges[1].getKey()).toBe(edgeDE.getKey()); |
| 136 | + expect(bridges[2].getKey()).toBe(edgeCD.getKey()); |
| 137 | + }); |
| 138 | + |
| 139 | + it('should find bridges in yet another graph #1', () => { |
| 140 | + const vertexA = new GraphVertex('A'); |
| 141 | + const vertexB = new GraphVertex('B'); |
| 142 | + const vertexC = new GraphVertex('C'); |
| 143 | + const vertexD = new GraphVertex('D'); |
| 144 | + const vertexE = new GraphVertex('E'); |
| 145 | + |
| 146 | + const edgeAB = new GraphEdge(vertexA, vertexB); |
| 147 | + const edgeAC = new GraphEdge(vertexA, vertexC); |
| 148 | + const edgeBC = new GraphEdge(vertexB, vertexC); |
| 149 | + const edgeCD = new GraphEdge(vertexC, vertexD); |
| 150 | + const edgeDE = new GraphEdge(vertexD, vertexE); |
| 151 | + |
| 152 | + const graph = new Graph(); |
| 153 | + |
| 154 | + graph |
| 155 | + .addEdge(edgeAB) |
| 156 | + .addEdge(edgeAC) |
| 157 | + .addEdge(edgeBC) |
| 158 | + .addEdge(edgeCD) |
| 159 | + .addEdge(edgeDE); |
| 160 | + |
| 161 | + const bridges = graphBridges(graph); |
| 162 | + |
| 163 | + expect(bridges.length).toBe(2); |
| 164 | + expect(bridges[0].getKey()).toBe(edgeDE.getKey()); |
| 165 | + expect(bridges[1].getKey()).toBe(edgeCD.getKey()); |
| 166 | + }); |
| 167 | + |
| 168 | + it('should find bridges in yet another graph #2', () => { |
| 169 | + const vertexA = new GraphVertex('A'); |
| 170 | + const vertexB = new GraphVertex('B'); |
| 171 | + const vertexC = new GraphVertex('C'); |
| 172 | + const vertexD = new GraphVertex('D'); |
| 173 | + const vertexE = new GraphVertex('E'); |
| 174 | + const vertexF = new GraphVertex('F'); |
| 175 | + const vertexG = new GraphVertex('G'); |
| 176 | + |
| 177 | + const edgeAB = new GraphEdge(vertexA, vertexB); |
| 178 | + const edgeAC = new GraphEdge(vertexA, vertexC); |
| 179 | + const edgeBC = new GraphEdge(vertexB, vertexC); |
| 180 | + const edgeCD = new GraphEdge(vertexC, vertexD); |
| 181 | + const edgeCE = new GraphEdge(vertexC, vertexE); |
| 182 | + const edgeCF = new GraphEdge(vertexC, vertexF); |
| 183 | + const edgeEG = new GraphEdge(vertexE, vertexG); |
| 184 | + const edgeFG = new GraphEdge(vertexF, vertexG); |
| 185 | + |
| 186 | + const graph = new Graph(); |
| 187 | + |
| 188 | + graph |
| 189 | + .addEdge(edgeAB) |
| 190 | + .addEdge(edgeAC) |
| 191 | + .addEdge(edgeBC) |
| 192 | + .addEdge(edgeCD) |
| 193 | + .addEdge(edgeCE) |
| 194 | + .addEdge(edgeCF) |
| 195 | + .addEdge(edgeEG) |
| 196 | + .addEdge(edgeFG); |
| 197 | + |
| 198 | + const bridges = graphBridges(graph); |
| 199 | + |
| 200 | + expect(bridges.length).toBe(1); |
| 201 | + expect(bridges[0].getKey()).toBe(edgeCD.getKey()); |
| 202 | + }); |
| 203 | +}); |
0 commit comments