Commit 5457d04f authored by Tiago Peixoto's avatar Tiago Peixoto
Browse files

Add 'edges' parameter to all_paths()

This fixes issue #505.
parent 7b85fa40
Loading
Loading
Loading
Loading
+28 −9
Original line number Diff line number Diff line
@@ -561,9 +561,9 @@ python::object do_get_all_shortest_paths(GraphInterface& gi, size_t s, size_t t,
}


template <class Graph, class Yield, class VMap>
template <bool edges, class Graph, class Yield, class VMap>
void get_all_paths(size_t s, size_t t, size_t cutoff, VMap visited,
                   Yield& yield, Graph& g)
                   Yield& yield, Graph& g, GraphInterface& gi)
{
    typedef typename graph_traits<Graph>::out_edge_iterator eiter_t;
    typedef std::pair<eiter_t, eiter_t> item_t;
@@ -587,12 +587,23 @@ void get_all_paths(size_t s, size_t t, size_t cutoff, VMap visited,
        auto v = target(*pos.first, g);

        if (v == t)
        {
            if (!edges)
            {
                vector<size_t> path = {s};
                for (auto& ei : stack)
                    path.push_back(target(*ei.first, g));

                yield(wrap_vector_owned<size_t>(path));
            }
            else
            {
                auto gp = retrieve_graph_view<Graph>(gi, g);
                boost::python::list path;
                for (auto& ei : stack)
                    path.append(PythonEdge<Graph>(gp, *ei.first));
                yield(path);
            }

            ++pos.first;
        }
@@ -613,7 +624,7 @@ void get_all_paths(size_t s, size_t t, size_t cutoff, VMap visited,
};

python::object do_get_all_paths(GraphInterface& gi, size_t s, size_t t,
                                size_t cutoff, boost::any avisited)
                                size_t cutoff, boost::any avisited, bool edges)
{
#ifdef HAVE_BOOST_COROUTINE
    typedef vprop_map_t<uint8_t>::type vprop_t;
@@ -621,9 +632,17 @@ python::object do_get_all_paths(GraphInterface& gi, size_t s, size_t t,
    auto dispatch = [&](auto& yield)
        {
            run_action<>()
            (gi, [&](auto& g) {get_all_paths(s, t, cutoff,
                (gi, [&](auto& g)
                {
                    if (edges)
                        get_all_paths<true>(s, t, cutoff,
                                            visited.get_unchecked(), yield,
                                            g, gi);
                    else
                        get_all_paths<false>(s, t, cutoff,
                                             visited.get_unchecked(), yield,
                                             g);})();
                                             g, gi);
                })();
        };
    return python::object(CoroGenerator(dispatch));
#else
+11 −6
Original line number Diff line number Diff line
@@ -1943,7 +1943,7 @@ def all_shortest_paths(g, source, target, weights=None, negative_weights=False,
        Source vertex of the search.
    target : :class:`~graph_tool.Vertex`
        Target vertex of the search.
    weights : :class:`~graph_tool.PropertyMap` (optional, default: None)
    weights : :class:`~graph_tool.PropertyMap` (optional, default: ``None``)
        The edge weights.
    negative_weights : ``bool`` (optional, default: ``False``)
        If ``True``, this will trigger the use of the Bellman-Ford algorithm.
@@ -2017,7 +2017,7 @@ def all_shortest_paths(g, source, target, weights=None, negative_weights=False,
                                                      _prop("v", g, all_preds_map))
    return path_iterator

def all_paths(g, source, target, cutoff=None):
def all_paths(g, source, target, cutoff=None, edges=False):
    """Return an iterator over all paths from `source` to `target`.

    Parameters
@@ -2028,14 +2028,17 @@ def all_paths(g, source, target, cutoff=None):
        Source vertex of the search.
    target : :class:`~graph_tool.Vertex`
        Target vertex of the search.
    cutoff : `int` (optional, default: None)
    cutoff : ``int`` (optional, default: ``None``)
        Maximum path length.
    edges : ``bool`` (optional, default: ``False``)
        If ``True``, the returned iterator is over edge descriptors.

    Returns
    -------
    path_iterator : iterator over a sequence of integers
    path_iterator : iterator over a sequence of integers (or :class:`~graph_tool.Edge`)
        Iterator over sequences of vertices from `source` to `target` in the
        path.
        path. If ``edges == True``, the iterator is over sequences of edge
        descriptors (:class:`~graph_tool.Edge`).

    Notes
    -----
@@ -2057,6 +2060,7 @@ def all_paths(g, source, target, cutoff=None):
    [13 64  2]
    [ 13 100   2]
    [ 13 106   2]

    """

    if cutoff is None:
@@ -2066,7 +2070,8 @@ def all_paths(g, source, target, cutoff=None):
                                                         int(source),
                                                         int(target),
                                                         cutoff,
                                                         _prop("v", g, visited))
                                                         _prop("v", g, visited),
                                                         edges)
    return path_iterator

def all_circuits(g, unique=False):