-
Notifications
You must be signed in to change notification settings - Fork 1
/
DEPTH_FIRST_SEARCH.cpp
53 lines (53 loc) · 872 Bytes
/
DEPTH_FIRST_SEARCH.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//Depth First Search
#include<iostream.h>
#include<conio.h>
class Graph
{
int a[10][10], n, s;
public:
void getdata();
void dfs_traverse();
};
void Graph::getdata()
{
cout<<"\n Enter the number of vertices in the graph: ";
cin>>n;
cout<<"\n Enter the adjacency matrix of graph: "<<endl;
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
cin>>a[i][j];
cout<<"\n Enter the vertex from which you want to traverse: ";
cin>>s;
}
void Graph::dfs_traverse()
{
int *visited= new int[n];
int stack[10],top= -1,i;
for(int j=0;j<n;j++)
visited[j]=0;
cout<<"\n The Depth First Search Traversal : "<<endl;
i=stack[++top]=s;
visited[s]=1;
while(top>=0)
{
i=stack[top];
cout<<stack[top--]<<endl;
for(int j=n-1;j>=0;j--)
{
if(a[i][j]!=0&&visited[j]!=1)
{
stack[++top]=j;
visited[j]=1;
}
}
}
}
int main()
{
Graph DFS;
clrscr();
DFS.getdata();
DFS.dfs_traverse();
getch();
return 0;
}