Use a Graph Library/Node Network Library or Write My Own?

后端 未结 9 1849
逝去的感伤
逝去的感伤 2020-12-23 22:23

I\'m trying to decide between going with a pre-made graph/node network library or to roll my own.

I\'m implementing some graph search algorithms which might require

9条回答
  •  攒了一身酷
    2020-12-23 22:52

    I can perhaps provide a little guidance on the BGL.

    The library is very flexible. The cost of this is that the syntax can be very baroque, in order to accommodate all the possibilities. However, it is sufficiently flexible that simple things can be done simply.

    Unfortunately the boost documentation goes at things full tilt, providing a description only of the full complexity, without a hint of how simple things can be.

    ( "Any sufficiently advanced technology is indistinguishable from magic" - Arthur C. Clarke. What he should have said is "Any advanced technology, sufficiently badly documented, is indistinguishable from magic )

    Consider:

    typedef property_map::type IndexMap;
    IndexMap index = get(vertex_index, g);
    typedef graph_traits::vertex_iterator vertex_iter;
    std::pair vp;
    for (vp = vertices(g); vp.first != vp.second; ++vp.first) {
       std::cout << index[*vp.first] <<  " ";
    }
    

    This is how the "quick tour" suggests we print out a list of the graph vertices. However, a little study shows that a vertex is no more than an integer index, and the code can be greatly simplified to

    for (int v = *vertices(g).first; v != *vertices(g).second; ++v)
        std::cout << v << " ";
    

    Perhaps there are some magical things that cannot be achieved with this simplified code, but for every day use it reasonable to drastically prune the syntax that encrust BGL so you can see what your are coding.

    Sometimes the elaborate syntax cannot be removed. ( Or perhaps I have just not noticed the underlying truth ). Then I usually use a little utility function to encapsulate the complexity abd keep it away from the algorithm I am working on.

    For example, I often need to loop over the children of a vertex

    vector getVertexChildren( int v )
    {
        vector vc;
        typedef std::pair::out_edge_iterator, graph_traits::out_edge_iterator> out_edge_iter_pair_t;
        for( out_edge_iter_pair_t ep = out_edges(v,m_tree);
            ep.first != ep.second; ++(ep.first))
        {
            vc.push_back( target( *ep.first, m_tree ) );
        }
        return vc;
    }
    #define FOR_ALL_CHILDREN( v ) vector vc=getVertexChildren(v); BOOST_FOR_EACH( int child, vc )
    

    The bottom line is: go ahead and use BGL. It can be simplified to do simple things, but once you have learned to use it, all the immense flexibility will be available whenever you do need it.

提交回复
热议问题