UndirectedGraph.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 UndirectedGraph.cpp
22 
23  \brief This is a implementation of a UndirectedGraph Graph.
24  By definition a undirected graph has no direction
25  information about his edges.
26 */
27 
28 // Terralib Includes
29 #include "../../common/STLUtils.h"
30 #include "../core/Edge.h"
31 #include "../core/GraphCache.h"
32 #include "../core/GraphData.h"
33 #include "../core/GraphDataManager.h"
34 #include "../core/GraphMetadata.h"
35 #include "../core/Vertex.h"
36 #include "../graphs/Graph.h"
37 #include "UndirectedGraph.h"
38 
39 
41 {
42 }
43 
45  Graph(metadata)
46 {
47 }
48 
50  Graph(cp, ls)
51 {
52 }
53 
55 
56 std::vector<te::graph::Vertex*> te::graph::UndirectedGraph::getVertexNeighborhood(int id)
57 {
58  std::vector<te::graph::Vertex*> vec;
59 
61 
62  if(v)
63  {
64  std::set<int>::iterator it = v->getNeighborhood().begin();
65 
66  while(it != v->getNeighborhood().end())
67  {
68  te::graph::Edge* e = getEdge(*it);
69 
70  if(e)
71  {
72  te::graph::Vertex* vNeighbor = nullptr;
73 
74  if(e->getIdFrom() == id)
75  {
76  vNeighbor = getVertex(e->getIdTo());
77  }
78  else
79  {
80  vNeighbor = getVertex(e->getIdFrom());
81  }
82 
83  if(vNeighbor)
84  {
85  vec.push_back(vNeighbor);
86  }
87  }
88  ++it;
89  }
90  }
91 
92  return vec;
93 }
94 
96 {
98 
99  if(v)
100  {
101  if(v->getNeighborhood().empty())
102  {
103  flag = true;
104  }
105  else
106  {
107  flag = false;
108  }
109  return true;
110  }
111 
112  return false;
113 }
114 
116 {
117  bool hasVFrom = false;
118 
120  {
122  }
123 
124  if(m_graphData)
125  {
126  //set successor information
127  te::graph::Vertex* vFrom = getVertex(e->getIdFrom());
128  if(vFrom)
129  {
130  vFrom->getNeighborhood().insert(e->getId());
131  hasVFrom = true;
132  }
133  }
134 
135  bool hasVTo = false;
136 
138  {
140  }
141 
142  if(m_graphData)
143  {
144  //set predecessor information
145  te::graph::Vertex* vTo = getVertex(e->getIdTo());
146  if(vTo)
147  {
148  vTo->getNeighborhood().insert(e->getId());
149  hasVTo = true;
150  }
151  }
152 
153  if(hasVFrom && hasVTo)
154  {
156  }
157 }
158 
160 {
161  te::graph::Edge* e = getEdge(id);
162 
163  if(e == nullptr)
164  {
165  return;
166  }
167 
168  //remove id from neighborhood information from vFrom
169  te::graph::Vertex* vFrom = getVertex(e->getIdFrom());
170 
171  if(vFrom)
172  {
173  std::set<int>::iterator it = vFrom->getNeighborhood().begin();
174 
175  while(it != vFrom->getNeighborhood().end())
176  {
177  if(*it == id)
178  {
179  vFrom->getNeighborhood().erase(it);
180  break;
181  }
182 
183  ++it;
184  }
185  }
186 
187  //remove id from neighborhood information from vTo
188  te::graph::Vertex* vTo = getVertex(e->getIdTo());
189 
190  if(vTo)
191  {
192  std::set<int>::iterator it = vTo->getNeighborhood().begin();
193 
194  while(it != vTo->getNeighborhood().end())
195  {
196  if(*it == id)
197  {
198  vTo->getNeighborhood().erase(it);
199  break;
200  }
201 
202  ++it;
203  }
204  }
205 
207 }
208 
209 std::vector<te::graph::Edge*> te::graph::UndirectedGraph::getEdges(int vId)
210 {
211  std::vector<te::graph::Edge*> vec;
212 
213  te::graph::Vertex* v = getVertex(vId);
214 
215  if(v)
216  {
217  std::set<int>::iterator it = v->getNeighborhood().begin();
218 
219  while(it != v->getNeighborhood().end())
220  {
221  te::graph::Edge* e = getEdge(*it);
222 
223  if(e)
224  {
225  vec.push_back(e);
226  }
227 
228  ++it;
229  }
230  }
231 
232  return vec;
233 }
GraphMetadata * m_metadata
Graph Data loader strategy.
Definition: Graph.h:294
virtual void add(Edge *e)
Add a new edge element to a graph.
virtual void removeEdge(int id)
This function removes the edge element from graph, also was removed in data source.
Definition: Graph.cpp:219
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
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
~UndirectedGraph()
Virtual destructor.
From the point of view of graph theory, vertices are treated as featureless and indivisible objects...
Definition: Vertex.h:68
virtual bool isIsolateVertex(int id, bool &flag)
This function indicates if a desired element is a isolated vertex.
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.
GraphCache * m_graphCache
Class used to keep all graph data loaded.
Definition: Graph.h:292
This class define the main functions necessary to save and load the graph data and metadata informati...
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
std::set< int > & getNeighborhood()
Returns the Neighborhood vector.
Definition: Vertex.cpp:111
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...
GraphData * m_graphData
This class has the graph data and properties.
Definition: Graph.h:298
virtual te::graph::Vertex * getVertex(int id)
It returns the vertex element if it&#39;s exist.
Definition: Graph.cpp:141
This is a implementation of a UndirectedGraph Graph. By definition a undirected graph has no directio...
virtual void removeEdge(int id)
This function removes the edge element from graph, also was removed in data source.
This is the main graph implementation, that uses a cache policy anda graph loader to get all elements...
Definition: Graph.h:72
virtual std::vector< te::graph::Edge * > getEdges(int vId)
It returns all edges that belongs to a vertex.
Class used to define the graph metadata informations.
Definition: GraphMetadata.h:56
int getIdTo()
It returns the vertex destiny identification.
Definition: Edge.cpp:76