File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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}
You can’t perform that action at this time.
0 commit comments