-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
235 lines (195 loc) · 4.99 KB
/
main.cpp
File metadata and controls
235 lines (195 loc) · 4.99 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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <queue>
using namespace std;
// comparator class for priority queue to do distance comparisons from
// the <u, w> pair format
class Comparator
{
public:
int operator() ( const pair<int, int>& p1, const pair<int, int>& p2 )
{
return p1.second < p2.second;
}
};
// Graph type is adjacency list of edges (v, u) and weights w
// Graph is a vector of root 'v' vertices, of a vector of pairs,
// where each pair is of format <u, w>
typedef vector<vector<pair<int, int> > > Graph;
void printGraph(Graph g);
void inputGraph(string fileName, Graph& Gref);
void Dijkstras(Graph g, char source);
void SRP(Graph g, char source, int k);
int main(int argc, char* argv[])
{
if(argc != 2)
{cout << "Usage: " << argv[0] << " <filename>\n"; return 0;}
Graph G;
inputGraph(argv[1], G);
printGraph(G);
char source; int k;
cout << "\nEnter source node for Dijkstra's: "; cin >> source;
Dijkstras(G, source);
cout << "\nEnter source node for Reliable Shortest Path: "; cin >> source;
cout << "Enter 'k' for Reliable Shortest Path: "; cin >> k;
SRP(G, source, k);
return 0;
}
void SRP(Graph g, char source, int k)
{
// priority queue consists of int pairs of format
// <vertex, distance to vertex from source>
priority_queue<pair<int, int>, vector<pair<int, int> >, Comparator > PQ;
// int arrays to record distances from source to vertices, and edge counts
// from source to vertices. Support max of 50 vertices
int dist[50], edges[50];
// initializations of distance and path edge counts
for(int i=0; i<g.size(); i++)
{
if(g[i] != g[source-65]);
{
dist[i] = 999;
edges[i] = 0;
}
}
dist[source-65] = 0;
// push initial source vertex
PQ.push(pair<int, int>(source-65, dist[source-65]));
int v, u, w;
while(!PQ.empty())
{
// pop highest priority vertex for exploring
u = PQ.top().first;
PQ.pop();
int size = g[u].size();
// only progess to neighboring vertices if edge count is not >= k
if(edges[u] < k)
{
for(int i=0; i<size; i++)
{
v = g[u][i].first;
w = g[u][i].second;
// checking distances for possible update or distance and
// path edge count
if(dist[v] > dist[u] + w)
{
dist[v] = dist[u] + w;
PQ.push(pair<int, int>(v, dist[v]));
edges[v] = edges[u]+1;
}
}
}
}
ofstream outFile;
outFile.open("out.txt", ios::ate | ios:: app);
outFile << "\n------Shortest Reliable Path's------\nSource: " << source
<< ", k : " << k << endl;
for(int i=0; i<g.size(); i++)
{
outFile << "Node " << (char)(i+65) << ": ";
if(dist[i] != 999)
outFile << dist[i] << ", " << edges[i] << " edges\n";
else
outFile << "No path with edges < " << k << endl;
}
outFile << "---End of Shortest Reliable Path's---\n\n";
}
void Dijkstras(Graph g, char source)
{
// priority queue consists of int pairs of format
// <vertex, distance to vertex from source>
priority_queue<pair<int, int>, vector<pair<int, int> >, Comparator > PQ;
// int array to record distances from source to vertices
int dist[50];
// initializations
for(int i=0; i<g.size(); i++)
{
if(g[i] != g[source-65]);
{
dist[i] = 999;
}
}
dist[source-65] = 0;
// initial push of source vertex
PQ.push(pair<int, int>(source-65, dist[source-65]));
int v, u, w;
// dijkstra's algorithm
while(!PQ.empty())
{
u = PQ.top().first;
PQ.pop();
int size = g[u].size();
for(int i=0; i<size; i++)
{
v = g[u][i].first;
w = g[u][i].second;
if(dist[v] > dist[u] + w)
{
dist[v] = dist[u] + w;
PQ.push(pair<int, int>(v, dist[v]));
}
}
}
ofstream outFile;
outFile.open("out.txt", ios::ate | ios:: app);
outFile << "\n-----------Dijkstra's-----------\nSource: " << source << endl;
for(int i=0; i<g.size(); i++)
{
outFile << "Node " << (char)(i+65) << ": ";
if(dist[i] != 999)
outFile << dist[i] << endl;
else
outFile << "No possible path\n";
}
outFile << "--------End of Dijkstra's--------\n";
}
void inputGraph(string fileName, Graph& Gref)
{
ifstream inFile(fileName.c_str());
if(!inFile.is_open())
cout << "FILE READ ERROR";
char graphType;
string poo;
if(inFile.get() == '#')
getline(inFile, poo);
// get graphType of 'U' or 'D'
inFile.get(graphType);
getline(inFile, poo);
int Gsize = 1; Gref.resize(Gsize);
char v, u; int weight;
while(!inFile.eof())
{
inFile >> v >> u >> weight;
getline(inFile, poo);
while((v-65) >= Gref.size()-1)
{
Gsize++;
Gref.resize(Gsize);
}
Gref[v-65].push_back(make_pair(u-65, weight));
/// input for undirected graph (reverse edge)
if(graphType == 'U')
{
while((u-65) > Gref.size()-1)
{
Gsize++;
Gref.resize(Gsize);
}
Gref[u-65].push_back(make_pair(v-65, weight));
}
}
}
void printGraph(Graph G)
{
ofstream outFile;
outFile.open("out.txt");
outFile << "Graph contains: \n";
for(int i=0; i<G.size(); i++)
{
outFile << (char)(i+65) << ":\n";
for(int j=0; j<G[i].size(); j++)
outFile << "\t" << (char)(G[i][j].first+65) << ", " << G[i][j].second << endl;
}
}