-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput.cpp
49 lines (43 loc) · 1.21 KB
/
input.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
#include <bits/stdc++.h>
#include "input.h"
using namespace std;
void readInput(Graph& graph, string inputFile) {
/* Function to read the input from the file and map strings to integers so reduce complexity */
ifstream file;
file.open(inputFile);
string vertex1, vertex2;
vector<pair<string, string>> edgeSet;
// Make Edge Set.
while(file >> vertex1 >> vertex2) {
if(vertex1.compare("F")==0){
graph.undeletableVertices.insert(stoi(vertex2));
} else {
graph.addEdge(vertex1, vertex2);
}
}
file.close();
// Assign a number to each unique vertex. Map string to int.
set<string> done;
int vertexNumber = 1;
for(auto i : graph.edgeList) {
if(done.find(i.first) == done.end()) {
done.insert(i.first);
graph.strToInt[i.first] = vertexNumber;
graph.intToStr[vertexNumber] = i.first;
vertexNumber ++;
}
if(done.find(i.second) == done.end()) {
done.insert(i.second);
graph.strToInt[i.second] = vertexNumber;
graph.intToStr[vertexNumber] = i.second;
vertexNumber ++;
}
}
// Create Graph as adjancency list representation.
for(auto i : graph.edgeList) {
int u = graph.strToInt[i.first];
int v = graph.strToInt[i.second];
graph.adjList[u].insert(v);
graph.adjList[v].insert(u);
}
}