-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdijkastra_heap.cpp
101 lines (78 loc) · 1.9 KB
/
dijkastra_heap.cpp
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
#include <cstdio>
#include <utility>
#include <cstdlib>
#include <queue>
#include <sys/time.h>
#include <vector>
#include <iostream>
#include <fstream>
using namespace std;
#define MAX 100001
#define INF (1<<20)
#define pii pair< int, int >
#define pb(x) push_back(x)
struct comp {
bool operator() (const pii &a, const pii &b) {
return a.second > b.second;
}
};
priority_queue< pii, vector< pii >, comp > Q;
vector< pii > G[MAX];
int D[MAX];
bool F[MAX];
int main(int argc, char *argv[])
{
struct timeval tv1, tv2;
if(argc != 3)
{
cout<<"Invalid arguments!!!\n";
exit(-1);
}
int i, u, v, w, sz, nodes, edges, starting;
char *file_name = argv[1];
int iter = atoi(argv[2]);
// create graph
// read content from file
std::ifstream infile(argv[1]);
infile >> nodes >> edges;
w = 1;
vector<int> neighbors;
for(i=0; i<edges; i++)
{
infile>>u>>v;
G[u].pb(pii(v, w));
G[v].pb(pii(u, w)); // for undirected
}
scanf("%d", &starting);
int ending;
scanf("%d", &ending);
gettimeofday(&tv1, NULL);
// now finding using heaps */
// initialize distance vector
for(i=1; i<=nodes; i++) D[i] = INF;
D[starting] = 0;
Q.push(pii(starting, 0));
// dijkstra algorithm using heaps
while(!Q.empty())
{
u = Q.top().first;
Q.pop();
if(F[u]) continue;
sz = G[u].size();
for(i=0; i<sz; i++) {
v = G[u][i].first;
w = G[u][i].second;
if(!F[v] && D[u]+w < D[v]) {
D[v] = D[u] + w;
Q.push(pii(v, D[v]));
}
}
F[u] = 1; // done with u
}
gettimeofday(&tv2, NULL);
double time_taken = (tv2.tv_sec - tv1.tv_sec)*1000000.0 + ((tv2.tv_usec - tv1.tv_usec));
// print the result
printf("For Node %d, to node %d minimum distance using heaps= %d\n", starting,ending, D[ending]);
printf("Time taken in finding path using heaps = %f micro seconds\n",time_taken);
return 0;
}