test/utils/bruteforce.cpp
/**
 * Attempts to assign ranks to the vertices in the graph g by performing
 * a search of the graph from the vertex 0. The produced ranking is not
 * necessarily normalized.
 * 
 * This function assumes that g is a tree and it also checks this fact.
 * That is achieved by counting the number of vertices and edges reachable
 * from vertex 0. If it is 'g.size()' and 'g.size() - 1' respectively, the graph
 * must be a tree.
 * 
 * If the graph is not a tree the content of 'rank' after calling this
 * function is undefined.
 */
Attempts to assign ranks to the vertices in the graph g by performing
a search of the graph from the vertex 0. The produced ranking is not
necessarily normalized.

This function assumes that g is a tree and it also checks this fact.
That is achieved by counting the number of vertices and edges reachable
from vertex 0. If it is 'g.size()' and 'g.size() - 1' respectively, the graph
must be a tree.

If the graph is not a tree the content of 'rank' after calling this
function is undefined.
Attempts to assign ranks to the vertices in the graph g by performing a search of the graph from the vertex 0 . The produced ranking is not necessarily normalized . This function assumes that g is a tree and it also checks this fact . That is achieved by counting the number of vertices and edges reachable from vertex 0 . If it is ' g . size () ' and ' g . size () - 1 ' respectively the graph must be a tree . If the graph is not a tree the content of ' rank ' after calling this function is undefined .
bool spanning_tree_to_ranks(const graph& g, vertex_map<int>& rank) {
    std::vector<vertex_t> todo;
    vertex_map<bool> done(g);
    size_t vertex_count = 0;
    size_t edge_count = 0;
    done[0] = true;
    todo.push_back(0);
    rank[0] = 0;
    while (!todo.empty()) {
        auto u = todo.back();
        todo.pop_back();
        vertex_count++;
        for (auto v : g.out_neighbours(u)) {
            edge_count++;
            if (!done[v]) {
                todo.push_back(v);
                done[v] = true;
                rank[v] = rank[u] + 1;
            }
        }
        for (auto v : g.in_neighbours(u)) {
            if (!done[v]) {
                todo.push_back(v);
                done[v] = true;
                rank[v] = rank[u] - 1;
            }
        }
    }
    return edge_count == g.size() - 1 && vertex_count == g.size();
}
bool spanning_tree_to_ranks ( const graph & g , vertex_map < int > & rank ) { std :: vector < vertex_t > todo ; vertex_map < bool > done ( g ) ; size_t vertex_count = 0 ; size_t edge_count = 0 ; done [ 0 ] = true ; todo . push_back ( 0 ) ; rank [ 0 ] = 0 ; while ( ! todo . empty ( ) ) { auto u = todo . back ( ) ; todo . pop_back ( ) ; vertex_count ++ ; for ( auto v : g . out_neighbours ( u ) ) { edge_count ++ ; if ( ! done [ v ] ) { todo . push_back ( v ) ; done [ v ] = true ; rank [ v ] = rank [ u ] + 1 ; } } for ( auto v : g . in_neighbours ( u ) ) { if ( ! done [ v ] ) { todo . push_back ( v ) ; done [ v ] = true ; rank [ v ] = rank [ u ] - 1 ; } } } return edge_count == g . size ( ) - 1 && vertex_count == g . size ( ) ; }
Attempts to assign ranks to the vertices in the graph g by performing
 a search of the graph from the vertex 0.
Attempts to assign ranks to the vertices in the graph g by performing a search of the graph from the vertex 0 .