Repository navigation
Expand file tree
/
Copy pathMain.cpp
More file actions
178 lines (147 loc) · 4.02 KB
/
Copy pathMain.cpp
File metadata and controls
178 lines (147 loc) · 4.02 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
#include "PriorityQueue.h"
#include "Vertex.h"
#include <algorithm>
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <limits>
using namespace std;
typedef vector<vector<pair<int,float> > > Graph;
void dijkstra(const Graph &graph,const int &source,const int &destination,vector<int> &path)
{
PriorityQueue<Vertex> pq;
vector<float> d(graph.size());
vector<int> parent(graph.size());
for(unsigned int i = 0 ;i < graph.size(); i++)
{
d[i] = std::numeric_limits<float>::max();
parent[i] = -1;
}
d[source] = 0.0f;
Vertex *v = new Vertex();
v->adjacent_vertex = source;
v->weight = d[source];
pq.insertItem(source, v);
while(!pq.isEmpty())
{
int u = pq.minElement()->adjacent_vertex;
if(u==destination) break;
pq.removeMin();
for(unsigned int i=0; i < graph[u].size(); i++)
{
int v= graph[u][i].first;
float w = graph[u][i].second;
if(d[v] > d[u]+w)
{
d[v] = d[u]+w;
parent[v] = u;
Vertex *vert = new Vertex();
vert->adjacent_vertex = v;
vert->weight = d[v];
pq.insertItem(source, vert);
}
}
}
path.clear();
int p = destination;
path.push_back(destination);
while(p!=source)
{
p = parent[p];
path.push_back(p);
}
}
int main()
{
Graph graph;
char file[50];
string js_nodes, js_paths;
cout << "Please enter the name of file to read: ";
cin >> file;
ifstream ifs (file);
if(ifs.is_open())
{
int vert_count;
ifs >> vert_count;
cout << "Number of Verts: " << vert_count << endl;
graph.resize(vert_count+1);
for(int i = 0; i < vert_count; ++i)
js_nodes += string("{\"index\":"+to_string(i)+", \"value\":"+to_string(i+1)+", \"r\":20}\n");
while(true)
{
int vert;
ifs >> vert;
if(vert == -1)
break;
cout << "-> Vert: " << vert << endl;
while(true)
{
int adj_vert, weight;
ifs >> adj_vert;
if(adj_vert == -1)
break;
ifs >> weight;
cout << "Adjacent Vert: " << adj_vert << endl;
cout << "Weight: " << weight << endl;
graph[vert].push_back(make_pair(adj_vert, weight));
js_paths += string("{\"source\":"+to_string(vert-1)+", \"target\":"+to_string(adj_vert-1)+", \"distance\":"+to_string(weight*3)+"}\n");
}
if(ifs.eof()){ // EOF? Break.
ifs.close();
break;
}
}
}
int source, destination;
cout << endl << "Please enter the source vertex: ";
cin >> source;
cout << endl << "Please enter the destination vertex: ";
cin >> destination;
ofstream html_index, txt_nodes, txt_paths;
html_index.open ("index.html", ios::trunc);
ifstream html_template ("template.html");
if(html_template.is_open() && html_index.is_open())
{
while(true)
{
string line;
getline( html_template, line );
size_t needle0 = line.find("@STYLE@");
size_t needle1 = line.find("@ROUTE@");
if( needle0 != string::npos)
{
if(js_nodes.size() > 4000)
html_index << "\t<link href=\"stylesheets/shortest-path-dense.css\" rel=\"stylesheet\" type=\"text/css\" />\n";
else
html_index << "\t<link href=\"stylesheets/shortest-path.css\" rel=\"stylesheet\" type=\"text/css\" />\n";
}
else if( needle1 != string::npos)
html_index << string("\t\t\t\tvar route = sp1.findRoute("+to_string(source-1)+","+to_string(destination-1)+");\n");
else
html_index << line;
html_index << endl;
if(html_template.eof()){ // EOF? Break.
html_template.close();
html_index.close();
break;
}
}
}else{
cerr << "index.html or template.html could not be opened, exiting..." << endl;
return 1;
}
txt_nodes.open ("nodes.txt", ios::trunc);
txt_nodes << js_nodes.substr(0, js_nodes.size()-1);
txt_nodes.close();
txt_paths.open ("paths.txt", ios::trunc);
txt_paths << js_paths.substr(0, js_paths.size()-1);
txt_paths.close();
cout << "Calculating shortest path..." << endl;
vector<int> path;
dijkstra(graph, source, destination, path);
for(int i=path.size()-1;i>=0;i--)
cout << "->" << path[i];
cout << endl;
return 0;
}