-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpractica.cpp
More file actions
147 lines (124 loc) · 3.74 KB
/
Copy pathpractica.cpp
File metadata and controls
147 lines (124 loc) · 3.74 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
/*
* Guillermo Garcia Patiño Lenza
* MAR Grupo A
* practica.cpp
*/
#include <iostream>
#include <stdexcept>
#include <vector>
#include <list>
#include <limits.h>
#include <unordered_map>
#include <ctime>
#include <chrono>
#include <cmath>
#include "skew_heap.h"
#include "grafo.h"
#include "graph_generator.h"
using namespace std;
void dijsktra(grafo<int> const & g, grafo<int>::vertice const & v0, bool m){
using node = grafo<int>::vertice;
skew_heap h;
unordered_map<node,int> distancias;
unordered_map<node,node> predecesores;
int nv = g.getNumVertices();
// inicializacion de vectores de distancias y predecesores, y
// del monticulo binario
for(auto &v : g.vertSet()){
if(v != v0){
h.insert(v, INT_MAX);
distancias[v] = INT_MAX;
predecesores[v] = v0;
}
}
/*
* Reduzco las distancias de los vertices adyacentes
* al inicial en el mapa y en el monticulo
*/
for(grafo<int>::gnode ng : g.adyacentes(v0)){
h.decrease_key(ng->vert, ng->cost);
distancias[ng->vert] = ng->cost;
predecesores[ng->vert] = v0;
}
/*
* Bucle principal del algoritmo. Sigue el esquema voraz visto
* en clase de teoria
*/
for(int j = 0; (j < nv-2 ); j++){
pair<grafo<int>::vertice, int> p = h.borra_Min();
node actual = p.first;
for(grafo<int>::gnode ng : g.adyacentes(actual)){
if(h.contains(ng->vert)){
if( ng->cost + distancias[actual] < distancias[ng->vert] ){
distancias[ng->vert] = ng->cost + distancias[actual];
predecesores[ng->vert] = actual;
h.decrease_key(ng->vert, distancias[ng->vert]);
}
}
}
}
/*
* Muestra el resultado (solo en showmode)
*/
if(m){
for(auto p : distancias){
cout << " Nodo " << p.first << " distancia " << p.second << " predecesor " << predecesores[p.first] << endl;
}
}
}
void showmode(int initial_nodes, int max_weight, float edge_chance){
graph_generator gg (initial_nodes, edge_chance, max_weight);
grafo<int> g = gg.generar(false);
cout << "Se ha generado el siguiente grafo: " << endl;
cout << g.print() << endl;
dijsktra(g, g.getFirst(), true);
}
void timemode(int initial_nodes, int max_nodes, int inc, int max_weight, float edge_chance){
graph_generator gg(1,edge_chance,max_weight);
cout << "# Vertices" << " " << "Tiempo" << endl;
for(int i = initial_nodes; i < max_nodes ; i+= inc){
gg.setNumVertices(i);
float tiempo = 0;
for(int j = 0; j < 3; j++){
grafo<int> g = gg.generar(true);
auto start = std::chrono::steady_clock::now();
dijsktra(g, g.getFirst(), false);
auto end = std::chrono::steady_clock::now();
tiempo += std::chrono::duration<double, std::milli>(end - start).count();
}
tiempo = tiempo / 3.0;
if(tiempo < 10.0){
tiempo = 0;
for(int j = 0; j < 100; j++){
grafo<int> g = gg.generar(true);
auto start = std::chrono::steady_clock::now();
dijsktra(g, g.getFirst(), false);
auto end = std::chrono::steady_clock::now();
tiempo += std::chrono::duration<double, std::milli>(end - start).count();
}
tiempo = tiempo / 100.0;
}
cout << i << " " << tiempo << '\n';
}
cout << "DONE" << endl;
}
int main(int argc, char * argv[]){
if(argc < 6 || argc > 7){
cout << "Error: Usage is ./practica <-s/-t> <initial_nodes> <max nodes> <inc> <max_weight> <edge_chance>" << endl;
return 0;
}
string maux = argv[1];
bool mode = (maux == "-s")? true : (maux == "-t")? false : throw invalid_argument("El primer argumento debe ser o -s (showmode) o -t (timemode) \n");
int initial_nodes = std::stoi(argv[2]);
int max_nodes = std::stoi(argv[3]);
int inc = std::stoi(argv[4]);
int max_weight = std::stoi(argv[5]);
float edge_chance = std::stof(argv[6]);
if(mode){
showmode(initial_nodes, max_weight, edge_chance);
}
else{
timemode(initial_nodes, max_nodes, inc, max_weight, edge_chance);
}
return 0;
}