/* Write C++ programs for the implementation of Depth-first search(DFS) for a given graph */
#include<iostream> #include<conio.h> #include<stdlib.h> using namespace std; int cost[10][10],i,j,k,n,stk[10],top,v,visit[10],visited[10]; main() { int m; cout <<"enterno of vertices"; cin >> n; cout <<"ente no of edges"; cin >> m; cout <<"\nEDGES \n"; for(k=1;k<=m;k++) { cin >>i>>j; cost[i][j]=1; } cout <<"enter initial vertex"; cin >>v; cout <<"ORDER OF VISITED VERTICES"; cout << v <<" "; visited[v]=1; k=1; while(k<n) { for(j=n;j>=1;j--) if(cost[v][j]!=0 && visited[j]!=1 && visit[j]!=1) { visit[j]=1; stk[top]=j; top++; } v=stk[--top]; cout<<v << " "; k++; visit[v]=0; visited[v]=1; } } |
OUTPUT
enterno of vertices9
ente no of edges9
EDGES
1 2
2 3
2 6
1 5
1 4
4 7
5 7
7 8
8 9
enter initial vertex1
ORDER OF VISITED VERTICES1 2 3 6 4 7 8 9 5
this program is good.but i want Program for traversing a graph through DepthFirstSearch . please hlp me
need explation thank you
explanation plz…..
i can’t understand this…..
umara it uses stack methode
plz expalin this program every step with comments plzzzzzzz
and reply me on my e-mail.it’s a request not an order
this program is awesome, the english was shit
i love this soo much! i love my indian *.* muwah muwah