-
Notifications
You must be signed in to change notification settings - Fork 0
/
DFS.cc
156 lines (152 loc) · 2.99 KB
/
DFS.cc
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include <vector>
#include <queue>
using namespace std;
struct Edge{
int v;
int u;
double weight;
Edge(int v, int u,double weight){
this->v = v;
this->u = u;
this->weight = weight;
}
};
struct Graph{
int v;
vector<vector<Edge*> > edges;
Graph(int v){
this->v = v;
for(int i = 0; i<v; i++){
vector<Edge*> a;
this->edges.push_back(a);
}
}
void addEdge(int v, int w, double weight){
this->edges[v].push_back(new Edge(v,w,weight));
}
};
struct DFS{
vector<int> preorder;
vector<int> postorder;
vector<bool> visted;
Graph* g;
vector<int> connectComponents;
DFS(Graph* g){
for(int i = 0; i< g->v; i++){
visted.push_back(false);
connectComponents.push_back(-1);
}
this->g = g;
}
void dfs(){
for(int i = 0; i<this->g->v; i++){
if(!visted[i]){
search(i,i);
}
}
}
void search(int v, int id){
visted[v] = true;
preorder.push_back(v);
connectComponents[v] = id;
for(int i = 0; i<g->edges[v].size(); i++){
Edge* e = g->edges[v][i];
if(visted[e->u] == false){
search(e->u,id);
}
}
postorder.push_back(v);
}
int getPostorder(int v){
for(int i = 0; i<this->postorder.size(); i++){
if(this->postorder[i] == v){
return i;
}
}
return -1;
}
int getPreorder(int v){
for(int i = 0; i<this->preorder.size();i++){
if(this->preorder[i] == v){
return i;
}
}
return -1;
}
bool detectCycle(){
for(int i = 0; i < this->g->v; i++){
vector<Edge*> edges = this->g->edges[i];
for(Edge* e: edges){
int v = e->v;
int u = e->u;
if(getPostorder(v) < getPostorder(u)){
return true;
}
}
}
return false;
}
vector<int> topologicalSort(){
return postorder;
}
};
struct BFS{
Graph* g;
vector<bool> marked;
vector<Edge*> edgeTo;
int s;
BFS(Graph* g, int s){
this->g = g;
for(int i = 0; i<g->v; i++){
marked.push_back(false);
edgeTo.push_back(new Edge(i,i,0.0));
}
this->s = s;
}
void bfs(){
queue<int> queue;
queue.push(this->s);
marked[s] = true;
while(!queue.empty()){
int v = queue.front();
queue.pop();
vector<Edge*> edges = g->edges[v];
for(int i = 0; i<edges.size();i++){
int w = edges[i]->u;
if(!marked[w]){
marked[w] = true;
edgeTo[w] = edges[i];
queue.push(w);
}
}
}
}
vector<Edge*> shortestPath(int w){
vector<Edge*> shortest;
if(marked[w]){
while(w!=s){
shortest.push_back(edgeTo[w]);
w = edgeTo[w]->v;
}
return shortest;
}
else{
return shortest;
}
}
};
int main(){
Graph* g = new Graph(7);
g->addEdge(0,1,0);
g->addEdge(0,2,0);
g->addEdge(1,3,0);
g->addEdge(1,4,0);
g->addEdge(2,5,0);
DFS* d = new DFS(g);
BFS* b = new BFS(g,0);
d->dfs();
b->bfs();
for(Edge* e: b->shortestPath(5)){
printf("%d\n",e->v);
}
}