DirectedGraph.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 DirectedGraph.cpp
22 
23  \brief This is a implementation of a Directed Graph.
24  By convention a directed graph provides access to out-edges only.
25 */
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 "DirectedGraph.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::DirectedGraph::getVertexNeighborhood(int id)
57 {
58  std::vector<te::graph::Vertex*> vec;
59 
61 
62  if(v)
63  {
64  std::set<int>::iterator it = v->getSuccessors().begin();
65 
66  while(it != v->getSuccessors().end())
67  {
68  te::graph::Edge* e = getEdge(*it);
69 
70  if(e)
71  {
72  te::graph::Vertex* vNeighbor = getVertex(e->getIdTo());
73 
74  if(vNeighbor)
75  {
76  vec.push_back(vNeighbor);
77  }
78  }
79  ++it;
80  }
81  }
82 
83  return vec;
84 }
85 
86 
88 {
90 
91  if(v)
92  {
93  if(v->getSuccessors().empty())
94  {
95  flag = true;
96  }
97  else
98  {
99  flag = false;
100  }
101 
102  return true;
103  }
104 
105  return false;
106 }
107 
109 {
111  {
113  }
114 
115  if(m_graphData)
116  {
117  //set successor information
118  te::graph::Vertex* vFrom = getVertex(e->getIdFrom());
119 
120  if(vFrom)
121  {
122  vFrom->getSuccessors().insert(e->getId());
123 
125  }
126  }
127 }
128 
130 {
131  te::graph::Edge* e = getEdge(id);
132 
133  if(e == nullptr)
134  {
135  return;
136  }
137 
138  te::graph::Vertex* vFrom = getVertex(e->getIdFrom());
139 
140  if(vFrom)
141  {
142  //remove id from successor information
143  std::set<int>::iterator it = vFrom->getSuccessors().begin();
144 
145  while(it != vFrom->getSuccessors().end())
146  {
147  if(*it == id)
148  {
149  vFrom->getSuccessors().erase(it);
150  break;
151  }
152 
153  ++it;
154  }
155  }
156 
158 }
159 
160 std::vector<te::graph::Edge*> te::graph::DirectedGraph::getOutEdges(int vId)
161 {
162  std::vector<te::graph::Edge*> vec;
163 
164  te::graph::Vertex* v = getVertex(vId);
165 
166  if(v)
167  {
168  std::set<int>::iterator it = v->getSuccessors().begin();
169 
170  while(it != v->getSuccessors().end())
171  {
172  te::graph::Edge* e = getEdge(*it);
173 
174  if(e)
175  {
176  vec.push_back(e);
177  }
178  ++it;
179  }
180  }
181 
182  return vec;
183 }
virtual void removeEdge(int id)
This function removes the edge element from graph, also was removed in data source.
GraphMetadata * m_metadata
Graph Data loader strategy.
Definition: Graph.h:294
DirectedGraph()
constructor.
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
From the point of view of graph theory, vertices are treated as featureless and indivisible objects...
Definition: Vertex.h:68
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...
virtual bool isSinkVertex(int id, bool &flag)
This function indicates if a desired element is a sink 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...
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:101
int getIdFrom()
It returns the vertex origin identification.
Definition: Edge.cpp:71
virtual void add(Edge *e)
Add a new edge element to a graph.
virtual std::vector< te::graph::Edge * > getOutEdges(int vId)
It returns all edges that came out a vertex.
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 the main graph implementation, that uses a cache policy anda graph loader to get all elements...
Definition: Graph.h:72
~DirectedGraph()
Virtual destructor.
Class used to define the graph metadata informations.
Definition: GraphMetadata.h:56
int getIdTo()
It returns the vertex destiny identification.
Definition: Edge.cpp:76
This is a implementation of a Directed Graph. By convention a directed graph provides access to out-e...