-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgramFlowStaticFunction.cpp
271 lines (240 loc) · 7.76 KB
/
ProgramFlowStaticFunction.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#include <iostream>
#include <fstream>
#include "AbstractGraph.h"
#include "ConstTypes.h"
#include "AdjacencyListGraph.h"
#include "AdjacencyMatrixGraph.h"
#include "GraphAlgorithms.h"
#include <chrono>
#include <iomanip>
#include "ProgramFlowStaticFunction.h"
#define LINE_MAX 256
#define COMMAND_ARGS 3
using namespace std;
namespace AlgoGraph
{
bool CheckInputFileValidity(string i_inputFileName, int& amountOfEdges)
{
ifstream inputFile;
string toCheck;
inputFile.open(i_inputFileName);
char line[LINE_MAX];
amountOfEdges = 0;
// We check the first three lines
for (int i = 0; i < 3; i++)
{
inputFile.getline(line, LINE_MAX);
if (line[0] == '0')
return false;
toCheck = line;
if (!(onlyDigitsWithSpaces(toCheck)))
return false;
}
// check the other lines and sum how much Edges there are.
string currentLine;
while (inputFile)
{
inputFile.getline(line, LINE_MAX);
currentLine = line;
if (!onlyWhiteSpaces(currentLine))
{
for (int i = 0; i < 2; i++)
{
string Num = currentLine.substr(0, currentLine.find(' '));
if (!onlyDigits(Num))
PrintWrongInput();
currentLine.erase(0, Num.length()+1);
}
if (currentLine.length() == 0)
PrintWrongInput();
if (!onlyDigitsWithDot(currentLine))
PrintWrongInput();
amountOfEdges++;
}
}
return true;
}
bool onlyWhiteSpaces(string& toCheck)
{
if (toCheck.length() == 0)
return true;
for (unsigned int i = 0; i < toCheck.length(); i++)
{
if (!(toCheck[i] == ' ' || toCheck[i] == '\t' || toCheck[i] == '\n'))
return false;
}
return true;
}
bool onlyDigits(string& toCheck)
{
for (unsigned int i = 0; i < toCheck.length(); i++)
{
if (toCheck[i] < '0' || toCheck[i]>'9')
return false;
}
return true;
}
bool onlyDigitsWithSpaces(string& toCheck)
{
int i = 0;
while (toCheck[i] >= '0' && toCheck[i] <= '9')
{
i++;
}
toCheck.erase(0,i);
return onlyWhiteSpaces(toCheck);
}
bool onlyDigitsWithDot(string& toCheck)
{
if (toCheck.length() < 2)
return onlyDigits(toCheck);
else
{
if (toCheck[0] < '0' || toCheck[0]>'9')
return false;
unsigned int k = 1;
while (k<toCheck.length())
{
if (toCheck[k] < '0' || toCheck[k]>'9')
{
if (toCheck[k] == '.' || toCheck[k] == ' ' || toCheck[k] == '\t')
{
break;
}
else return false;
}
k++;
}
toCheck.erase(0, k+1);
if (toCheck.length() == 0)
return true;
return(onlyDigitsWithSpaces(toCheck));
}
return true;
}
bool firstThreeSelectionAreValid(int numberOfVertex, int pathStartingVertex, int pathEndVertex)
{
if (numberOfVertex < 0)
return false;
if (pathStartingVertex > numberOfVertex || pathStartingVertex < 1)
return false;
if (pathEndVertex > numberOfVertex || pathEndVertex < 1)
return false;
return true;
}
void PrintWrongInput()
{
cout << "invalid input";
exit(1);
}
bool CheckComandArguments(int argc)
{
return argc == COMMAND_ARGS;
}
void GetFileNames(int argc, char* argv[],string& inputFileName,string& outputFileName)
{
if (CheckComandArguments(argc))
inputFileName = argv[1];
else
PrintWrongInput();
inputFileName = argv[1];
outputFileName = argv[2];
}
void GetOriginVariables(int& NumberOfVertices, int& OriginVertex, int& EndVertex, int& FileIndentation, string FileName)
{
ifstream inputFile;
inputFile.open(FileName);
// we know that the file is valid
inputFile >> NumberOfVertices;
inputFile >> OriginVertex;
inputFile >> EndVertex;
if (!firstThreeSelectionAreValid(NumberOfVertices, OriginVertex, EndVertex))
PrintWrongInput();
FileIndentation = (int)inputFile.tellg();
inputFile.close();
}
std::chrono::steady_clock::time_point StartTimer()
{
std::chrono::steady_clock::time_point start = chrono::high_resolution_clock::now();
ios_base::sync_with_stdio(false);
return start;
}
void EndTimer(ofstream& fileName, std::chrono::steady_clock::time_point start,const char* FuncName)
{
auto end = chrono::high_resolution_clock::now();
double time_taken = (double)chrono::duration_cast<chrono::nanoseconds>(end - start).count();
time_taken *= 1e-9;
fileName << FuncName << " <" << fixed << time_taken << setprecision(9);
fileName << "> sec" << endl;
}
void RunBelmanFordMatrix(AdjacencyMatrixGraph& MatrixImplementedGraph, int& OriginVertex, int& EndVertex, ofstream& outputFile)
{
auto start = StartTimer();
float ShortestMatrixPath;
Result PathOfAdjacencyMatrixGraph = GraphAlgorithms::BellmanFord(&MatrixImplementedGraph, OriginVertex, EndVertex, ShortestMatrixPath);
if (PathOfAdjacencyMatrixGraph == Result::SUCCESS)
cout << "Matrix Bellman Ford " << ShortestMatrixPath << endl;
else if ((PathOfAdjacencyMatrixGraph == Result::NEGATIVE_CYCLE))
cout << "Matrix Bellman Ford: There is a Negative Cycle in the graph" << endl;
else
cout << "Matrix Bellman Ford: No route from " << OriginVertex << " to " << EndVertex << endl;
EndTimer(outputFile,start, "Matrix Bellman Ford" );
}
void RunBelmanFordList(AdjancencyListGraph& listImplementedGraph, int& OriginVertex, int& EndVertex, ofstream& outputFile)
{
auto start = StartTimer();
float ShortestListPath;
Result PathOfListGraph = GraphAlgorithms::BellmanFord(&listImplementedGraph, OriginVertex, EndVertex, ShortestListPath);
if (PathOfListGraph == Result::SUCCESS)
cout << "Adjacency Bellman Ford " << ShortestListPath << endl;
else if ((PathOfListGraph == Result::NEGATIVE_CYCLE))
cout << "Adjacency Bellman Ford: There is a Negative Cycle in the graph" << endl;
else
cout << "Adjacency Bellman Ford: No route from " << OriginVertex << " to " << EndVertex << endl;
EndTimer(outputFile, start, "Adjacency Bellman Ford");
}
void RunHeapMatrixDijkstra(AdjacencyMatrixGraph& MatrixImplementedGraph, int& OriginVertex, int& EndVertex, ofstream& outputFile )
{
auto start = StartTimer();
float ShortestMatrixPath;
if (GraphAlgorithms::DijkstraHeap(&MatrixImplementedGraph, OriginVertex, EndVertex, ShortestMatrixPath))
{
cout << "Matrix Dijkstra heap: No Route from " << OriginVertex << " to " << EndVertex << endl;
}
else cout << "Matrix Dijkstra heap " << ShortestMatrixPath << endl;
EndTimer(outputFile, start, "Matrix Dijkstra heap:");
}
void RunHeapListDijkstra(AdjancencyListGraph& ListImplementedGraph, int& OriginVertex, int& EndVertex, ofstream& outputFile)
{
auto start = StartTimer();
float ShortestListPath;
if (GraphAlgorithms::DijkstraHeap(&ListImplementedGraph, OriginVertex, EndVertex, ShortestListPath))
{
cout << "Adjacency Dijkstra heap: No Route from " << OriginVertex << " to " << EndVertex << endl;
}
else cout << "Adjacency Dijkstra heap " << ShortestListPath << endl;
EndTimer(outputFile, start, "Adjacency Dijkstra heap");
}
void RunArrayMatrixDijkstra(AdjacencyMatrixGraph& MatrixImplementedGraph, int& OriginVertex, int& EndVertex, ofstream& outputFile)
{
auto start = StartTimer();
float ShortestMatrixPath;
if (GraphAlgorithms::DijkstraArray(&MatrixImplementedGraph, OriginVertex, EndVertex, ShortestMatrixPath))
{
cout << "Matrix Dijkstra array: No Route from " << OriginVertex << " to " << EndVertex << endl;
}
else cout << "Matrix Dijkstra array " << ShortestMatrixPath << endl;
EndTimer(outputFile, start, "Matrix Dijkstra array");
}
void RunArrayListDijkstra(AdjancencyListGraph& ListImplementedGraph, int& OriginVertex, int& EndVertex, ofstream& outputFile)
{
auto start = StartTimer();
float ShortestListPath;
if (GraphAlgorithms::DijkstraArray(&ListImplementedGraph, OriginVertex, EndVertex, ShortestListPath))
{
cout << "Adjacency Dijkstra array: No Route from " << OriginVertex << " to " << EndVertex << endl;
}
else cout << "Adjacency Dijkstra array " << ShortestListPath << endl;
EndTimer(outputFile, start, "Adjacency Dijkstra array");
}
}