| 1 | // graph-tool -- a general graph modification and manipulation thingy |
|---|
| 2 | // |
|---|
| 3 | // Copyright (C) 2007 Tiago de Paula Peixoto <tiago@forked.de> |
|---|
| 4 | // |
|---|
| 5 | // This program is free software; you can redistribute it and/or |
|---|
| 6 | // modify it under the terms of the GNU General Public License |
|---|
| 7 | // as published by the Free Software Foundation; either version 3 |
|---|
| 8 | // of the License, or (at your option) any later version. |
|---|
| 9 | // |
|---|
| 10 | // This program is distributed in the hope that it will be useful, |
|---|
| 11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 13 | // GNU General Public License for more details. |
|---|
| 14 | // |
|---|
| 15 | // You should have received a copy of the GNU General Public License |
|---|
| 16 | // along with this program; if not, write to the Free Software |
|---|
| 17 | // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|---|
| 18 | |
|---|
| 19 | #ifndef GRAPH_HH |
|---|
| 20 | #define GRAPH_HH |
|---|
| 21 | |
|---|
| 22 | #include <tr1/unordered_map> |
|---|
| 23 | #include <boost/graph/graph_traits.hpp> |
|---|
| 24 | #include <boost/graph/adjacency_list.hpp> |
|---|
| 25 | #include <boost/vector_property_map.hpp> |
|---|
| 26 | #include <boost/dynamic_property_map.hpp> |
|---|
| 27 | #include <boost/variant.hpp> |
|---|
| 28 | #include <boost/python/object.hpp> |
|---|
| 29 | #include "histogram.hh" |
|---|
| 30 | #include "config.h" |
|---|
| 31 | #include "graph_properties.hh" |
|---|
| 32 | |
|---|
| 33 | namespace graph_tool |
|---|
| 34 | { |
|---|
| 35 | |
|---|
| 36 | //============================================================================== |
|---|
| 37 | // GraphInterface |
|---|
| 38 | // this structure is an interface to the internally kept graph |
|---|
| 39 | //============================================================================== |
|---|
| 40 | |
|---|
| 41 | class GraphInterface |
|---|
| 42 | { |
|---|
| 43 | public: |
|---|
| 44 | GraphInterface(); |
|---|
| 45 | ~GraphInterface(); |
|---|
| 46 | |
|---|
| 47 | enum degree_t |
|---|
| 48 | { |
|---|
| 49 | IN_DEGREE, |
|---|
| 50 | OUT_DEGREE, |
|---|
| 51 | TOTAL_DEGREE, |
|---|
| 52 | SCALAR |
|---|
| 53 | }; |
|---|
| 54 | |
|---|
| 55 | // histogram types |
|---|
| 56 | typedef std::tr1::unordered_map<double,size_t> hist_t; |
|---|
| 57 | typedef std::tr1::unordered_map<std::pair<double,double>,double, boost::hash<std::pair<double,double> > > hist2d_t; |
|---|
| 58 | typedef std::tr1::unordered_map<boost::tuple<double,double,double>,double_t, boost::hash<boost::tuple<double,double,double> > > hist3d_t; |
|---|
| 59 | typedef std::tr1::unordered_map<double,std::pair<double,double> > avg_corr_t; |
|---|
| 60 | |
|---|
| 61 | typedef boost::variant<degree_t,std::string> deg_t; |
|---|
| 62 | |
|---|
| 63 | //graph generation |
|---|
| 64 | typedef boost::function<double (size_t j, size_t k)> pjk_t; |
|---|
| 65 | typedef boost::function<std::pair<size_t,size_t> (double r1, double r2)> inv_ceil_t; |
|---|
| 66 | typedef boost::function<double (size_t jl, size_t kl, size_t j, size_t k)> corr_t; |
|---|
| 67 | typedef boost::function<std::pair<size_t,size_t> (double r1, double r2, size_t j, size_t k)> inv_corr_t; |
|---|
| 68 | void GenerateCorrelatedConfigurationalModel(size_t N, pjk_t p, pjk_t ceil, inv_ceil_t inv_ceil, double ceil_pjk_bound, corr_t corr, corr_t ceil_corr, inv_corr_t inv_ceil_corr, double ceil_corr_bound, bool undirected_corr, size_t seed, bool verbose); |
|---|
| 69 | |
|---|
| 70 | // basic stats |
|---|
| 71 | size_t GetNumberOfVertices() const; |
|---|
| 72 | size_t GetNumberOfEdges() const; |
|---|
| 73 | hist_t GetVertexHistogram(deg_t degree) const; |
|---|
| 74 | hist_t GetEdgeHistogram(std::string property) const; |
|---|
| 75 | |
|---|
| 76 | //correlations |
|---|
| 77 | hist2d_t GetCombinedVertexHistogram(deg_t degree1, deg_t degree2) const; |
|---|
| 78 | avg_corr_t GetAverageCombinedVertexCorrelation(deg_t degree1, deg_t degree2) const; |
|---|
| 79 | hist2d_t GetVertexCorrelationHistogram(deg_t degree1, deg_t degree2, std::string weight) const; |
|---|
| 80 | hist3d_t GetEdgeVertexCorrelationHistogram(deg_t deg1, std::string scalar, deg_t deg2) const; |
|---|
| 81 | avg_corr_t GetAverageNearestNeighboursCorrelation(deg_t origin_degree, deg_t neighbour_degree, std::string weight) const; |
|---|
| 82 | |
|---|
| 83 | // mixing |
|---|
| 84 | std::pair<double,double> GetAssortativityCoefficient(deg_t deg) const; |
|---|
| 85 | std::pair<double,double> GetScalarAssortativityCoefficient(deg_t deg) const; |
|---|
| 86 | |
|---|
| 87 | //clustering |
|---|
| 88 | void SetLocalClusteringToProperty(std::string property); |
|---|
| 89 | std::pair<double,double> GetGlobalClustering(); |
|---|
| 90 | void SetExtendedClusteringToProperty(std::string property_prefix, size_t max_depth); |
|---|
| 91 | |
|---|
| 92 | // other |
|---|
| 93 | void LabelComponents(std::string property); |
|---|
| 94 | void LabelParallelEdges(std::string property); |
|---|
| 95 | hist_t GetDistanceHistogram(std::string weight) const; |
|---|
| 96 | hist_t GetSampledDistanceHistogram(std::string weight, size_t samples, size_t seed) const; |
|---|
| 97 | double GetReciprocity() const; |
|---|
| 98 | void GetMinimumSpanningTree(std::string weight, std::string property); |
|---|
| 99 | void GetLineGraph(std::string out_file, std::string format); |
|---|
| 100 | void GetBetweenness(std::string weight, std::string edge_betweenness, std::string vertex_betweenness); |
|---|
| 101 | double GetCentralPointDominance(std::string vertex_betweenness); |
|---|
| 102 | |
|---|
| 103 | // community structure |
|---|
| 104 | enum comm_corr_t |
|---|
| 105 | { |
|---|
| 106 | ERDOS_REYNI, |
|---|
| 107 | UNCORRELATED, |
|---|
| 108 | CORRELATED |
|---|
| 109 | }; |
|---|
| 110 | |
|---|
| 111 | void GetCommunityStructure(double gamma, comm_corr_t corr, size_t n_iter, double Tmin, double Tmax, size_t Nseeds, size_t seed, bool verbose, std::string history_file, std::string weight, std::string property); |
|---|
| 112 | double GetModularity(std::string weight, std::string property); |
|---|
| 113 | void GetCommunityNetwork(std::string property, std::string out_file, std::string format) const; |
|---|
| 114 | |
|---|
| 115 | // filtering |
|---|
| 116 | void SetDirected(bool directed) {_directed = directed;} |
|---|
| 117 | bool GetDirected() const {return _directed;} |
|---|
| 118 | |
|---|
| 119 | void SetReversed(bool reversed) {_reversed = reversed;} |
|---|
| 120 | bool GetReversed() const {return _reversed;} |
|---|
| 121 | |
|---|
| 122 | void SetVertexFilterProperty(std::string property); |
|---|
| 123 | std::string GetVertexFilterProperty() const {return _vertex_filter_property;} |
|---|
| 124 | void SetVertexFilterRange(std::pair<double,double> allowed_range); |
|---|
| 125 | std::pair<double, double> GetVertexFilterRange() const {return _vertex_range;} |
|---|
| 126 | bool IsVertexFilterActive() const; |
|---|
| 127 | |
|---|
| 128 | void SetEdgeFilterProperty(std::string property); |
|---|
| 129 | std::string GetEdgeFilterProperty() const {return _edge_filter_property;} |
|---|
| 130 | void SetEdgeFilterRange(std::pair<double,double> allowed_range) {_edge_range = allowed_range;} |
|---|
| 131 | std::pair<double,double> GetEdgeFilterRange() const {return _edge_range;} |
|---|
| 132 | bool IsEdgeFilterActive() const; |
|---|
| 133 | |
|---|
| 134 | void SetGenericVertexFilter(boost::python::object filter); |
|---|
| 135 | void SetGenericEdgeFilter(boost::python::object filter); |
|---|
| 136 | |
|---|
| 137 | // modification |
|---|
| 138 | void RemoveVertexProperty(std::string property); |
|---|
| 139 | void RemoveEdgeProperty(std::string property); |
|---|
| 140 | void RemoveGraphProperty(std::string property); |
|---|
| 141 | void InsertEdgeIndexProperty(std::string property); |
|---|
| 142 | void InsertVertexIndexProperty(std::string property); |
|---|
| 143 | void EditVertexProperty(std::string property, std::string type, boost::python::object op); |
|---|
| 144 | void EditEdgeProperty(std::string property, std::string type, boost::python::object op); |
|---|
| 145 | void EditGraphProperty(std::string property, std::string type, boost::python::object op); |
|---|
| 146 | void ListProperties() const; |
|---|
| 147 | |
|---|
| 148 | // layout |
|---|
| 149 | void ComputeGraphLayoutGursoy(size_t iter = 0, size_t seed = 4357); |
|---|
| 150 | void ComputeGraphLayoutSpringBlock(size_t iter = 0, size_t seed = 4357); |
|---|
| 151 | |
|---|
| 152 | // i/o |
|---|
| 153 | void WriteToFile(std::string s); |
|---|
| 154 | void WriteToFile(std::string s, std::string format); |
|---|
| 155 | void ReadFromFile(std::string s); |
|---|
| 156 | void ReadFromFile(std::string s, std::string format); |
|---|
| 157 | |
|---|
| 158 | // signal handling |
|---|
| 159 | void InitSignalHandling(); |
|---|
| 160 | |
|---|
| 161 | // the following defines the edges' internal properties |
|---|
| 162 | typedef boost::property<boost::edge_index_t, size_t> EdgeProperty; |
|---|
| 163 | |
|---|
| 164 | // this is the main graph type |
|---|
| 165 | typedef boost::adjacency_list <boost::vecS, // edges |
|---|
| 166 | boost::vecS, // vertices |
|---|
| 167 | boost::bidirectionalS, |
|---|
| 168 | boost::no_property, |
|---|
| 169 | EdgeProperty > multigraph_t; |
|---|
| 170 | |
|---|
| 171 | private: |
|---|
| 172 | template <class Action, class ReverseCheck, class DirectedCheck> |
|---|
| 173 | friend void check_filter(const GraphInterface &g, Action a, ReverseCheck, DirectedCheck); |
|---|
| 174 | template <class Graph, class Action, class ReverseCheck, class DirectedCheck> |
|---|
| 175 | friend void check_python_filter(const Graph& g, const GraphInterface &gi, Action a, bool& found, ReverseCheck, DirectedCheck); |
|---|
| 176 | |
|---|
| 177 | friend class scalarS; |
|---|
| 178 | |
|---|
| 179 | multigraph_t _mg; |
|---|
| 180 | |
|---|
| 181 | bool _reversed; |
|---|
| 182 | bool _directed; |
|---|
| 183 | |
|---|
| 184 | // vertex index map |
|---|
| 185 | typedef boost::property_map<multigraph_t, boost::vertex_index_t>::type vertex_index_map_t; |
|---|
| 186 | vertex_index_map_t _vertex_index; |
|---|
| 187 | |
|---|
| 188 | // edge index map |
|---|
| 189 | typedef boost::property_map<multigraph_t, boost::edge_index_t>::type edge_index_map_t; |
|---|
| 190 | edge_index_map_t _edge_index; |
|---|
| 191 | |
|---|
| 192 | // graph properties |
|---|
| 193 | boost::dynamic_properties _properties; |
|---|
| 194 | |
|---|
| 195 | // vertex filter |
|---|
| 196 | std::string _vertex_filter_property; |
|---|
| 197 | typedef boost::variant<boost::vector_property_map<double, vertex_index_map_t>, |
|---|
| 198 | HashedDescriptorMap<vertex_index_map_t,double>, |
|---|
| 199 | boost::vector_property_map<size_t, vertex_index_map_t>, |
|---|
| 200 | HashedDescriptorMap<vertex_index_map_t, size_t>, |
|---|
| 201 | vertex_index_map_t, |
|---|
| 202 | DynamicPropertyMapWrap<double, boost::graph_traits<multigraph_t>::vertex_descriptor> > vertex_filter_map_t; |
|---|
| 203 | vertex_filter_map_t _vertex_filter_map; |
|---|
| 204 | std::pair<double,double> _vertex_range; |
|---|
| 205 | boost::python::object _vertex_python_filter; |
|---|
| 206 | |
|---|
| 207 | // edge filter |
|---|
| 208 | std::string _edge_filter_property; |
|---|
| 209 | typedef boost::variant<boost::vector_property_map<double, edge_index_map_t>, |
|---|
| 210 | HashedDescriptorMap<edge_index_map_t, double>, |
|---|
| 211 | boost::vector_property_map<size_t, edge_index_map_t>, |
|---|
| 212 | HashedDescriptorMap<edge_index_map_t, size_t>, |
|---|
| 213 | edge_index_map_t, |
|---|
| 214 | DynamicPropertyMapWrap<double, boost::graph_traits<multigraph_t>::edge_descriptor> > edge_filter_map_t; |
|---|
| 215 | edge_filter_map_t _edge_filter_map; |
|---|
| 216 | std::pair<double,double> _edge_range; |
|---|
| 217 | boost::python::object _edge_python_filter; |
|---|
| 218 | |
|---|
| 219 | }; |
|---|
| 220 | |
|---|
| 221 | std::pair<GraphInterface::degree_t,std::string> get_degree_type(GraphInterface::deg_t degree); |
|---|
| 222 | |
|---|
| 223 | //============================================================================== |
|---|
| 224 | // GraphException |
|---|
| 225 | //============================================================================== |
|---|
| 226 | |
|---|
| 227 | class GraphException : public std::exception |
|---|
| 228 | { |
|---|
| 229 | std::string _error; |
|---|
| 230 | public: |
|---|
| 231 | GraphException(std::string error) {_error = error;} |
|---|
| 232 | virtual ~GraphException() throw () {} |
|---|
| 233 | virtual const char * what () const throw () {return _error.c_str();} |
|---|
| 234 | }; |
|---|
| 235 | |
|---|
| 236 | } //namespace graph_tool |
|---|
| 237 | |
|---|
| 238 | //#include "node_graph_io.hh" |
|---|
| 239 | |
|---|
| 240 | #endif |
|---|
| 241 | |
|---|
| 242 | |
|---|
| 243 | |
|---|