0% found this document useful (0 votes)
26 views79 pages

Bfs Dfs Demo

1) The document demonstrates breadth-first search (BFS) on a graph using a queue. 2) BFS starts at a source node and enqueues its neighbors. It then dequeues the next vertex and enqueues any previously unvisited neighbors. 3) This process continues until the queue is empty, providing a breadth-first traversal of the graph.

Uploaded by

Babuji Baskaran
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views79 pages

Bfs Dfs Demo

1) The document demonstrates breadth-first search (BFS) on a graph using a queue. 2) BFS starts at a source node and enqueues its neighbors. It then dequeues the next vertex and enqueues any previously unvisited neighbors. 3) This process continues until the queue is empty, providing a breadth-first traversal of the graph.

Uploaded by

Babuji Baskaran
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 79

Data Structures Algorithm and Design

DSECFZG519 Akshaya Ganesan


BITS Pilani Asst. Prof [OFF-CAMPUS]
Pilani|Dubai|Goa|Hyderabad

1
BFS-Demo

BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956


Breadth First Search
A B C D

E F G H

front

FIFO Queue
3

BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956


Breadth First Search
A B C D

E F G H

enqueue source node front A


FIFO Queue
4

BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956


Breadth First Search
A B C D

E F G H

dequeue next vertex front A


FIFO Queue
5

BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956


Breadth First Search
A B C D

E F G H

visit neighbors of A front

FIFO Queue
6

BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956


Breadth First Search
A B C D

E F G H

visit neighbors of A front

FIFO Queue
7

BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956


Breadth First Search
A
A B C D

E F G H

B discovered front B

BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956


Breadth First Search
- A
A B C D

E F G H

visit neighbors of A front B


FIFO Queue
9

BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956


Breadth First Search
A
A B C D

E F G H

I discovered front B I
FIFO Queue
10

BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956


Breadth First Search
A
A B C D

E F G H

finished with A front B I


FIFO Queue
11

BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956


Breadth First Search
- A
A B C D

E F G H

dequeue next vertex front B I


FIFO Queue
12

BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956


Breadth First Search
- A
A B C D

E F G H

visit neighbors of B front I


FIFO Queue
13

BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956


Breadth First Search
- A
A B C D

E F G H

visit neighbors of B front I


FIFO Queue
14

BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956


Breadth First Search
- A
A B C D

E F G H

F discovered front I F
FIFO Queue
15

BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956


Breadth First Search
- A
A B C D

E F G H

visit neighbors of B front I F


FIFO Queue
16

BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956


Breadth First Search
- A
A B C D

E F G H

A already discovered front I F


FIFO Queue
17

BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956


Breadth First Search
- A
A B C D

E F G H

finished with B front I F


FIFO Queue
18

BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956


Breadth First Search
- A
A B C D

E F G H

dequeue next vertex front I F


FIFO Queue
19

BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956


Breadth First Search
- A
A B C D

E F G H

visit neighbors of I front F


FIFO Queue
20

BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956


Breadth First Search
- A
A B C D

E F G H

visit neighbors of I front F


FIFO Queue
21

BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956


Breadth First Search
- A
A B C D

E F G H

A already discovered front F


FIFO Queue
22

BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956


Breadth First Search
- A
A B C D

E F G H

visit neighbors of I front F


FIFO Queue
23

BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956


Breadth First Search
- A
A B C D

E F G H

I B

E discovered front F E
FIFO Queue
24

BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956


Breadth First Search
- A
A B C D

E F G H

I B

visit neighbors of I front F E


FIFO Queue
25

BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956


Breadth First Search
- A
A B C D

E F G H

I B

F already discovered front F E


FIFO Queue
26

BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956


Breadth First Search
- A
A B C D

E F G H

I B

I finished front F E
FIFO Queue
27

BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956


Breadth First Search
- A
A B C D

E F G H

I B

dequeue next vertex front F E


FIFO Queue
28

BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956


Breadth First Search
- A
A B C D

E F G H

I B

visit neighbors of F front E


FIFO Queue
29

BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956


Breadth First Search
- A
A B C D

E F G H

I B F

G discovered front E G
FIFO Queue
30

BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956


Breadth First Search
- A
A B C D

E F G H

I B F

F finished front E G
FIFO Queue
31

BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956


