File tree 1 file changed +27
-11
lines changed 1 file changed +27
-11
lines changed Original file line number Diff line number Diff line change @@ -246,6 +246,8 @@ class grapht
246
246
247
247
void visit_reachable (node_indext src);
248
248
249
+ std::vector<node_indext> get_reachable (node_indext src, bool forwards) const ;
250
+
249
251
void make_chordal ();
250
252
251
253
// return value: number of connected subgraphs
@@ -447,26 +449,40 @@ void grapht<N>::shortest_path(
447
449
template <class N >
448
450
void grapht<N>::visit_reachable(node_indext src)
449
451
{
450
- // DFS
452
+ std::vector<node_indext> reachable = get_reachable (src, true );
453
+ for (const auto index : reachable)
454
+ nodes[index ].visited = true ;
455
+ }
456
+
457
+ template <class N >
458
+ std::vector<typename N::node_indext>
459
+ grapht<N>::get_reachable(node_indext src, bool forwards) const
460
+ {
461
+ std::vector<node_indext> result;
462
+ std::vector<bool > visited (size (), false );
451
463
452
- std::stack<node_indext> s;
464
+ std::stack<node_indext, std::vector<node_indext> > s;
453
465
s.push (src);
454
466
455
467
while (!s.empty ())
456
468
{
457
- node_indext n= s.top ();
469
+ node_indext n = s.top ();
458
470
s.pop ();
459
471
460
- nodet &node=nodes [n];
461
- node. visited = true ;
472
+ if (visited [n])
473
+ continue ;
462
474
463
- for (typename edgest::const_iterator
464
- it=node.out .begin ();
465
- it!=node.out .end ();
466
- it++)
467
- if (!nodes[it->first ].visited )
468
- s.push (it->first );
475
+ result.push_back (n);
476
+ visited[n] = true ;
477
+
478
+ const auto &node = nodes[n];
479
+ const auto &succs = forwards ? node.out : node.in ;
480
+ for (const auto succ : succs)
481
+ if (!visited[succ.first ])
482
+ s.push (succ.first );
469
483
}
484
+
485
+ return result;
470
486
}
471
487
472
488
template <class N >
You can’t perform that action at this time.
0 commit comments