All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
GetSubGraph.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 GetSubGraph.h
22 
23  \brief This class defines a function used to get a sub graph for a graph
24 
25 */
26 
27 // Terralib
28 #include "../../common/Translator.h"
29 #include "../../common/progress/TaskProgress.h"
30 #include "../core/AbstractGraph.h"
31 #include "../core/Edge.h"
32 #include "../core/Vertex.h"
33 #include "../graphs/BidirectionalGraph.h"
34 #include "../graphs/Graph.h"
35 #include "../iterator/SequenceIterator.h"
36 #include "../Config.h"
37 #include "../Exception.h"
38 #include "GetSubGraph.h"
39 
40 // STL
41 #include <cstdlib>
42 #include <iostream>
43 
44 
46 {
47  te::graph::Vertex* vertex = graph->getVertex(vertexId);
48 
49  te::graph::Vertex* outVertex = new te::graph::Vertex(vertex);
50 
51  outGraph->add(outVertex);
52 
53  std::set<int> predecessors;
54 
55  predecessors.insert(vertex->getPredecessors().begin(), vertex->getPredecessors().end());
56 
57  std::set<int>::iterator it = predecessors.begin();
58 
59  std::set<int> vertexIdSet;
60 
61  vertexIdSet.insert(vertex->getId());
62 
63  while(it != predecessors.end())
64  {
65  int edgeId = *(it);
66 
67  te::graph::Edge* e = graph->getEdge(edgeId);
68 
69  if(e)
70  {
71  te::graph::Edge* outE = new te::graph::Edge(e);
72 
73  outGraph->add(outE);
74 
75  te::graph::Vertex* vFrom = graph->getVertex(e->getIdFrom());
76 
77  if(vFrom)
78  {
79  std::set<int>::iterator itSet = vertexIdSet.find(vFrom->getId());
80 
81  if(itSet == vertexIdSet.end())
82  {
83  vertexIdSet.insert(vFrom->getId());
84 
85  te::graph::Vertex* outVFrom = new te::graph::Vertex(vFrom);
86 
87  outGraph->add(outVFrom);
88 
89  getPredecessor(vFrom, graph, outGraph, vertexIdSet);
90  }
91  }
92  }
93 
94  ++it;
95  }
96 }
97 
99 {
100 }
101 
103 {
104  if(v->getPredecessors().empty() == false)
105  {
106  std::set<int> predecessors;
107 
108  predecessors.insert(v->getPredecessors().begin(), v->getPredecessors().end());
109 
110  std::set<int>::iterator it = predecessors.begin();
111 
112  while(it != predecessors.end())
113  {
114  int edgeId = *(it);
115 
116  te::graph::Edge* e = g->getEdge(edgeId);
117 
118  if(e)
119  {
120  te::graph::Edge* outE = new te::graph::Edge(e);
121 
122  outGraph->add(outE);
123 
124  te::graph::Vertex* vFrom = g->getVertex(e->getIdFrom());
125 
126  if(vFrom)
127  {
128  std::set<int>::iterator itSet = vertexIdSet.find(vFrom->getId());
129 
130  if(itSet == vertexIdSet.end())
131  {
132  vertexIdSet.insert(vFrom->getId());
133 
134  te::graph::Vertex* outVFrom = new te::graph::Vertex(vFrom);
135 
136  outGraph->add(outVFrom);
137 
138  getPredecessor(vFrom, g, outGraph, vertexIdSet);
139  }
140  }
141  }
142 
143  ++it;
144  }
145  }
146 }
std::set< int > & getPredecessors()
Returns the Predecessors vector.
Definition: Vertex.cpp:101
void getPredecessor(te::graph::Vertex *v, te::graph::BidirectionalGraph *g, te::graph::AbstractGraph *outGraph, std::set< int > &vertexIdSet)
Recursive function used to calculate the deep attribute.
GetSubGraph(te::graph::BidirectionalGraph *graph, te::graph::AbstractGraph *outGraph, int vertexId)
Default constructor.
Definition: GetSubGraph.cpp:45
virtual te::graph::Edge * getEdge(int id)
It returns the edge element if it's exist.
Definition: Graph.cpp:234
virtual void add(Vertex *v)=0
Add a new vertex element to a graph.
From the point of view of graph theory, vertices are treated as featureless and indivisible objects...
Definition: Vertex.h:68
Class used to define the edge struct of a graph. Its compose with a identifier, the vertex origin and...
Definition: Edge.h:58
virtual ~GetSubGraph()
Virtual destructor.
Definition: GetSubGraph.cpp:98
Abstract class used to define the main functions of graph struct. All graph implementations must used...
Definition: AbstractGraph.h:55
int getIdFrom()
It returns the vertex origin identification.
Definition: Edge.cpp:71
This class defines a function used to get a sub graph for a graph.
virtual te::graph::Vertex * getVertex(int id)
It returns the vertex element if it's exist.
Definition: Graph.cpp:138
int getId()
It returns the vertex id.
Definition: Vertex.cpp:69
This is a implementation of a Bidirectional Graph. By convention a bidirectional graph provides acces...