Breadth First Search
- A
A B C D

E F G H

I B F

dequeue next vertex front E G


FIFO Queue
32

BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956


Breadth First Search
- A
A B C D

E F G H

I B F

visit neighbors of E front G


FIFO Queue
33

BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956


Breadth First Search
A B C D

E F G H

I B F

E finished front G
FIFO Queue
34

BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956


Breadth First Search
- A
A B C D

E F G H

I B F

dequeue next vertex front G


FIFO Queue
35

BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956


Breadth First Search
- A
A B C D

E F G H

I B F

visit neighbors of G front

FIFO Queue
36

BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956


Breadth First Search
- A G
A B C D

E F G H

I B F

C discovered front C
FIFO Queue
37

BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956


Breadth First Search
- A G
A B C D

E F G H

I B F

visit neighbors of G front C


FIFO Queue
38

BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956


Breadth First Search
- A G
A B C D

E F G H

I B F G

H discovered front C H
FIFO Queue
39

BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956


Breadth First Search
- A G
A B C D

E F G H

I B F G

G finished front C H
FIFO Queue
40

BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956


Breadth First Search
- A G
A B C D

E F G H

I B F G

dequeue next vertex front C H


FIFO Queue
41

BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956


Breadth First Search
- A G
A B C D

E F G H

I B F G

visit neighbors of C front H


FIFO Queue
42

BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956


Breadth First Search
- A G C
A B C D

E F G H

I B F G

D discovered front H D
FIFO Queue
43

BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956


Breadth First Search
- A G C
A B C D

E F G H

I B F G

C finished front H D
FIFO Queue
44

BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956


Breadth First Search
- A G C
A B C D

E F G H

I B F G

get next vertex front H D


FIFO Queue
45

BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956


Breadth First Search
- A G C
A B C D

E F G H

I B F G

visit neighbors of H front D


FIFO Queue
46

BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956


Breadth First Search
- A G C
A B C D

E F G H

I B F G

finished H front D
FIFO Queue
47

BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956


Breadth First Search
- A G C
A B C D

E F G H

I B F G

dequeue next vertex front D


FIFO Queue
48

BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956


Breadth First Search
- A G C
A B C D

E F G H

I B F G

visit neighbors of D front

FIFO Queue
49

BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956


Breadth First Search
- A G C
A B C D

E F G H

I B F G

D finished front

FIFO Queue
50

BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956


Breadth First Search
- A G C
A B C D

E F G H

I B F G

dequeue next vertex front

FIFO Queue
51

BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956


Breadth First Search
- A G C
A B C D

E F G H

I B F G

STOP front

FIFO Queue
52

BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956


DFS-Demo

53

BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956


Walk-Through

Visited Array
F C A
A B
B C
D
H D
E
G E
F
G
H

Task: Conduct a depth-first search of the


graph starting with node D 54

BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956


Walk-Through

Visited Array
F C A
A B
B C
D
H D √
E
G E
F
G
D
H
The order nodes are visited:
D Visit D
55

BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956


Walk Through

Visited Array
F C A
A B
B C
D
H D √
E
G E
F
G
D
H
The order nodes are visited:
Consider nodes adjacent to D,
D decide to visit C first (Rule:
visit adjacent nodes in 56

alphabetical order)
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Walk Through

F C A
A B

B
C √
D
H D √
E
G E F
G C
H D
The order nodes are visited:
Visit C
D, C
57

BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956


Walk-Through

Visited Array
F C A
A B
B C √
D
H D √
E
G E
F
C
G
D
H
The order nodes are visited:
No nodes adjacent to C; cannot
D, C continue → backtrack, i.e.,
pop stack and restore 58

previous state
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Walk-Through

Visited Array
F C A
A B
B C √
D
H D √
E
G E
F
G
D
H
The order nodes are visited:
Back to D – C has been visited,
D, C decide to visit E next
59

BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956


Walk Through

Visited Array
F C A
A B
B C √
D
H D √
E √
G E
F
E
G
D
H
The order nodes are visited:
Back to D – C has been visited,
D, C, E decide to visit E next
60

BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956


Walk-Through

Visited Array
F C A
A B
B C √
D
H D √
E √
G E
F
E
G
D
H
The order nodes are visited:
Only G is adjacent to E
D, C, E
61

BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956


Walk-Through

Visited Array
F C A
A B
B C √
D
H D √
E √
G E
G
F
E
G √
D
H
The order nodes are visited:
Visit G
D, C, E, G
62

BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956


Walk-Through

Visited Array
F C A
A B
B C √
D
H D √
E √
G E
G
F
E
G √
D
H
The order nodes are visited:
Nodes D and H are adjacent to
D, C, E, G G. D has already been
visited. Decide to visit H. 63
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Walk-Through

Visited Array
F C A
A B
B C √
D
H D √ H
E √
G E
G
F
E
G √
D
H √
The order nodes are visited:
Visit H
D, C, E, G, H
64

BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956


Walk-Through

Visited Array
F C A
A B
B C √
D
H D √ H
E √
G E
G
F
E
G √
D
H √
The order nodes are visited:
Nodes A and B are adjacent to F.
D, C, E, G, H Decide to visit A next.
65

BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956


Walk-Through

Visited Array
F C A √
A B
B C √ A
D
H D √ H
E √
G E
G
F
E
G √
D
H √
The order nodes are visited:
Visit A
D, C, E, G, H, A
66

BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956


Walk-Through

Visited Array
F C A √
A B
B C √ A
D
H D √ H
E √
G E
G
F
E
G √
D
H √
The order nodes are visited:
Only Node B is adjacent to A.
D, C, E, G, H, A Decide to visit B next.
67

BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956


Walk Through

Visited Array
F C A √
A B √ B
B C √ A
D
H D √ H
E √
G E
G
F
E
G √
D
H √
The order nodes are visited:
Visit B
D, C, E, G, H, A, B
68

BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956


Walk-Through

Visited Array
F C A √
A B √
B C √ A
D
H D √ H
E √
G E
G
F
E
G √
D
H √
The order nodes are visited:
No unvisited nodes adjacent to
D, C, E, G, H, A, B B. Backtrack (pop the stack).
69

BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956


Walk Through

Visited Array
F C A √
A B √
B C √
D
H D √ H
E √
G E
G
F
E
G √
D
H √
The order nodes are visited:
No unvisited nodes adjacent to
D, C, E, G, H, A, B A. Backtrack (pop the stack).
70

BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956


Walk Through

Visited Array
F C A √
A B √
B C √
D
H D √
E √
G E
G
F
E
G √
D
H √
The order nodes are visited:
No unvisited nodes adjacent to
D, C, E, G, H, A, B H. Backtrack (pop the
stack). 71

BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956


Walk-Through

Visited Array
F C A √
A B √
B C √
D
H D √
E √
G E
F
E
G √
D
H √
The order nodes are visited:
No unvisited nodes adjacent to
D, C, E, G, H, A, B G. Backtrack (pop the
stack). 72

BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956


Walk-Through

Visited Array
F C A √
A B √
B C √
D
H D √
E √
G E
F
G √
D
H √
The order nodes are visited:
No unvisited nodes adjacent to
D, C, E, G, H, A, B E. Backtrack (pop the stack).
73

BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956


Walk-Through

Visited Array
F C A √
A B √
B C √
D
H D √
E √
G E
F
G √
D
H √
The order nodes are visited:
F is unvisited and is adjacent to
D, C, E, G, H, A, B D. Decide to visit F next.
74

BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956


Walk-Through

Visited Array
F C A √
A B √
B C √
D
H D √
E √
G E
F √
F
G √
D
H √
The order nodes are visited:
Visit F
D, C, E, G, H, A, B, F
75

BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956


Walk-Through

F C A √
A B √
B C √
D
H D √
E √
G E
F √
G √
D
H √
The order nodes are visited:
No unvisited nodes adjacent to
D, C, E, G, H, A, B, F F. Backtrack.
76

BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956


Walk-Through

Visited Array
F C A √
A B √
B C √
D
H D √
E √
G E
F √
G √
H √
The order nodes are visited:
No unvisited nodes adjacent to
D, C, E, G, H, A, B, F D. Backtrack.
77

BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956


Walk-Through

Visited Array
F C A √
A B √
B C √
D
H D √
E √
G E
F √
G √
H √
The order nodes are visited:
Stack is empty. Depth-first
D, C, E, G, H, A, B, F traversal is done.
78

BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956


Thank You!!

79

BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956

You might also like