-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraph.cpp
91 lines (70 loc) · 2.27 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
88
89
90
91
#include "Graph.h"
#include "Session.h"
Graph::Graph(std::vector<std::vector<int>> matrix) : edges(matrix), infected(matrix.size(), false) {}
Graph::Graph() : edges(), infected() {}
Graph::Graph(const Graph &other) : edges(other.edges), infected(other.infected) {} // copy constructor
Graph &Graph::operator=(const Graph &other) { // copy assignment
if (this == &other) {return *this;}
edges = other.edges;
infected = other.infected;
return *this;
}
void Graph::infectNode(int nodeInd) {
infected[nodeInd] = true;
}
bool Graph::isInfected(int nodeInd) {
return infected[nodeInd];
}
std::vector<int> Graph::getNeighbors(int nodeId) const {
std::vector<int> neighbors {};
for(size_t i = 0; i < edges[nodeId].size(); ++i){
if (edges[nodeId][i]==1){
neighbors.push_back(i);
}
}
return neighbors;
}
int Graph::getSize() const{return edges.size();}
void Graph::removeEdge(int node1, int node2) {
int matrixSize = getSize();
if (node1 < matrixSize && node2 < matrixSize){
edges[node1][node2] = 0;
edges[node2][node1] = 0;
}
}
bool Graph::condition(const Session &session) {
int size = this->getSize();
bool check {true};
std::vector<char> color (size,'w');
for (int i {0};i<size && check;++i){
if (color[i]=='w'){
bool sick = isInfected(i);
check = dfsVisit(i,sick,color, session);
}
}
return check;
}
bool Graph::dfsVisit(int i, bool sick, std::vector<char> &color, const Session &session) { //Changed implementation
color[i] = 'g';
bool bContinueDfs;
if (isInfected(i) != sick){
bContinueDfs = false;
}
else if (session.isCarrier(i) && !isInfected(i)){
bContinueDfs = false;
} else {
bContinueDfs = true;
std::vector<int> neighbors(this->getNeighbors(i));
for (auto node : neighbors){
if (color[node]=='w'){
bContinueDfs = dfsVisit(node, sick, color, session);
if (!bContinueDfs) break;
}
}
}
color[i]='b';
return bContinueDfs;
}
const std::vector<std::vector<int>> &Graph::getEdges() const {
return edges;
}