mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-12-25 22:46:20 +08:00
Make it possible to reverse the graph.
This commit is contained in:
parent
ff8f9c49df
commit
0c2561197a
@ -138,6 +138,26 @@ export default class Graph {
|
||||
}, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse all the edges in directed graph.
|
||||
* @return {Graph}
|
||||
*/
|
||||
reverse() {
|
||||
/** @param {GraphEdge} edge */
|
||||
this.getAllEdges().forEach((edge) => {
|
||||
// Delete straight edge from graph and from vertices.
|
||||
this.deleteEdge(edge);
|
||||
|
||||
// Reverse the edge.
|
||||
edge.reverse();
|
||||
|
||||
// Add reversed edge back to the graph and its vertices.
|
||||
this.addEdge(edge);
|
||||
});
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return {string}
|
||||
*/
|
||||
|
@ -20,6 +20,17 @@ export default class GraphEdge {
|
||||
return `${startVertexKey}_${endVertexKey}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return {GraphEdge}
|
||||
*/
|
||||
reverse() {
|
||||
const tmp = this.startVertex;
|
||||
this.startVertex = this.endVertex;
|
||||
this.endVertex = tmp;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return {string}
|
||||
*/
|
||||
|
@ -261,4 +261,43 @@ describe('Graph', () => {
|
||||
|
||||
expect(deleteNotExistingEdge).toThrowError();
|
||||
});
|
||||
|
||||
it('should be possible to reverse graph', () => {
|
||||
const vertexA = new GraphVertex('A');
|
||||
const vertexB = new GraphVertex('B');
|
||||
const vertexC = new GraphVertex('C');
|
||||
const vertexD = new GraphVertex('D');
|
||||
|
||||
const edgeAB = new GraphEdge(vertexA, vertexB);
|
||||
const edgeAC = new GraphEdge(vertexA, vertexC);
|
||||
const edgeCD = new GraphEdge(vertexC, vertexD);
|
||||
|
||||
const graph = new Graph(true);
|
||||
graph
|
||||
.addEdge(edgeAB)
|
||||
.addEdge(edgeAC)
|
||||
.addEdge(edgeCD);
|
||||
|
||||
expect(graph.toString()).toBe('A,B,C,D');
|
||||
expect(graph.getAllEdges().length).toBe(3);
|
||||
expect(graph.getNeighbors(vertexA).length).toBe(2);
|
||||
expect(graph.getNeighbors(vertexA)[0].getKey()).toBe(vertexB.getKey());
|
||||
expect(graph.getNeighbors(vertexA)[1].getKey()).toBe(vertexC.getKey());
|
||||
expect(graph.getNeighbors(vertexB).length).toBe(0);
|
||||
expect(graph.getNeighbors(vertexC).length).toBe(1);
|
||||
expect(graph.getNeighbors(vertexC)[0].getKey()).toBe(vertexD.getKey());
|
||||
expect(graph.getNeighbors(vertexD).length).toBe(0);
|
||||
|
||||
graph.reverse();
|
||||
|
||||
expect(graph.toString()).toBe('A,B,C,D');
|
||||
expect(graph.getAllEdges().length).toBe(3);
|
||||
expect(graph.getNeighbors(vertexA).length).toBe(0);
|
||||
expect(graph.getNeighbors(vertexB).length).toBe(1);
|
||||
expect(graph.getNeighbors(vertexB)[0].getKey()).toBe(vertexA.getKey());
|
||||
expect(graph.getNeighbors(vertexC).length).toBe(1);
|
||||
expect(graph.getNeighbors(vertexC)[0].getKey()).toBe(vertexA.getKey());
|
||||
expect(graph.getNeighbors(vertexD).length).toBe(1);
|
||||
expect(graph.getNeighbors(vertexD)[0].getKey()).toBe(vertexC.getKey());
|
||||
});
|
||||
});
|
||||
|
@ -23,4 +23,20 @@ describe('GraphEdge', () => {
|
||||
expect(edge.endVertex).toEqual(endVertex);
|
||||
expect(edge.weight).toEqual(10);
|
||||
});
|
||||
|
||||
it('should be possible to do edge reverse', () => {
|
||||
const vertexA = new GraphVertex('A');
|
||||
const vertexB = new GraphVertex('B');
|
||||
const edge = new GraphEdge(vertexA, vertexB, 10);
|
||||
|
||||
expect(edge.startVertex).toEqual(vertexA);
|
||||
expect(edge.endVertex).toEqual(vertexB);
|
||||
expect(edge.weight).toEqual(10);
|
||||
|
||||
edge.reverse();
|
||||
|
||||
expect(edge.startVertex).toEqual(vertexB);
|
||||
expect(edge.endVertex).toEqual(vertexA);
|
||||
expect(edge.weight).toEqual(10);
|
||||
});
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user