Skip to content

Commit

Permalink
fix: #86
Browse files Browse the repository at this point in the history
  • Loading branch information
zrwusa committed Mar 16, 2024
1 parent ec82420 commit 5dc132c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/data-structures/graph/directed-graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
28 changes: 28 additions & 0 deletions test/unit/data-structures/graph/directed-graph.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
});
})

0 comments on commit 5dc132c

Please sign in to comment.