Commit 684efca7 authored by Tiago Peixoto's avatar Tiago Peixoto

Implement optional wrapping of graphs to deal with edge index housekeeping

Thins changes the graph filtering code slightly to wrap graph types with
GraphWrap, which automatically updates the edge index list when edges
are removed and added to the graph.

This also changes how graphs are passed to algorithms, which is now by
reference instead of pointer. (hence this touches lots of code, but
changes are trivial)
parent e03b1758
...@@ -33,12 +33,11 @@ struct get_eigentrust ...@@ -33,12 +33,11 @@ struct get_eigentrust
{ {
template <class Graph, class VertexIndex, class EdgeIndex, class TrustMap, template <class Graph, class VertexIndex, class EdgeIndex, class TrustMap,
class InferredTrustMap> class InferredTrustMap>
void operator()(Graph* gp, VertexIndex vertex_index, EdgeIndex edge_index, void operator()(Graph& g, VertexIndex vertex_index, EdgeIndex edge_index,
TrustMap c, InferredTrustMap t, double epslon, TrustMap c, InferredTrustMap t, double epslon,
size_t max_iter, rng_t& rng) size_t max_iter, rng_t& rng)
const const
{ {
Graph& g = *gp;
typedef typename property_traits<TrustMap>::value_type c_type; typedef typename property_traits<TrustMap>::value_type c_type;
typedef typename property_traits<InferredTrustMap>::value_type typedef typename property_traits<InferredTrustMap>::value_type
::value_type t_type; ::value_type t_type;
......
...@@ -66,28 +66,28 @@ void normalize_betweenness(const Graph& g, ...@@ -66,28 +66,28 @@ void normalize_betweenness(const Graph& g,
struct get_betweenness struct get_betweenness
{ {
template <class Graph, class EdgeBetweenness, class VertexBetweenness> template <class Graph, class EdgeBetweenness, class VertexBetweenness>
void operator()(Graph* gp, void operator()(Graph& g,
GraphInterface::vertex_index_map_t index_map, GraphInterface::vertex_index_map_t index_map,
EdgeBetweenness edge_betweenness, EdgeBetweenness edge_betweenness,
VertexBetweenness vertex_betweenness, VertexBetweenness vertex_betweenness,
bool normalize, size_t n) const bool normalize, size_t n) const
{ {
vector<vector<typename graph_traits<Graph>::edge_descriptor> > vector<vector<typename graph_traits<Graph>::edge_descriptor> >
incoming_map(num_vertices(*gp)); incoming_map(num_vertices(g));
vector<size_t> distance_map(num_vertices(*gp)); vector<size_t> distance_map(num_vertices(g));
vector<typename property_traits<VertexBetweenness>::value_type> vector<typename property_traits<VertexBetweenness>::value_type>
dependency_map(num_vertices(*gp)); dependency_map(num_vertices(g));
vector<size_t> path_count_map(num_vertices(*gp)); vector<size_t> path_count_map(num_vertices(g));
brandes_betweenness_centrality brandes_betweenness_centrality
(*gp, vertex_betweenness, edge_betweenness, (g, vertex_betweenness, edge_betweenness,
make_iterator_property_map(incoming_map.begin(), index_map), make_iterator_property_map(incoming_map.begin(), index_map),
make_iterator_property_map(distance_map.begin(), index_map), make_iterator_property_map(distance_map.begin(), index_map),
make_iterator_property_map(dependency_map.begin(), index_map), make_iterator_property_map(dependency_map.begin(), index_map),
make_iterator_property_map(path_count_map.begin(), index_map), make_iterator_property_map(path_count_map.begin(), index_map),
index_map); index_map);
if (normalize) if (normalize)
normalize_betweenness(*gp, edge_betweenness, vertex_betweenness, n); normalize_betweenness(g, edge_betweenness, vertex_betweenness, n);
} }
}; };
...@@ -95,29 +95,29 @@ struct get_weighted_betweenness ...@@ -95,29 +95,29 @@ struct get_weighted_betweenness
{ {
template <class Graph, class EdgeBetweenness, class VertexBetweenness, template <class Graph, class EdgeBetweenness, class VertexBetweenness,
class VertexIndexMap> class VertexIndexMap>
void operator()(Graph* gp, VertexIndexMap vertex_index, void operator()(Graph& g, VertexIndexMap vertex_index,
EdgeBetweenness edge_betweenness, EdgeBetweenness edge_betweenness,
VertexBetweenness vertex_betweenness, VertexBetweenness vertex_betweenness,
boost::any weight_map, bool normalize, boost::any weight_map, bool normalize,
size_t n) const size_t n) const
{ {
vector<vector<typename graph_traits<Graph>::edge_descriptor> > vector<vector<typename graph_traits<Graph>::edge_descriptor> >
incoming_map(num_vertices(*gp)); incoming_map(num_vertices(g));
vector<typename property_traits<EdgeBetweenness>::value_type> vector<typename property_traits<EdgeBetweenness>::value_type>
distance_map(num_vertices(*gp)); distance_map(num_vertices(g));
vector<typename property_traits<VertexBetweenness>::value_type> vector<typename property_traits<VertexBetweenness>::value_type>
dependency_map(num_vertices(*gp)); dependency_map(num_vertices(g));
vector<size_t> path_count_map(num_vertices(*gp)); vector<size_t> path_count_map(num_vertices(g));
brandes_betweenness_centrality brandes_betweenness_centrality
(*gp, vertex_betweenness, edge_betweenness, (g, vertex_betweenness, edge_betweenness,
make_iterator_property_map(incoming_map.begin(), vertex_index), make_iterator_property_map(incoming_map.begin(), vertex_index),
make_iterator_property_map(distance_map.begin(), vertex_index), make_iterator_property_map(distance_map.begin(), vertex_index),
make_iterator_property_map(dependency_map.begin(), vertex_index), make_iterator_property_map(dependency_map.begin(), vertex_index),
make_iterator_property_map(path_count_map.begin(), vertex_index), make_iterator_property_map(path_count_map.begin(), vertex_index),
vertex_index, any_cast<EdgeBetweenness>(weight_map)); vertex_index, any_cast<EdgeBetweenness>(weight_map));
if (normalize) if (normalize)
normalize_betweenness(*gp, edge_betweenness, vertex_betweenness, n); normalize_betweenness(g, edge_betweenness, vertex_betweenness, n);
} }
}; };
...@@ -158,10 +158,10 @@ void betweenness(GraphInterface& g, boost::any weight, ...@@ -158,10 +158,10 @@ void betweenness(GraphInterface& g, boost::any weight,
struct get_central_point_dominance struct get_central_point_dominance
{ {
template <class Graph, class VertexBetweenness> template <class Graph, class VertexBetweenness>
void operator()(Graph* g, VertexBetweenness vertex_betweenness, double& c) void operator()(Graph& g, VertexBetweenness vertex_betweenness, double& c)
const const
{ {
c = central_point_dominance(*g, vertex_betweenness); c = central_point_dominance(g, vertex_betweenness);
} }
}; };
......
...@@ -31,12 +31,10 @@ struct get_eigentrust ...@@ -31,12 +31,10 @@ struct get_eigentrust
{ {
template <class Graph, class VertexIndex, class EdgeIndex, class TrustMap, template <class Graph, class VertexIndex, class EdgeIndex, class TrustMap,
class InferredTrustMap> class InferredTrustMap>
void operator()(Graph* gp, VertexIndex vertex_index, void operator()(Graph& g, VertexIndex vertex_index,
EdgeIndex edge_index, TrustMap c, InferredTrustMap t, EdgeIndex edge_index, TrustMap c, InferredTrustMap t,
double epslon, size_t max_iter) double epslon, size_t max_iter) const
const
{ {
Graph& g = *gp;
typedef typename property_traits<TrustMap>::value_type c_type; typedef typename property_traits<TrustMap>::value_type c_type;
typedef typename property_traits<InferredTrustMap>::value_type t_type; typedef typename property_traits<InferredTrustMap>::value_type t_type;
......
...@@ -30,11 +30,10 @@ using namespace boost; ...@@ -30,11 +30,10 @@ using namespace boost;
struct get_pagerank struct get_pagerank
{ {
template <class Graph, class VertexIndex, class RankMap> template <class Graph, class VertexIndex, class RankMap>
void operator()(Graph* gp, VertexIndex vertex_index, RankMap rank, void operator()(Graph& g, VertexIndex vertex_index, RankMap rank,
double damping, double epslon, size_t max_iter, double damping, double epslon, size_t max_iter,
size_t& iter) const size_t& iter) const
{ {
Graph& g = *gp;
typedef typename property_traits<RankMap>::value_type rank_type; typedef typename property_traits<RankMap>::value_type rank_type;
RankMap r_temp(vertex_index,num_vertices(g)); RankMap r_temp(vertex_index,num_vertices(g));
......
...@@ -86,9 +86,8 @@ get_triangles(typename graph_traits<Graph>::vertex_descriptor v, const Graph &g) ...@@ -86,9 +86,8 @@ get_triangles(typename graph_traits<Graph>::vertex_descriptor v, const Graph &g)
struct get_global_clustering struct get_global_clustering
{ {
template <class Graph> template <class Graph>
void operator()(const Graph* gp, double& c, double& c_err) const void operator()(const Graph& g, double& c, double& c_err) const
{ {
const Graph& g = *gp;
size_t triangles = 0, n = 0; size_t triangles = 0, n = 0;
pair<size_t, size_t> temp; pair<size_t, size_t> temp;
...@@ -133,9 +132,8 @@ struct get_global_clustering ...@@ -133,9 +132,8 @@ struct get_global_clustering
struct set_clustering_to_property struct set_clustering_to_property
{ {
template <class Graph, class ClustMap> template <class Graph, class ClustMap>
void operator()(const Graph* gp, ClustMap clust_map) const void operator()(const Graph& g, ClustMap clust_map) const
{ {
const Graph& g = *gp;
typename get_undirected_graph<Graph>::type ug(g); typename get_undirected_graph<Graph>::type ug(g);
int i, N = num_vertices(g); int i, N = num_vertices(g);
......
...@@ -113,10 +113,9 @@ void collect_targets(Vertex v, Graph& g, Targets& t, undirected_tag) ...@@ -113,10 +113,9 @@ void collect_targets(Vertex v, Graph& g, Targets& t, undirected_tag)
struct get_extended_clustering struct get_extended_clustering
{ {
template <class Graph, class IndexMap, class ClusteringMap> template <class Graph, class IndexMap, class ClusteringMap>
void operator()(const Graph* gp, IndexMap vertex_index, void operator()(const Graph& g, IndexMap vertex_index,
vector<ClusteringMap> cmaps) const vector<ClusteringMap> cmaps) const
{ {
const Graph& g = *gp;
typedef typename graph_traits<Graph>::vertex_descriptor vertex_t; typedef typename graph_traits<Graph>::vertex_descriptor vertex_t;
int i, N = num_vertices(g); int i, N = num_vertices(g);
......
...@@ -41,14 +41,14 @@ struct append_to_list ...@@ -41,14 +41,14 @@ struct append_to_list
{ {
template <class Graph> template <class Graph>
void operator()(Graph* gp, boost::any& list) const void operator()(Graph& g, boost::any& list) const
{ {
typedef typename mpl::if_<typename is_directed::apply<Graph>::type, typedef typename mpl::if_<typename is_directed::apply<Graph>::type,
d_graph_t, d_graph_t,
u_graph_t>::type graph_t; u_graph_t>::type graph_t;
vector<graph_t>& glist = any_cast<vector<graph_t>&>(list); vector<graph_t>& glist = any_cast<vector<graph_t>&>(list);
glist.push_back(graph_t()); glist.push_back(graph_t());
copy_graph(*gp, glist.back(), vertex_copy(null_copy()). copy_graph(g, glist.back(), vertex_copy(null_copy()).
edge_copy(null_copy())); edge_copy(null_copy()));
} }
}; };
...@@ -56,7 +56,7 @@ struct append_to_list ...@@ -56,7 +56,7 @@ struct append_to_list
struct retrieve_from_list struct retrieve_from_list
{ {
template <class Graph> template <class Graph>
void operator()(Graph* gp, boost::any& list, bool& done) const void operator()(Graph& g, boost::any& list, bool& done) const
{ {
typedef typename mpl::if_<typename is_directed::apply<Graph>::type, typedef typename mpl::if_<typename is_directed::apply<Graph>::type,
d_graph_t, d_graph_t,
...@@ -67,7 +67,7 @@ struct retrieve_from_list ...@@ -67,7 +67,7 @@ struct retrieve_from_list
done = true; done = true;
return; return;
} }
copy_graph(glist.back(), *gp, edge_copy(null_copy())); copy_graph(glist.back(), g, edge_copy(null_copy()));
glist.pop_back(); glist.pop_back();
} }
}; };
......
...@@ -53,7 +53,7 @@ bool has_val(vector<Value>& v, const Value& val) ...@@ -53,7 +53,7 @@ bool has_val(vector<Value>& v, const Value& val)
} }
template <class Graph, class Sampler> template <class Graph, class Sampler>
void get_subgraphs(Graph g, typename graph_traits<Graph>::vertex_descriptor v, void get_subgraphs(Graph& g, typename graph_traits<Graph>::vertex_descriptor v,
size_t n, size_t n,
vector<vector<typename graph_traits<Graph>::vertex_descriptor> >& subgraphs, vector<vector<typename graph_traits<Graph>::vertex_descriptor> >& subgraphs,
Sampler sampler) Sampler sampler)
...@@ -264,13 +264,10 @@ struct wrap_undirected ...@@ -264,13 +264,10 @@ struct wrap_undirected
struct get_all_motifs struct get_all_motifs
{ {
template <class Graph, class Sampler> template <class Graph, class Sampler>
void operator()(Graph* gp, size_t k, boost::any& list, void operator()(Graph& g, size_t k, boost::any& list,
vector<size_t>& hist, Sampler sampler, double p, vector<size_t>& hist, Sampler sampler, double p,
bool comp_iso, bool fill_list, rng_t& rng) bool comp_iso, bool fill_list, rng_t& rng) const
const
{ {
Graph& g = *gp;
typedef typename mpl::if_<typename is_directed::apply<Graph>::type, typedef typename mpl::if_<typename is_directed::apply<Graph>::type,
d_graph_t, d_graph_t,
u_graph_t>::type graph_sg_t; u_graph_t>::type graph_sg_t;
......
...@@ -53,7 +53,7 @@ struct get_communities ...@@ -53,7 +53,7 @@ struct get_communities
typedef typename graph_traits<Graph>::edge_descriptor edge_t; typedef typename graph_traits<Graph>::edge_descriptor edge_t;
typedef typename property_traits<WeightMap>::key_type weight_key_t; typedef typename property_traits<WeightMap>::key_type weight_key_t;
size_t N = HardNumVertices()(&g); size_t N = HardNumVertices()(g);
stringstream out_str; stringstream out_str;
ofstream out_file; ofstream out_file;
...@@ -74,7 +74,7 @@ struct get_communities ...@@ -74,7 +74,7 @@ struct get_communities
boost::uniform_real<double> uniform_p(0.0,1.0); boost::uniform_real<double> uniform_p(0.0,1.0);
if (Nspins.first == 0) if (Nspins.first == 0)
Nspins.first = HardNumVertices()(&g); Nspins.first = HardNumVertices()(g);
unordered_map<size_t, size_t> Ns; // spin histogram unordered_map<size_t, size_t> Ns; // spin histogram
// global energy term // global energy term
...@@ -683,13 +683,11 @@ struct get_communities_selector ...@@ -683,13 +683,11 @@ struct get_communities_selector
comm_corr_t _corr; comm_corr_t _corr;
template <class Graph, class WeightMap, class CommunityMap> template <class Graph, class WeightMap, class CommunityMap>
void operator()(const Graph* gp, WeightMap weights, CommunityMap s, double gamma, void operator()(const Graph& g, WeightMap weights, CommunityMap s, double gamma,
size_t n_iter, pair<double,double> Tinterval, size_t n_iter, pair<double,double> Tinterval,
pair<size_t,bool> Nspins, size_t seed, pair<size_t,bool> Nspins, size_t seed,
pair<bool,string> verbose) const pair<bool,string> verbose) const
{ {
const Graph& g = *gp;
switch (_corr) switch (_corr)
{ {
case ERDOS_REYNI: case ERDOS_REYNI:
...@@ -712,11 +710,10 @@ struct get_communities_selector ...@@ -712,11 +710,10 @@ struct get_communities_selector
struct get_modularity struct get_modularity
{ {
template <class Graph, class WeightMap, class CommunityMap> template <class Graph, class WeightMap, class CommunityMap>
void operator()(const Graph* gp, WeightMap weights, CommunityMap s, void operator()(const Graph& g, WeightMap weights, CommunityMap s,
double& modularity) const double& modularity) const
{ {
typedef typename property_traits<WeightMap>::key_type weight_key_t; typedef typename property_traits<WeightMap>::key_type weight_key_t;
const Graph& g = *gp;
modularity = 0.0; modularity = 0.0;
size_t E = 0; size_t E = 0;
......
...@@ -69,9 +69,9 @@ void community_network(GraphInterface& gi, GraphInterface& cgi, ...@@ -69,9 +69,9 @@ void community_network(GraphInterface& gi, GraphInterface& cgi,
throw GraphException("invalid edge count property"); throw GraphException("invalid edge count property");
run_action<>()(gi, bind<void>(get_community_network(), _1, run_action<>()(gi, bind<void>(get_community_network(), _1,
&cgi.GetGraph(), cgi.GetVertexIndex(), ref(cgi.GetGraph()), cgi.GetVertexIndex(),
cgi.GetEdgeIndex(), _2, cgi.GetEdgeIndex(), _2,
_3, vcount, _4), _3, vcount, _4),
vertex_properties(), weight_properties(), vertex_properties(), weight_properties(),
ecount_properties()) ecount_properties())
(community_property, weight, edge_count); (community_property, weight, edge_count);
......
...@@ -35,14 +35,11 @@ struct get_community_network ...@@ -35,14 +35,11 @@ struct get_community_network
template <class Graph, class CommunityGraph, class CommunityMap, template <class Graph, class CommunityGraph, class CommunityMap,
class WeightMap, class EdgeIndex, class VertexIndex, class WeightMap, class EdgeIndex, class VertexIndex,
class VertexProperty, class EdgeProperty> class VertexProperty, class EdgeProperty>
void operator()(const Graph* gp, CommunityGraph* cgp, void operator()(const Graph& g, CommunityGraph& cg,
VertexIndex cvertex_index, EdgeIndex cedge_index, VertexIndex cvertex_index, EdgeIndex cedge_index,
CommunityMap s_map, WeightMap weight, CommunityMap s_map, WeightMap weight,
VertexProperty vertex_count, EdgeProperty edge_count) const VertexProperty vertex_count, EdgeProperty edge_count) const
{ {
const Graph& g = *gp;
CommunityGraph& cg = *cgp;
typedef typename graph_traits<Graph>::vertex_descriptor vertex_t; typedef typename graph_traits<Graph>::vertex_descriptor vertex_t;
typedef typename graph_traits<Graph>::edge_descriptor edge_t; typedef typename graph_traits<Graph>::edge_descriptor edge_t;
typedef typename graph_traits<CommunityGraph>::vertex_descriptor typedef typename graph_traits<CommunityGraph>::vertex_descriptor
......
...@@ -26,7 +26,7 @@ using namespace std; ...@@ -26,7 +26,7 @@ using namespace std;
using namespace graph_tool; using namespace graph_tool;
pair<double,double> pair<double,double>
assortativity_coefficient(const GraphInterface& gi, assortativity_coefficient(GraphInterface& gi,
GraphInterface::deg_t deg) GraphInterface::deg_t deg)
{ {
using namespace boost::lambda; using namespace boost::lambda;
...@@ -38,7 +38,7 @@ assortativity_coefficient(const GraphInterface& gi, ...@@ -38,7 +38,7 @@ assortativity_coefficient(const GraphInterface& gi,
} }
pair<double,double> pair<double,double>
scalar_assortativity_coefficient(const GraphInterface& gi, scalar_assortativity_coefficient(GraphInterface& gi,
GraphInterface::deg_t deg) GraphInterface::deg_t deg)
{ {
using namespace boost::lambda; using namespace boost::lambda;
......
...@@ -34,10 +34,9 @@ using namespace boost; ...@@ -34,10 +34,9 @@ using namespace boost;
struct get_assortativity_coefficient struct get_assortativity_coefficient
{ {
template <class Graph, class DegreeSelector> template <class Graph, class DegreeSelector>
void operator()(const Graph* gp, DegreeSelector deg, double& r, void operator()(const Graph& g, DegreeSelector deg, double& r,
double& r_err) const double& r_err) const
{ {
const Graph& g = *gp;
typedef typename mpl::if_<typename is_directed::apply<Graph>::type, typedef typename mpl::if_<typename is_directed::apply<Graph>::type,
size_t, double>::type count_t; size_t, double>::type count_t;
...@@ -115,11 +114,9 @@ struct get_assortativity_coefficient ...@@ -115,11 +114,9 @@ struct get_assortativity_coefficient
struct get_scalar_assortativity_coefficient struct get_scalar_assortativity_coefficient
{ {
template <class Graph, class DegreeSelector> template <class Graph, class DegreeSelector>
void operator()(const Graph* gp, DegreeSelector deg, double& r, void operator()(const Graph& g, DegreeSelector deg, double& r,
double& r_err) const double& r_err) const
{ {
const Graph& g = *gp;
typedef typename mpl::if_<typename is_directed::apply<Graph>::type, typedef typename mpl::if_<typename is_directed::apply<Graph>::type,
size_t, double>::type count_t; size_t, double>::type count_t;
......
...@@ -35,7 +35,7 @@ using namespace graph_tool; ...@@ -35,7 +35,7 @@ using namespace graph_tool;
// implementations spread across different compile units to minimize memory // implementations spread across different compile units to minimize memory
// usage during compilation // usage during compilation
void graph_avg_corr_imp1(const GraphInterface& g, python::object& avg, void graph_avg_corr_imp1(GraphInterface& g, python::object& avg,
python::object& dev, python::object& ret_bins, python::object& dev, python::object& ret_bins,
boost::any deg1, boost::any deg2, boost::any deg1, boost::any deg2,
boost::any weight, boost::any weight,
...@@ -45,7 +45,7 @@ void graph_avg_corr_imp1(const GraphInterface& g, python::object& avg, ...@@ -45,7 +45,7 @@ void graph_avg_corr_imp1(const GraphInterface& g, python::object& avg,
typedef ConstantPropertyMap<int,GraphInterface::edge_t> cweight_map_t; typedef ConstantPropertyMap<int,GraphInterface::edge_t> cweight_map_t;
python::object python::object
get_vertex_avg_correlation(const GraphInterface& gi, get_vertex_avg_correlation(GraphInterface& gi,
GraphInterface::deg_t deg1, GraphInterface::deg_t deg1,
GraphInterface::deg_t deg2, GraphInterface::deg_t deg2,
boost::any weight, boost::any weight,
......
...@@ -36,7 +36,7 @@ using namespace graph_tool; ...@@ -36,7 +36,7 @@ using namespace graph_tool;
typedef ConstantPropertyMap<int,GraphInterface::edge_t> dummy_weight; typedef ConstantPropertyMap<int,GraphInterface::edge_t> dummy_weight;
python::object python::object
get_vertex_avg_combined_correlation(const GraphInterface& gi, get_vertex_avg_combined_correlation(GraphInterface& gi,
GraphInterface::deg_t deg1, GraphInterface::deg_t deg1,
GraphInterface::deg_t deg2, GraphInterface::deg_t deg2,
const vector<long double>& bins) const vector<long double>& bins)
......
...@@ -32,7 +32,7 @@ using namespace boost::lambda; ...@@ -32,7 +32,7 @@ using namespace boost::lambda;
using namespace graph_tool; using namespace graph_tool;
void graph_avg_correlations_imp1(const GraphInterface& g, python::object& avg, void graph_avg_correlations_imp1(GraphInterface& g, python::object& avg,
python::object& dev, python::object& dev,
python::object& ret_bins, python::object& ret_bins,
boost::any deg1, boost::any deg2, boost::any deg1, boost::any deg2,
......
...@@ -36,7 +36,7 @@ using namespace graph_tool; ...@@ -36,7 +36,7 @@ using namespace graph_tool;