This commit is contained in:
Revone 2024-03-16 09:59:48 +08:00
parent ec8242044a
commit 5dc132cb2a
2 changed files with 30 additions and 1 deletions

View file

@ -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);

View file

@ -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']);
});
})