-
Notifications
You must be signed in to change notification settings - Fork 0
/
07dfs.cpp
51 lines (48 loc) · 1.02 KB
/
07dfs.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
#include<iostream>
#include<ctime>
using namespace std;
#define MAX 100
bool visited[MAX];
int adj[MAX][MAX];
void dfs(int start, int n)
{
int stack[MAX];
int top = -1;
int v;
visited[start] = true;
stack[++top] = start;
while(top!=-1)
{
v = stack[top--];
cout<<v<<" ";
for(int i=0;i<n;i++)
{
if(adj[v][i]&& !visited[i])
{
visited[i]=true;
stack[++top]=i;
}
}
}
cout<<endl;
}
int main()
{
int start, n;
cout<<"enter num of vertices: ";
cin>>n;
cout<<"enter vertices:"<<endl;
for(int i = 0;i<n;i++)
for(int j=0;j<n;j++)
cin>>adj[i][j];
cout<<"enter starting vertice: ";
cin>>start;
for (int i=0;i<n;i++)
visited[i] = false;
clock_t start_t = clock();
dfs(start, n);
clock_t end = clock();
double time = double(end-start_t)/CLOCKS_PER_SEC;
cout<<"time taken: "<<time*1000000<<" Microsseconds\n";
return 0;
}