-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
aaa57b1
commit 53b68d5
Showing
9 changed files
with
117 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#include <iostream> | ||
#include <queue> | ||
#include <vector> | ||
|
||
using namespace std; | ||
|
||
int main(int argc, char const *argv[]) | ||
{ | ||
#ifndef ONLINE_JUDGE | ||
freopen("in.txt", "r", stdin); | ||
#endif | ||
queue<int> q; | ||
int i, n, e, x, y, cv, f; | ||
cv = 1; | ||
cout << "Enter no of nodes" << endl; | ||
cin >> n; | ||
|
||
cout << "Enter no of edges" << endl; | ||
cin >> e; | ||
|
||
int visit[n + 1]; | ||
|
||
for (i = 1; i <= n; ++i) { | ||
visit[i] = 0; | ||
} | ||
|
||
vector<int> v[n + 1]; | ||
//created an array of vectors to store the adjacency list n is equal to no of nodes | ||
//each row of this adjacency list shows the conencted edges of that particular vertex | ||
|
||
//creating the adjacency list | ||
for (i = 0; i < e; i++) { | ||
cin >> x >> y; | ||
v[x].push_back(y); | ||
v[y].push_back(x); | ||
} | ||
|
||
q.push(cv); | ||
visit[cv] = 1; | ||
|
||
cout << "BFS of the given graph \n"; | ||
while(!q.empty()) { | ||
f = q.front(); | ||
q.pop(); | ||
cout << f << " "; | ||
|
||
//until all connected vertices are in the queue | ||
for(i = 0; i < v[f].size(); i++) { | ||
//if not previously visited | ||
if (visit[v[f][i]] == 0) { | ||
//push in queue and and the vertex to visited list | ||
q.push(v[f][i]); | ||
visit[v[f][i]] = 1; | ||
} | ||
} | ||
} | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#include <iostream> | ||
#include <stack> | ||
#include <vector> | ||
|
||
using namespace std; | ||
|
||
int main(int argc, char const *argv[]) | ||
{ | ||
#ifndef ONLINE_JUDGE | ||
freopen("in.txt", "r", stdin); | ||
#endif | ||
stack<int> s; | ||
int i, n, e, x, y, cv, f; | ||
cv = 1; | ||
cout << "Enter no of nodes" << endl; | ||
cin >> n; | ||
|
||
cout << "Enter no of edges" << endl; | ||
cin >> e; | ||
|
||
vector<int> visit; | ||
|
||
vector<int> v[n + 1]; | ||
//created an array of vectors to store the adjacency list n is equal to no of nodes | ||
//each row of this adjacency list shows the conencted edges of that particular vertex | ||
|
||
//creating the adjacency list | ||
for (i = 0; i < e; i++) { | ||
cin >> x >> y; | ||
v[x].push_back(y); | ||
v[y].push_back(x); | ||
} | ||
|
||
s.push(cv); | ||
cout << "BFS of the given graph \n"; | ||
while(s.size() != n) { | ||
f = (int)s.pop(); | ||
x = v[f][0]; | ||
} | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
8 7 | ||
3 5 | ||
1 4 | ||
1 3 | ||
2 5 | ||
3 6 | ||
4 7 | ||
4 8 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters