BFS & DFS in Data Structure

MeghajKumarMallick 362 views 37 slides Apr 13, 2020
Slide 1
Slide 1 of 37
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37

About This Presentation

This is an PPT of Data Structure. It include the following topic "BFS & DFS in Data Structure".


Slide Content

BFS AND DFS Presented By: Muskaan MCA/25020/18

CONTENTS Introduction Types of graph Traversal Depth-First Search Breadth-First Search Application of BFS and DFS

INTRODUCTION Graph traversal   is also known as  graph  search refers to the process of visiting each vertex in a  graph . Such  traversals  are classified by the order in which the vertices are visited. Tree  traversal  is a special case of  graph traversal .

DEPTH FIRST SEARCH Depth-first search ( DFS ) is an algorithm for traversing or searching tree or graph data structures. DFS uses stack.

Depth-First Search // recursive, preorder void dfs (Node v) { if (v == null) return; if (v not yet visited) visit&mark(v); // visit node before adjacent nodes for (each w adjacent to v) if (w has not yet been visited) dfs(w); } // dfs

E xa m p le 7 1 5 4 3 2 6 Policy: Visit adjacent nodes in increasing index order

DFS: Start with Node 5 7 1 5 4 3 2 6 5 1 0 3 2 7 6 4

DFS: Start with Node 5 7 1 5 4 3 2 6 5 Push 5

DFS: Start with Node 5 7 1 5 4 3 2 6 Pop/Visit/Mark 5 5

DFS: Start with Node 5 7 1 5 4 3 2 6 1 2 5 Push 2, Push 1

DFS: Start with Node 5 7 1 5 4 3 2 6 2 Pop/Visit/Mark 1 5 1

DFS: Start with Node 5 7 1 5 4 3 2 6 4 2 5 1 Push 4, Push

DFS: Start with Node 5 7 1 5 4 3 2 6 4 2 5 1 0 Pop/Visit/Mark

DFS: Start with Node 5 7 1 5 4 3 2 6 3 7 4 2 Push 7, Push 3 5 1 0

DFS: Start with Node 5 7 1 5 4 3 2 6 7 4 2 5 1 0 3 Pop/Visit/Mark 3

DFS: Start with Node 5 7 1 5 4 3 2 6 7 4 2 5 1 0 3 2 is already in stack

DFS: Start with Node 5 7 1 5 4 3 2 6 7 4 2 Pop/Mark/Visit 7 5 1 0 3 7

DFS: Start with Node 5 7 1 5 4 3 2 6 6 4 2 5 1 0 3 7 Push 6

DFS: Start with Node 5 7 1 5 4 3 2 6 4 2 5 1 0 3 7 6 Pop/Mark/Visit 6

DFS: Start with Node 5 7 1 5 4 3 2 6 2 Pop/Mark/Visit 4 5 1 0 3 7 6 4

DFS: Start with Node 5 7 1 5 4 3 2 6 Pop/Mark/Visit 2 5 1 0 3 7 6 4 2

DFS: Start with Node 5 7 1 5 4 3 2 6 D on e 5 1 0 3 7 6 4 2

BREADTH FIRST SEARCH Breadth-first search  ( BFS ) is an  algorithm  for traversing or searching  tree  or  graph  data structures. It uses queue.

Breadth-First Search // non-recursive, preorder, breadth-first search void bfs (Node v) { if (v == null) return; enqueue(v); while (queue is not empty) { dequeue(v); if (v has not yet been visited) mark&visit(v); for (each w adjacent to v) if (w has not yet been visited && has not been queued) enqueue(w); } // while } // bfs

BFS: Start with Node 5 7 1 5 4 3 2 6

BFS: Start with Node 5 7 1 5 4 3 2 6 5

BFS: Node one-away 7 1 5 4 3 2 6 5 5 1 2

BFS: Visit 1 and add its adjacent nodes 7 1 5 4 3 2 6 5 1 5 1 2 4

BFS: Visit 2 and add its adjacent nodes 7 1 5 4 3 2 6 5 1 2 5 1 2 4

BFS: Visit 0 and add its adjacent nodes 7 1 5 4 3 2 6 5 1 2 0 5 1 2 4 3 7

BFS: Visit 4 and add its adjacent nodes 7 1 5 4 3 2 6 5 1 2 0 4 5 1 2 4 3 7

BFS: Visit 3 and add its adjacent nodes 7 1 5 4 3 2 6 5 1 2 0 4 3 5 1 2 4 3 7

BFS: Visit 7 and add its adjacent nodes 7 1 5 4 3 2 6 5 1 2 0 4 3 7 5 1 2 4 3 7 6

BFS: Visit 6 and add its adjacent nodes 7 1 5 4 3 2 6 5 1 2 0 4 3 7 6 5 1 2 4 3 7 6

BFS Traversal Result 7 1 5 4 3 2 6 5 1 2 0 4 3 7 6

Ap p l ic a t i ons of B FS • • • Computing Distances Checking for cycles in a graph Reachability : Given a graph G and vertices x and y , determine if there exists a path from x to y .

A pp l i c a t i ons of DFS • Checking for Biconnected Graph : A graph is biconnected if removal of any vertex does not affect connectivity of the other vertices. Used to test if network is robust to failure of certain nodes. A single DFS traversal algorithm .
Tags