Commit 59427bf3 authored by Tiago Peixoto's avatar Tiago Peixoto
Browse files

Improve performance of Graph.edge(s,t) when the degrees of the vertices are unbalanced

parent 8caa6fa4
......@@ -250,13 +250,34 @@ struct get_edge_dispatch
bool all_edges, boost::python::list& es) const
{
std::shared_ptr<Graph> gp = retrieve_graph_view<Graph>(gi, g);
for (auto e : out_edges_range(vertex(s, g), g))
size_t k_t = is_directed::apply<Graph>::type::value ?
in_degreeS()(t, g) : out_degree(t, g);
if (out_degree(s, g) <= k_t)
{
if (target(e, g) == vertex(t, g))
for (auto e : out_edges_range(vertex(s, g), g))
{
es.append(PythonEdge<Graph>(gp, e));
if (!all_edges)
break;
if (target(e, g) == vertex(t, g))
{
es.append(PythonEdge<Graph>(gp, e));
if (!all_edges)
break;
}
}
}
else
{
for (auto e : in_or_out_edges_range(vertex(t, g), g))
{
auto w = is_directed::apply<Graph>::type::value ?
source(e, g) : target(e, g);
if (w == vertex(s, g))
{
if (!is_directed::apply<Graph>::type::value)
e.inv ^= true;
es.append(PythonEdge<Graph>(gp, e));
if (!all_edges)
break;
}
}
}
}
......
......@@ -1763,8 +1763,9 @@ class Graph(object):
If ``add_missing == True``, a new edge is created and returned, if none
currently exists.
This operation will take :math:`O(k(s))` time, where :math:`k(s)` is the
out-degree of vertex :math:`s`.
This operation will take :math:`O(min(k(s), k(t)))` time, where
:math:`k(s)` and :math:`k(t)` are the out-degree and in-degree (or
out-degree if undirected) of vertices :math:`s` and :math:`t`.
"""
s = self.vertex(int(s))
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment