Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added BFS Code in CPP #517

Merged
merged 1 commit into from
Oct 4, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions Program's_Contributed_By_Contributors/C++_Programs/bfs_graph.cpp
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;
}