Implement  BFS in this code:

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

 

Implement  BFS in this code: 

 

 

#include<stdio.h> 
 
int selectOption(); 
int pop(int data[]); 
void push(int data[], int v); 
int getTopElement(int data[]); 
int getStackSize(); 
 
int top = -1; 
 
int main() 

//printf("hello"); 
int graph[100][100], data[100], n, ch, v, i, j;//data = stack 
int visited[100]; 
printf("Enter number of vertex: "); 
scanf("%d",&n); 
 
ch = selectOption(); 
while(ch) 

if(ch == 1) 

for(i = 0; i < n; i++) 

for(j = i+1; j < n; j++) 

printf("Edge between %c and %c? ",65+i,65+j); 
scanf("%d",&graph[i][j]); 
graph[j][i] = graph[i][j]; 



else if(ch == 2) 

int s; 
char c; 
printf("Enter source vertex: "); 
fflush(stdin); 
scanf("%c",&c); 
s = c - 65; 
 
//printf("s = %d\n",s); 
 
push(data, s); 
int vertex; 
while(getStackSize() != 0) 

vertex = pop(data); 
if(visited[vertex] != 1) 

for(i = 0; i < n; i++) 

if(graph[vertex][i] == 1) 
push(data, i); 

visited[vertex] = 1; 
printf("%c visited\n",65+vertex); 



else if(ch == 3) 

 

else if(ch == 4) 

for(i = 0; i < n; i++) 

for(j = 0; j < n; j++) 

printf("%d\t",graph[i][j]); 

printf("\n"); 


else 

printf("Invalid choice. Please try again.\n\n"); 

ch = selectOption(); 

 
 

 
int selectOption() 

printf("1. Create Graph\n"); 
printf("2. DFS\n"); 
printf("3. BFS\n"); 
printf("4. Print Graph\n"); 
printf("0. Exit\n"); 
printf("Enter your choice: "); 
int n; 
scanf("%d",&n); 
return n; 

 
int getStackSize() 

return top+1; 

 
int getTopElement(int data[]) 

return data[top]; 

 
void push(int data[], int v) 

top++; 
data[top] = v; 
//return top; 

 
int pop(int data[]) 

int temp = data[top]; 
top--; 
return temp; 

 

Expert Solution
steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Knowledge Booster
Randomized Select Algorithm
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education