Skip to content

Commit 0c25611

Browse files
committedMay 14, 2018
Make it possible to reverse the graph.
1 parent ff8f9c4 commit 0c25611

File tree

4 files changed

+86
-0
lines changed

4 files changed

+86
-0
lines changed
 

‎src/data-structures/graph/Graph.js

+20
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,26 @@ export default class Graph {
138138
}, 0);
139139
}
140140

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+
141161
/**
142162
* @return {string}
143163
*/

‎src/data-structures/graph/GraphEdge.js

+11
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,17 @@ export default class GraphEdge {
2020
return `${startVertexKey}_${endVertexKey}`;
2121
}
2222

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+
2334
/**
2435
* @return {string}
2536
*/

‎src/data-structures/graph/__test__/Graph.test.js

+39
Original file line numberDiff line numberDiff line change
@@ -261,4 +261,43 @@ describe('Graph', () => {
261261

262262
expect(deleteNotExistingEdge).toThrowError();
263263
});
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+
});
264303
});

‎src/data-structures/graph/__test__/GraphEdge.test.js

+16
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,20 @@ describe('GraphEdge', () => {
2323
expect(edge.endVertex).toEqual(endVertex);
2424
expect(edge.weight).toEqual(10);
2525
});
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+
});
2642
});

0 commit comments

Comments
 (0)
Please sign in to comment.