Sie sind auf Seite 1von 12

Traversing a graph

During execution of the algorithm, each node N of Graph G

will be in one of 3 states:

STATUS=1(Ready State): The initial state of Node N STATUS=2(Waiting State): Node N is on queue or stack,

waiting to be processed.
STATUS=3(Processed State): The node N has been

processed.
1 http://www.chitkara.edu.in

BREADTH-FIRST SEARCH
Uses queue to hold nodes that are waiting to be

processed
STATUS field tells the current status of each node

First we examine the starting node A, then neighbors

of A, then we examine all the neighbors of neighbors of A and so on

http://www.chitkara.edu.in

Algorithm for BFS


Consider a Graph G beginning at a starting node A 1.Initialize all nodes to ready state(STATUS=1)

2.Put the starting node A in QUEUE and change its status to waiting
state(STATUS=2) 3.Repeat steps 4 and 5 until QUEUE is empty 4. Remove front node N of QUEUE. Process N and change the status of N to processed state(STATUS=3) 5. Add to rear of QUEUE neighbors of N that are in steady state(STATUS =1) and change their status to waiting state(STATUS=2) [End of step 3 loop] 6. Exit
3 http://www.chitkara.edu.in

Example
To find minimum path: a.) Initially add A to queue and NULL to ORIG

FRONT= 1 REAR=1

QUEUE:A ORIG:

b.) Remove front element(A) by setting FRONT:=FRONT+1 and add neighbors of A to the queue as follows:

FRONT= 2
REAR=4
4

QUEUE: A,F,C,B
ORIG: ,A,A,A

http://www.chitkara.edu.in

c.) Remove F from queue by setting FRONT:=FRONT+1 and add neighbors of F as: FRONT= 3 QUEUE:A,F,C,B,D

REAR=5

ORIG: ,A,A,A,F

d.) Remove C and add neighbors of C as:


FRONT= 4 REAR=5 QUEUE:A,F,C,B,D ORIG: ,A,A,A,F

Here, F is not added because F is already added to QUEUE


5 http://www.chitkara.edu.in

e.) Remove B and add neighbors of B


FRONT= 5 REAR=6 QUEUE: A,F,C,B,D,G ORIG: ,A,A,A,F,B

(C is already there)

f.) Remove D and add its neighbors

FRONT= 6
REAR=6

QUEUE: A,F,C,B,D,G
ORIG: ,A,A,A,F,B

g.) Remove G and add its neighbors

FRONT= 7
REAR=7
6 http://www.chitkara.edu.in

QUEUE: A,F,C,B,D,G,E
ORIG: ,A,A,A,F,B,G

h.) Remove E from queue and add its neighbors

FRONT= 8

QUEUE: A,F,C,B,D,G,E,J

REAR=8

ORIG: ,A,A,A,F,B,G,E

Now stop as J is final destination and is added to QUEUE Find path P backtracking from J:

J
7

ishttp://www.chitkara.edu.in the required path P

DEPTH-FIRST SEARCH
Similar to BFS, but in DFS, we use stack

STATUS: used to tell current status of each node


Starting from node A, we examine each node N ,i.e., we

process neighbor of A, neighbor of neighbor of A.. And so on..


After coming to end, we backtrack on P

http://www.chitkara.edu.in

DEPTH-FIRST SEARCH Algorithm


Graph G, starting node A
1. 2.

Initialize all nodes to ready state(STATUS=1) Push Starting node A onto stack and change its status to waiting state(STATUS=2)

3. 4.

Repeat steps 4 and 5 until stack is empty Pop the top node N of STACK. Process N & change its status to processed state(STATUS=3)

5.

Push all neighbors of N that are in ready state(STATUS=1) and change their

status to waiting state(STATUS=2) [End of step 3 loop]


6.

Exit

http://www.chitkara.edu.in

Example
To find and print all nodes reachable from J:

a.) Initially, push J onto stack as follows:


STACK: J b.) Pop and print top element J and push neighbors of J(which are in ready state) into the stack as: Print J STACK: D,K

c.)Pop and print top element K, push neighbors of K Print K


10 http://www.chitkara.edu.in

STACK: D,E,G

d.) Pop and print G, push neighbors of G Print G STACK: D,E,C (E is already in stack) e.) Pop and print C, push all neighbors of C Print C STACK: D,E,F f.) Pop and print F, push all neighbors of F Print F STACK: D,E (D is already in stack) g.) Pop and print E, push the neighbors of E Print E STACK: D

(none of 3 neighbors of E is in ready state) h.) Pop and print D, push neighbors of D PRINT D
11 http://www.chitkara.edu.in

STACK:

Now stack is empty, so DFS of G starting at J is now complete

Nodes which are printed:

J,K,G,C,F,E,D

are nodes which are reachable from J

12

http://www.chitkara.edu.in

Das könnte Ihnen auch gefallen