-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbfs_dfs.cpp
More file actions
92 lines (81 loc) · 2.09 KB
/
bfs_dfs.cpp
File metadata and controls
92 lines (81 loc) · 2.09 KB
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
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <numeric>
#include <cstdlib>
#include <queue>
#include <stack>
using namespace std;
typedef long long ll;
#define pb push_back
#define FOR(i, n) for (int i=0;i<n;++i)
#define FOR2(i, s, n) for (int i=s;i<n;++i)
#define INF (1e9)
/* Adjacent List */
//template <typename T, typename U>
//using edge = pair<T, U>;
typedef pair<int,int> edge;
#define to first
#define cost second
/* Breadth First Search */
void bfs(vector<edge> G[], int s, char visited[])
{
queue<int> q;
visited[s] = 1;
q.push(s);
while (!q.empty()) {
int u = q.front(); q.pop();
for(auto v: G[u]) if (!visited[v.to]) {
cout << "visitting:" << v.to << endl;
visited[v.to] = 1;
q.push(v.to);
}
}
}
/* Depth First Search */
// reccurent
void dfs(vector<edge> G[], int u, char visited[])
{
visited[u] = 1;
cout << "visitting:" << u << endl;
for(auto v: G[u]) if (!visited[v.to]) dfs(G, v.to, visited);
}
void dfs2(vector<edge> G[], int s, char visited[])
{
stack<int> st;
visited[s] = 1;
st.push(s);
while (!st.empty()) {
int u = st.top(); st.pop();
for(auto v: G[u]) if (!visited[v.to]) {
cout << "visitting:" << v.to << endl;
visited[v.to] = 1;
st.push(v.to);
}
}
}
int main()
{
int V, E; cin>>V>>E;
vector<edge> G[V];
char visited[V];
FOR(i,E) {
int u,v,c; cin>>u>>v>>c; // edge (u,v) with cost c
// Undirected Graph
G[u].pb(edge(v, c));
G[v].pb(edge(u, c));
// Directed Graph
//G[u].pb(Edge<ll,ll>(v,c));
}
FOR(i,V) for(auto p:G[i]) cout << "(" << i << "," << p.to << ")" << "@" << p.cost << endl;
memset(visited, 0, sizeof(visited));
cout << "BFS" << endl;
bfs(G, 0, visited);
memset(visited, 0, sizeof(visited));
cout << "DFS" << endl;
dfs(G, 0, visited);
memset(visited, 0, sizeof(visited));
cout << "DFS2" << endl;
dfs2(G, 0, visited);
}