upsidedown

dfs

Feb 23rd, 2012
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.67 KB | None | 0 0
  1. #include<iostream.h>
  2. #include<conio.h>
  3. #include<stdlib.h>
  4.  
  5. int cost[10][10],i,j,k,n,stk[10],top,v,visit[10],visited[10];
  6.  
  7. void main()
  8. {
  9.     int m;
  10.     cout<<"Enter  no of vertices";
  11.     cin >> n;
  12.     cout <<"Enter no of edges";
  13.     cin>>m;
  14.     cout <<"\nEDGES \n";
  15.     for(k=1;k<=m;k++)
  16.     {
  17.         cin >>i>>j;
  18.         cost[i][j]=1;
  19.     }
  20.  
  21.     cout <<"Enter initial vertex\n";
  22.     cin >>v;
  23.     cout <<"ORDER OF VISITED VERTICES\n";
  24.     cout << v <<" ";
  25.     visited[v]=1;
  26.     k=1;
  27.     while(k<n)
  28.     {
  29.         for(j=n;j>=1;j--)
  30.         {
  31.             if(cost[v][j]!=0 && visited[j]!=1 && visit[j]!=1)
  32.             {
  33.                 visit[j]=1;
  34.                 stk[top]=j;
  35.                 top++;
  36.             }
  37.         }
  38.     v=stk[--top];
  39.     cout<<v<<" ";
  40.     k++;
  41.     visit[v]=0;
  42.     visited[v]=1;
  43.     }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment