All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 }
57 
58 std::vector<te::graph::Vertex*> te::graph::DirectedGraph::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->getSuccessors().begin();
67 
68  while(it != v->getSuccessors().end())
69  {
70  te::graph::Edge* e = getEdge(*it);
71 
72  if(e)
73  {
74  te::graph::Vertex* vNeighbor = getVertex(e->getIdTo());
75 
76  if(vNeighbor)
77  {
78  vec.push_back(vNeighbor);
79  }
80  }
81  ++it;
82  }
83  }
84 
85  return vec;
86 }
87 
88 
90 {
91  te::graph::Vertex* v = getVertex(id);
92 
93  if(v)
94  {
95  if(v->getSuccessors().empty())
96  {
97  flag = true;
98  }
99  else
100  {
101  flag = false;
102  }
103 
104  return true;
105  }
106 
107  return false;
108 }
109 
111 {
112  if(!m_metadata->m_memoryGraph)
113  {
114  m_graphData = m_graphCache->checkCacheByVertexId(e->getIdFrom());
115  }
116 
117  if(m_graphData)
118  {
119  //set successor information
120  te::graph::Vertex* vFrom = getVertex(e->getIdFrom());
121 
122  if(vFrom)
123  {
124  vFrom->getSuccessors().insert(e->getId());
125 
127  }
128  }
129 }
130 
132 {
133  te::graph::Edge* e = getEdge(id);
134 
135  if(e == 0)
136  {
137  return;
138  }
139 
140  te::graph::Vertex* vFrom = getVertex(e->getIdFrom());
141 
142  if(vFrom)
143  {
144  //remove id from successor information
145  std::set<int>::iterator it = vFrom->getSuccessors().begin();
146 
147  while(it != vFrom->getSuccessors().end())
148  {
149  if(*it == id)
150  {
151  vFrom->getSuccessors().erase(it);
152  break;
153  }
154 
155  ++it;
156  }
157  }
158 
160 }
161 
162 std::vector<te::graph::Edge*> te::graph::DirectedGraph::getOutEdges(int vId)
163 {
164  std::vector<te::graph::Edge*> vec;
165 
166  te::graph::Vertex* v = getVertex(vId);
167 
168  if(v)
169  {
170  std::set<int>::iterator it = v->getSuccessors().begin();
171 
172  while(it != v->getSuccessors().end())
173  {
174  te::graph::Edge* e = getEdge(*it);
175 
176  if(e)
177  {
178  vec.push_back(e);
179  }
180  ++it;
181  }
182  }
183 
184  return vec;
185 }
virtual void removeEdge(int id)
This function removes the edge element from graph, also was removed in data source.
DirectedGraph()
constructor.
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
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.
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:98
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.
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...