Commit db7589d6 authored by Tiago Peixoto's avatar Tiago Peixoto
Browse files

Move graph component code from 'stats' to 'topology' module

parent 6d1a9eff
......@@ -11,14 +11,12 @@ libgraph_tool_stats_LTLIBRARIES = libgraph_tool_stats.la
libgraph_tool_stats_la_includedir = $(pythondir)/graph_tool/include
libgraph_tool_stats_la_SOURCES = \
graph_components.cc \
graph_histograms.cc \
graph_average.cc \
graph_parallel.cc \
graph_stats_bind.cc
libgraph_tool_stats_la_include_HEADERS = \
graph_components.hh \
graph_parallel.hh \
graph_histograms.hh \
graph_average.hh
......
......@@ -17,14 +17,12 @@
using namespace boost;
void export_components();
void export_parallel();
void export_histograms();
void export_average();
BOOST_PYTHON_MODULE(libgraph_tool_stats)
{
export_components();
export_parallel();
export_histograms();
export_average();
......
......@@ -20,6 +20,8 @@ libgraph_tool_topology_la_SOURCES = \
graph_minimum_spanning_tree.cc \
graph_denominator_tree.cc \
graph_topological_sort.cc \
graph_transitive_closure.cc
graph_transitive_closure.cc \
graph_components.cc
libgraph_tool_topology_la_include_HEADERS =
libgraph_tool_topology_la_include_HEADERS = \
graph_components.hh
......@@ -37,6 +37,8 @@ bool denominator_tree(GraphInterface& gi, size_t entry, boost::any pred_map);
void transitive_closure(GraphInterface& gi, GraphInterface& tcgi);
void export_components();
BOOST_PYTHON_MODULE(libgraph_tool_topology)
{
def("check_isomorphism", &check_isomorphism);
......@@ -45,4 +47,5 @@ BOOST_PYTHON_MODULE(libgraph_tool_topology)
def("topological_sort", &topological_sort);
def("denominator_tree", &denominator_tree);
def("transitive_closure", &transitive_closure);
export_components();
}
......@@ -28,7 +28,6 @@ from .. core import _degree, _prop
from numpy import *
__all__ = ["vertex_hist", "edge_hist", "vertex_average", "edge_average",
"label_components", "label_biconnected_components",
"label_parallel_edges", "remove_parallel_edges",
"label_self_loops", "remove_self_loops", "remove_labeled_edges"]
......@@ -243,74 +242,6 @@ def edge_average(g, eprop):
get_edge_average(g._Graph__graph, _prop("e", g, eprop))
return ret
def label_components(g, vprop=None, directed=None):
"""
Labels the components to which each vertex in the graph belongs. If the
graph is directed, it finds the strongly connected components.
Parameters
----------
g : Graph
Graph to be used.
vprop : PropertyMap (optional, default: None)
Vertex property to store the component labels. If none is supplied, one
is created.
directed : bool (optional, default:None)
Treat graph as directed or not, independently of its actual
directionality.
Returns
-------
comp : PropertyMap
Vertex property map with component labels.
Notes
-----
The components are arbitrarily labeled from 0 to N-1, where N is the total
number of components.
The algorithm runs in :math:`O(|V| + |E|)` time.
Examples
--------
>>> g = gt.random_graph(100, lambda: (1, 1), seed=42)
>>> comp = gt.label_components(g)
>>> print comp.get_array()
[0 1 2 3 4 0 3 3 4 4 2 3 4 0 3 3 3 3 0 3 2 1 3 0 0 2 2 3 3 3 0 1 2 3 2 3 0
1 0 5 5 1 4 2 2 1 0 3 3 3 3 3 3 0 0 3 4 2 3 2 5 5 0 2 1 0 3 2 0 3 3 0 4 3
2 6 2 2 1 3 1 1 0 3 0 1 3 0 3 0 2 0 2 2 0 6 1 1 0 2]
"""
if directed != None:
g.stash_filter(directed=True)
g.set_directed(directed)
if vprop == None:
vprop = g.new_vertex_property("int32_t")
libgraph_tool_stats.\
label_components(g._Graph__graph, _prop("v", g, vprop))
if directed != None:
g.pop_filter(directed=True)
return vprop
def label_biconnected_components(g, eprop=None, vprop=None):
if vprop == None:
vprop = g.new_vertex_property("bool")
if eprop == None:
eprop = g.new_edge_property("int32_t")
g.stash_filter(directed=True)
g.set_directed(False)
nc = libgraph_tool_stats.\
label_biconnected_components(g._Graph__graph, _prop("e", g, eprop),
_prop("v", g, vprop))
g.pop_filter(directed=True)
return eprop, vprop, nc
def remove_labeled_edges(g, label):
g.stash_filter(all=False, directed=True, reversed=True)
libgraph_tool_stats.\
......
......@@ -31,12 +31,16 @@ __all__ = ["isomorphism", "min_spanning_tree", "denominator_tree",
"topological_sort", "transitive_closure", "label_components",
"label_biconnected_components"]
def isomorphism(g1, g2, isomap=None):
if isomap == None:
isomap = g1.new_vertex_property("int32_t")
return libgraph_tool_topology.\
def isomorphism(g1, g2, isomap=False):
imap = g1.new_vertex_property("int32_t")
iso = libgraph_tool_topology.\
check_isomorphism(g1._Graph__graph,g2._Graph__graph,
_prop("v", g1, isomap))
if isomap:
return iso, imap
else:
return iso
def min_spanning_tree(g, weights=None, root=None, tree_map=None):
if tree_map == None:
......@@ -77,8 +81,6 @@ def topological_sort(g):
libgraph_tool_topology.\
topological_sort(g._Graph__graph, topological_order)
return topological_order
<<<<<<< HEAD
=======
def transitive_closure(g):
if not g.is_directed():
......@@ -164,4 +166,3 @@ def label_biconnected_components(g, eprop=None, vprop=None):
_prop("v", g, vprop))
g.pop_filter(directed=True)
return eprop, vprop, nc
>>>>>>> 3b8ab1b... merge transitive closure
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