-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.cpp
250 lines (177 loc) · 6.8 KB
/
main.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
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#include <iostream>
using namespace std;
#include <fstream>
#include <cmath>
#include <list>
#include <stack>
#include <string>
double const INFINITE = 0xFFFFFFFFFFFFF;
struct Vertex
{
int xCoordinate;
int yCoordinate;
};
//Dijkstra's algorithm
void Dijkstra( double const* const* const &adjacencyMatrix, int const &totalVertices, int const &sourceVertex, int *&parentList, double *&distanceFromSourceVertexList)
{
struct DijkstraNode
{
int vertexNumber;
double distance;
};
for (int i = 0; i < totalVertices; i++)
{
distanceFromSourceVertexList[i] = INFINITE;
}
distanceFromSourceVertexList[sourceVertex] = 0; //distance of source vertex from source vertex is 0.
parentList[sourceVertex] = -1; //the parent of source vertex is NULL.
list<int> queue;
for (int i = 0; i < totalVertices; i++)
{
queue.push_back(i);
}
//extracting min from the queue
while (queue.size() != 0)
{
int minVertex = queue.front();
queue.pop_front();
int length = queue.size();
for (int i = 0; i < length; i++)
{
int secondVertex = queue.front();
queue.pop_front();
if (distanceFromSourceVertexList[minVertex] <= distanceFromSourceVertexList[secondVertex])
{
queue.push_back(secondVertex);
continue;
}
else
{
queue.push_back(minVertex);
minVertex = secondVertex;
}
}
//checking each vertex that has an edge with our minVertex
for (int i = 0; i < totalVertices; i++)
{
if (minVertex == i || adjacencyMatrix[i][minVertex] == -1)
continue;
if (distanceFromSourceVertexList[minVertex] + adjacencyMatrix[i][minVertex] < distanceFromSourceVertexList[i])
{
distanceFromSourceVertexList[i] = distanceFromSourceVertexList[minVertex] + adjacencyMatrix[i][minVertex];
parentList[i] = minVertex;
}
}
}
}//end of Dikstra function
//This function reads the graph from the file. fileName is the input parameter, all other parameters are output parameters.
void readGraph(string const& fileName, Vertex *&vertexList, double ** &adjacencyMatrix, int &totalEdges, int &totalVertices)
{
ifstream read(fileName);
if (read.is_open() == false)
{
cout << "File could not open!" << endl;
exit(1);
}
read >> totalVertices >> totalEdges;
adjacencyMatrix = new double*[totalVertices];
vertexList = new Vertex[totalVertices]; //vertexList stores information about the each vertex' coordinates
for (int i = 0; i < totalVertices; i++)
{
adjacencyMatrix[i] = new double[totalVertices];
for (int j = 0; j < totalVertices; j++)
adjacencyMatrix[i][j] = -1; /*-1 means that there is no edge between vertex i and vertex j. If there is an edge between i and j
it will be updated in the loop where edges are being read from the file*/
}
//reading coordinates of each vertex
for (int i = 0; i < totalVertices; i++)
{
int vertexNumber;
read >> vertexNumber;
read >> vertexList[vertexNumber].xCoordinate >> vertexList[vertexNumber].yCoordinate;
}
//reading the edges from file
for (int i = 0; i < totalEdges; i++)
{
int v1, v2;
read >> v1 >> v2;
//calculating the weight of the edge using distance formula
double weight = sqrt(((vertexList[v1].xCoordinate - vertexList[v2].xCoordinate)*(vertexList[v1].xCoordinate - vertexList[v2].xCoordinate)) + ((vertexList[v1].yCoordinate - vertexList[v2].yCoordinate)*(vertexList[v1].yCoordinate - vertexList[v2].yCoordinate)));
adjacencyMatrix[v1][v2] = weight;
adjacencyMatrix[v2][v1] = weight;
}
read.close();
}//end of readGraph function
//This function simply prints the information of graph.
void printGraphInfoOnScreen(Vertex const * const&vertexList, double const* const* const &adjacencyMatrix, int const & totalVertices, int const & totalEdges)
{
cout << endl << "Total Vertices= " << totalVertices << ". Total Edges= " << totalEdges << endl;
cout << endl << "Information About Vertices: " << endl;
for (int i = 0; i < totalVertices; i++)
{
cout << "Vertex: " << i << " " << vertexList[i].xCoordinate << " " << vertexList[i].yCoordinate << endl;
}
cout << endl << endl << "Information About Edges: " << endl;
for (int i = 0; i < totalVertices; i++)
{
for (int j = 0; j < totalVertices; j++)
{
if (i <= j && adjacencyMatrix[i][j] != -1)
cout << "There is an edge between Vertex: " << i << " and Vertex: " << j << "; the weight of this edge is " << adjacencyMatrix[i][j] << "." << endl << endl;
}
}
}//end of printGraphInfoOnScreen function
int main()
{
int totalVertices = 0,
totalEdges = 0;
Vertex *vertexList=NULL;
double **adjacencyMatrix=NULL;
string fileName;
cout << "Please enter the name of the file from which graph is to be read: ";
cin >> fileName;
//reading the graph from the file
readGraph(fileName, vertexList, adjacencyMatrix, totalEdges, totalVertices);
//printing the graph information on the screen.
printGraphInfoOnScreen(vertexList, adjacencyMatrix, totalVertices, totalEdges);
int **parentList = new int*[totalVertices];
double **distanceFromSourceVertex = new double*[totalVertices];
//calculating the distance of every source vertext from every other vertex using Dijkstra
for (int i = 0; i < totalVertices; i++)
{
distanceFromSourceVertex[i] = new double[totalVertices];
parentList[i] = new int[totalVertices];
//vertex i is source vertex in each iteration
Dijkstra( adjacencyMatrix, totalVertices, i, parentList[i], distanceFromSourceVertex[i]);
}
cout << endl;
//In the above section, Dikstra's algorithm has computed the distance of every vertex from every other vertex.
//Now we will simply print the distance and exact path between two vertices given by the user.
int sourceVertex, destinationVertex;
cout << "Please enter the source vertex Number: ";
cin >> sourceVertex;
cout << "Now please enter the destination vertex Number: ";
cin >> destinationVertex;
cout << endl;
cout << "-> The weight of the minimum path between source Vertex: " << sourceVertex << " and destination vertex: " << destinationVertex << " is " << distanceFromSourceVertex[sourceVertex][destinationVertex] <<"."<< endl;
cout << "-> The exact minimum path between source vertex: " << sourceVertex << " and destination vertex: " << destinationVertex << " is: " << endl << endl;
stack<int> path;
path.push(destinationVertex);
int j = destinationVertex;
for (int i = 0; i < totalVertices; i++)
{
if (parentList[sourceVertex][j] == -1)
break;
path.push(parentList[sourceVertex][j]);
j = parentList[sourceVertex][j];
}
//printing path
while (path.size() != 0)
{
cout << path.top();
path.pop();
if (path.size() != 0)
cout << " ---->";
}
cout << endl;
}