Tuesday, 26 June 2012

CLASSIFIACTION OF EDGES IN GRAPH 
EDGES OF A DIRECTED GRAPH CAN BE DIVIDE INTO FOUR GROUPS 
1.Tree Edge 
2.Back Edge 
3.Forward Edge 
4.Cross Edge                



TREE EDGE :
an edge included in the DFS spanning forest.
BACK EDGE :
an edge to a vertex to its spanning tree ancestor.  
FORWARD EDGE :
an edge from a vertex to a spanning tree non son descendant.
CROSS EDGE :
an edge between two vertices is a cross edge when there is no ancestor and descendant relationship between them in spanning forest.


code to identify the edges
static int time=0;
void DFS(int v)
{
int i;
time++;
d[v]=time;
state[v]=visited;
for(i=0;i<n;i++)
{
     if(adj[v][i]==1)
       {
          if(state[i]==initial)
              {
              cout<<"Tree Edge :"<<v<<" "<<i<<"\n";
              DFS(i);
               }
            else if(state[i]==visited)
               { 
                   cout<<"Back Edge :"<<v<<" "<<i<<"\n";    
                }
              else if(d[v]<d[i])
                  cout<<"Forward Edge :"<<v<<" "<<i<<"\n";
                else
                  cout<<"Cross Edge :"<<v" "<<i<<"\n";
              }
     }
state[v]=finished;
  f[v]=++time;
return ;
}

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;
}

Friday, 22 June 2012

well this is my first blog !!! 
first :-my name:- RAHUL SINGH
myintrest:-coding,take participation in programming contest;
what topics i discuss here untill the end is algorithms,programming questions ,data structures,c,c++ 
MY CODECHEF PROFILE