-
Notifications
You must be signed in to change notification settings - Fork 7.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #517 from kartikeya47/kartikeya47
Added BFS Code in CPP
- Loading branch information
Showing
1 changed file
with
80 additions
and
0 deletions.
There are no files selected for viewing
80 changes: 80 additions & 0 deletions
80
Program's_Contributed_By_Contributors/C++_Programs/bfs_graph.cpp
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,80 @@ | ||
#include <iostream> | ||
using namespace std; | ||
|
||
class bfs | ||
{ | ||
private: | ||
int a[10][10]; | ||
int n, *visit; | ||
|
||
public: | ||
bfs(); | ||
void read(); | ||
void searchfrom(); | ||
void print(); | ||
}; | ||
bfs::bfs() | ||
{ | ||
cout << "Breadth first Search" << endl; | ||
cout << "Enter the no of node:"; | ||
cin >> n; | ||
visit = new int[n]; | ||
for (int i = 0; i <= n; i++) | ||
{ | ||
visit[i] = 0; | ||
for (int j = 0; j <= n; j++) | ||
{ | ||
a[i][j] = 0; | ||
} | ||
} | ||
} | ||
void bfs::read() | ||
{ | ||
for (int i = 1; i < n; i++) | ||
{ | ||
for (int j = 1; j <= n; j++) | ||
{ | ||
if (i != j) | ||
{ | ||
cout << "\n Enter the valuesof:" << i << "," << j << "->"; | ||
cin >> a[i][j]; | ||
} | ||
} | ||
} | ||
} | ||
void bfs::print() | ||
{ | ||
cout << "\n Nodes are visited inthe order:" << endl; | ||
for (int i = 1; i <= n; i++) | ||
{ | ||
if (visit[i] == 0) | ||
{ | ||
cout << i << "->"; | ||
visit[i] = 1; | ||
searchfrom(); | ||
} | ||
} | ||
} | ||
|
||
void bfs::searchfrom() | ||
{ | ||
for (int k = 1; k <= n; k++) | ||
{ | ||
for (int i = k + 1; i <= n; i++) | ||
{ | ||
if (visit[i] == 0 && a[k][i] != 0) | ||
{ | ||
cout << i << "->"; | ||
visit[i] = 1; | ||
} | ||
} | ||
} | ||
} | ||
int main() | ||
{ | ||
bfs b1; | ||
b1.read(); | ||
b1.print(); | ||
|
||
return 0; | ||
} |