0

Running breadth-first search on an unweighted, directed graph on 2 vertices where each vertex is connected to the other yields a predecessor map where the source of the breadth-first search is not its own predecessor. The following program is sufficient to produce this behavior:

#include <vector>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/breadth_first_search.hpp>

using namespace boost;
using std::vector;

enum family { one, two, N };

typedef adjacency_list< vecS, vecS, directedS> Graph;
typedef graph_traits<Graph>::vertex_descriptor Vertex;

int main() {
    Graph g(N);

    const char *name[] = { "one", "two" };

    add_edge(one, two, g);
    add_edge(two, one, g);

    vector<Vertex> p(num_vertices(g));

    breadth_first_search(g, two, visitor(make_bfs_visitor(
                     record_predecessors(&p[0],
                             on_tree_edge()))));

    //At this point, p[0] == 1 and p[1] == 0

    return 0;
}

This seems to contradict the Boost Graph Library documentation. More importantly, the predecessor map should represent a spanning tree of the graph breadth-first search is run on, which is not the case when the source of the search is not its own predecessor.

Sam
  • 417
  • 1
  • 6
  • 13
  • 2
    ["Algorithms such as Dijkstra's and breadth-first search will not assign a predecessor to the source vertex (which is the root of the search tree). It is often useful to initialize the source vertex's predecessor to itself, thereby identifying the root vertex as the only vertex which is its own parent. When using an algorithm like depth-first search that creates a forest (multiple search trees) it is useful to initialize the predecessor of every vertex to itself. This way all the root nodes can be distinguished."](http://www.boost.org/libs/graph/doc/predecessor_recorder.html). – llonesmiz Aug 10 '13 at 09:23
  • That was the problem! I believe I misread that portion to mean that they would initialize the predecessor map for us. Out of curiosity, why didn't you post your response as an answer? – Sam Aug 11 '13 at 04:45

0 Answers0