-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.cpp
151 lines (126 loc) · 4.15 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
#include <iostream>
#include <string>
#include <map>
#include <ctime>
#include <fstream>
#include "graph.h"
#include "dijkstra.h"
#include "bellmanford.h"
#include "floydwarshall.h"
using namespace std;
void printResult(const string& filename,
const string& header,
int source,
const vector<int>& result,
clock_t time);
int main(int argc, char *argv[])
{
try
{
if (argc < 2)
{
cout << "No filename entered\n";
return EXIT_FAILURE;
}
string inFilename = argv[1];
string outFilename = (argc >= 3) ? argv[2] : "out.txt";
int source = (argc >= 4) ? stoul(argv[3]) : 0;
Graph graph(inFilename);
if (!graph.isValid())
return EXIT_FAILURE;
ofstream outs;
outs.open(outFilename);
outs << "Demo\n";
outs << "Compare shortest path algorithms\n";
outs.close();
graph.printToFile(outFilename);
if (!graph.isSymmetric())
{
outs.open(outFilename, ios_base::app);
outs << "Graph is not symmetric\n";
outs << "Only summetric graphs supported\n";
outs << '\n';
outs.close();
}
if (graph.size() > 10)
{
cout << "Demo restricted to graphs with less than ten nodes\n";
return EXIT_FAILURE;
}
if (!graph.isNegativeEdgeWeight())
{
clock_t dijkstraTime = clock();
Dijkstra dijkstra(graph);
vector <int> dijkstraResults = dijkstra.runAlgorithm(source);
dijkstraTime = clock() - dijkstraTime;
printResult(outFilename,
"Dijkstra shortest path algorithm",
source,
dijkstraResults,
dijkstraTime);
}
else
{
outs.open(outFilename, ios_base::app);
outs << "Dijkstra shortest path algorithm\n";
outs << "Negative edge weights found\n";
outs << "Dijkstra algorithm does not support negative edge weightings\n";
outs << '\n';
outs.close();
}
clock_t bellmanFordTime = clock();
BellmanFord bellmanFord(graph);
vector <int> bellmanFordResults = bellmanFord.runAlgorithm(source);
bellmanFordTime = clock() - bellmanFordTime;
if (!bellmanFord.isNegativeWeigthCycle())
{
printResult(outFilename,
"Bellman-Ford shortest path algorithm",
source,
bellmanFordResults,
bellmanFordTime);
}
else
{
outs.open(outFilename, ios_base::app);
outs << "Bellman-Ford path algorithm\n";
outs << "Negative weight cycle found, no solution to problem\n";
outs << '\n';
outs.close();
}
clock_t floydWarshallTime = clock();
FloydWarshall floydWarshall(graph);
vector <int> floydWarshallResults = floydWarshall.runAlgorithm(source);
floydWarshallTime = clock() - floydWarshallTime;
printResult(outFilename,
"Floyd-Warshall shortest path algorithm",
source,
floydWarshallResults,
floydWarshallTime);
return EXIT_SUCCESS;
}
catch (exception e)
{
cout << e.what();
return EXIT_FAILURE;
}
}
void printResult(const string& filename,
const string& header,
int source,
const vector<int>& result,
clock_t time)
{
ofstream outs(filename, ios_base::app);
outs << header << '\n';
outs << "Demo\n";
outs << "The source node position: " << source << '\n';
outs << "Distances" << '\n';
for ( int i = 0 ; i < result.size() ; i++ )
{
outs << "Node " << i << "\tdistance: " << result[i] << '\n';
}
//outs << '\n' << "Time taken: " << time / CLOCKS_PER_SEC << " seconds\n";
outs << '\n' << "Time taken: " << time << " ms\n";
outs << '\n';
}