-
Notifications
You must be signed in to change notification settings - Fork 0
/
Algorithm.h
178 lines (134 loc) · 4.98 KB
/
Algorithm.h
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
//
// Created by luise on 5/5/2020.
//
#ifndef ALGORITHMS_ALGORITHM_H
#define ALGORITHMS_ALGORITHM_H
#include <map>
#include <string>
#include "Graph.h"
#include <fstream>
#include <cstring>
#include "Vertex.h"
#include "FileReader.h"
#include "BellmanFord.h"
#include <bits/stdc++.h>
#include <unordered_map>
#include "FloydWarshall.h"
using std::ios;
using std::map;
using std::cout;
using std::endl;
using std::string;
using std::fstream;
using std::to_string;
using std::unordered_map;
using map_Vertex = unordered_map<string,int>;
using map_Edges = pair<map_Vertex,map_Vertex>;
template<class U>
class Algorithm {
private:
fstream fin; // Reads in from inputFile;
Graph<Vertex> networks;
// network_file holds the file path of the network data file
U network_file;
// trivial_file holds the file path of the Trivial output file
U trivial_file;
// bellman_ford_file holds the file path of the Bellman-Ford output file
U bellman_ford_file;
// floyd_warshall_file holds the file path of the Floyd-Warshall output file
U floyd_warshall_file;
public:
// Default Constructor
Algorithm();
void readNetworkFile(U);
// Used as constructor
void setFileName(string);
//explicit Algorithm(NetworkFile<U>&);
void compute();
// Set functions to virtual so they use polymorphism with the NetworkFile
virtual U get_Network_file() = 0;
virtual U get_Trivial_file() = 0;
virtual U get_Bellman_Ford_file() = 0;
virtual U get_Floyd_Warshall_file() = 0;
// Set Destructor to deallocate memory from perhaps the NetworkFile
virtual ~Algorithm();
};
// Default constructor
template<class U>
Algorithm<U>::Algorithm()= default;
// Used to initialize the Algorithm process
template<class U>
void Algorithm<U>::compute(){
network_file = get_Network_file();
trivial_file = get_Trivial_file();
bellman_ford_file = get_Bellman_Ford_file();
floyd_warshall_file = get_Floyd_Warshall_file();
readNetworkFile(network_file);
}
template<class U>
void Algorithm<U>::readNetworkFile(U networkFile) {
//Graph<Vertex> graph;
map_Vertex vertexOrder;
vector<string> listOfVertex;
string vertexNode;
char startBracket, endBracket;
int totalNumberOfVertex, totalNumberOfEdges, vertexNode_A, vertexNode_B, weight, edgeCounter = 0;
setFileName(networkFile);
fin >> startBracket >> totalNumberOfVertex >> endBracket;
fin >> startBracket >> totalNumberOfEdges >> endBracket;
// Create edges starting from 1 and add them to listOfVertex
for (int vertexMap = 1; vertexMap <= totalNumberOfVertex; vertexMap++) {
vertexNode = to_string(vertexMap);
listOfVertex.push_back(vertexNode);
}
for (int i = 1; i <= totalNumberOfEdges; i++) {
fin >> vertexNode_A >> vertexNode_B >> weight;
Vertex newVertexNode(to_string(vertexNode_A), to_string(vertexNode_B), weight, false);
networks.add(newVertexNode);
Vertex newReversedVertexNode(to_string(vertexNode_B), to_string(vertexNode_A), weight, false);
networks.add(newReversedVertexNode);
}
//networks.printMatrix();
fin.close();
int tempStart = 1;
auto * bellman_ford = new BellmanFord<string>(get_Bellman_Ford_file());
bellman_ford->findShortestPath(networks, tempStart, totalNumberOfVertex + 1);
delete bellman_ford;
auto * floyd_warshall = new FloydWarshall<string>(get_Floyd_Warshall_file());
floyd_warshall->findShortestPath(networks, totalNumberOfVertex + 1);
delete floyd_warshall;
}
// setFileName sets the given file name in the inputFile, as well as checking if it is the
// correct format type.
template<class U>
void Algorithm<U>::setFileName(string networkFile){
int outputFilename_size = networkFile.size();
// Create a backupnetworkFile (simple original filename with .txt added to the end)
string backupnetworkFile = networkFile + ".txt";
// Save the last 4 characters of the string to verify it is a .txt file
string file_extension;
for(int i = 4; i > 0; i--){
file_extension += networkFile[outputFilename_size - i];
}
// if given file does not have .txt file extension, set networkFile equal to the backupnetworkFile
if(file_extension != ".txt"){
fin.open(backupnetworkFile.c_str());
if(!fin.is_open()){
cout << "'" << networkFile << "' or '" << backupnetworkFile << "' could not be opened. Please check command line." << endl;
exit(-1);
}
networkFile = backupnetworkFile;
}
else{
fin.open(networkFile.c_str());
if(!fin.is_open()){
cout << "'" << networkFile << "' could not be opened. Please check command line." << endl;
exit(-1);
}
}
}
// Default destructor
template<class U>
Algorithm<U>::~Algorithm()= default;
//cout << "Deallocating Algorithm<U>" << endl;
#endif //ALGORITHMS_ALGORITHM_H