Skip to content

Commit 64fdb9b

Browse files
committed
depht limited search for grapht
This performs a depth limited depth-first search on a grapht
1 parent 2111f7c commit 64fdb9b

File tree

1 file changed

+94
-0
lines changed

1 file changed

+94
-0
lines changed

src/util/graph.h

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,18 @@ class grapht
255255
void disconnect_unreachable(node_indext src);
256256
void disconnect_unreachable(const std::vector<node_indext> &src);
257257

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+
258270
void make_chordal();
259271

260272
// return value: number of connected subgraphs
@@ -278,6 +290,11 @@ class grapht
278290
std::function<void(const node_indext &)> f) const;
279291

280292
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+
281298
class tarjant
282299
{
283300
public:
@@ -584,6 +601,83 @@ std::vector<typename N::node_indext> grapht<N>::get_reachable(
584601
return result;
585602
}
586603

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+
587681
template<class N>
588682
std::size_t grapht<N>::connected_subgraphs(
589683
std::vector<node_indext> &subgraph_nr)

0 commit comments

Comments
 (0)