Lecture-39-Traversing a Graph
Lecture-39-Traversing a Graph
Traversing a Graphs
Section Summary
Each node N of G will be one of the three state, called the state of the
N, as follows:
• STATUS=1: (Ready state) The initial state of the node N.
• STATUS=2: (Waiting state) The node N is on the queue or stack,
waiting to be processed
• STATUS=3: (Processed state) The node N has been processed.
BFS – Queue
DFS - Stack
Basic Concept of BFS and DFS
BFS – a b c d g e f
QUEUE
Basic Concept of BFS and DFS
DFS – a d f e g b c
Stack
Breadth-First Search
-
A B C D
E F G H
front
FIFO Queue
Breadth First Search
-
A B C D
E F G H
-
A B C D
E F G H
-
A B C D
E F G H
FIFO Queue
Breadth First Search
-
A B C D
E F G H
FIFO Queue
Breadth First Search
- A
A B C D
E F G H
B discovered front B
FIFO Queue
Breadth First Search
- A
A B C D
E F G H
- A
A B C D
E F G H
I discovered front B I
FIFO Queue
Breadth First Search
- A
A B C D
E F G H
- A
A B C D
E F G H
- A
A B C D
E F G H
- A
A B C D
E F G H
- A
A B C D
E F G H
F discovered front I F
FIFO Queue
Breadth First Search
- A
A B C D
E F G H
- A
A B C D
E F G H
- A
A B C D
E F G H
- A
A B C D
E F G H
- A
A B C D
E F G H
- A
A B C D
E F G H
- A
A B C D
E F G H
- A
A B C D
E F G H
- A
A B C D
E F G H
I B
E discovered front F E
FIFO Queue
Breadth First Search
- A
A B C D
E F G H
I B
- A
A B C D
E F G H
I B
- A
A B C D
E F G H
I B
I finished front F E
FIFO Queue
Breadth First Search
- A
A B C D
E F G H
I B
- A
A B C D
E F G H
I B
- A
A B C D
E F G H
I B F
G discovered front E G
FIFO Queue
Breadth First Search
- A
A B C D
E F G H
I B F
F finished front E G
FIFO Queue
Breadth First Search
- A
A B C D
E F G H
I B F
- A
A B C D
E F G H
I B F
- A
A B C D
E F G H
I B F
E finished front G
FIFO Queue
Breadth First Search
- A
A B C D
E F G H
I B F
- A
A B C D
E F G H
I B F
FIFO Queue
Breadth First Search
- A G
A B C D
E F G H
I B F
C discovered front C
FIFO Queue
Breadth First Search
- A G
A B C D
E F G H
I B F
- A G
A B C D
E F G H
I B F G
H discovered front C H
FIFO Queue
Breadth First Search
- A G
A B C D
E F G H
I B F G
G finished front C H
FIFO Queue
Breadth First Search
- A G
A B C D
E F G H
I B F G
- A G
A B C D
E F G H
I B F G
- A G C
A B C D
E F G H
I B F G
D discovered front H D
FIFO Queue
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
Breadth First Search
- A G C
A B C D
E F G H
I B F G
- A G C
A B C D
E F G H
I B F G
- A G C
A B C D
E F G H
I B F G
finished H front D
FIFO Queue
Breadth First Search
- A G C
A B C D
E F G H
I B F G
- A G C
A B C D
E F G H
I B F G
FIFO Queue
Breadth First Search
- A G C
A B C D
E F G H
I B F G
D finished front
FIFO Queue
Breadth First Search
- A G C
A B C D
E F G H
I B F G
FIFO Queue
Breadth First Search
- A G C
A B C D
E F G H
I B F G
STOP front
FIFO Queue
Flow Chart of BFS…
Flow Chart of BFS…
Flow Chart of BFS…
Flow Chart of BFS…
Flow Chart of BFS…
Flow Chart of BFS…
Flow Chart of BFS…
Flow Chart of BFS…
Depth-First Search
b
c d
e f
START ALGORITHM
STACK:
Demo of DFS
b
c d
e f
rting Node a
sh: a and it is now waiting state
STACK: a
Demo of DFS
b
c d
e f
Pop: a
Process a STACK:
Demo of DFS
b
c d
e f
STACK: bcd
Demo of DFS
b
c d
e f
Pop: d
Process d
STACK: bc
Demo of DFS
b
c d
e f
Push: Neighbor of d
STACK: bcf
Demo of DFS
b
c d
e f
Pop: f
Process f
STACK: bc
Demo of DFS
b
c d
e f
Push: Neighbor of f
STACK: bc
Demo of DFS
b
c d
e f
Pop: c
Process c
STACK: b
Demo of DFS
b
c d
e f
Push: Neighbor of c
STACK: be
Demo of DFS
b
c d
e f
Pop: e
Process e
STACK: b
Demo of DFS
b
c d
e f
Push: Neighbor of e
STACK: b
Demo of DFS
b
c d
e f
STACK:
Demo of DFS
b
c d
e f
Push: Neighbor of b
STACK:
Demo of DFS
b
c d
e f
1 2 3 4.... xy ( x y ) ?
1
x 1 x ? x 1
?
x
x ( | x ) ? xy ( x y ) ?
1
1 2 3 4.... ?
1 1 1 1 1......... ?
x 1
x
?