All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 }
57 
58 std::vector<te::graph::Vertex*> te::graph::UndirectedGraph::getVertexNeighborhood(int id)
59 {
60  std::vector<te::graph::Vertex*> vec;
61 
62  te::graph::Vertex* v = getVertex(id);
63 
64  if(v)
65  {
66  std::set<int>::iterator it = v->getNeighborhood().begin();
67 
68  while(it != v->getNeighborhood().end())
69  {
70  te::graph::Edge* e = getEdge(*it);
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  ++it;
91  }
92  }
93 
94  return vec;
95 }
96 
98 {
99  te::graph::Vertex* v = getVertex(id);
100 
101  if(v)
102  {
103  if(v->getNeighborhood().empty())
104  {
105  flag = true;
106  }
107  else
108  {
109  flag = false;
110  }
111  return true;
112  }
113 
114  return false;
115 }
116 
118 {
119  bool hasVFrom = false;
120 
121  if(!m_metadata->m_memoryGraph)
122  {
123  m_graphData = m_graphCache->checkCacheByVertexId(e->getIdFrom());
124  }
125 
126  if(m_graphData)
127  {
128  //set successor information
129  te::graph::Vertex* vFrom = getVertex(e->getIdFrom());
130  if(vFrom)
131  {
132  vFrom->getNeighborhood().insert(e->getId());
133  hasVFrom = true;
134  }
135  }
136 
137  bool hasVTo = false;
138 
139  if(!m_metadata->m_memoryGraph)
140  {
141  m_graphData = m_graphCache->checkCacheByVertexId(e->getIdTo());
142  }
143 
144  if(m_graphData)
145  {
146  //set predecessor information
147  te::graph::Vertex* vTo = getVertex(e->getIdTo());
148  if(vTo)
149  {
150  vTo->getNeighborhood().insert(e->getId());
151  hasVTo = true;
152  }
153  }
154 
155  if(hasVFrom && hasVTo)
156  {
158  }
159 }
160 
162 {
163  te::graph::Edge* e = getEdge(id);
164 
165  if(e == 0)
166  {
167  return;
168  }
169 
170  //remove id from neighborhood information from vFrom
171  te::graph::Vertex* vFrom = getVertex(e->getIdFrom());
172 
173  if(vFrom)
174  {
175  std::set<int>::iterator it = vFrom->getNeighborhood().begin();
176 
177  while(it != vFrom->getNeighborhood().end())
178  {
179  if(*it == id)
180  {
181  vFrom->getNeighborhood().erase(it);
182  break;
183  }
184 
185  ++it;
186  }
187  }
188 
189  //remove id from neighborhood information from vTo
190  te::graph::Vertex* vTo = getVertex(e->getIdTo());
191 
192  if(vTo)
193  {
194  std::set<int>::iterator it = vTo->getNeighborhood().begin();
195 
196  while(it != vTo->getNeighborhood().end())
197  {
198  if(*it == id)
199  {
200  vTo->getNeighborhood().erase(it);
201  break;
202  }
203 
204  ++it;
205  }
206  }
207 
209 }
210 
211 std::vector<te::graph::Edge*> te::graph::UndirectedGraph::getEdges(int vId)
212 {
213  std::vector<te::graph::Edge*> vec;
214 
215  te::graph::Vertex* v = getVertex(vId);
216 
217  if(v)
218  {
219  std::set<int>::iterator it = v->getNeighborhood().begin();
220 
221  while(it != v->getNeighborhood().end())
222  {
223  te::graph::Edge* e = getEdge(*it);
224 
225  if(e)
226  {
227  vec.push_back(e);
228  }
229 
230  ++it;
231  }
232  }
233 
234  return vec;
235 }
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:216
int getId()
It returns the edge identification.
Definition: Edge.cpp:66
~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.
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:98
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...
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