Skip to content

Commit 4b39f2c

Browse files
committed
[feature] Port algorithms.dag.descendants from Python
1 parent 16a1f88 commit 4b39f2c

1 file changed

Lines changed: 18 additions & 2 deletions

File tree

src/algorithms/dag.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,23 @@ import {
1515
gcd
1616
} from '../_internals';
1717

18-
// TODO: descendants
18+
/**
19+
* Returns all nodes reachable from `source` in `G`
20+
*
21+
* @param {Graph} G A NetworkX directed acyclic graph
22+
* @param {Node} Source: node in `G`
23+
* @return {Set} The descendants of `source` in `G`
24+
*/
25+
export async function descendants(G, source) {
26+
if (!G.hasNode(source)) {
27+
throw new JSNetworkXError(
28+
`The node ${source} is not in the graph`
29+
)
30+
}
31+
const descendantNodes = new Set(shortestPathLength(G, { source }).keys());
32+
descendantNodes.delete(source); // cannot be own parent
33+
return descendantNodes;
34+
}
1935

2036
/**
2137
* Returns all nodes having a path to `source` in `G`
@@ -30,7 +46,7 @@ export async function ancestors(G, source) {
3046
`The node ${source} is not in the graph`
3147
)
3248
}
33-
const ancestorNodes = new Set(shortestPathLength(G, { source }).keys());
49+
const ancestorNodes = new Set(shortestPathLength(G, { target: source }).keys());
3450
ancestorNodes.delete(source); // cannot be own parent
3551
return ancestorNodes;
3652
}

0 commit comments

Comments
 (0)