-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGraph.cpp
87 lines (62 loc) · 1.57 KB
/
Graph.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
#include "Graph.hpp"
Graph::Graph(){
}
Graph::Graph(string name){
loadGraph(name);
}
void Graph::loadGraph(string filePath){
ifstream file(filePath.c_str());
if(!file.is_open()){
cerr << "FILE ERROR" << endl;
return;
}
file.ignore(256,' ');
file.ignore(256,' ');
file >> nVertex;
file >> nEdge;
// cout << nVertex << " " << nEdge << endl;
// initializing vectors
nAdjacencies.assign(nVertex, 0);
adjacency.resize(nVertex);
neighbors.resize(nVertex);
for(int i=0 ; i<nVertex ; i++){
adjacency[i].assign(nVertex, 0);
}
// reading edges from file
int src, target;
char ignore;
for(int i=0 ; i<nEdge ; i++){
file >> ignore; // ignore "p" from line
file >> src; file >> target;
adjacency[src-1][target-1] = 1;
adjacency[target-1][src-1] = 1;
nAdjacencies[src-1]++;
nAdjacencies[target-1]++;
neighbors[src-1].push_back(target-1);
neighbors[target-1].push_back(src-1);
}
for(int i=0 ; i<nVertex ; i++){
sort(neighbors[i].begin(), neighbors[i].end());
}
file.close();
}
void Graph::generateComplement(){
}
void Graph::revertComplement(){
generateComplement();
}
int Graph::getNVertex(){
return nVertex;
}
int Graph::getNEdge(){
return nEdge;
}
int Graph::getNAdjacencyOf(int id){
return nAdjacencies[id];
}
vector<int>::iterator Graph::getNeighborsOf(int id){
return neighbors[id].begin();
}
vector<int>::iterator Graph::getNeighborsReverseOf(int id){
return neighbors[id].end();
}