All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 }
60 
61 std::vector<te::graph::Vertex*> te::graph::BidirectionalGraph::getVertexNeighborhood(int id)
62 {
63  std::vector<te::graph::Vertex*> vec;
64 
65  te::graph::Vertex* v = getVertex(id);
66 
67  if(v)
68  {
69  std::vector<int> edges;
70 
71  std::copy(v->getPredecessors().begin(), v->getPredecessors().end(), std::back_inserter(edges));
72  std::copy(v->getSuccessors().begin(), v->getSuccessors().end(), std::back_inserter(edges));
73 
74  for(size_t i = 0; i < edges.size(); ++i)
75  {
76  te::graph::Edge* e = getEdge(edges[i]);
77 
78  if(e)
79  {
80  te::graph::Vertex* vNeighbor = 0;
81 
82  if(e->getIdFrom() == id)
83  {
84  vNeighbor = getVertex(e->getIdTo());
85  }
86  else
87  {
88  vNeighbor = getVertex(e->getIdFrom());
89  }
90 
91  if(vNeighbor)
92  {
93  vec.push_back(vNeighbor);
94  }
95  }
96  }
97  }
98 
99  return vec;
100 }
101 
103 {
104  te::graph::Vertex* v = getVertex(id);
105 
106  if(v)
107  {
108  if(v->getPredecessors().empty() && v->getSuccessors().empty())
109  {
110  flag = true;
111  }
112  else
113  {
114  flag = false;
115  }
116 
117  return true;
118  }
119 
120  return false;
121 }
122 
124 {
125  te::graph::Vertex* v = getVertex(id);
126 
127  if(v)
128  {
129  if(v->getPredecessors().empty())
130  {
131  flag = true;
132  }
133  else
134  {
135  flag = false;
136  }
137 
138  return true;
139  }
140 
141  return false;
142 }
143 
145 {
146  te::graph::Vertex* v = getVertex(id);
147 
148  if(v)
149  {
150  if(v->getSuccessors().empty())
151  {
152  flag = true;
153  }
154  else
155  {
156  flag = false;
157  }
158 
159  return true;
160  }
161 
162  return false;
163 }
164 
166 {
167  if(!m_metadata->m_memoryGraph)
168  {
169  m_graphData = m_graphCache->checkCacheByVertexId(e->getIdFrom());
170  }
171 
172  if(m_graphData)
173  {
174  //set successor information
175  te::graph::Vertex* vFrom = getVertex(e->getIdFrom());
176 
177  if(vFrom)
178  vFrom->getSuccessors().insert(e->getId());
179  }
180 
181  if(!m_metadata->m_memoryGraph)
182  {
183  m_graphData = m_graphCache->checkCacheByVertexId(e->getIdTo());
184  }
185 
186  if(m_graphData)
187  {
188  //set successor information
189  te::graph::Vertex* vTo = getVertex(e->getIdTo());
190 
191  if(vTo)
192  vTo->getPredecessors().insert(e->getId());
193  }
194 
196 }
197 
199 {
200  te::graph::Edge* e = getEdge(id);
201 
202  if(e == 0)
203  {
204  return;
205  }
206 
207  //remove id from successor information from vFrom
208  te::graph::Vertex* vFrom = getVertex(e->getIdFrom());
209 
210  if(vFrom)
211  {
212  std::set<int>::iterator it = vFrom->getSuccessors().begin();
213 
214  while(it != vFrom->getSuccessors().end())
215  {
216  if(*it == id)
217  {
218  vFrom->getSuccessors().erase(it);
219  break;
220  }
221 
222  ++it;
223  }
224  }
225 
226  //remove id from predecessor information from vTo
227  te::graph::Vertex* vTo = getVertex(e->getIdTo());
228 
229  if(vTo)
230  {
231  std::set<int>::iterator it = vTo->getPredecessors().begin();
232 
233  while(it != vTo->getPredecessors().end())
234  {
235  if(*it == id)
236  {
237  vTo->getPredecessors().erase(it);
238  break;
239  }
240 
241  ++it;
242  }
243  }
244 
245 
247 }
248 
249 
250 std::vector<te::graph::Edge*> te::graph::BidirectionalGraph::getInEdges(int vId)
251 {
252  std::vector<te::graph::Edge*> vec;
253 
254  te::graph::Vertex* v = getVertex(vId);
255 
256  if(v)
257  {
258  std::set<int>::iterator it = v->getPredecessors().begin();
259 
260  while(it != v->getPredecessors().end())
261  {
262  te::graph::Edge* e = getEdge(*it);
263 
264  if(e)
265  {
266  vec.push_back(e);
267  }
268 
269  ++it;
270  }
271  }
272 
273  return vec;
274 }
275 
276 std::vector<te::graph::Edge*> te::graph::BidirectionalGraph::getOutEdges(int vId)
277 {
278  std::vector<te::graph::Edge*> vec;
279 
280  te::graph::Vertex* v = getVertex(vId);
281 
282  if(v)
283  {
284  std::set<int>::iterator it = v->getSuccessors().begin();
285 
286  while(it != v->getSuccessors().end())
287  {
288  te::graph::Edge* e = getEdge(*it);
289 
290  if(e)
291  {
292  vec.push_back(e);
293  }
294 
295  ++it;
296  }
297  }
298 
299  return vec;
300 }
This is a implementation of a Bidirectional Graph. By convention a bidirectional graph provides acces...
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:216
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
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.
~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.
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:98
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...
virtual std::vector< te::graph::Edge * > getInEdges(int vId)
It returns all edges that came in a vertex.
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