@@ -255,6 +255,18 @@ class grapht
255
255
void disconnect_unreachable (node_indext src);
256
256
void disconnect_unreachable (const std::vector<node_indext> &src);
257
257
258
+ std::vector<node_indext> depth_limited_search (
259
+ const node_indext &src,
260
+ std::size_t &limit,
261
+ bool forwards) const ;
262
+
263
+ std::vector<typename N::node_indext>
264
+ depth_limited_search (typename N::node_indext src, std::size_t limit) const ;
265
+
266
+ std::vector<typename N::node_indext> depth_limited_search (
267
+ std::vector<typename N::node_indext> &src,
268
+ std::size_t limit) const ;
269
+
258
270
void make_chordal ();
259
271
260
272
// return value: number of connected subgraphs
@@ -278,6 +290,11 @@ class grapht
278
290
std::function<void (const node_indext &)> f) const ;
279
291
280
292
protected:
293
+ std::vector<typename N::node_indext> depth_limited_search (
294
+ std::vector<typename N::node_indext> &src,
295
+ std::size_t limit,
296
+ std::vector<bool > &visited) const ;
297
+
281
298
class tarjant
282
299
{
283
300
public:
@@ -584,6 +601,83 @@ std::vector<typename N::node_indext> grapht<N>::get_reachable(
584
601
return result;
585
602
}
586
603
604
+ // / Run recursive depth-limited search on the graph, starting
605
+ // / from multiple source nodes, to find the nodes reachable within n steps.
606
+ // / This function initialises the search.
607
+ // / \param src The node to start the search from.
608
+ // / \param limit limit on steps
609
+ // / \return a vector of reachable node indices
610
+ template <class N >
611
+ std::vector<typename N::node_indext> grapht<N>::depth_limited_search(
612
+ const typename N::node_indext src,
613
+ std::size_t limit) const
614
+ {
615
+ std::vector<node_indext> start_vector (1 , src);
616
+ return depth_limited_search (start_vector, limit);
617
+ }
618
+
619
+ // / Run recursive depth-limited search on the graph, starting
620
+ // / from multiple source nodes, to find the nodes reachable within n steps.
621
+ // / This function initialises the search.
622
+ // / \param src The nodes to start the search from.
623
+ // / \param limit limit on steps
624
+ // / \return a vector of reachable node indices
625
+ template <class N >
626
+ std::vector<typename N::node_indext> grapht<N>::depth_limited_search(
627
+ std::vector<typename N::node_indext> &src,
628
+ std::size_t limit) const
629
+ {
630
+ std::vector<bool > visited (nodes.size (), false );
631
+
632
+ for (const auto &node : src)
633
+ {
634
+ PRECONDITION (node < nodes.size ());
635
+ visited[node] = true ;
636
+ }
637
+
638
+ return depth_limited_search (src, limit, visited);
639
+ }
640
+
641
+ // / Run recursive depth-limited search on the graph, starting
642
+ // from multiple source nodes, to find the nodes reachable within n steps
643
+ // / \param src The nodes to start the search from.
644
+ // / \param limit limit on steps
645
+ // / \param visited vector of booleans indicating whether a node has been visited
646
+ // / \return a vector of reachable node indices
647
+ template <class N >
648
+ std::vector<typename N::node_indext> grapht<N>::depth_limited_search(
649
+ std::vector<typename N::node_indext> &src,
650
+ std::size_t limit,
651
+ std::vector<bool > &visited) const
652
+ {
653
+ if (limit == 0 )
654
+ return src;
655
+
656
+ std::vector<node_indext> next_ring;
657
+
658
+ for (const auto &n : src)
659
+ {
660
+ for (const auto &o : nodes[n].out )
661
+ {
662
+ if (!visited[o.first ])
663
+ {
664
+ next_ring.push_back (o.first );
665
+ visited[o.first ] = true ;
666
+ }
667
+ }
668
+ }
669
+
670
+ if (next_ring.empty ())
671
+ return src;
672
+
673
+ limit--;
674
+
675
+ for (const auto &succ : depth_limited_search (next_ring, limit, visited))
676
+ src.push_back (succ);
677
+
678
+ return src;
679
+ }
680
+
587
681
template <class N >
588
682
std::size_t grapht<N>::connected_subgraphs(
589
683
std::vector<node_indext> &subgraph_nr)
0 commit comments