-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreachability.cpp
127 lines (113 loc) · 3.72 KB
/
reachability.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
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include "ArgumentManager.h"
#include "FileManager.h"
#include "csrMatrix.h"
#include "COOArray.h"
#include "Graph.h"
/*
static void writeVectorToFile(std::vector<int> bigS, std::string outputFile) {
std::ofstream bigSOutput;
bigSOutput.open(outputFile);
for(int i = 0; i < bigS.size(); i++) {
bigSOutput << bigS.at(i) << "\n";
}
}*/
static void writeVectorToFile(std::vector<bool> &bigS, std::string outputFile){
std::ofstream bigSOutput;
bigSOutput.open(outputFile);
for(int i = 0; i < bigS.size(); i++) {
if(bigS.at(i))
bigSOutput << i + 1 << "\n";
}
}
static void createHashTable(std::string output, std::string hash[]) {
std::cout << "creating hash table\n";
ifstream fileRead;
fileRead.open(output);
if(fileRead.is_open()) {
while(true) {
std::string line;
std::getline(fileRead, line);
std::stringstream ss;
ss << line;
int index;
ss >> index;
std::string label;
label = ss.str();
std::cout << "setting hash at index: " << index << "\n";
hash[index-1] = label;
if(fileRead.eof()) {
break;
}
}
}
else {
std::cout << "error reading file\n";
}
}
int main(int argc, char * argv[]) {
if(argc != 2) {
std::cout << "Usage: " << argv[0] << "reachability E=<file>;labels=<file>;result=<file>;source=<integer>;dfs=no|recursive|iterative; k=<integer>\n";
}
else {
// Parse command line argument string
ArgumentManager aManager(argc, argv);
// Use the manager to get input matrix E
std::string inputE = aManager.get("E");
FileManager manager(inputE);
COOArray matrixE;
manager.getContentAsMatrix(matrixE);
if(!matrixE.isSorted())
matrixE.sort(0,matrixE.length() - 1);
std::string result = aManager.get("result");
std::string dfs = aManager.get("dfs");
std::string source = aManager.get("source");
if(source == "") {
std::cout << "Invalid source vertex\n";
return 1;
}
std::vector<bool> bigS(matrixE.getRows(), false);
if(dfs == "no" || dfs == ""){
csrMatrix csr_e(matrixE);
//csr_e.print();
bigS.at(std::stoi(source)-1) = true;
csr_e.reachabilityWithMult(bigS);
/*for(int i = 0; i < bigS.size(); i++) {
if(bigS.at(i))
std::cout << i+1 << "\n";
}*/
}
else if(dfs == "recursive") {
//delete matrixE;
Graph graph_e(matrixE, std::stoi(source));
//graph_e.print();
bigS = graph_e.dfs(bigS, std::stoi(source));
/*for(int i = 0; i < bigS.size(); i++) {
if(bigS.at(i))
std::cout << i+1 << "\n";
}*/
}
else {
// Phase 2
Graph graph_e(matrixE,0);
bigS = graph_e.dfs_iterative(bigS,std::stoi(source));
}
if(aManager.get("labels") != "") {
std::string hashTable [10000];
createHashTable(aManager.get("labels"), hashTable);
std::cout << "created hashtable\n";
std::ofstream bigSOutput;
bigSOutput.open(result);
for(int i = 0; i < bigS.size(); i++) {
if(bigS.at(i))
bigSOutput << hashTable[i] << "\n";
}
}
else {
writeVectorToFile( bigS, result);
}
}
}