Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit cdbf6b0

Browse files
Merge pull request #114 from tom635/master
Added BFS ,DFS traversal and Kahn's Algorithum ("add code")
2 parents 411bc08 + 52d11e1 commit cdbf6b0

File tree

4 files changed

+208
-0
lines changed

4 files changed

+208
-0
lines changed

‎25-Graph Theory/BFS.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
void BFS(vector<int> adj[], int V, int s)
5+
{
6+
bool visited[V];
7+
for(int i = 0; i < V; i++)
8+
visited[i] = false;
9+
10+
queue<int> q;
11+
12+
visited[s] = true;
13+
q.push(s);
14+
15+
while(q.empty()==false)
16+
{
17+
int u = q.front();
18+
q.pop();
19+
cout << u << " ";
20+
21+
for(int v:adj[u]){
22+
if(visited[v]==false){
23+
visited[v]=true;
24+
q.push(v);
25+
}
26+
}
27+
}
28+
}
29+
30+
void addEdge(vector<int> adj[], int u, int v){
31+
adj[u].push_back(v);
32+
adj[v].push_back(u);
33+
}
34+
35+
int main()
36+
{
37+
int V=5;
38+
vector<int> adj[V];
39+
addEdge(adj,0,1);
40+
addEdge(adj,0,2);
41+
addEdge(adj,1,2);
42+
addEdge(adj,2,3);
43+
addEdge(adj,1,3);
44+
addEdge(adj,3,4);
45+
addEdge(adj,2,4);
46+
47+
cout << "Breadth First Traversal: "<< endl;
48+
BFS(adj,V,0);
49+
50+
return 0;
51+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
class Solution {
5+
public:
6+
bool isCyclic(int N, vector<int> adj[]) {
7+
queue<int> q;
8+
vector<int> indegree(N, 0);
9+
for(int i = 0;i<N;i++) {
10+
for(auto it: adj[i]) {
11+
indegree[it]++;
12+
}
13+
}
14+
15+
for(int i = 0;i<N;i++) {
16+
if(indegree[i] == 0) {
17+
q.push(i);
18+
}
19+
}
20+
int cnt = 0;
21+
while(!q.empty()) {
22+
int node = q.front();
23+
q.pop();
24+
cnt++;
25+
for(auto it : adj[node]) {
26+
indegree[it]--;
27+
if(indegree[it] == 0) {
28+
q.push(it);
29+
}
30+
}
31+
}
32+
if(cnt == N) return false;
33+
return true;
34+
}
35+
};
36+
37+
38+
39+
int main()
40+
{
41+
42+
int t;
43+
cin >> t;
44+
while(t--)
45+
{
46+
int V, E;
47+
cin >> V >> E;
48+
49+
vector<int> adj[V];
50+
51+
for(int i = 0; i < E; i++)
52+
{
53+
int u, v;
54+
cin >> u >> v;
55+
adj[u].push_back(v);
56+
}
57+
58+
Solution obj;
59+
cout << obj.isCyclic(V, adj) << "\n";
60+
}
61+
62+
return 0;
63+
}

‎25-Graph Theory/DFS.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
void DFSRec(vector<int> adj[], int s, bool visited[])
5+
{
6+
visited[s]=true;
7+
cout<< s <<" ";
8+
9+
for(int u:adj[s]){
10+
if(visited[u]==false)
11+
DFSRec(adj,u,visited);
12+
}
13+
}
14+
15+
void DFS(vector<int> adj[], int V, int s){
16+
bool visited[V];
17+
for(int i = 0;i<V; i++)
18+
visited[i] = false;
19+
20+
DFSRec(adj,s,visited);
21+
}
22+
23+
void addEdge(vector<int> adj[], int u, int v){
24+
adj[u].push_back(v);
25+
adj[v].push_back(u);
26+
}
27+
28+
int main()
29+
{
30+
int V=5;
31+
vector<int> adj[V];
32+
addEdge(adj,0,1);
33+
addEdge(adj,0,2);
34+
addEdge(adj,2,3);
35+
addEdge(adj,1,3);
36+
addEdge(adj,1,4);
37+
addEdge(adj,3,4);
38+
39+
cout << "Following is Depth First Traversal: "<< endl;
40+
DFS(adj,V,0);
41+
42+
return 0;
43+
}

‎BFS.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
void BFS(vector<int> adj[], int V, int s)
5+
{
6+
bool visited[V];
7+
for(int i = 0; i < V; i++)
8+
visited[i] = false;
9+
10+
queue<int> q;
11+
12+
visited[s] = true;
13+
q.push(s);
14+
15+
while(q.empty()==false)
16+
{
17+
int u = q.front();
18+
q.pop();
19+
cout << u << " ";
20+
21+
for(int v:adj[u]){
22+
if(visited[v]==false){
23+
visited[v]=true;
24+
q.push(v);
25+
}
26+
}
27+
}
28+
}
29+
30+
void addEdge(vector<int> adj[], int u, int v){
31+
adj[u].push_back(v);
32+
adj[v].push_back(u);
33+
}
34+
35+
int main()
36+
{
37+
int V=5;
38+
vector<int> adj[V];
39+
addEdge(adj,0,1);
40+
addEdge(adj,0,2);
41+
addEdge(adj,1,2);
42+
addEdge(adj,2,3);
43+
addEdge(adj,1,3);
44+
addEdge(adj,3,4);
45+
addEdge(adj,2,4);
46+
47+
cout << "Breadth First Traversal: "<< endl;
48+
BFS(adj,V,0);
49+
50+
return 0;
51+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /