| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -138,6 +138,26 @@ export default class Graph { | |||
| 138 | 138 | }, 0); | |
| 139 | 139 | } | |
| 140 | 140 | ||
| 141 | + /** | ||
| 142 | + * Reverse all the edges in directed graph. | ||
| 143 | + * @return {Graph} | ||
| 144 | + */ | ||
| 145 | + reverse() { | ||
| 146 | + /** @param {GraphEdge} edge */ | ||
| 147 | + this.getAllEdges().forEach((edge) => { | ||
| 148 | + // Delete straight edge from graph and from vertices. | ||
| 149 | + this.deleteEdge(edge); | ||
| 150 | + | ||
| 151 | + // Reverse the edge. | ||
| 152 | + edge.reverse(); | ||
| 153 | + | ||
| 154 | + // Add reversed edge back to the graph and its vertices. | ||
| 155 | + this.addEdge(edge); | ||
| 156 | + }); | ||
| 157 | + | ||
| 158 | + return this; | ||
| 159 | + } | ||
| 160 | + | ||
| 141 | 161 | /** | |
| 142 | 162 | * @return {string} | |
| 143 | 163 | */ | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -20,6 +20,17 @@ export default class GraphEdge { | |||
| 20 | 20 | return `${startVertexKey}_${endVertexKey}`; | |
| 21 | 21 | } | |
| 22 | 22 | ||
| 23 | + /** | ||
| 24 | + * @return {GraphEdge} | ||
| 25 | + */ | ||
| 26 | + reverse() { | ||
| 27 | + const tmp = this.startVertex; | ||
| 28 | + this.startVertex = this.endVertex; | ||
| 29 | + this.endVertex = tmp; | ||
| 30 | + | ||
| 31 | + return this; | ||
| 32 | + } | ||
| 33 | + | ||
| 23 | 34 | /** | |
| 24 | 35 | * @return {string} | |
| 25 | 36 | */ | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -261,4 +261,43 @@ describe('Graph', () => { | |||
| 261 | 261 | ||
| 262 | 262 | expect(deleteNotExistingEdge).toThrowError(); | |
| 263 | 263 | }); | |
| 264 | + | ||
| 265 | + it('should be possible to reverse graph', () => { | ||
| 266 | + const vertexA = new GraphVertex('A'); | ||
| 267 | + const vertexB = new GraphVertex('B'); | ||
| 268 | + const vertexC = new GraphVertex('C'); | ||
| 269 | + const vertexD = new GraphVertex('D'); | ||
| 270 | + | ||
| 271 | + const edgeAB = new GraphEdge(vertexA, vertexB); | ||
| 272 | + const edgeAC = new GraphEdge(vertexA, vertexC); | ||
| 273 | + const edgeCD = new GraphEdge(vertexC, vertexD); | ||
| 274 | + | ||
| 275 | + const graph = new Graph(true); | ||
| 276 | + graph | ||
| 277 | + .addEdge(edgeAB) | ||
| 278 | + .addEdge(edgeAC) | ||
| 279 | + .addEdge(edgeCD); | ||
| 280 | + | ||
| 281 | + expect(graph.toString()).toBe('A,B,C,D'); | ||
| 282 | + expect(graph.getAllEdges().length).toBe(3); | ||
| 283 | + expect(graph.getNeighbors(vertexA).length).toBe(2); | ||
| 284 | + expect(graph.getNeighbors(vertexA)[0].getKey()).toBe(vertexB.getKey()); | ||
| 285 | + expect(graph.getNeighbors(vertexA)[1].getKey()).toBe(vertexC.getKey()); | ||
| 286 | + expect(graph.getNeighbors(vertexB).length).toBe(0); | ||
| 287 | + expect(graph.getNeighbors(vertexC).length).toBe(1); | ||
| 288 | + expect(graph.getNeighbors(vertexC)[0].getKey()).toBe(vertexD.getKey()); | ||
| 289 | + expect(graph.getNeighbors(vertexD).length).toBe(0); | ||
| 290 | + | ||
| 291 | + graph.reverse(); | ||
| 292 | + | ||
| 293 | + expect(graph.toString()).toBe('A,B,C,D'); | ||
| 294 | + expect(graph.getAllEdges().length).toBe(3); | ||
| 295 | + expect(graph.getNeighbors(vertexA).length).toBe(0); | ||
| 296 | + expect(graph.getNeighbors(vertexB).length).toBe(1); | ||
| 297 | + expect(graph.getNeighbors(vertexB)[0].getKey()).toBe(vertexA.getKey()); | ||
| 298 | + expect(graph.getNeighbors(vertexC).length).toBe(1); | ||
| 299 | + expect(graph.getNeighbors(vertexC)[0].getKey()).toBe(vertexA.getKey()); | ||
| 300 | + expect(graph.getNeighbors(vertexD).length).toBe(1); | ||
| 301 | + expect(graph.getNeighbors(vertexD)[0].getKey()).toBe(vertexC.getKey()); | ||
| 302 | + }); | ||
| 264 | 303 | }); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -23,4 +23,20 @@ describe('GraphEdge', () => { | |||
| 23 | 23 | expect(edge.endVertex).toEqual(endVertex); | |
| 24 | 24 | expect(edge.weight).toEqual(10); | |
| 25 | 25 | }); | |
| 26 | + | ||
| 27 | + it('should be possible to do edge reverse', () => { | ||
| 28 | + const vertexA = new GraphVertex('A'); | ||
| 29 | + const vertexB = new GraphVertex('B'); | ||
| 30 | + const edge = new GraphEdge(vertexA, vertexB, 10); | ||
| 31 | + | ||
| 32 | + expect(edge.startVertex).toEqual(vertexA); | ||
| 33 | + expect(edge.endVertex).toEqual(vertexB); | ||
| 34 | + expect(edge.weight).toEqual(10); | ||
| 35 | + | ||
| 36 | + edge.reverse(); | ||
| 37 | + | ||
| 38 | + expect(edge.startVertex).toEqual(vertexB); | ||
| 39 | + expect(edge.endVertex).toEqual(vertexA); | ||
| 40 | + expect(edge.weight).toEqual(10); | ||
| 41 | + }); | ||
| 26 | 42 | }); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments