BidirectionalGraph.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib 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 Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file BidirectionalGraph.cpp
22 
23  \brief This is a implementation of a Bidirectional Graph.
24  By convention a bidirectional graph provides access to
25  out-and in edges.
26 */
27 
28 // Terralib Includes
29 #include "../../common/STLUtils.h"
30 #include "../core/GraphCache.h"
31 #include "../core/GraphData.h"
32 #include "../core/GraphDataManager.h"
33 #include "../core/GraphMetadata.h"
34 #include "../core/Edge.h"
35 #include "../core/Vertex.h"
36 #include "BidirectionalGraph.h"
37 #include "Graph.h"
38 
39 //STL
40 #include <iterator>
41 
42 
44 {
45 }
46 
48  Graph(metadata)
49 {
50 }
51 
53  Graph(cp, ls)
54 {
55 }
56 
58 
59 std::vector<te::graph::Vertex*> te::graph::BidirectionalGraph::getVertexNeighborhood(int id)
60 {
61  std::vector<te::graph::Vertex*> vec;
62 
64 
65  if(v)
66  {
67  std::vector<int> edges;
68 
69  std::copy(v->getPredecessors().begin(), v->getPredecessors().end(), std::back_inserter(edges));
70  std::copy(v->getSuccessors().begin(), v->getSuccessors().end(), std::back_inserter(edges));
71 
72  for(size_t i = 0; i < edges.size(); ++i)
73  {
74  te::graph::Edge* e = getEdge(edges[i]);
75 
76  if(e)
77  {
78  te::graph::Vertex* vNeighbor = nullptr;
79 
80  if(e->getIdFrom() == id)
81  {
82  vNeighbor = getVertex(e->getIdTo());
83  }
84  else
85  {
86  vNeighbor = getVertex(e->getIdFrom());
87  }
88 
89  if(vNeighbor)
90  {
91  vec.push_back(vNeighbor);
92  }
93  }
94  }
95  }
96 
97  return vec;
98 }
99 
101 {
102  te::graph::Vertex* v = getVertex(id);
103 
104  if(v)
105  {
106  if(v->getPredecessors().empty() && v->getSuccessors().empty())
107  {
108  flag = true;
109  }
110  else
111  {
112  flag = false;
113  }
114 
115  return true;
116  }
117 
118  return false;
119 }
120 
122 {
123  te::graph::Vertex* v = getVertex(id);
124 
125  if(v)
126  {
127  if(v->getPredecessors().empty())
128  {
129  flag = true;
130  }
131  else
132  {
133  flag = false;
134  }
135 
136  return true;
137  }
138 
139  return false;
140 }
141 
143 {
144  te::graph::Vertex* v = getVertex(id);
145 
146  if(v)
147  {
148  if(v->getSuccessors().empty())
149  {
150  flag = true;
151  }
152  else
153  {
154  flag = false;
155  }
156 
157  return true;
158  }
159 
160  return false;
161 }
162 
164 {
166  {
168  }
169 
170  if(m_graphData)
171  {
172  //set successor information
173  te::graph::Vertex* vFrom = getVertex(e->getIdFrom());
174 
175  if(vFrom)
176  vFrom->getSuccessors().insert(e->getId());
177  }
178 
180  {
182  }
183 
184  if(m_graphData)
185  {
186  //set successor information
187  te::graph::Vertex* vTo = getVertex(e->getIdTo());
188 
189  if(vTo)
190  vTo->getPredecessors().insert(e->getId());
191  }
192 
194 }
195 
197 {
198  te::graph::Edge* e = getEdge(id);
199 
200  if(e == nullptr)
201  {
202  return;
203  }
204 
205  //remove id from successor information from vFrom
206  te::graph::Vertex* vFrom = getVertex(e->getIdFrom());
207 
208  if(vFrom)
209  {
210  std::set<int>::iterator it = vFrom->getSuccessors().begin();
211 
212  while(it != vFrom->getSuccessors().end())
213  {
214  if(*it == id)
215  {
216  vFrom->getSuccessors().erase(it);
217  break;
218  }
219 
220  ++it;
221  }
222  }
223 
224  //remove id from predecessor information from vTo
225  te::graph::Vertex* vTo = getVertex(e->getIdTo());
226 
227  if(vTo)
228  {
229  std::set<int>::iterator it = vTo->getPredecessors().begin();
230 
231  while(it != vTo->getPredecessors().end())
232  {
233  if(*it == id)
234  {
235  vTo->getPredecessors().erase(it);
236  break;
237  }
238 
239  ++it;
240  }
241  }
242 
243 
245 }
246 
247 
248 std::vector<te::graph::Edge*> te::graph::BidirectionalGraph::getInEdges(int vId)
249 {
250  std::vector<te::graph::Edge*> vec;
251 
252  te::graph::Vertex* v = getVertex(vId);
253 
254  if(v)
255  {
256  std::set<int>::iterator it = v->getPredecessors().begin();
257 
258  while(it != v->getPredecessors().end())
259  {
260  te::graph::Edge* e = getEdge(*it);
261 
262  if(e)
263  {
264  vec.push_back(e);
265  }
266 
267  ++it;
268  }
269  }
270 
271  return vec;
272 }
273 
274 std::vector<te::graph::Edge*> te::graph::BidirectionalGraph::getOutEdges(int vId)
275 {
276  std::vector<te::graph::Edge*> vec;
277 
278  te::graph::Vertex* v = getVertex(vId);
279 
280  if(v)
281  {
282  std::set<int>::iterator it = v->getSuccessors().begin();
283 
284  while(it != v->getSuccessors().end())
285  {
286  te::graph::Edge* e = getEdge(*it);
287 
288  if(e)
289  {
290  vec.push_back(e);
291  }
292 
293  ++it;
294  }
295  }
296 
297  return vec;
298 }
This is a implementation of a Bidirectional Graph. By convention a bidirectional graph provides acces...
GraphMetadata * m_metadata
Graph Data loader strategy.
Definition: Graph.h:294
virtual bool isSourceVertex(int id, bool &flag)
This function indicates if a desired element is a source vertex.
virtual void removeEdge(int id)
This function removes the edge element from graph, also was removed in data source.
Definition: Graph.cpp:219
virtual std::vector< te::graph::Vertex * > getVertexNeighborhood(int id)
The neighborhood of a vertex v is an induced subgraph of the graph, formed by all vertices adjacent t...
std::set< int > & getPredecessors()
Returns the Predecessors vector.
Definition: Vertex.cpp:101
virtual te::graph::Edge * getEdge(int id)
It returns the edge element if it&#39;s exist.
Definition: Graph.cpp:237
bool m_memoryGraph
Flag used to indicate if the graph is a memory graph.
int getId()
It returns the edge identification.
Definition: Edge.cpp:66
virtual bool isSinkVertex(int id, bool &flag)
This function indicates if a desired element is a sink vertex.
GraphData * checkCacheByVertexId(int id)
This functions check in cache if the vertex element with a given id was alredy in memory...
Definition: GraphCache.cpp:246
~BidirectionalGraph()
Virtual destructor.
From the point of view of graph theory, vertices are treated as featureless and indivisible objects...
Definition: Vertex.h:68
Class used to define the edge struct of a graph. Its compose with a identifier, the vertex origin and...
Definition: Edge.h:58
This class is used to set the main functions of a cache policy.
virtual std::vector< te::graph::Edge * > getOutEdges(int vId)
It returns all edges that came out a vertex.
GraphCache * m_graphCache
Class used to keep all graph data loaded.
Definition: Graph.h:292
virtual void removeEdge(int id)
This function removes the edge element from graph, also was removed in data source.
This class define the main functions necessary to save and load the graph data and metadata informati...
std::set< int > & getSuccessors()
Returns the Successors vector.
Definition: Vertex.cpp:106
virtual void add(Vertex *v)
Add a new vertex element to a graph.
Definition: Graph.cpp:101
int getIdFrom()
It returns the vertex origin identification.
Definition: Edge.cpp:71
This is the main graph implementation, that uses a cache policy anda graph loader to get all elements...
GraphData * m_graphData
This class has the graph data and properties.
Definition: Graph.h:298
virtual std::vector< te::graph::Edge * > getInEdges(int vId)
It returns all edges that came in a vertex.
virtual te::graph::Vertex * getVertex(int id)
It returns the vertex element if it&#39;s exist.
Definition: Graph.cpp:141
virtual bool isIsolateVertex(int id, bool &flag)
This function indicates if a desired element is a isolated vertex.
This is the main graph implementation, that uses a cache policy anda graph loader to get all elements...
Definition: Graph.h:72
Class used to define the graph metadata informations.
Definition: GraphMetadata.h:56
virtual void add(Edge *e)
Add a new edge element to a graph.
int getIdTo()
It returns the vertex destiny identification.
Definition: Edge.cpp:76