Boost logo

Boost Users :

Subject: Re: [Boost-users] DAG Graph Search
From: Dan Bailey (drb_at_[hidden])
Date: 2009-10-19 10:58:44


Jeremiah Willcock wrote:
> On Thu, 8 Oct 2009, Dan Bailey wrote:
>
>> Hi,
>>
>> I'm looking to do a downstream search over a DAG graph where I never
>> advance past a node until all of the nodes linking to it have been seen.
>>
>> Clearly a breadth first search won't work, but using a topological
>> sort, I can get a list of all the nodes in the correct order. However
>> I only wish to search over the nodes downstream from any particular
>> node (until I reach the leaf nodes), which doesn't work as this is
>> the entire network even if it contains various sub-graphs.
>>
>> Is there a better algorithm or approach I can use to do this?
>>
>> Here's a simple example:
>>
>> a -> b
>> b -> c
>> c -> d
>> a -> c
>>
>> I wouldn't ever want to look at c before b, but if I started at b for
>> example, I would only want to see downstream of this.
>
> Could you please try:
>
> depth_first_visit(
> g,
> your_start_vertex,
> boost::topo_sort_visitor<YourOutputIterator>(your_output_iterator),
> color);
>
> (with color as a color map for your graph set to all white)? That
> should match what topological sort does but starting at a single
> vertex rather than all roots.
>
> -- Jeremiah Willcock
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users
>
This seems to have worked well for my graph, which is relatively small.
It also requires really fast traversal and doesn't care about the time
to add or remove an edge.

The solution I have opted for is as follows:

- cache the topological order into a vector
- iterate through the vector removing and adding each edge in turn (in
topological order)
- perform a breadth first search on the resulting graph (which is now
ordered by topological order)

Though this could clearly be optimized to speed it up if necessary, feel
free to explain to me why this approach might be a bad idea.

One thing I'd like to do is halt the search of a particular branch of
the graph if a certain condition is true, what would be the best way of
accomplishing this? Using predicates or pre-computing the color map to
satisfy this condition?

In my graph, it's often the case that a node may not have any edges at
all, which seems to crash the bgl. I've put in this little test to
ensure this never happens, but is this a common problem to have to work
around?

std::pair<OutEdgeIterator, OutEdgeIterator> edges = out_edges(index, graph);
if (edges.first == edges.second) return true;

Thanks,
Dan


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net