-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDFS_iter.cpp
More file actions
77 lines (67 loc) · 1.23 KB
/
DFS_iter.cpp
File metadata and controls
77 lines (67 loc) · 1.23 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include<iostream>
#include <fstream>
#include "stacklink.cpp"
using namespace std;
int v;
int graph[1000][1000];
int visited[100]={0};
void dfs(int n)
{
stack st;
st.push(n);
visited[n]=1;
while(!st.isempty())
{
int i=st.pop();
char c=i+65;
cout<<c<<" ";
for(int w=0;w<v;++w)
{
if(graph[i][w]==1)
{
if(visited[w]!=1)
{
visited[w]=1;
st.push(w);
}
}
}
}
}
int main()
{
fstream infile;
infile.open("BFS_input.txt",ios::in);
if(!infile)
{
cout<<"Error!"<<endl;
return 0;
}
infile>>v;
for(int i=0;i<v;i++)
{
for(int j=0;j<v;j++)
infile>>graph[i][j];
}
cout<<"Input graph is:"<<endl;
for(int i=0;i<v;i++)
{
for(int j=0;j<v;j++)
cout<<graph[i][j]<<" ";
cout<<endl;
}
int m;
cout<<"Enter the starting vertex: ";
cin>>m;
cout<<"DFS is: "<<endl;
dfs(m);
return 0;
}
/*
0 1 1 0 0 0
1 0 0 1 1 0
1 0 0 0 1 0
0 1 0 0 1 1
0 1 1 1 0 1
0 0 0 1 1 0
*/