-
Notifications
You must be signed in to change notification settings - Fork 0
/
Graph.cpp
41 lines (37 loc) · 856 Bytes
/
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
#include "Graph.h"
//------------------------------------------------------------------------------
Graph::Graph(int V){
this->V = V;
this->adj = new std::vector<int>[V];
for (int i = 0; i < this->V; i++) {
this->white.insert(i);
}
}
Graph::~Graph(){
delete [] adj;
}
void Graph::addEdge(int v,int w){
adj[v].push_back(w);
}
bool Graph::isCyclic(int v) {
white.erase(v);
grey.insert(v);
int size = (int) adj[v].size();
for (int i = 0; i < size; ++i) {
if (white.find(adj[v][i]) != white.end()) {
isCyclic(adj[v][i]);
}
if (grey.find(adj[v][i]) != grey.end()) {
return true;
}
}
black.insert(v);
grey.erase(v);
return false;
}
bool Graph::unusedInstruction(){
if (white.empty()){
return false;
}
return true;
}