-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph_BFS.cpp
More file actions
116 lines (100 loc) · 1.97 KB
/
Graph_BFS.cpp
File metadata and controls
116 lines (100 loc) · 1.97 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/******************************************************************************
Online C++ Compiler.
Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.
*******************************************************************************/
#include<iostream>
using namespace std;
#define max 30
int queue[max],front=-1,rear=-1;
void enqueue(int num)
{
if(rear==max-1)
{
cout<<"OVERFLOW!";
}
else if(front==-1 && rear==-1)
{
front++;
rear++;
queue[rear]=num;
}
else
{
rear++;
queue[rear]=num;
}
}
bool isEmpty()
{
if(front==-1 && rear==-1)
return 1;
else
return 0;
}
int dequeue()
{
int deleted;
if(front==-1 && rear==-1)
{
cout<<"UNDERFLOW!";
exit(1) ;
}
else if(front == rear)
{
deleted=queue[front];
front =-1;
rear =-1;
}
else
{
deleted=queue[front];
front++;
}
return deleted;
}
void show()
{
if(front==-1 && rear==-1)
{
cout<<"UNDERFLOW!";
}
else
{
for(int i=front;i<=rear;i++)
{
cout<<"\t"<<queue[i];
}
cout<<endl;
}
}
int main(){
int u;
int i = 5;
int visited[7] = {0,0,0,0,0,0,0};
int a [7][7] = {
{0,1,1,1,0,0,0},
{1,0,1,0,0,0,0},
{1,1,0,1,1,0,0},
{1,0,1,0,1,0,0},
{0,0,1,1,0,1,1},
{0,0,0,1,1,0,0},
{0,0,0,0,1,0,0}
};
cout<<i;
visited[i] = 1;
enqueue(i); // Enqueue i for INSERTION
while (!isEmpty())
{
int u = dequeue();
for (int j = 0; j < 7; j++)
{
if(a[u][j] ==1 && visited[j] == 0){
cout<<"\t"<<j;
visited[j] = 1;
enqueue(j);
}
}
}
return 0;
}