Skip to content

Commit 9af83ae

Browse files
committed
fix: handle undirected graph neighbors correctly in graph adapter
1 parent 93749e6 commit 9af83ae

File tree

2 files changed

+4
-5
lines changed

2 files changed

+4
-5
lines changed

src/algorithms/extraction/path.unit.test.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,10 +178,7 @@ describe("findShortestPath", () => {
178178
}
179179
});
180180

181-
// Note: Backward traversal in undirected graphs has a known limitation
182-
// in GraphAdapter.getNeighbors - it always maps to edge.target instead
183-
// of handling the case where the node is the edge's target (should return source).
184-
it.skip("should find path in reverse direction for undirected graph", () => {
181+
it("should find path in reverse direction for undirected graph", () => {
185182
const graph = new Graph<TestNode, TestEdge>(false);
186183
graph.addNode(createNode("A"));
187184
graph.addNode(createNode("B"));

src/algorithms/graph/graph-adapter.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ export class GraphAdapter<N extends Node, E extends Edge> implements ReadableGra
4242
if (!result.ok) {
4343
return [];
4444
}
45-
return result.value.map(edge => edge.target);
45+
// For undirected graphs, getOutgoingEdges returns edges where node is
46+
// either source or target. We need to return the "other" node in each edge.
47+
return result.value.map(edge => edge.source === id ? edge.target : edge.source);
4648
}
4749

4850
getAllNodes(): N[] {

0 commit comments

Comments
 (0)