0% found this document useful (0 votes)
4 views

Lecture-39-Traversing a Graph

Traversing a graph
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Lecture-39-Traversing a Graph

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

Extra Section

Traversing a Graphs
Section Summary

 Breadth-First Search (BFS)


 Depth-First Search (DFS)
Traversing A Graph

 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

Algorithm BFS(G) this algorithm executes a BFS on a graph G


beginning at a starting node a
1. Initialize all nodes to the ready state. ( STATUS =1 )
2. Put the starting node a into QUEUE and change its
status to the waiting state (STATUS=2)
3. Repeat steps 4 and 5 until QUEUE is empty:
4. Remove the front node N of QUEUE, process N and change
the status of N to the processed state (STATUS = 3)
5. Add to the rear of QUEUE all the neighbors of N that are
in the steady state(STATUS=1), and change their status to
the waiting status(STATUS = 2).
[ End of step 3 loop.]
6. Exit.
Demo of BFS…
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

enqueue source node front A


FIFO Queue
Breadth First Search

-
A B C D

E F G H

dequeue next vertex front A


FIFO Queue
Breadth First Search

-
A B C D

E F G H

visit neighbors of A front

FIFO Queue
Breadth First Search

-
A B C D

E F G H

visit neighbors of A front

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

visit neighbors of A front B


FIFO Queue
Breadth First Search

- 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

finished with A front B I


FIFO Queue
Breadth First Search

- A
A B C D

E F G H

dequeue next vertex front B I


FIFO Queue
Breadth First Search

- A
A B C D

E F G H

visit neighbors of B front I


FIFO Queue
Breadth First Search

- A
A B C D

E F G H

visit neighbors of B front I


FIFO Queue
Breadth First Search

- 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

visit neighbors of B front I F


FIFO Queue
Breadth First Search

- A
A B C D

E F G H

A already discovered front I F


FIFO Queue
Breadth First Search

- A
A B C D

E F G H

finished with B front I F


FIFO Queue
Breadth First Search

- A
A B C D

E F G H

dequeue next vertex front I F


FIFO Queue
Breadth First Search

- A
A B C D

E F G H

visit neighbors of I front F


FIFO Queue
Breadth First Search

- A
A B C D

E F G H

visit neighbors of I front F


FIFO Queue
Breadth First Search

- A
A B C D

E F G H

A already discovered front F


FIFO Queue
Breadth First Search

- A
A B C D

E F G H

visit neighbors of I front F


FIFO Queue
Breadth First Search

- 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

visit neighbors of I front F E


FIFO Queue
Breadth First Search

- A
A B C D

E F G H

I B

F already discovered front F E


FIFO Queue
Breadth First Search

- 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

dequeue next vertex front F E


FIFO Queue
Breadth First Search

- A
A B C D

E F G H

I B

visit neighbors of F front E


FIFO Queue
Breadth First Search

- 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

dequeue next vertex front E G


FIFO Queue
Breadth First Search

- A
A B C D

E F G H

I B F

visit neighbors of E front G


FIFO Queue
Breadth First Search

- 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

dequeue next vertex front G


FIFO Queue
Breadth First Search

- A
A B C D

E F G H

I B F

visit neighbors of G front

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

visit neighbors of G front C


FIFO Queue
Breadth First Search

- 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

dequeue next vertex front C H


FIFO Queue
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
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
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

get next vertex front H D


FIFO Queue
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
Breadth First Search

- 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

dequeue next vertex front D


FIFO Queue
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
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

dequeue next vertex front

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

Algorithm DFS(G) this algorithm executes a BFS on a graph G


beginning at a starting node a
1. Initialize all nodes to the ready state. ( STATUS =1 )
2. Put the starting node a into STACK and change its
status to the waiting state (STATUS=2)
3. Repeat steps 4 and 5 until QUEUE is empty:
4. Pop the Top node N of QUEUE, process N and change the
status of N to the processed state (STATUS = 3)
5. Push onto STACK all the neighbors of N that are in the
steady state(STATUS=1), and change their status to the
waiting status(STATUS = 2).
[ End of step 3 loop.]
6. Exit.
Demo of DFS…
Demo of DFS

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

h: Neighbor of a which are in ready state

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

Pop: b and process it

STACK:
Demo of DFS

b
c d

e f

Push: Neighbor of b

STACK:
Demo of DFS

b
c d

e f

Pop: Empty Stack


STOP
STACK:
Flow Chart of DFS…
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…
Query???

1  2  3  4.... xy ( x  y ) ?

  1
 x 1 x ?  x 1
?
x

 x (  | x ) ? xy ( x  y ) ?

 1
1  2  3  4.... ?
1  1  1  1  1......... ?
 x 1
x
?

You might also like