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

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
{
template <class Graph, class VertexIndex, class EdgeIndex, class TrustMap,
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,
size_t max_iter, rng_t& rng)
const
{
Graph& g = *gp;
typedef typename property_traits<TrustMap>::value_type c_type;
typedef typename property_traits<InferredTrustMap>::value_type
::value_type t_type;
......
......@@ -66,28 +66,28 @@ void normalize_betweenness(const Graph& g,
struct get_betweenness
{
template <class Graph, class EdgeBetweenness, class VertexBetweenness>
void operator()(Graph* gp,
void operator()(Graph& g,
GraphInterface::vertex_index_map_t index_map,
EdgeBetweenness edge_betweenness,
VertexBetweenness vertex_betweenness,
bool normalize, size_t n) const
{
vector<vector<typename graph_traits<Graph>::edge_descriptor> >
incoming_map(num_vertices(*gp));
vector<size_t> distance_map(num_vertices(*gp));
incoming_map(num_vertices(g));
vector<size_t> distance_map(num_vertices(g));
vector<typename property_traits<VertexBetweenness>::value_type>
dependency_map(num_vertices(*gp));
vector<size_t> path_count_map(num_vertices(*gp));
dependency_map(num_vertices(g));
vector<size_t> path_count_map(num_vertices(g));
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(distance_map.begin(), index_map),
make_iterator_property_map(dependency_map.begin(), index_map),
make_iterator_property_map(path_count_map.begin(), index_map),
index_map);
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
{
template <class Graph, class EdgeBetweenness, class VertexBetweenness,
class VertexIndexMap>
void operator()(Graph* gp, VertexIndexMap vertex_index,
void operator()(Graph& g, VertexIndexMap vertex_index,
EdgeBetweenness edge_betweenness,
VertexBetweenness vertex_betweenness,
boost::any weight_map, bool normalize,
size_t n) const
{
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>
distance_map(num_vertices(*gp));
distance_map(num_vertices(g));
vector<typename property_traits<VertexBetweenness>::value_type>
dependency_map(num_vertices(*gp));
vector<size_t> path_count_map(num_vertices(*gp));
dependency_map(num_vertices(g));
vector<size_t> path_count_map(num_vertices(g));
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(distance_map.begin(), vertex_index),
make_iterator_property_map(dependency_map.begin(), vertex_index),
make_iterator_property_map(path_count_map.begin(), vertex_index),
vertex_index, any_cast<EdgeBetweenness>(weight_map));
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,
struct get_central_point_dominance
{
template <class Graph, class VertexBetweenness>
void operator()(Graph* g, VertexBetweenness vertex_betweenness, double& c)
void operator()(Graph& g, VertexBetweenness vertex_betweenness, double& c)
const
{
c = central_point_dominance(*g, vertex_betweenness);
c = central_point_dominance(g, vertex_betweenness);
}
};
......
......@@ -31,12 +31,10 @@ struct get_eigentrust
{
template <class Graph, class VertexIndex, class EdgeIndex, class TrustMap,
class InferredTrustMap>
void operator()(Graph* gp, VertexIndex vertex_index,
void operator()(Graph& g, VertexIndex vertex_index,
EdgeIndex edge_index, TrustMap c, InferredTrustMap t,
double epslon, size_t max_iter)
const
double epslon, size_t max_iter) const
{
Graph& g = *gp;
typedef typename property_traits<TrustMap>::value_type c_type;
typedef typename property_traits<InferredTrustMap>::value_type t_type;
......
......@@ -30,11 +30,10 @@ using namespace boost;
struct get_pagerank
{
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,
size_t& iter) const
{
Graph& g = *gp;
typedef typename property_traits<RankMap>::value_type rank_type;
RankMap r_temp(vertex_index,num_vertices(g));
......
......@@ -86,9 +86,8 @@ get_triangles(typename graph_traits<Graph>::vertex_descriptor v, const Graph &g)
struct get_global_clustering
{
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;
pair<size_t, size_t> temp;
......@@ -133,9 +132,8 @@ struct get_global_clustering
struct set_clustering_to_property
{
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);
int i, N = num_vertices(g);
......
......@@ -113,10 +113,9 @@ void collect_targets(Vertex v, Graph& g, Targets& t, undirected_tag)
struct get_extended_clustering
{
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
{
const Graph& g = *gp;
typedef typename graph_traits<Graph>::vertex_descriptor vertex_t;
int i, N = num_vertices(g);
......
......@@ -41,14 +41,14 @@ struct append_to_list
{
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,
d_graph_t,
u_graph_t>::type graph_t;
vector<graph_t>& glist = any_cast<vector<graph_t>&>(list);
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()));
}
};
......@@ -56,7 +56,7 @@ struct append_to_list
struct retrieve_from_list
{
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,
d_graph_t,
......@@ -67,7 +67,7 @@ struct retrieve_from_list
done = true;
return;
}
copy_graph(glist.back(), *gp, edge_copy(null_copy()));
copy_graph(glist.back(), g, edge_copy(null_copy()));
glist.pop_back();
}
};
......
......@@ -53,7 +53,7 @@ bool has_val(vector<Value>& v, const Value& val)
}
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,
vector<vector<typename graph_traits<Graph>::vertex_descriptor> >& subgraphs,
Sampler sampler)
......@@ -264,13 +264,10 @@ struct wrap_undirected
struct get_all_motifs
{
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,
bool comp_iso, bool fill_list, rng_t& rng)
const
bool comp_iso, bool fill_list, rng_t& rng) const
{
Graph& g = *gp;
typedef typename mpl::if_<typename is_directed::apply<Graph>::type,
d_graph_t,
u_graph_t>::type graph_sg_t;
......
......@@ -53,7 +53,7 @@ struct get_communities
typedef typename graph_traits<Graph>::edge_descriptor edge_t;
typedef typename property_traits<WeightMap>::key_type weight_key_t;
size_t N = HardNumVertices()(&g);
size_t N = HardNumVertices()(g);
stringstream out_str;
ofstream out_file;
......@@ -74,7 +74,7 @@ struct get_communities
boost::uniform_real<double> uniform_p(0.0,1.0);
if (Nspins.first == 0)
Nspins.first = HardNumVertices()(&g);
Nspins.first = HardNumVertices()(g);
unordered_map<size_t, size_t> Ns; // spin histogram
// global energy term
......@@ -683,13 +683,11 @@ struct get_communities_selector
comm_corr_t _corr;
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,
pair<size_t,bool> Nspins, size_t seed,
pair<bool,string> verbose) const
{
const Graph& g = *gp;
switch (_corr)
{
case ERDOS_REYNI:
......@@ -712,11 +710,10 @@ struct get_communities_selector
struct get_modularity
{
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
{
typedef typename property_traits<WeightMap>::key_type weight_key_t;
const Graph& g = *gp;
modularity = 0.0;
size_t E = 0;
......
......@@ -69,9 +69,9 @@ void community_network(GraphInterface& gi, GraphInterface& cgi,
throw GraphException("invalid edge count property");
run_action<>()(gi, bind<void>(get_community_network(), _1,
&cgi.GetGraph(), cgi.GetVertexIndex(),
cgi.GetEdgeIndex(), _2,
_3, vcount, _4),
ref(cgi.GetGraph()), cgi.GetVertexIndex(),
cgi.GetEdgeIndex(), _2,
_3, vcount, _4),
vertex_properties(), weight_properties(),
ecount_properties())
(community_property, weight, edge_count);
......
......@@ -35,14 +35,11 @@ struct get_community_network
template <class Graph, class CommunityGraph, class CommunityMap,
class WeightMap, class EdgeIndex, class VertexIndex,
class VertexProperty, class EdgeProperty>
void operator()(const Graph* gp, CommunityGraph* cgp,
void operator()(const Graph& g, CommunityGraph& cg,
VertexIndex cvertex_index, EdgeIndex cedge_index,
CommunityMap s_map, WeightMap weight,
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>::edge_descriptor edge_t;
typedef typename graph_traits<CommunityGraph>::vertex_descriptor
......
......@@ -26,7 +26,7 @@ using namespace std;
using namespace graph_tool;
pair<double,double>
assortativity_coefficient(const GraphInterface& gi,
assortativity_coefficient(GraphInterface& gi,
GraphInterface::deg_t deg)
{
using namespace boost::lambda;
......@@ -38,7 +38,7 @@ assortativity_coefficient(const GraphInterface& gi,
}
pair<double,double>
scalar_assortativity_coefficient(const GraphInterface& gi,
scalar_assortativity_coefficient(GraphInterface& gi,
GraphInterface::deg_t deg)
{
using namespace boost::lambda;
......
......@@ -34,10 +34,9 @@ using namespace boost;
struct get_assortativity_coefficient
{
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
{
const Graph& g = *gp;
typedef typename mpl::if_<typename is_directed::apply<Graph>::type,
size_t, double>::type count_t;
......@@ -115,11 +114,9 @@ struct get_assortativity_coefficient
struct get_scalar_assortativity_coefficient
{
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
{
const Graph& g = *gp;
typedef typename mpl::if_<typename is_directed::apply<Graph>::type,
size_t, double>::type count_t;
......
......@@ -35,7 +35,7 @@ using namespace graph_tool;
// implementations spread across different compile units to minimize memory
// 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,
boost::any deg1, boost::any deg2,
boost::any weight,
......@@ -45,7 +45,7 @@ void graph_avg_corr_imp1(const GraphInterface& g, python::object& avg,
typedef ConstantPropertyMap<int,GraphInterface::edge_t> cweight_map_t;
python::object
get_vertex_avg_correlation(const GraphInterface& gi,
get_vertex_avg_correlation(GraphInterface& gi,
GraphInterface::deg_t deg1,
GraphInterface::deg_t deg2,
boost::any weight,
......
......@@ -36,7 +36,7 @@ using namespace graph_tool;
typedef ConstantPropertyMap<int,GraphInterface::edge_t> dummy_weight;
python::object
get_vertex_avg_combined_correlation(const GraphInterface& gi,
get_vertex_avg_combined_correlation(GraphInterface& gi,
GraphInterface::deg_t deg1,
GraphInterface::deg_t deg2,
const vector<long double>& bins)
......
......@@ -32,7 +32,7 @@ using namespace boost::lambda;
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& ret_bins,
boost::any deg1, boost::any deg2,
......
......@@ -36,7 +36,7 @@ using namespace graph_tool;
// implementations spread across different compile units to minimize memory
// usage during compilation
void graph_correlations_imp1(const GraphInterface& g, python::object& hist,
void graph_correlations_imp1(GraphInterface& g, python::object& hist,
python::object& ret_bins,
boost::any deg1, boost::any deg2,
boost::any weight,
......@@ -46,7 +46,7 @@ void graph_correlations_imp1(const GraphInterface& g, python::object& hist,
typedef ConstantPropertyMap<int,GraphInterface::edge_t> cweight_map_t;
python::object
get_vertex_correlation_histogram(const GraphInterface& gi,
get_vertex_correlation_histogram(GraphInterface& gi,
GraphInterface::deg_t deg1,
GraphInterface::deg_t deg2,
boost::any weight,
......
......@@ -185,10 +185,9 @@ struct get_correlation_histogram
template <class Graph, class DegreeSelector1, class DegreeSelector2,
class WeightMap>
void operator()(Graph* gp, DegreeSelector1 deg1, DegreeSelector2 deg2,
void operator()(Graph& g, DegreeSelector1 deg1, DegreeSelector2 deg2,
WeightMap weight) const
{
Graph& g = *gp;
GetDegreePair put_point;
typedef typename DegreeSelector1::value_type type1;
......@@ -258,16 +257,15 @@ template <class GetDegreePair>
struct get_avg_correlation
{
get_avg_correlation(python::object& avg, python::object& dev,
const vector<long double>& bins,
python::object& ret_bins)
const vector<long double>& bins,
python::object& ret_bins)
: _avg(avg), _dev(dev), _bins(bins), _ret_bins(ret_bins) {}
template <class Graph, class DegreeSelector1, class DegreeSelector2,
class WeightMap>
void operator()(Graph* gp, DegreeSelector1 deg1, DegreeSelector2 deg2,
void operator()(Graph& g, DegreeSelector1 deg1, DegreeSelector2 deg2,
WeightMap weight) const
{
Graph& g = *gp;
GetDegreePair put_point;
typedef typename DegreeSelector1::value_type type1;
......
......@@ -35,7 +35,7 @@ using namespace graph_tool;
typedef ConstantPropertyMap<int,GraphInterface::edge_t> dummy_weight;
python::object
get_vertex_combined_correlation_histogram(const GraphInterface& gi,
get_vertex_combined_correlation_histogram(GraphInterface& gi,
GraphInterface::deg_t deg1,
GraphInterface::deg_t deg2,
const vector<long double>& xbin,
......
......@@ -32,7 +32,7 @@ using namespace boost::lambda;
using namespace graph_tool;
void graph_correlations_imp1(const GraphInterface& g, python::object& hist,
void graph_correlations_imp1(GraphInterface& g, python::object& hist,
python::object& ret_bins,
boost::any deg1, boost::any deg2,
boost::any weight,
......
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