BFS(Breadth First Search) Algorithm(Part-3)

BFS(Breadth First Search) Algorithm(Part-3)

Posted By 1 year ago

এতক্ষণ আমরা যে এলগরিদম শিখলাম সেটি ব্যবহার করে বিভিন্ন সমস্যা প্রোগ্রামিং এর মাধ্যমে সমাধান করা যায়।তাই বিভিন্ন প্রোগ্রামিং ল্যাংগুয়েজ এ কিভাবে বি এফ এস চালানো যায় এখন আমরা সেটি শিখব।

 


Code for BFS in C++


#include <iostream>
#include <conio.h>
using namespace std;
int c = 0, t = 0;
struct node_info
{
int no;
int st_time;
}*q = NULL, *r = NULL, *x = NULL;
struct node
{
node_info *pt;
node *next;
}*front = NULL, *rear = NULL, *p = NULL, *np = NULL;
void push(node_info *ptr)
{
np = new node;
np->pt = ptr;
np->next = NULL;
if (front == NULL)
{
front = rear = np;
rear->next = NULL;
}
else
{
rear->next = np;
rear = np;
rear->next = NULL;
}
}
node_info *remove()
{
if (front == NULL)
{
cout<<"empty queue\n";
}
else
{
p = front;
x = p->pt;
front = front->next;
delete(p);
return(x);
}
}
void bfs(int *v,int am[][7],int i)
{
if (c == 0)
{
q = new node_info;
q->no = i;
q->st_time = t++;
cout<<"time of visitation for node "<<q->no<<":"<<q->st_time<<"\n\n";
v[i] = 1;
push(q);
}
c++;
for (int j = 0; j < 7; j++)
{
if (am[i][j] == 0 || (am[i][j] == 1 && v[j] == 1))
continue;
else if (am[i][j] == 1 && v[j] == 0)
{
r = new node_info;
r->no = j;
r->st_time = t++;
cout<<"time of visitation for node "<<r->no<<":"<<r->st_time<<"\n\n";
v[j] = 1;
push(r);
}
}
remove();
if (c <= 6 && front != NULL)
bfs(v, am, remove()->no);
}
int main()
{
int v[7], am[7][7];
for (int i = 0; i < 7; i++)
v[i] = 0;
for (int i = 0; i < 7; i++)
{
cout<<"enter the values for adjacency matrix row:"<<i+1<<endl;
for (int j = 0; j < 7; j++)
{
cin>>am[i][j];
}
}
bfs(v, am, 0);
getch();
}

Blog Topics
  • PHP (php: Hypertext preprocessor)

    8 Article
  • Agile

    2 Article
  • Freelancing

    3 Article
  • JavaScript

    7 Article
  • AngularJs

    1 Article
  • Programming Language C

    22 Article
  • Object Oriented Programming(C++)

    1 Article
  • Algorithm Design

    14 Article
  • Subject

    Python

    682 Article
  • Computer

    756 Article
  • Subject

    Technology Tips & Tricks

    3 Article