mirror of
https://github.com/zrwusa/data-structure-typed.git
synced 2024-11-23 12:54:04 +00:00
fix: #86
This commit is contained in:
parent
ec8242044a
commit
5dc132cb2a
|
@ -261,7 +261,8 @@ export class DirectedGraph<
|
|||
if (vertex) {
|
||||
const neighbors = this.getNeighbors(vertex);
|
||||
for (const neighbor of neighbors) {
|
||||
this._inEdgeMap.delete(neighbor);
|
||||
// this._inEdgeMap.delete(neighbor);
|
||||
this.deleteEdgeSrcToDest(vertex, neighbor);
|
||||
}
|
||||
this._outEdgeMap.delete(vertex);
|
||||
this._inEdgeMap.delete(vertex);
|
||||
|
|
|
@ -995,3 +995,31 @@ describe('DirectedGraph tarjan', () => {
|
|||
expect(getAsVerticesArrays(sccs)).toEqual([['K', 'J', 'I', 'H', 'D', 'C', 'B'], ['G', 'F', 'E'], ['A']]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('delete', () => {
|
||||
it(`deleteVertex deletes all of it's neighbors from the inEdge Map`, () => {
|
||||
|
||||
const graph = new DirectedGraph();
|
||||
graph.addVertex('A');
|
||||
graph.addVertex('B');
|
||||
graph.addVertex('C');
|
||||
|
||||
graph.addEdge('B', 'A');
|
||||
graph.addEdge('C', 'A');
|
||||
|
||||
// 'Incoming to A should contain ['B','C']
|
||||
expect(graph.incomingEdgesOf('A').map((e) => e.src)).toEqual(['B','C']); // ['B','C']
|
||||
|
||||
// Now delete B, which has no direct link to C, only that C -> A.
|
||||
graph.deleteVertex('B');
|
||||
|
||||
// Now if we do the same call to incoming edges for we should get only ['C']
|
||||
expect(graph.incomingEdgesOf('A').map((e) => e.src)).toEqual(['C']); // [];
|
||||
|
||||
// but it only shows an empty array, since we deleted all of `A's edges, not just the one to `B`.
|
||||
|
||||
// If we check C, it correctly shows A as an outgoing edge,
|
||||
// even though A no longer has any knowledge of C linking to it.
|
||||
expect(graph.outgoingEdgesOf('C').map((e) => e.dest)).toEqual(['A']);
|
||||
});
|
||||
})
|
||||
|
|
Loading…
Reference in a new issue