All Distance Sketch  0.1
All distance sketch based algorithms
lemon_graph_adaptor.h
1 #ifndef ALL_DISTANCE_SKETCH_ALL_DISTANCE_SKETCH_GRAPH_LEMON_GRAPH_ADAPTOR_H_
2 #define ALL_DISTANCE_SKETCH_ALL_DISTANCE_SKETCH_GRAPH_LEMON_GRAPH_ADAPTOR_H_
3 
4 #include <lemon/smart_graph.h>
5 #include "graph.h"
6 
7 namespace all_distance_sketch {
8 namespace graph {
9 
10 typedef std::vector<bool> NodeList;
11 typedef std::vector<int> DegVector;
12 typedef std::unordered_map<int, NodeList > EdgesList;
13 typedef std::unordered_map<int, int> ConvertionMap;
14 
15 
16 template <class T>
17 class GenericGraphBaseAdaptor {
18  public:
19 
20  class GenericIterator {
21  public:
22  GenericIterator(const T* g, DegVector * _deg_v) : m_graph(g), nodeIt((*g)), out_neighbor_index(0), _e((*m_graph), nodeIt), _deg_vector(_deg_v) { }
23 
24  GenericIterator(const T* g, int aNId, const DegVector * _deg_v) : m_graph(g) {
25  typename T::Node n = g->nodeFromId(aNId);
26  typename T::NodeIt _nodeIt((*g), n);
27  nodeIt = _nodeIt;
28  out_neighbor_index = 0;
29  typename T::OutArcIt e((*m_graph), nodeIt);
30  _e = e;
31  _deg_vector = _deg_v;
32  }
33 
34  int GetId() const {
35  return m_graph->id(nodeIt); // parent->ConvertIntToExt(m_graph->id(nodeIt));
36  }
37 
38  int GetInDeg() const {
39  return lemon::countInArcs((*m_graph), nodeIt) + lemon::countOutArcs((*m_graph), nodeIt);
40  }
41 
42  int GetDeg() const {
43  return lemon::countInArcs((*m_graph), nodeIt) + lemon::countOutArcs((*m_graph), nodeIt);
44  }
45 
46  int GetNbrNId(int n) {
47  int cnt = 0;
48  for (typename T::OutArcIt e((*m_graph), nodeIt); e != lemon::INVALID; ++e) {
49  if (cnt == n) {
50  return _GetOther(&e);
51  }
52  cnt++;
53  }
54 
55  for (typename T::InArcIt e((*m_graph), nodeIt); e != lemon::INVALID; ++e) {
56  if (cnt == n) {
57  return _GetOther(&e);
58  }
59  cnt++;
60  }
61  return -1;
62  }
63 
64  int GetOutDeg() const {
65  if (m_graph->id(nodeIt) >= _deg_vector->size()) {
66  return 0;
67  }
68  return (*_deg_vector)[m_graph->id(nodeIt)];
69  //return lemon::countOutArcs((*m_graph), nodeIt);
70  }
71 
72  int _GetOther(typename T::OutArcIt* e) const {
73  int source = m_graph->id(m_graph->source(*e));
74  int target = m_graph->id(m_graph->target(*e));
75  source = ( (source == GetId()) ? target : source);
76  return source; // parent->ConvertIntToExt(source);
77  }
78 
79  int _GetOther(typename T::InArcIt* e) const {
80  int source = m_graph->id(m_graph->source(*e));
81  int target = m_graph->id(m_graph->target(*e));
82  source = ( (source == GetId()) ? target : source);
83  return source; // parent->ConvertIntToExt(source);
84  }
85 
86  int GetOutNId(int n) {
87  if (n == out_neighbor_index) {
88  int _id = _GetOther(&_e);
89  ++out_neighbor_index;
90  ++_e;
91  return _id;
92  }
93 
94  int cnt = 0;
95  int node_id = -1;
96  for (typename T::OutArcIt e((*m_graph), nodeIt); e != lemon::INVALID; ++e) {
97  if (cnt == n) {
98  node_id = _GetOther(&e);
99  break;
100  }
101  cnt++;
102  }
103 
104  return node_id;
105  }
106 
107  bool HasMore() {
108  return nodeIt != lemon::INVALID;
109  }
110 
111  GenericIterator& operator++(int) {
112  ++nodeIt;
113  return *this;
114  }
115 
116  inline bool operator==(const GenericIterator& rhs) {
117  return (nodeIt == rhs.nodeIt);
118  }
119 
120  inline bool operator!=(const GenericIterator& rhs) {
121  return !(*this == rhs);
122  }
123  const T* m_graph;
124  // const GenericGraphBaseAdaptor<T>* parent;
125  typename T::NodeIt nodeIt;
126  int out_neighbor_index;
127  typename T::OutArcIt _e;
128  const DegVector* _deg_vector;
129  };
130 
131  class GenericNode {
132  public:
133  GenericNode(int id) : node_id(id) {}
134  int GetId() {
135  return node_id;
136  }
137  int node_id;
138  };
139 
140  typedef GenericNode TNode;
141  typedef GenericIterator TNodeI;
142 
143  int AddNode(int aNId = -1) {
144  int assigned_id = -1;
145  if (aNId >= GetMxNId()) {
146  for (int i = GetMxNId(); i <= aNId; i++) {
147  graph.addNode();
148  }
149  _AddNodeToVec(&(nodes_list), aNId);
150  } else if (aNId == -1) {
151  typename T::Node node = graph.addNode();
152  assigned_id = graph.id(node);
153  _AddNodeToVec(&(nodes_list), assigned_id);
154  } else {
155  _AddNodeToVec(&(nodes_list), aNId);
156  }
157  LOG_M(DEBUG5, " Called add node with node_id = " << aNId);
158 
159  // _AddNode(node_id, aNid);
160  return assigned_id;
161  }
162 
163  TNodeI BegNI() {
164  TNodeI iteratorDummy(&graph, &deg_vector);
165  return iteratorDummy;
166  }
167 
168  TNodeI EndNI() {
169  TNodeI iteratorDummy(&graph, &deg_vector);
170  return iteratorDummy;
171  }
172 
173  bool IsEdge(const int& aSrcNId, const int& aDstNId) const {
174  // int srcNId = externalToInternal.GetMapping(aSrcNId);
175  // int dstNId = externalToInternal.GetMapping(aDstNId);
176  int srcNId = aSrcNId;
177  int dstNId = aDstNId;
178  auto edge_it = edges_list.find(srcNId);
179  if (edge_it == edges_list.end()) {
180  return false;
181  } else {
182  if (dstNId >= edge_it->second.size()) {
183  return false;
184  }
185  return ((edge_it->second)[dstNId] == 1);
186  }
187  return false;
188  }
189 
190  bool IsNode(const int& NId) const {
191  if (NId >= nodes_list.size()) {
192  return false;
193  }
194  return nodes_list[NId]; // ConvertExtToInt(NId) != -1;
195  }
196 
197  int GetNodes() const {
198  return countNodes(graph);
199  }
200 
201  int GetEdges() const {
202  return countEdges(graph);
203  }
204 
205  TNodeI GetNI(const int& aNId) const {
206  // int node_id = externalToInternal.GetMapping(aNId);
207  int node_id = aNId;
208  TNodeI iteratorDummy(&graph, node_id, &deg_vector);
209  return iteratorDummy;
210  }
211 
212  int GetMxNId() const {
213  return graph.maxNodeId() + 1; // externalToInternal.GetMaxSrc() + 1;
214  }
215 
216 protected:
217  void _CountEdge(int aSrcNId, int aDstNId) {
218  if (aSrcNId >= deg_vector.size()) {
219  deg_vector.resize(aSrcNId + 1, 0);
220  }
221  deg_vector[aSrcNId]++;
222  }
223 
224  void _AddNode(int aInt, int aExt) {
225  if (aExt == -1) {
226  aExt = aInt;
227  }
228  LOG_M(DEBUG5, " Adding node int= " << aInt << " ext=" << aExt);
229  // InsertIntToExt(aInt, aExt);
230  // InsertExtToInt(aExt, aInt);
231  }
232 
233  void _AddNodeToVec(NodeList * aVec, int aDstNId) {
234  if (aDstNId >= aVec->size()) {
235  aVec->resize(aDstNId+1);
236  }
237  (*aVec)[aDstNId] = true;
238  }
239 
240  void _AddEdge(const int& aSrcNId, const int& aDstNId) {
241  NodeList emptyList;
242  auto it = edges_list.insert(std::make_pair(aSrcNId, emptyList));
243  // Element exists
244  _AddNodeToVec(&(it.first->second), aDstNId);
245  }
246 
247  T graph;
248  EdgesList edges_list;
249  NodeList nodes_list;
250  public:
251  DegVector deg_vector;
252 };
253 
254 
255 class GenericGraphDirectedAdaptor : public GenericGraphBaseAdaptor<lemon::SmartDigraph> {
256  public:
257  int AddEdge(const int& aSrcNId, const int& aDstNId, int aWeight = 1) {
258  int srcNId = aSrcNId; // ConvertExtToInt(aSrcNId);
259  int dstNId = aDstNId; // ConvertExtToInt(aSrcNId);
260  if (IsEdge(srcNId, dstNId) == false) {
261  lemon::SmartDigraph::Node source = graph.nodeFromId(srcNId);
262  lemon::SmartDigraph::Node dest = graph.nodeFromId(dstNId);
263  graph.addArc(source, dest);
264  _AddEdge(srcNId, dstNId);
265  _CountEdge(srcNId, dstNId);
266  return -1;
267  } else {
268  return 1;
269  }
270  return 1;
271  }
272 };
273 
274 
275 class GenericGraphUnDirectedAdaptor : public GenericGraphBaseAdaptor<lemon::SmartGraph> {
276  public:
277  bool IsEdge(const int& aSrcNId, const int& aDstNId) const {
278  LOG_M(DEBUG5, " IsEdge? lookup src == " << aSrcNId
279  << " dst == " << aDstNId);
280 
281  int _srcNId = aSrcNId; // ConvertExtToInt(aSrcNId);
282  int _destNId = aDstNId; // ConvertExtToInt(aDstNId);
283  int srcNId = std::min(_srcNId, _destNId);
284  int destNId = std::max(_srcNId, _destNId);
285 
286  LOG_M(DEBUG5, " lookup src == " << srcNId
287  << " dst == " << destNId);
288 
289  auto edge_it = edges_list.find(srcNId);
290  if (edge_it == edges_list.end()) {
291  LOG_M(DEBUG5, " key not found in map, key = " << srcNId);
292  return false;
293  } else {
294  if (destNId >= edge_it->second.size()) {
295  LOG_M(DEBUG5, " dest not found in map, dest = " << destNId);
296  return false;
297  }
298  LOG_M(DEBUG5, " value in map, dest = " << (edge_it->second)[destNId]);
299  return ((edge_it->second)[destNId] == 1);
300  }
301  return false;
302  }
303 
304  int AddEdge(const int& aSrcNId, const int& aDstNId, int aWeight = 1) {
305  LOG_M(DEBUG5, "Start: Source node Id = " << aSrcNId
306  << " Dest node Id = " << aDstNId);
307  int _srcNId = aSrcNId; // ConvertExtToInt(aSrcNId);
308  int _destNId = aDstNId; // ConvertExtToInt(aDstNId);
309  LOG_M(DEBUG5, "Convert: Source node Id = " << _srcNId
310  << " Dest node Id = " << _destNId);
311  int srcNId = std::min(_srcNId, _destNId);
312  int destNId = std::max(_srcNId, _destNId);
313  if (IsEdge(srcNId, destNId) == false) {
314  lemon::SmartGraph::Node source = graph.nodeFromId(srcNId);
315  lemon::SmartGraph::Node dest = graph.nodeFromId(destNId);
316  graph.addEdge(source, dest);
317  _AddEdge(srcNId, destNId);
318  _CountEdge(srcNId, destNId);
319  _CountEdge(destNId, srcNId);
320  return -1;
321  } else {
322  return 1;
323  }
324  return 1;
325  }
326 };
327 
328 
329 typedef GenericGraphUnDirectedAdaptor TUnDirectedGraph;
330 typedef GenericGraphDirectedAdaptor TDirectedGraph;
331 
332 template<>
333 struct GraphTrait< TUnDirectedGraph > {
334  static const bool directed = false;
335 };
336 
337 template<>
338 struct GraphTrait< TDirectedGraph > {
339  static const bool directed = true;
340 };
341 
342 
343 } // namespace graph
344 } // namespace all_distance_sketch
345 
346 #endif // ALL_DISTANCE_SKETCH_ALL_DISTANCE_SKETCH_GRAPH_LEMON_GRAPH_ADAPTOR_H_
Definition: common.h:53
Graph class.