-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlgorithm.h
More file actions
152 lines (134 loc) · 4.41 KB
/
Copy pathAlgorithm.h
File metadata and controls
152 lines (134 loc) · 4.41 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
#ifndef MENUMAKER_ALGORITHM_H
#define MENUMAKER_ALGORITHM_H
#include <iostream>
#include <fstream>
#include <sstream>
#include <chrono>
#include <cstdlib>
#include <ctime>
#include "Graph.h"
using namespace std::chrono;
typedef high_resolution_clock Clock;
class Algorithm {
private:
Graph graph;
map<int, string> IPs;
public:
// reads in the connections and builds the graph as an adjacency list
void buildAdjList(ifstream& connections, Graph& graph, map<int, string>& IPs);
void runKruskal();
void runPrim();
int start;
int end;
string startIP;
string endIP;
int sum;
int time;
};
void Algorithm::runPrim()
{
std::ifstream connections("smallConnections.txt");
buildAdjList(connections, graph, IPs);
graph.buildEdgeList();
auto t1 = Clock::now();
vector<double> primResult = graph.createPrim();
auto t2 = Clock::now();
start = primResult[0];
end = primResult[1];
startIP = IPs[primResult[0]];
endIP = IPs[primResult[1]];
sum = primResult[2];
time = duration_cast<milliseconds>(t2 - t1).count();
}
void Algorithm::runKruskal()
{
std::ifstream connections("smallConnections.txt");
buildAdjList(connections, graph, IPs);
graph.buildEdgeList();
auto t1 = Clock::now();
vector<double> kruskalResult = graph.createKruskal();
auto t2 = Clock::now();
start = kruskalResult[0];
end = kruskalResult[1];
sum = kruskalResult[2];
startIP = IPs[kruskalResult[0]];
endIP = IPs[kruskalResult[1]];
time = duration_cast<milliseconds>(t2 - t1).count();
}
void Algorithm::buildAdjList(ifstream& connections, Graph& graph, map<int, string>& IPs){
// stores the from vertex as a string
string tempFrom;
// stores the from vertex as an int
int from;
// stores the to vertex as a string
string tempTo;
// stores the to vertex as an int
int to;
// stores the edge weight as a string
string tempWeight;
// stores the edge weight as an int
int weight;
// stores the IP address
string IP;
// stores the current index
int index = 1;
// stores a line from the connections file
string fileLine;
while(getline(connections, fileLine)) {
istringstream infoStream(fileLine);
// gets the from vertex
getline(infoStream, tempFrom, '|');
// converts the from vertex to an int
from = stoi(tempFrom);
// gets the IP address
getline(infoStream, IP, '|');
// adds the IP address to the map of them
IPs[index] = IP;
// checks if this is a high capacity router
if(index <= 500){
for(int i = 1; i <= 16; i++){
// gets the to vertex
getline(infoStream, tempTo, ',');
// converts the to vertex to an int
to = stoi(tempTo);
// gets the edge weight
getline(infoStream, tempWeight, '|');
// converts the edge weight to an int
weight = stoi(tempWeight);
// adds the connection to the graph
graph.addEdge(from, to, weight);
}
// checks if this is a medium capacity router
} else if((index >= 501) && (index <= 5000)){
for(int i = 1; i <= 8; i++){
// gets the to vertex
getline(infoStream, tempTo, ',');
// converts the to vertex to an int
to = stoi(tempTo);
// gets the edge weight
getline(infoStream, tempWeight, '|');
// converts the edge weight to an int
weight = stoi(tempWeight);
// adds the connection to the graph
graph.addEdge(from, to, weight);
}
// the router must be low capacity
} else{
for(int i = 1; i <= 4; i++){
// gets the to vertex
getline(infoStream, tempTo, ',');
// converts the to vertex to an int
to = stoi(tempTo);
// gets the edge weight
getline(infoStream, tempWeight, '|');
// converts the edge weight to an int
weight = stoi(tempWeight);
// adds the connection to the graph
graph.addEdge(from, to, weight);
}
}
index++;
}
return;
}
#endif //MENUMAKER_ALGORITHM_H