All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
BidirectionalGraph.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2001-2009 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/Edge.h"
34 #include "../core/Vertex.h"
35 #include "BidirectionalGraph.h"
36 #include "Graph.h"
37 
38 //STL
39 #include <iterator>
40 
41 
43 {
44 }
45 
47  Graph(cp, ls)
48 {
49 }
50 
52 {
53 }
54 
55 std::vector<te::graph::Vertex*> te::graph::BidirectionalGraph::getVertexNeighborhood(int id)
56 {
57  std::vector<te::graph::Vertex*> vec;
58 
59  te::graph::Vertex* v = getVertex(id);
60 
61  if(v)
62  {
63  std::vector<int> edges;
64 
65  std::copy(v->getPredecessors().begin(), v->getPredecessors().end(), std::back_inserter(edges));
66  std::copy(v->getSuccessors().begin(), v->getSuccessors().end(), std::back_inserter(edges));
67 
68  for(size_t i = 0; i < edges.size(); ++i)
69  {
70  te::graph::Edge* e = getEdge(edges[i]);
71 
72  if(e)
73  {
74  te::graph::Vertex* vNeighbor = 0;
75 
76  if(e->getIdFrom() == id)
77  {
78  vNeighbor = getVertex(e->getIdTo());
79  }
80  else
81  {
82  vNeighbor = getVertex(e->getIdFrom());
83  }
84 
85  if(vNeighbor)
86  {
87  vec.push_back(vNeighbor);
88  }
89  }
90  }
91  }
92 
93  return vec;
94 }
95 
97 {
98  te::graph::Vertex* v = getVertex(id);
99 
100  if(v)
101  {
102  if(v->getPredecessors().empty() && v->getSuccessors().empty())
103  {
104  flag = true;
105  }
106  else
107  {
108  flag = false;
109  }
110 
111  return true;
112  }
113 
114  return false;
115 }
116 
118 {
119  te::graph::Vertex* v = getVertex(id);
120 
121  if(v)
122  {
123  if(v->getPredecessors().empty())
124  {
125  flag = true;
126  }
127  else
128  {
129  flag = false;
130  }
131 
132  return true;
133  }
134 
135  return false;
136 }
137 
139 {
140  te::graph::Vertex* v = getVertex(id);
141 
142  if(v)
143  {
144  if(v->getSuccessors().empty())
145  {
146  flag = true;
147  }
148  else
149  {
150  flag = false;
151  }
152 
153  return true;
154  }
155 
156  return false;
157 }
158 
160 {
161  if(m_graphCache->checkCacheByVertexId(e->getIdFrom()))
162  {
163  te::graph::Vertex* vFrom = getVertex(e->getIdFrom());
164  if(vFrom)
165  vFrom->getSuccessors().insert(e->getId());
166  }
167 
168  if(m_graphCache->checkCacheByVertexId(e->getIdTo()))
169  {
170  te::graph::Vertex* vTo = getVertex(e->getIdTo());
171  if(vTo)
172  vTo->getPredecessors().insert(e->getId());
173  }
174 
176 }
177 
179 {
180  te::graph::Edge* e = getEdge(id);
181 
182  if(e == 0)
183  {
184  return;
185  }
186 
187  //remove id from successor information from vFrom
188  te::graph::Vertex* vFrom = getVertex(e->getIdFrom());
189 
190  if(vFrom)
191  {
192  std::set<int>::iterator it = vFrom->getSuccessors().begin();
193 
194  while(it != vFrom->getSuccessors().end())
195  {
196  if(*it == id)
197  {
198  vFrom->getSuccessors().erase(it);
199  break;
200  }
201 
202  ++it;
203  }
204  }
205 
206  //remove id from predecessor information from vTo
207  te::graph::Vertex* vTo = getVertex(e->getIdTo());
208 
209  if(vTo)
210  {
211  std::set<int>::iterator it = vTo->getPredecessors().begin();
212 
213  while(it != vTo->getPredecessors().end())
214  {
215  if(*it == id)
216  {
217  vTo->getPredecessors().erase(it);
218  break;
219  }
220 
221  ++it;
222  }
223  }
224 
225 
227 }
228 
229 
230 std::vector<te::graph::Edge*> te::graph::BidirectionalGraph::getInEdges(int vId)
231 {
232  std::vector<te::graph::Edge*> vec;
233 
234  te::graph::Vertex* v = getVertex(vId);
235 
236  if(v)
237  {
238  std::set<int>::iterator it = v->getPredecessors().begin();
239 
240  while(it != v->getPredecessors().end())
241  {
242  te::graph::Edge* e = getEdge(*it);
243 
244  if(e)
245  {
246  vec.push_back(e);
247  }
248 
249  ++it;
250  }
251  }
252 
253  return vec;
254 }
255 
256 std::vector<te::graph::Edge*> te::graph::BidirectionalGraph::getOutEdges(int vId)
257 {
258  std::vector<te::graph::Edge*> vec;
259 
260  te::graph::Vertex* v = getVertex(vId);
261 
262  if(v)
263  {
264  std::set<int>::iterator it = v->getSuccessors().begin();
265 
266  while(it != v->getSuccessors().end())
267  {
268  te::graph::Edge* e = getEdge(*it);
269 
270  if(e)
271  {
272  vec.push_back(e);
273  }
274 
275  ++it;
276  }
277  }
278 
279  return vec;
280 }
Class used to define the edge struct of a graph. Its compose with a identifier, the vertex origin and...
Definition: Edge.h:58
virtual std::vector< te::graph::Edge * > getInEdges(int vId)
It returns all edges that came in a vertex.
This class define the main functions necessary to save and load the graph data and metadata informati...
virtual bool isSourceVertex(int id, bool &flag)
This function indicates if a desired element is a source vertex.
From the point of view of graph theory, vertices are treated as featureless and indivisible objects...
Definition: Vertex.h:68
int getIdFrom()
It returns the vertex origin identification.
Definition: Edge.cpp:71
This is a implementation of a Bidirectional Graph. By convention a bidirectional graph provides acces...
int getIdTo()
It returns the vertex destiny identification.
Definition: Edge.cpp:76
virtual void add(Edge *e)
Add a new edge element to a graph.
This is the main graph implementation, that uses a cache policy anda graph loader to get all elements...
virtual bool isIsolateVertex(int id, bool &flag)
This function indicates if a desired element is a isolated vertex.
virtual std::vector< te::graph::Edge * > getOutEdges(int vId)
It returns all edges that came out a vertex.
This is the main graph implementation, that uses a cache policy anda graph loader to get all elements...
Definition: Graph.h:72
virtual void removeEdge(int id)
This function removes the edge element from graph, also was removed in data source.
std::set< int > & getPredecessors()
Returns the Predecessors vector.
Definition: Vertex.cpp:101
virtual bool isSinkVertex(int id, bool &flag)
This function indicates if a desired element is a sink vertex.
int getId()
It returns the edge identification.
Definition: Edge.cpp:66
This class is used to set the main functions of a cache policy.
~BidirectionalGraph()
Virtual destructor.
virtual void removeEdge(int id)
This function removes the edge element from graph, also was removed in data source.
Definition: Graph.cpp:232
virtual void add(Vertex *v)
Add a new vertex element to a graph.
Definition: Graph.cpp:86
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 > & getSuccessors()
Returns the Successors vector.
Definition: Vertex.cpp:106