Saturday, 23 June 2012

THIS IS DEPTH FIRST SEARCH (WAY TO TRAVERSE A GRAPH)  DFS!!!!!!!!!!
THERE IS TWO APPROACHES TO DO IT 
1:- ITERATIVE APPROACH
2:-RECURSIVE APPROACH 
APPLICATION:- FINDING CONNECTED COMPONENTS
           :-TOPOLOGICAL SORTING
           :-FINDING BRIDGE ON A GRAPH
           :-FIND STRONGLY CONNECTED COMPONENTS
           :-FINDING BICONNECTIVITY IN GRAPH
   LINK TO STUDY :-DFS wiki link

CODE OF ITERATIVE APPROACH 
#include<iostream>
#include<stack>
#include<vector>
#include<stdio.h>
using namespace std;
#define MAX 100
#define initial 1
#define visited 2
int n;/* number of nodes in the graph*/
int adj[MAX][MAX]; //adjancency matrix
int state[MAX]; /*can be visited or initial*/
stack<int> stac_k;
void create_graph()   //create a graph //
{
int i,o,d,max_edge;
printf("enter the number of nodes\n");
scanf("%d",&n);
max_edge=n*(n-1);
       for(i=1;i<=max_edge;i++)
         {
          printf("enter the edge %d:(-1,-1 to quit) ",i);
          scanf("%d%d",&o,&d);
          if((o==-1)&&(d==-1))
          break;
          if((o>=n)||(d>=n)||(o<0)||(d<0))
          {cout<<"invalid edge";i--;}
          else
          adj[o][d]=1;
         }
return ;
}

void display_graph()  //display graph
{
           for(int i=0;i<n;i++)
           {
           for(int j=0;j<n;j++)
           {
           if(adj[i][j]==1)
           cout<<"1";
           else
           cout<<"0";
           }
           cout<<endl;
           }
return;
}
void DFS(int v)
{
  int i;
  stac_k.push(v);
  while(!stac_k.empty())
   {
     v=stac_k.top();
        stac_k.pop();
               if(state[v]==initial)
              {
                   state[v]=visited;
                   cout<<v<<":->";
               }
               for(i=n-1;i>=0;i--)
              {
                   if((state[i]==initial)&&(adj[v][i]==1))
                   stac_k.push(i);
              }
  }
return ;
}
void df_traversal()
{
   int v=0;
   for(v=0;v<n;v++)
    state[v]=initial;
   printf("enter starting vertex for DFS\n");
   scanf("%d",&v);
   DFS(v);
return ;
}
int main()
{
create_graph();
display_graph();
df_traversal();
return 0;
}
CODE OF RECURSIVE APPROACH:-


#include<iostream>
#include<stdio.h>
#include<stack>
using namespace std;
#define initial 1
#define visited 2
#define finished 3
#define MAX 100
int n ; //number of vertices
stack<int> stac_k;
int adj[MAX][MAX];
int state[MAX];
void create_graph()   //create a graph //
{
int i,o,d,max_edge;
printf("enter the number of nodes\n");
scanf("%d",&n);
max_edge=n*(n-1);
       for(i=1;i<=max_edge;i++)
         {
          printf("enter the edge %d:(-1,-1 to quit) ",i);
          scanf("%d%d",&o,&d);
          if((o==-1)&&(d==-1))
          break;
          if((o>=n)||(d>=n)||(o<0)||(d<0))
          {cout<<"invalid edge";i--;}
          else
          adj[o][d]=1;
         }
return ;
}
void display_graph()  //display graph
{
           for(int i=0;i<n;i++)
           {
           for(int j=0;j<n;j++)
           {
           if(adj[i][j]==1)
           cout<<"1";
           else
           cout<<"0";
           }
           cout<<endl;
           }
return;
}
void DFS(int v)
{
  int i;
  cout<<v<<":->";
  state[v]=visited;
  for(i=0;i<n;i++)
  if((adj[v][i]==1)&&(state[i]==initial))
  DFS(i);
  state[v]=finished;
  return ;
}
void df_traversal()
{
   int v=0;
   for(v=0;v<n;v++)
    state[v]=initial;
   printf("enter starting vertex for DFS\n");
   scanf("%d",&v);
   DFS(v);
return ;
}
int main()
{
create_graph();
display_graph();
df_traversal();
return 0;
}

No comments:

Post a Comment