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_
4 #include <lemon/smart_graph.h>
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;
17 class GenericGraphBaseAdaptor {
20 class GenericIterator {
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) { }
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);
28 out_neighbor_index = 0;
29 typename T::OutArcIt e((*m_graph), nodeIt);
35 return m_graph->id(nodeIt);
38 int GetInDeg()
const {
39 return lemon::countInArcs((*m_graph), nodeIt) + lemon::countOutArcs((*m_graph), nodeIt);
43 return lemon::countInArcs((*m_graph), nodeIt) + lemon::countOutArcs((*m_graph), nodeIt);
46 int GetNbrNId(
int n) {
48 for (
typename T::OutArcIt e((*m_graph), nodeIt); e != lemon::INVALID; ++e) {
55 for (
typename T::InArcIt e((*m_graph), nodeIt); e != lemon::INVALID; ++e) {
64 int GetOutDeg()
const {
65 if (m_graph->id(nodeIt) >= _deg_vector->size()) {
68 return (*_deg_vector)[m_graph->id(nodeIt)];
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);
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);
86 int GetOutNId(
int n) {
87 if (n == out_neighbor_index) {
88 int _id = _GetOther(&_e);
96 for (
typename T::OutArcIt e((*m_graph), nodeIt); e != lemon::INVALID; ++e) {
98 node_id = _GetOther(&e);
108 return nodeIt != lemon::INVALID;
111 GenericIterator& operator++(
int) {
116 inline bool operator==(
const GenericIterator& rhs) {
117 return (nodeIt == rhs.nodeIt);
120 inline bool operator!=(
const GenericIterator& rhs) {
121 return !(*
this == rhs);
125 typename T::NodeIt nodeIt;
126 int out_neighbor_index;
127 typename T::OutArcIt _e;
128 const DegVector* _deg_vector;
133 GenericNode(
int id) : node_id(id) {}
140 typedef GenericNode TNode;
141 typedef GenericIterator TNodeI;
143 int AddNode(
int aNId = -1) {
144 int assigned_id = -1;
145 if (aNId >= GetMxNId()) {
146 for (
int i = GetMxNId(); i <= aNId; i++) {
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);
155 _AddNodeToVec(&(nodes_list), aNId);
157 LOG_M(DEBUG5,
" Called add node with node_id = " << aNId);
164 TNodeI iteratorDummy(&graph, °_vector);
165 return iteratorDummy;
169 TNodeI iteratorDummy(&graph, °_vector);
170 return iteratorDummy;
173 bool IsEdge(
const int& aSrcNId,
const int& aDstNId)
const {
176 int srcNId = aSrcNId;
177 int dstNId = aDstNId;
178 auto edge_it = edges_list.find(srcNId);
179 if (edge_it == edges_list.end()) {
182 if (dstNId >= edge_it->second.size()) {
185 return ((edge_it->second)[dstNId] == 1);
190 bool IsNode(
const int& NId)
const {
191 if (NId >= nodes_list.size()) {
194 return nodes_list[NId];
197 int GetNodes()
const {
198 return countNodes(graph);
201 int GetEdges()
const {
202 return countEdges(graph);
205 TNodeI GetNI(
const int& aNId)
const {
208 TNodeI iteratorDummy(&graph, node_id, °_vector);
209 return iteratorDummy;
212 int GetMxNId()
const {
213 return graph.maxNodeId() + 1;
217 void _CountEdge(
int aSrcNId,
int aDstNId) {
218 if (aSrcNId >= deg_vector.size()) {
219 deg_vector.resize(aSrcNId + 1, 0);
221 deg_vector[aSrcNId]++;
224 void _AddNode(
int aInt,
int aExt) {
228 LOG_M(DEBUG5,
" Adding node int= " << aInt <<
" ext=" << aExt);
233 void _AddNodeToVec(NodeList * aVec,
int aDstNId) {
234 if (aDstNId >= aVec->size()) {
235 aVec->resize(aDstNId+1);
237 (*aVec)[aDstNId] =
true;
240 void _AddEdge(
const int& aSrcNId,
const int& aDstNId) {
242 auto it = edges_list.insert(std::make_pair(aSrcNId, emptyList));
244 _AddNodeToVec(&(it.first->second), aDstNId);
248 EdgesList edges_list;
251 DegVector deg_vector;
255 class GenericGraphDirectedAdaptor :
public GenericGraphBaseAdaptor<lemon::SmartDigraph> {
257 int AddEdge(
const int& aSrcNId,
const int& aDstNId,
int aWeight = 1) {
258 int srcNId = aSrcNId;
259 int dstNId = aDstNId;
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);
275 class GenericGraphUnDirectedAdaptor :
public GenericGraphBaseAdaptor<lemon::SmartGraph> {
277 bool IsEdge(
const int& aSrcNId,
const int& aDstNId)
const {
278 LOG_M(DEBUG5,
" IsEdge? lookup src == " << aSrcNId
279 <<
" dst == " << aDstNId);
281 int _srcNId = aSrcNId;
282 int _destNId = aDstNId;
283 int srcNId = std::min(_srcNId, _destNId);
284 int destNId = std::max(_srcNId, _destNId);
286 LOG_M(DEBUG5,
" lookup src == " << srcNId
287 <<
" dst == " << destNId);
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);
294 if (destNId >= edge_it->second.size()) {
295 LOG_M(DEBUG5,
" dest not found in map, dest = " << destNId);
298 LOG_M(DEBUG5,
" value in map, dest = " << (edge_it->second)[destNId]);
299 return ((edge_it->second)[destNId] == 1);
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;
308 int _destNId = 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);
329 typedef GenericGraphUnDirectedAdaptor TUnDirectedGraph;
330 typedef GenericGraphDirectedAdaptor TDirectedGraph;
333 struct GraphTrait< TUnDirectedGraph > {
334 static const bool directed =
false;
338 struct GraphTrait< TDirectedGraph > {
339 static const bool directed =
true;
346 #endif // ALL_DISTANCE_SKETCH_ALL_DISTANCE_SKETCH_GRAPH_LEMON_GRAPH_ADAPTOR_H_