You either need to maintain a reversed graph, or create a wrapper over the graph that reverses every edge.
QuickGraph has the ReversedBidirectionalGraph class that is a wrapper intended just for that, but it does not seem to work with the algorithm classes because of generic type incompatibility.
I had to create my own wrapper class:
class ReversedBidirectionalGraphWrapper<TVertex, TEdge> : IVertexListGraph<TVertex, TEdge> where TEdge : IEdge<TVertex>
{
private BidirectionalGraph<TVertex, TEdge> _originalGraph;
public IEnumerable<TEdge> OutEdges(TVertex v)
{
foreach (var e in _graph.InEdges(v))
{
yield return (TEdge)Convert.ChangeType(new Edge<TVertex>(e.Target, e.Source), typeof(TEdge));
}
} //...implement the rest of the interface members using the same trick
}
Then run DFS or BFS on this wrapper:
var w = new ReversedBidirectionalGraphWrapper<int, Edge<int>>(graph);
var result = new List<int>();
var alg = new DepthFirstSearchAlgorithm<int, Edge<int>>(w);
alg.TreeEdge += e => result.Add(e.Target);
alg.Compute(node);
Doug's answer is not correct, because DFS will only visit the downstream subgraph. The predecessor observer does not help.