diff --git a/src/data-structures/graph/directed-graph.ts b/src/data-structures/graph/directed-graph.ts index d1cc26d..762e80f 100644 --- a/src/data-structures/graph/directed-graph.ts +++ b/src/data-structures/graph/directed-graph.ts @@ -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); diff --git a/test/unit/data-structures/graph/directed-graph.test.ts b/test/unit/data-structures/graph/directed-graph.test.ts index 6fa46e2..0dc08d0 100644 --- a/test/unit/data-structures/graph/directed-graph.test.ts +++ b/test/unit/data-structures/graph/directed-graph.test.ts @@ -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']); + }); +})