All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DirectedGraph.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 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/Vertex.h"
35 #include "../graphs/Graph.h"
36 #include "DirectedGraph.h"
37 
38 
40 {
41 }
42 
44  Graph(cp, ls)
45 {
46 }
47 
49 {
50 }
51 
52 std::vector<te::graph::Vertex*> te::graph::DirectedGraph::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->getSuccessors().begin();
61 
62  while(it != v->getSuccessors().end())
63  {
64  te::graph::Edge* e = getEdge(*it);
65 
66  if(e)
67  {
68  te::graph::Vertex* vNeighbor = getVertex(e->getIdTo());
69 
70  if(vNeighbor)
71  {
72  vec.push_back(vNeighbor);
73  }
74  }
75  ++it;
76  }
77  }
78 
79  return vec;
80 }
81 
82 
84 {
85  te::graph::Vertex* v = getVertex(id);
86 
87  if(v)
88  {
89  if(v->getSuccessors().empty())
90  {
91  flag = true;
92  }
93  else
94  {
95  flag = false;
96  }
97 
98  return true;
99  }
100 
101  return false;
102 }
103 
105 {
106  m_graphData = m_graphCache->checkCacheByVertexId(e->getIdFrom());
107 
108  if(m_graphData)
109  {
110  //set successor information
111  te::graph::Vertex* vFrom = getVertex(e->getIdFrom());
112 
113  if(vFrom)
114  {
115  vFrom->getNeighborhood().insert(e->getId());
116 
118  }
119  }
120 }
121 
123 {
124  te::graph::Edge* e = getEdge(id);
125 
126  if(e == 0)
127  {
128  return;
129  }
130 
131  te::graph::Vertex* vFrom = getVertex(e->getIdFrom());
132 
133  if(vFrom)
134  {
135  //remove id from successor information
136  std::set<int>::iterator it = vFrom->getSuccessors().begin();
137 
138  while(it != vFrom->getSuccessors().end())
139  {
140  if(*it == id)
141  {
142  vFrom->getSuccessors().erase(it);
143  break;
144  }
145 
146  ++it;
147  }
148  }
149 
151 }
152 
153 std::vector<te::graph::Edge*> te::graph::DirectedGraph::getOutEdges(int vId)
154 {
155  std::vector<te::graph::Edge*> vec;
156 
157  te::graph::Vertex* v = getVertex(vId);
158 
159  if(v)
160  {
161  std::set<int>::iterator it = v->getSuccessors().begin();
162 
163  while(it != v->getSuccessors().end())
164  {
165  te::graph::Edge* e = getEdge(*it);
166 
167  if(e)
168  {
169  vec.push_back(e);
170  }
171  ++it;
172  }
173  }
174 
175  return vec;
176 }
Class used to define the edge struct of a graph. Its compose with a identifier, the vertex origin and...
Definition: Edge.h:58
This is a implementation of a Directed Graph. By convention a directed graph provides access to out-e...
virtual bool isSinkVertex(int id, bool &flag)
This function indicates if a desired element is a sink vertex.
This class define the main functions necessary to save and load the graph data and metadata informati...
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
DirectedGraph()
constructor.
int getIdTo()
It returns the vertex destiny identification.
Definition: Edge.cpp:76
This is the main graph implementation, that uses a cache policy anda graph loader to get all elements...
Definition: Graph.h:72
virtual void add(Edge *e)
Add a new edge element to a graph.
int getId()
It returns the edge identification.
Definition: Edge.cpp:66
~DirectedGraph()
Virtual destructor.
This class is used to set the main functions of a cache policy.
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 std::vector< te::graph::Edge * > getOutEdges(int vId)
It returns all edges that came out a vertex.
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
std::set< int > & getSuccessors()
Returns the Successors vector.
Definition: Vertex.cpp:106