-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGrafoUtils.H
More file actions
executable file
·207 lines (161 loc) · 4.9 KB
/
GrafoUtils.H
File metadata and controls
executable file
·207 lines (161 loc) · 4.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#ifndef GRAFOUTILS_H
#define GRAFOUTILS_H
#include <iostream>
#include "Grafo.H"
/**
@brief Recorrido en profundidad sobre un nodo recursivo
@param grafo es el grafo sobre el cual se le realiza el recorrido
@param nodo el cual sera visitado
@param num numero de nodos visitados hasta el momento
@return numero de nodos visitados
*/
template <class NodeInfo,class ArcInfo>
void visitarNodo(Grafo<NodeInfo,ArcInfo> & grafo, NodoGrafo<NodeInfo> & nodo,unsigned int &num)
{
//visitamos el nodo
grafo.VisitarNodo(nodo.getDataNode());
num++;
//mostramos la info del Nodo
std::cout<<nodo.getDataNode()<<std::endl;
//obtenemos los nodos adyacentes a el
DNode<NodoGrafo<NodeInfo> > * listaNodosAdyacentes;
listaNodosAdyacentes = grafo.getListaNodosAdyacentes(nodo.getDataNode());
if(listaNodosAdyacentes == NULL)
return;
DNode<NodoGrafo<NodeInfo> > * aux;
aux = listaNodosAdyacentes;
aux = listaNodosAdyacentes->getNext();
while(aux != listaNodosAdyacentes)
{
//si no esta visitado... lo visitamos
if(!grafo.VisitadoEsteNodo(aux->getData().getDataNode()))
{
visitarNodo(grafo,aux->getData(),num);
}
aux = aux->getNext();
}
}
/**
@brief Recorrido en profundidad sobre un Grafo
@param grafo es el grafo sobre el cual se le realiza el recorrido
@return numero de nodos visitados
*/
template <class NodeInfo,class ArcInfo>
int recorridoProfundidad(Grafo<NodeInfo,ArcInfo> & grafo)
{
unsigned int num = 0;
//reiniciamos el grafo
grafo.resetGrafo();
DNode<NodoGrafo<NodeInfo> > listaNodos;
listaNodos = grafo.getListaNodos(); //Metodo Recomendado
if(!listaNodos.isEmpty())
visitarNodo(grafo,listaNodos.getNext()->getData(),num);
return num;
}
/**
@brief Recorrido en amplitud sobre un Grafo
@param grafo es el grafo sobre el cual se le realiza el recorrido
@return numero de nodos visitados
*/
template <class NodeInfo,class ArcInfo>
int recorridoAmplitud(Grafo<NodeInfo,ArcInfo> & grafo)
{
unsigned int num = 0;
//cola de arcos
Queue<NodoGrafo<NodeInfo> > queue;
//reiniciamos el grafo
grafo.resetGrafo();
DNode<NodoGrafo<NodeInfo> > listaNodos;
listaNodos = grafo.getListaNodos(); //Metodo Recomendado
//metemos el primer nodo en la cola
queue.put(listaNodos.getNext()->getData());
//mientras la cola de arcos no este vacia
while(!queue.isEmpty())
{
//saco el nodo y lo visito
NodoGrafo<NodeInfo> nodo;
nodo = queue.front();
grafo.VisitarNodo(nodo.getDataNode());
num++;
//imprimimos el valor de nodo
std::cout<<nodo.getDataNode()<<std::endl;
//obtenemos todos sus nodos adyacentes
DNode<NodoGrafo<NodeInfo> > * listaNodosAdyacentes;
listaNodosAdyacentes = grafo.getListaNodosAdyacentes(nodo.getDataNode());
//los metemos en la cola
if(listaNodosAdyacentes != NULL)
{
DNode<NodoGrafo<NodeInfo> > * aux;
aux = listaNodosAdyacentes;
aux = listaNodosAdyacentes->getNext();
while(aux != listaNodosAdyacentes)
{
//si no esta visitado... lo visitamos y que no este ya en la cola
if(!grafo.VisitadoEsteNodo(aux->getData().getDataNode()))
{
grafo.VisitarNodo(aux->getData().getDataNode());
queue.put(aux->getData());
}
aux = aux->getNext();
}
}
}
return num;
}
/**
@brief Recorrido en amplitud sobre un Grafo
@param grafo es el grafo sobre el cual se le realiza el recorrido
@return numero de nodos visitados
*/
template <class NodeInfo,class ArcInfo>
void caminoMinimoDijkstra(Grafo<NodeInfo,ArcInfo> & grafo , NodeInfo & nodoInicio, DNode<NodoGrafo<NodeInfo> > & lista)
{
unsigned int num = 0;
//Lista Arcos
DNode<ArcoGrafo<ArcInfo,NodeInfo> > * ListaArcos;
//reiniciamos el grafo
grafo.resetGrafo();
//veo si el nodo no ha sido expandido
if(grafo.ExpandidoEsteNodo(nodoInicio))
{
return;
}
grafo.ExpandirNodo(nodoInicio);
DNode<NodoGrafo<NodeInfo> > * nodito = new DNode<NodoGrafo<NodeInfo> >;
nodito->getData().getDataNode() = nodoInicio;
lista.insertPrev(nodito);
ListaArcos = grafo.getListaArcosAdyacentes(nodoInicio); //Metodo Recomendado
//ordenamos los arcos de menor a mayor
Heap < ArcoGrafo<ArcInfo,NodeInfo> > heap;
if(ListaArcos != NULL)
{
//pasamos todos los elementos al heap
DNode<ArcoGrafo<ArcInfo,NodeInfo> > *aux,*aux1;
aux = ListaArcos;
aux1 = aux->getNext();
while(aux1 != aux)
{
int acu = grafo.acumuladoDeEsteNodo(aux1->getData().getNodoDestino());
if(acu != -1)
{
if(acu > aux1->getData().getDataArc().getValue())
{
grafo.aumentarAcumuladoNodo(aux1->getData().getNodoDestino(),aux1->getData().getDataArc().getValue());
}
}
else
{
grafo.aumentarAcumuladoNodo(aux1->getData().getNodoDestino(),aux1->getData().getDataArc().getValue());
}
heap.insert(aux1->getData());
aux1 = aux1->getNext();
}
}
//mientras la lista de arcos no este vacia
while(!heap.isEmpty())
{
ArcoGrafo<ArcInfo,NodeInfo> arco = heap.deleteMin();
caminoMinimoDijkstra(grafo , arco.getNodoDestino(),lista );
}
}
#endif