SlideShare a Scribd company logo
2
Most read
3
Most read
4
Most read
BFS – Breadth First Search
DFS – Depth First Search
Prepared by:
Kevin Jadiya
Subject : Data Structure
Topic:
 What is BFS ?????
•BFS stands for Breadth First Search.
•BFS is an algorithm for traversing or searching a tree or
graph data structures.
•It uses a queue data structure for implementation.
•In BFS traversal we visit all the nodes level by level and the
traversal completed when all the nodes are visited.
 Algorithm for BFS :
Step 1: Initialize all nodes with status=1.(ready state)
Step 2: Put starting node in a queue and change status to
status=2.(waiting state)
Step 3: loop:
repeat step 4 and step 5 until queue gets empty.
Step 4: Remove front node N from queue, process them and
change the status of N to status=3.(processed state)
Step 5: Add all the neighbours of N to the rear of queue and
change status to status=2.(waiting status)
A
B C
D E
F
A
B C
C D
D E
E F
A
A,B
A,B,C
A,B,C,D
A,B,C,D,E,F
F
F
F
F
F
R
R
R
R
 Working of BFC :
queue data structure
Queue gets empty and the algorithm ends
Code for BFC in C
#include<stdio.h>
#include<conio.h>
#define n 8
int A[8][8]={ {0,1,1,1,0,0,0,0},
{1,0,0,0,1,0,0,0},
{1,0,0,0,0,1,0,0},
{1,0,0,0,0,0,1,0},
{0,1,0,0,0,0,0,1},
{0,0,1,0,0,0,0,1},
{0,0,0,1,0,0,0,1},
{0,0,0,0,1,1,1,0}
};
int Front=-1;
int Rear=-1;
int Q[n];
int Visit[n];
void enqueue(int);
int dequeue();
void BFS();
void main()
{
int i,j,s;
clrscr();
printf("n Adjacency Matrix is : n");
for(i=0;i<=n-1;i++);
{
for(j=0;j<=n-1;j++)
{
printf(" %d",A[i][j]);
}
printf("n");
}
printf("Enter source code : ");
scanf("%d",&s);
printf("BFS traversal is : ");
BFS(s);
getch();
}
void BFS(int s)
{
int i,p;
enqueue(s);
Visit[s]=1;
loop:
p=dequeue();
if(p!=-1)
{
printf(" %d“,p);
for(i=0;i<=n-1;i++)
{
if(A[p][i]==1 && Visit[i]==0)
{
enqueue(i);
Visit[i]=1;
}
}
goto loop;
}
}
void enqueue(int s)
{
if(Rear==n-1)
printf("Queue is overflow");
else
{
Rear++;
Q[Rear]=s;
if(Front==-1)
{
Front=0;
}
}
}
int dequeue()
{
int item;
if(Front==-1)
{
return -1;
}
else
{
item=Q[Front];
if(Front==Rear)
{
Front=-1;
Rear=-1;
}
else
{
Front++;
}
return item;
}
}
What is DFS ?????
•DFS stands for Depth First Search.
•DFS is an algorithm for traversing or searching a
tree or graph data structures.
•It uses a stack data structure for implementation.
•In DFS one starts at the root and explores as far as
possible along each branch before backtracking.
Algorithm of DFS
[1]-- Initialize all nodes with status=1.(ready state)
[2]– Put starting node in the stack and change status
to status=2(waiting state).
[3]– Loop:-
Repeat step- 4 and step- 5 until stack Get empty.
[4]– Remove top node N from stack process them and
change the status of N processed state (status=3).
[5]– Add all the neighbours of N to the top of stack and
change status to waiting status-2.
 Working of DFC :
A
B C
D E
F
A
B
C
D
C
F
C
E
C C
output
A A
B
A
B
D
A
B
D
F
A
B
D
F
E
A
B
D
F
E
C
Stack data structure
#include<stdio.h>
#include<conio.h>
#define n 8
Int a[8][8]={
{0,1,1,1,0,0,0,0},
{1,0,0,0,1,0,0,0},
{1,0,0,0,0,1,0,0},
{1,0,0,0,0,0,1,0},
{0,1,0,0,0,0,0,1},
{0,0,1,0,0,0,0,1},
{0,0,0,1,0,0,0,1},
{0,0,0,0,1,1,1,0}
} ;
Int stack[20];
Int visit[8];
Int top=-1;
void dfs(int s);
void push(int item);
int pop();
void main()
{
int i,j,s;
clrscr();
printf("nn THE ADJACENCY
MATRIX IS nn");
for(i=0;i<=n-1;i++)
{
for(j=0;j<=n-1;j++)
{
printf(" %d ",a[i][j]);
}
printf("n");
}
printf("nn ENTER THE
SOURCE VERTEX : ");
Scanf("%d",&s);
printf("nn DFS
TRAVERSAL IS : ");
dfs(s);
getch();
}
Code for DFS in C :
void dfs(int s)
{
int i,k;
visit[s]=1;
k=pop();
if(k!=-1)
{
printf(" %d ",k);
visit[k]=1;
}
while(k!=-1)
{
for(i=n-1;i>=0;i--)
{
if(a[k][i]==1 &&
visit[i]==0)
{
push(i);
}
}
k=pop();
if(k!=-1)
{
if(visit[k]==0)
{
printf(" %d ",k);
visit[k]=1;
getch();
}
}
}
}
int pop()
{
int k;
if(top==-1)
{
return -1;
}
else
{
k=stack[top];
top = top - 1;
return k;
}
}
void push(int item)
{
if(top==9)
{
printf("Stack overflow ");
}
else
{ top = top + 1;
stack[top]=item;
}
}
DFS V/S BFS
• DFS stands for Depth
First Search.
• DFS can be done with
the help of STACK i.e.,
LIOF.
• In DFS has higher time
and space complexity,
because at a time it
needs to back tracing in
graph for traversal.
• BFS stands for Breadth
First Search.
• BFS can be done with
the help of QUEUE i.e.,
FIOF.
• In BFS the space & time
complexity is lesser as
there is no need to do
back tracing
DFS V/S BFS
• DFS is more faster then
BFS.
• DFS requires less
memory compare to
BFS.
• DFS is not so useful in
finding shortest path.
• Example :
A
/ 
B C
/ / 
D E F
Ans : A,B,D,C,E,F
• BFS is slower than DFS.
• BFS requires more
memory compare to
DFS.
• BFS is useful in finding
shortest path.
• Example :
A
/ 
B C
/ / 
D E F
Ans : A,B,C,D,E,F
Thank You

More Related Content

PPTX
Graph traversals in Data Structures
PPTX
Bfs and Dfs
PPT
Graph traversal-BFS & DFS
PPTX
DFS and BFS
PPT
Breadth first search and depth first search
PPTX
Dfs presentation
PPT
Depth First Search ( DFS )
PDF
Breadth First Search Algorithm In 10 Minutes | Artificial Intelligence Tutori...
Graph traversals in Data Structures
Bfs and Dfs
Graph traversal-BFS & DFS
DFS and BFS
Breadth first search and depth first search
Dfs presentation
Depth First Search ( DFS )
Breadth First Search Algorithm In 10 Minutes | Artificial Intelligence Tutori...

What's hot (20)

PPTX
Abstract Data Types
PPTX
Linked list
PPT
1.1 binary tree
PPTX
Dijkstra's Algorithm
PPT
BINARY TREE REPRESENTATION.ppt
PPTX
Topological Sorting
PDF
Red black tree
PPT
Binary search tree(bst)
PPTX
Shortest path algorithm
PPTX
Graph in data structure
PPTX
Data structure - Graph
PPTX
AVL Tree in Data Structure
PPTX
Tree Traversal
PPTX
Performance analysis(Time & Space Complexity)
PPTX
Presentation on Breadth First Search (BFS)
PPTX
heap Sort Algorithm
PPTX
Depth-First Search
PPTX
PPTX
Graph representation
PPTX
All pair shortest path
Abstract Data Types
Linked list
1.1 binary tree
Dijkstra's Algorithm
BINARY TREE REPRESENTATION.ppt
Topological Sorting
Red black tree
Binary search tree(bst)
Shortest path algorithm
Graph in data structure
Data structure - Graph
AVL Tree in Data Structure
Tree Traversal
Performance analysis(Time & Space Complexity)
Presentation on Breadth First Search (BFS)
heap Sort Algorithm
Depth-First Search
Graph representation
All pair shortest path
Ad

Similar to Breadth First Search & Depth First Search (20)

PPTX
Breath first Search and Depth first search
PPTX
Breadth-First-Search algorithm with Code
PPTX
BFS slide.pptx
PPTX
Breadth First Searching Algorithm (BFS).pptx
PPTX
algoritmagraph_breadthfirstsearch_depthfirstsearch.pptx
PPT
17. Trees and Graphs
PPTX
BFS & DFS in Data Structure
PPTX
Depth First Searching Algorithm (DFS).pptx
PPTX
BFS_DFS_Enhanced_Presentation124567.pptx
PPT
14_Graph Traversalllllllllllllllllll.ppt
PPTX
BFS_Presentation_Sourabh.pptx. Explain the bfs
PPTX
p.p.pptx
PPTX
Breadth first search
PPTX
WEB DEVELOPMET FRONT END WITH ADVANCED RECEAT
PPTX
Data structure Graph PPT ( BFS & DFS ) NOTES
PDF
Breadth First Search and Depth First Search Algorithm
PPTX
bfs tree searching ,sortingUntitled presentation.pptx
PPTX
Breadth First Search or BFS for a Graph traversal
PPTX
Sec B Graph traversal.pptx
PPTX
Data structure
Breath first Search and Depth first search
Breadth-First-Search algorithm with Code
BFS slide.pptx
Breadth First Searching Algorithm (BFS).pptx
algoritmagraph_breadthfirstsearch_depthfirstsearch.pptx
17. Trees and Graphs
BFS & DFS in Data Structure
Depth First Searching Algorithm (DFS).pptx
BFS_DFS_Enhanced_Presentation124567.pptx
14_Graph Traversalllllllllllllllllll.ppt
BFS_Presentation_Sourabh.pptx. Explain the bfs
p.p.pptx
Breadth first search
WEB DEVELOPMET FRONT END WITH ADVANCED RECEAT
Data structure Graph PPT ( BFS & DFS ) NOTES
Breadth First Search and Depth First Search Algorithm
bfs tree searching ,sortingUntitled presentation.pptx
Breadth First Search or BFS for a Graph traversal
Sec B Graph traversal.pptx
Data structure
Ad

Recently uploaded (20)

PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PDF
Well-logging-methods_new................
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PDF
Categorization of Factors Affecting Classification Algorithms Selection
PPTX
Construction Project Organization Group 2.pptx
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PPT
Introduction, IoT Design Methodology, Case Study on IoT System for Weather Mo...
PPT
Total quality management ppt for engineering students
PPTX
Fundamentals of safety and accident prevention -final (1).pptx
PDF
Human-AI Collaboration: Balancing Agentic AI and Autonomy in Hybrid Systems
PDF
Artificial Superintelligence (ASI) Alliance Vision Paper.pdf
DOCX
573137875-Attendance-Management-System-original
PDF
A SYSTEMATIC REVIEW OF APPLICATIONS IN FRAUD DETECTION
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PDF
III.4.1.2_The_Space_Environment.p pdffdf
PPTX
6ME3A-Unit-II-Sensors and Actuators_Handouts.pptx
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
Well-logging-methods_new................
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
Categorization of Factors Affecting Classification Algorithms Selection
Construction Project Organization Group 2.pptx
Automation-in-Manufacturing-Chapter-Introduction.pdf
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
Introduction, IoT Design Methodology, Case Study on IoT System for Weather Mo...
Total quality management ppt for engineering students
Fundamentals of safety and accident prevention -final (1).pptx
Human-AI Collaboration: Balancing Agentic AI and Autonomy in Hybrid Systems
Artificial Superintelligence (ASI) Alliance Vision Paper.pdf
573137875-Attendance-Management-System-original
A SYSTEMATIC REVIEW OF APPLICATIONS IN FRAUD DETECTION
CYBER-CRIMES AND SECURITY A guide to understanding
UNIT-1 - COAL BASED THERMAL POWER PLANTS
III.4.1.2_The_Space_Environment.p pdffdf
6ME3A-Unit-II-Sensors and Actuators_Handouts.pptx

Breadth First Search & Depth First Search

  • 1. BFS – Breadth First Search DFS – Depth First Search Prepared by: Kevin Jadiya Subject : Data Structure Topic:
  • 2.  What is BFS ????? •BFS stands for Breadth First Search. •BFS is an algorithm for traversing or searching a tree or graph data structures. •It uses a queue data structure for implementation. •In BFS traversal we visit all the nodes level by level and the traversal completed when all the nodes are visited.
  • 3.  Algorithm for BFS : Step 1: Initialize all nodes with status=1.(ready state) Step 2: Put starting node in a queue and change status to status=2.(waiting state) Step 3: loop: repeat step 4 and step 5 until queue gets empty. Step 4: Remove front node N from queue, process them and change the status of N to status=3.(processed state) Step 5: Add all the neighbours of N to the rear of queue and change status to status=2.(waiting status)
  • 4. A B C D E F A B C C D D E E F A A,B A,B,C A,B,C,D A,B,C,D,E,F F F F F F R R R R  Working of BFC : queue data structure Queue gets empty and the algorithm ends
  • 5. Code for BFC in C #include<stdio.h> #include<conio.h> #define n 8 int A[8][8]={ {0,1,1,1,0,0,0,0}, {1,0,0,0,1,0,0,0}, {1,0,0,0,0,1,0,0}, {1,0,0,0,0,0,1,0}, {0,1,0,0,0,0,0,1}, {0,0,1,0,0,0,0,1}, {0,0,0,1,0,0,0,1}, {0,0,0,0,1,1,1,0} }; int Front=-1; int Rear=-1; int Q[n]; int Visit[n]; void enqueue(int); int dequeue(); void BFS(); void main() { int i,j,s; clrscr(); printf("n Adjacency Matrix is : n"); for(i=0;i<=n-1;i++); { for(j=0;j<=n-1;j++) { printf(" %d",A[i][j]); } printf("n"); } printf("Enter source code : "); scanf("%d",&s); printf("BFS traversal is : "); BFS(s); getch(); }
  • 6. void BFS(int s) { int i,p; enqueue(s); Visit[s]=1; loop: p=dequeue(); if(p!=-1) { printf(" %d“,p); for(i=0;i<=n-1;i++) { if(A[p][i]==1 && Visit[i]==0) { enqueue(i); Visit[i]=1; } } goto loop; } } void enqueue(int s) { if(Rear==n-1) printf("Queue is overflow"); else { Rear++; Q[Rear]=s; if(Front==-1) { Front=0; } } }
  • 7. int dequeue() { int item; if(Front==-1) { return -1; } else { item=Q[Front]; if(Front==Rear) { Front=-1; Rear=-1; } else { Front++; } return item; } }
  • 8. What is DFS ????? •DFS stands for Depth First Search. •DFS is an algorithm for traversing or searching a tree or graph data structures. •It uses a stack data structure for implementation. •In DFS one starts at the root and explores as far as possible along each branch before backtracking.
  • 9. Algorithm of DFS [1]-- Initialize all nodes with status=1.(ready state) [2]– Put starting node in the stack and change status to status=2(waiting state). [3]– Loop:- Repeat step- 4 and step- 5 until stack Get empty. [4]– Remove top node N from stack process them and change the status of N processed state (status=3). [5]– Add all the neighbours of N to the top of stack and change status to waiting status-2.
  • 10.  Working of DFC : A B C D E F A B C D C F C E C C output A A B A B D A B D F A B D F E A B D F E C Stack data structure
  • 11. #include<stdio.h> #include<conio.h> #define n 8 Int a[8][8]={ {0,1,1,1,0,0,0,0}, {1,0,0,0,1,0,0,0}, {1,0,0,0,0,1,0,0}, {1,0,0,0,0,0,1,0}, {0,1,0,0,0,0,0,1}, {0,0,1,0,0,0,0,1}, {0,0,0,1,0,0,0,1}, {0,0,0,0,1,1,1,0} } ; Int stack[20]; Int visit[8]; Int top=-1; void dfs(int s); void push(int item); int pop(); void main() { int i,j,s; clrscr(); printf("nn THE ADJACENCY MATRIX IS nn"); for(i=0;i<=n-1;i++) { for(j=0;j<=n-1;j++) { printf(" %d ",a[i][j]); } printf("n"); } printf("nn ENTER THE SOURCE VERTEX : "); Scanf("%d",&s); printf("nn DFS TRAVERSAL IS : "); dfs(s); getch(); } Code for DFS in C :
  • 12. void dfs(int s) { int i,k; visit[s]=1; k=pop(); if(k!=-1) { printf(" %d ",k); visit[k]=1; } while(k!=-1) { for(i=n-1;i>=0;i--) { if(a[k][i]==1 && visit[i]==0) { push(i); } } k=pop(); if(k!=-1) { if(visit[k]==0) { printf(" %d ",k); visit[k]=1; getch(); } } } }
  • 13. int pop() { int k; if(top==-1) { return -1; } else { k=stack[top]; top = top - 1; return k; } } void push(int item) { if(top==9) { printf("Stack overflow "); } else { top = top + 1; stack[top]=item; } }
  • 14. DFS V/S BFS • DFS stands for Depth First Search. • DFS can be done with the help of STACK i.e., LIOF. • In DFS has higher time and space complexity, because at a time it needs to back tracing in graph for traversal. • BFS stands for Breadth First Search. • BFS can be done with the help of QUEUE i.e., FIOF. • In BFS the space & time complexity is lesser as there is no need to do back tracing
  • 15. DFS V/S BFS • DFS is more faster then BFS. • DFS requires less memory compare to BFS. • DFS is not so useful in finding shortest path. • Example : A / B C / / D E F Ans : A,B,D,C,E,F • BFS is slower than DFS. • BFS requires more memory compare to DFS. • BFS is useful in finding shortest path. • Example : A / B C / / D E F Ans : A,B,C,D,E,F