Friday, May 24, 2013

Implementing BFS(Breath's First Search) using C++

/***********************************************************
* You can use all the programs on  www.engineercse.blogspot.comprogrammers' blog
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact azam.ruet10@gmail.com
* To find more C programs, do visit www.engineercse.blogspot.com
* and browse!
*
*                            Coding is poetry!!!
***********************************************************/

#include
#include
#include
#include
#define inf 1000000
#define clear(x,a) memeset(x,a,sizeof(x))
using namespace std;

bool adj[100][100];
int n, white=1, gray=2, black=3, color[100],d[100],edge, node[100],parent[100],source;
queue Q;
void bfs();

int main()
{
   int start,end;
   cout<<"vertices::";
   cin>>n;
   cout<<"\nedges::";
   for(int i=1;i<=n;i++)
     color[i]=white,d[i]=inf,parent[i]=-1;
     cin>>edge;
     cout<<"\nstart and ending vertex::";
     while(edge--)
     cin>>start>>end,adj[start][end]=true,adj[end][start]=true;
     cout<<"\nsource vertex:";
     cin>>source;
     bfs();
     for(int i=1;i<=n;i++)
     cout<     return 0;
}

void bfs()
{
    d[source]=0;
    Q.push(source);
    color[source]=gray;
    while(!Q.empty())
    {
        int s=Q.front();
        for(int i=0;i<=n;++i)
        {
            if(adj[s][i]and color[i]==white)
            Q.push(i), color[i]=gray, d[i]=d[s]+1, parent[i]=s;
        }
        Q.pop();
    }
}


To get more C Program go here
To get more DFS algorithmic code click

No comments:

Post a Comment