Commit 08d96f56 authored by Tiago Peixoto's avatar Tiago Peixoto
Browse files

Adds support for removal of labeled edges, self-loops and parallel-edges

parent 58e45bfd
......@@ -477,6 +477,12 @@ struct never_reversed:
get_all_graph_views::apply<scalar_pairs,mpl::bool_<false>,
mpl::bool_<false>,mpl::bool_<false>,
mpl::bool_<true> >::type {};
struct always_directed_never_reversed:
get_all_graph_views::apply<scalar_pairs,mpl::bool_<true>,
mpl::bool_<false>,mpl::bool_<false>,
mpl::bool_<true> >::type {};
struct never_filtered:
get_all_graph_views::apply<scalar_pairs,mpl::bool_<false>,
mpl::bool_<false>,mpl::bool_<false>,
......
......@@ -43,10 +43,18 @@ void do_label_self_loops(GraphInterface& gi, boost::any property)
edge_scalar_properties())(property);
}
void do_remove_labeled_edges(GraphInterface& gi, boost::any property)
{
run_action<graph_tool::detail::always_directed_never_reversed>()
(gi, bind<void>(remove_labeled_edges(), _1, _2),
edge_scalar_properties())(property);
}
using namespace boost::python;
void export_parallel()
{
def("label_parallel_edges", &do_label_parallel_edges);
def("label_self_loops", &do_label_self_loops);
def("remove_labeled_edges", &do_remove_labeled_edges);
}
......@@ -23,7 +23,7 @@ namespace graph_tool
using namespace std;
using namespace boost;
// label parallel edges in the order they are found, starting from 1, all others
// label parallel edges in the order they are found, starting from 1
struct label_parallel_edges
{
template <class Graph, class EdgeIndexMap, class ParallelMap>
......@@ -43,14 +43,15 @@ struct label_parallel_edges
tr1::unordered_set<edge_t,DescriptorHash<EdgeIndexMap> >
p_edges(0, DescriptorHash<EdgeIndexMap>(edge_index));
typename graph_traits<Graph>::out_edge_iterator e1, e2, e_end;
for (tie(e1, e_end) = out_edges(v, g); e1 != e_end; ++e1)
typename graph_traits<Graph>::out_edge_iterator e1, e2,
e_end1, e_end2;
for (tie(e1, e_end1) = out_edges(v, g); e1 != e_end1; ++e1)
{
if (p_edges.find(*e1) != p_edges.end())
continue;
size_t n = 0;
put(parallel, *e1, n);
for (tie(e2, e_end) = out_edges(v, g); e2 != e_end; ++e2)
for (tie(e2, e_end2) = out_edges(v, g); e2 != e_end2; ++e2)
if (*e2 != *e1 && target(*e1, g) == target(*e2, g))
{
put(parallel, *e2, ++n);
......@@ -91,6 +92,35 @@ struct label_self_loops
}
};
// remove edges with label larger than 0
struct remove_labeled_edges
{
template <class Graph, class LabelMap>
void operator()(Graph& g, LabelMap label) const
{
int i, N = num_vertices(g);
for (i = 0; i < N; ++i)
{
typename graph_traits<Graph>::vertex_descriptor v = vertex(i, g);
if (v == graph_traits<Graph>::null_vertex())
continue;
typedef typename graph_traits<Graph>::edge_descriptor edge_t;
vector<edge_t> r_edges;
typename graph_traits<Graph>::out_edge_iterator e, e_end;
for (tie(e, e_end) = out_edges(v, g); e != e_end; ++e)
{
if (label[*e] > 0)
r_edges.push_back(*e);
}
for (size_t j = 0; j < r_edges.size(); ++j)
remove_edge(r_edges[j], g);
}
}
};
} // graph_tool namespace
#endif //GRAPH_PARALLEL_HH
......@@ -23,7 +23,8 @@ from .. core import _degree, _prop
from numpy import *
__all__ = ["vertex_hist", "edge_hist", "vertex_average", "edge_average",
"label_components", "label_parallel_edges", "label_self_loops"]
"label_components", "label_parallel_edges", "remove_parallel_edges",
"label_self_loops", "remove_self_loops", "remove_labeled_edges"]
def vertex_hist(g, deg, bins=[1], float_count=True):
ret = libgraph_tool_stats.\
......@@ -52,16 +53,31 @@ def label_components(g, vprop=None):
label_components(g._Graph__graph, _prop("v", g, vprop))
return vprop
def label_parallel_edges(g, eprop):
def remove_labeled_edges(g, label):
g.stash_filter(all=False, directed=True, reversed=True)
libgraph_tool_stats.\
remove_labeled_edges(g._Graph__graph, _prop("e", g, label))
g.pop_filter(all=False, directed=True, reversed=True)
def label_parallel_edges(g, eprop=None):
if eprop == None:
eprop = g.new_edge_property("int32_t")
libgraph_tool_stats.\
label_parallel_edges(g._Graph__graph, _prop("e", g, eprop))
return eprop
def label_self_loops(g, eprop):
def remove_parallel_edges(g):
eprop = label_parallel_edges(g)
remove_labeled_edges(g, eprop)
def label_self_loops(g, eprop=None):
if eprop == None:
eprop = g.new_edge_property("int32_t")
libgraph_tool_stats.\
label_self_loops(g._Graph__graph, _prop("e", g, eprop))
return eprop
def remove_self_loops(g):
eprop = label_self_loops(g)
remove_labeled_edges(g, eprop)
Markdown is supported
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