Commit 217b9d7a authored by Tiago Peixoto's avatar Tiago Peixoto

Implement predecessor_tree()

parent c21a36c3
......@@ -16,7 +16,8 @@ libgraph_tool_generation_la_LDFLAGS = $(MOD_LDFLAGS)
libgraph_tool_generation_la_SOURCES = \
graph_generation.cc \
graph_rewiring.cc
graph_rewiring.cc \
graph_predecessor.cc
libgraph_tool_generation_la_include_HEADERS = \
graph_generation.hh \
......
......@@ -96,6 +96,8 @@ void generate_random_graph(GraphInterface& gi, size_t N,
void random_rewire(GraphInterface& gi, string strat, bool self_loops,
bool parallel_edges, size_t seed);
void predecessor_graph(GraphInterface& gi, GraphInterface& gpi,
boost::any pred_map);
using namespace boost::python;
......@@ -103,4 +105,5 @@ BOOST_PYTHON_MODULE(libgraph_tool_generation)
{
def("gen_random_graph", &generate_random_graph);
def("random_rewire", &random_rewire);
def("predecessor_graph", &predecessor_graph);
}
// graph-tool -- a general graph modification and manipulation thingy
//
// Copyright (C) 2007 Tiago de Paula Peixoto <tiago@forked.de>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 3
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#include <boost/lambda/bind.hpp>
#include "graph.hh"
#include "graph_filtering.hh"
#include "graph_predecessor.hh"
using namespace graph_tool;
using namespace boost;
using namespace boost::lambda;
void predecessor_graph(GraphInterface& gi, GraphInterface& gpi,
boost::any pred_map)
{
run_action<>()(gi, bind<void>(get_predecessor_graph(), _1,
gi.GetVertexIndex(), ref(gpi.GetGraph()),
_2),
vertex_scalar_properties())(pred_map);
}
// graph-tool -- a general graph modification and manipulation thingy
//
// Copyright (C) 2007 Tiago de Paula Peixoto <tiago@forked.de>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 3
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#ifndef GRAPH_PREDECESSOR_HH
#define GRAPH_PREDECESSOR_HH
#include "graph.hh"
#include "graph_filtering.hh"
#include "graph_util.hh"
namespace graph_tool
{
using namespace std;
using namespace boost;
struct get_predecessor_graph
{
template <class Graph, class IndexMap, class PredGraph, class PredMap>
void operator()(Graph& g, IndexMap vertex_index, PredGraph& pg,
PredMap pred_map) const
{
unchecked_vector_property_map<size_t,IndexMap>
index_map(vertex_index, num_vertices(g));
size_t count = 0;
typename graph_traits<Graph>::vertex_iterator v,v_end;
for (tie(v,v_end) = vertices(g); v != v_end; ++v)
{
index_map[*v] = count++;
add_vertex(pg);
}
for (tie(v,v_end) = vertices(g); v != v_end; ++v)
{
size_t pred_i = get(pred_map, *v);
if (pred_i >= num_vertices(g))
continue;
typename graph_traits<Graph>::vertex_descriptor pred =
vertex(pred_i, g);
if (pred == graph_traits<Graph>::null_vertex())
continue;
if (pred != *v)
{
typename graph_traits<PredGraph>::vertex_descriptor s, t;
s = vertex(index_map[pred], pg);
t = vertex(index_map[*v], pg);
add_edge(s, t, pg);
}
}
}
};
} // graph_tool namespace
#endif // GRAPH_PREDECESSOR_HH
......@@ -24,10 +24,10 @@
from .. dl_import import dl_import
dl_import("import libgraph_tool_generation")
from .. core import Graph
from .. core import Graph, _check_prop_scalar, _prop
import sys, numpy
__all__ = ["random_graph", "random_rewire"]
__all__ = ["random_graph", "random_rewire", "predecessor_tree"]
def _corr_wrap(i, j, corr):
return corr(i[1], j[1])
......@@ -376,3 +376,14 @@ def random_rewire(g, strat= "uncorrelated", parallel_edges = False,
libgraph_tool_generation.random_rewire(g._Graph__graph, strat, self_loops,
parallel_edges, seed)
g.pop_filter(reversed=True)
def predecessor_tree(g, pred_map):
"""Return a graph from a list of predecessors given by
the 'pred_map' vertex property."""
_check_prop_scalar(pred_map, "pred_map")
pg = Graph()
libgraph_tool_generation.predecessor_graph(g._Graph__graph,
pg._Graph__graph,
_prop("v", g, pred_map))
return pg
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