-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNetwork.cpp
116 lines (99 loc) · 2.32 KB
/
Network.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
#include "Network.h"
#include <unordered_set>
#include <string>
#include <iostream>
#include <fstream>
#include <sstream>
#include <assert.h>
using namespace std;
string fitnessNameToStr(fitnessName x){
switch(x){
case ICSFit:
return "ICS";
case ECFit:
return "EC";
case BitscoreSumFit:
return "BitscoreSum";
case EvalsSumFit:
return "EvalsSum";
case SizeFit:
return "Size";
case GOCFit:
return "GOC";
case S3Fit:
return "S3";
case S3DenomFit:
return "S3Denom";
case ICSTimesEC:
return "ICS*EC";
case S3Variant:
return "S3Variant";
default:
return "invalid fitness name";
}
}
Network::Network(string filename){
unsigned int count = 0;
unordered_set<string> alreadySeen;
adjList = vector<vector<node> >();
ifstream infile(filename);
if(!infile){
throw LineReadException(string("Given network file ")+filename+
string(" failed to open!"));
}
string line;
while(getline(infile,line)){
istringstream iss(line);
string a, b;
node u, v;
if (!(iss >> a >> b)){
throw LineReadException(string("Parse error in network: ") + filename +
string("on line: ") + line + "\n");
}
if(!alreadySeen.count(a)){
alreadySeen.insert(a);
nodeToNodeName[count] = a;
nodeNameToNode[a] = count;
auto toPushBack = vector<node>();
adjList.push_back(toPushBack);
u = count;
count++;
}
else{
u = nodeNameToNode.at(a);
}
if(!alreadySeen.count(b)){
alreadySeen.insert(b);
nodeToNodeName[count] = b;
nodeNameToNode[b] = count;
auto toPushBack = vector<node>();
adjList.push_back(toPushBack);
v = count;
count++;
}
else{
v = nodeNameToNode.at(b);
}
Edge e = Edge(u,v);
edges.insert(e);
adjList.at(u).push_back(v);
if(u != v){
adjList.at(v).push_back(u);
}
}
//cout<<"ignored "<<ignoredSelfLoops<<" self-loops"<<endl;
//set up adjacency matrix
for(int i = 0; i < nodeToNodeName.size(); i++){
adjMatrix.push_back(vector<bool>(nodeToNodeName.size(),false));
}
assert(nodeNameToNode.size() == adjList.size());
for(int i = 0; i < adjList.size(); i++){
for(int j = 0; j < adjList.at(i).size(); j++){
adjMatrix[i][adjList.at(i).at(j)] = true;
}
}
assert(nodeNameToNode.size() == nodeToNodeName.size());
}
int Network::degree(node x) const{
return adjList.at(x).size();
}