-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraphTraversal.cpp
52 lines (48 loc) · 1.15 KB
/
GraphTraversal.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
#include <iostream>
#include<queue>
using namespace std;
void BFS(int G[][7],int start,int n){
int i=start;
queue<int> qq;
int visited[7]={0};
cout<<i<<"\t";
visited[i]=1;
qq.push(i);
while(!qq.empty()){
i=qq.front();
for(int j=1;j<n;j++){
if(G[i][j]==1 && visited[j]==0){
cout<<j<<"\t";
visited[j]=1;
qq.push(j);
}
}
qq.pop();
}
}
void DFS(int G[][7],int start,int n){
static int visited[7]={0};
if(visited[start]==0){
cout<<start<<"\t";
visited[start]=1;
for(int j=1;j<n;j++){
if(G[start][j]==1 && visited[j]==0)
DFS(G,j,n);
}
}
}
int main()
{
int G[7][7]={{0,0,0,0,0,0,0},
{0,0,1,1,0,0,0},
{0,1,0,0,1,0,0},
{0,1,0,0,1,0,0},
{0,0,1,1,0,1,1},
{0,0,0,0,1,0,0},
{0,0,0,0,1,0,0}
};
BFS(G,4,7);
cout<<endl;
DFS(G,4,7);
return 0;
}