|
| 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 articulationPoints from '../articulationPoints'; |
| 5 | + |
| 6 | +describe('articulationPoints', () => { |
| 7 | + it('should find articulation points 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 articulationPointsSet = articulationPoints(graph); |
| 25 | + |
| 26 | + expect(articulationPointsSet).toEqual([ |
| 27 | + vertexC, |
| 28 | + vertexB, |
| 29 | + ]); |
| 30 | + }); |
| 31 | + |
| 32 | + it('should find articulation points 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 articulationPointsSet = articulationPoints(graph); |
| 52 | + |
| 53 | + expect(articulationPointsSet).toEqual([ |
| 54 | + vertexC, |
| 55 | + ]); |
| 56 | + }); |
| 57 | + |
| 58 | + it('should find articulation points in graph', () => { |
| 59 | + const vertexA = new GraphVertex('A'); |
| 60 | + const vertexB = new GraphVertex('B'); |
| 61 | + const vertexC = new GraphVertex('C'); |
| 62 | + const vertexD = new GraphVertex('D'); |
| 63 | + const vertexE = new GraphVertex('E'); |
| 64 | + const vertexF = new GraphVertex('F'); |
| 65 | + const vertexG = new GraphVertex('G'); |
| 66 | + const vertexH = new GraphVertex('H'); |
| 67 | + |
| 68 | + const edgeAB = new GraphEdge(vertexA, vertexB); |
| 69 | + const edgeBC = new GraphEdge(vertexB, vertexC); |
| 70 | + const edgeAC = new GraphEdge(vertexA, vertexC); |
| 71 | + const edgeCD = new GraphEdge(vertexC, vertexD); |
| 72 | + const edgeDE = new GraphEdge(vertexD, vertexE); |
| 73 | + const edgeEG = new GraphEdge(vertexE, vertexG); |
| 74 | + const edgeEF = new GraphEdge(vertexE, vertexF); |
| 75 | + const edgeGF = new GraphEdge(vertexG, vertexF); |
| 76 | + const edgeFH = new GraphEdge(vertexF, vertexH); |
| 77 | + |
| 78 | + const graph = new Graph(); |
| 79 | + |
| 80 | + graph |
| 81 | + .addEdge(edgeAB) |
| 82 | + .addEdge(edgeBC) |
| 83 | + .addEdge(edgeAC) |
| 84 | + .addEdge(edgeCD) |
| 85 | + .addEdge(edgeDE) |
| 86 | + .addEdge(edgeEG) |
| 87 | + .addEdge(edgeEF) |
| 88 | + .addEdge(edgeGF) |
| 89 | + .addEdge(edgeFH); |
| 90 | + |
| 91 | + const articulationPointsSet = articulationPoints(graph); |
| 92 | + |
| 93 | + expect(articulationPointsSet).toEqual([ |
| 94 | + vertexF, |
| 95 | + vertexE, |
| 96 | + vertexD, |
| 97 | + vertexC, |
| 98 | + ]); |
| 99 | + }); |
| 100 | + |
| 101 | + it('should find articulation points in graph starting with articulation root vertex', () => { |
| 102 | + const vertexA = new GraphVertex('A'); |
| 103 | + const vertexB = new GraphVertex('B'); |
| 104 | + const vertexC = new GraphVertex('C'); |
| 105 | + const vertexD = new GraphVertex('D'); |
| 106 | + const vertexE = new GraphVertex('E'); |
| 107 | + const vertexF = new GraphVertex('F'); |
| 108 | + const vertexG = new GraphVertex('G'); |
| 109 | + const vertexH = new GraphVertex('H'); |
| 110 | + |
| 111 | + const edgeAB = new GraphEdge(vertexA, vertexB); |
| 112 | + const edgeBC = new GraphEdge(vertexB, vertexC); |
| 113 | + const edgeAC = new GraphEdge(vertexA, vertexC); |
| 114 | + const edgeCD = new GraphEdge(vertexC, vertexD); |
| 115 | + const edgeDE = new GraphEdge(vertexD, vertexE); |
| 116 | + const edgeEG = new GraphEdge(vertexE, vertexG); |
| 117 | + const edgeEF = new GraphEdge(vertexE, vertexF); |
| 118 | + const edgeGF = new GraphEdge(vertexG, vertexF); |
| 119 | + const edgeFH = new GraphEdge(vertexF, vertexH); |
| 120 | + |
| 121 | + const graph = new Graph(); |
| 122 | + |
| 123 | + graph |
| 124 | + .addEdge(edgeDE) |
| 125 | + .addEdge(edgeAB) |
| 126 | + .addEdge(edgeBC) |
| 127 | + .addEdge(edgeAC) |
| 128 | + .addEdge(edgeCD) |
| 129 | + .addEdge(edgeEG) |
| 130 | + .addEdge(edgeEF) |
| 131 | + .addEdge(edgeGF) |
| 132 | + .addEdge(edgeFH); |
| 133 | + |
| 134 | + const articulationPointsSet = articulationPoints(graph); |
| 135 | + |
| 136 | + expect(articulationPointsSet).toEqual([ |
| 137 | + vertexF, |
| 138 | + vertexE, |
| 139 | + vertexC, |
| 140 | + vertexD, |
| 141 | + ]); |
| 142 | + }); |
| 143 | +}); |
0 commit comments