All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UndirectedGraph.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 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/Vertex.h"
35 #include "../graphs/Graph.h"
36 #include "UndirectedGraph.h"
37 
38 
40 {
41 }
42 
44  Graph(cp, ls)
45 {
46 }
47 
49 {
50 }
51 
52 std::vector<te::graph::Vertex*> te::graph::UndirectedGraph::getVertexNeighborhood(int id)
53 {
54  std::vector<te::graph::Vertex*> vec;
55 
56  te::graph::Vertex* v = getVertex(id);
57 
58  if(v)
59  {
60  std::set<int>::iterator it = v->getNeighborhood().begin();
61 
62  while(it != v->getNeighborhood().end())
63  {
64  te::graph::Edge* e = getEdge(*it);
65 
66  if(e)
67  {
68  te::graph::Vertex* vNeighbor = 0;
69 
70  if(e->getIdFrom() == id)
71  {
72  vNeighbor = getVertex(e->getIdTo());
73  }
74  else
75  {
76  vNeighbor = getVertex(e->getIdFrom());
77  }
78 
79  if(vNeighbor)
80  {
81  vec.push_back(vNeighbor);
82  }
83  }
84  ++it;
85  }
86  }
87 
88  return vec;
89 }
90 
92 {
93  te::graph::Vertex* v = getVertex(id);
94 
95  if(v)
96  {
97  if(v->getNeighborhood().empty())
98  {
99  flag = true;
100  }
101  else
102  {
103  flag = false;
104  }
105  return true;
106  }
107 
108  return false;
109 }
110 
112 {
113  bool hasVFrom = false;
114  m_graphData = m_graphCache->checkCacheByVertexId(e->getIdFrom());
115  if(m_graphData)
116  {
117  //set successor information
118  te::graph::Vertex* vFrom = getVertex(e->getIdFrom());
119  if(vFrom)
120  {
121  vFrom->getNeighborhood().insert(e->getId());
122  hasVFrom = true;
123  }
124  }
125 
126  bool hasVTo = false;
127  m_graphData = m_graphCache->checkCacheByVertexId(e->getIdTo());
128  if(m_graphData)
129  {
130  //set predecessor information
131  te::graph::Vertex* vTo = getVertex(e->getIdTo());
132  if(vTo)
133  {
134  vTo->getNeighborhood().insert(e->getId());
135  hasVTo = true;
136  }
137  }
138 
139  if(hasVFrom && hasVTo)
140  {
142  }
143 }
144 
146 {
147  te::graph::Edge* e = getEdge(id);
148 
149  if(e == 0)
150  {
151  return;
152  }
153 
154  //remove id from neighborhood information from vFrom
155  te::graph::Vertex* vFrom = getVertex(e->getIdFrom());
156 
157  if(vFrom)
158  {
159  std::set<int>::iterator it = vFrom->getNeighborhood().begin();
160 
161  while(it != vFrom->getNeighborhood().end())
162  {
163  if(*it == id)
164  {
165  vFrom->getNeighborhood().erase(it);
166  break;
167  }
168 
169  ++it;
170  }
171  }
172 
173  //remove id from neighborhood information from vTo
174  te::graph::Vertex* vTo = getVertex(e->getIdTo());
175 
176  if(vTo)
177  {
178  std::set<int>::iterator it = vTo->getNeighborhood().begin();
179 
180  while(it != vTo->getNeighborhood().end())
181  {
182  if(*it == id)
183  {
184  vTo->getNeighborhood().erase(it);
185  break;
186  }
187 
188  ++it;
189  }
190  }
191 
193 }
194 
195 std::vector<te::graph::Edge*> te::graph::UndirectedGraph::getEdges(int vId)
196 {
197  std::vector<te::graph::Edge*> vec;
198 
199  te::graph::Vertex* v = getVertex(vId);
200 
201  if(v)
202  {
203  std::set<int>::iterator it = v->getNeighborhood().begin();
204 
205  while(it != v->getNeighborhood().end())
206  {
207  te::graph::Edge* e = getEdge(*it);
208 
209  if(e)
210  {
211  vec.push_back(e);
212  }
213 
214  ++it;
215  }
216  }
217 
218  return vec;
219 }
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 define the main functions necessary to save and load the graph data and metadata informati...
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...
~UndirectedGraph()
Virtual destructor.
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
int getIdTo()
It returns the vertex destiny identification.
Definition: Edge.cpp:76
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
int getId()
It returns the edge identification.
Definition: Edge.cpp:66
This is a implementation of a UndirectedGraph Graph. By definition a undirected graph has no directio...
virtual void add(Edge *e)
Add a new edge element to a graph.
virtual std::vector< te::graph::Edge * > getEdges(int vId)
It returns all edges that belongs to a vertex.
This class is used to set the main functions of a cache policy.
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 removeEdge(int id)
This function removes the edge element from graph, also was removed in data source.
std::set< int > & getNeighborhood()
Returns the Neighborhood vector.
Definition: Vertex.cpp:111
virtual void add(Vertex *v)
Add a new vertex element to a graph.
Definition: Graph.cpp:86