breadth first search

18
Breadth First- Search

Upload: mutasadiq-iqbal

Post on 21-Jun-2015

1.287 views

Category:

Education


2 download

TRANSCRIPT

Page 1: Breadth first search

Breadth First-Search

Page 2: Breadth first search

Lecturer: Miss Sana Rizwan

Page 3: Breadth first search

Muhammad Arslan KhanDDP-FA12-BSE-072

Mutasadiq IqbalDDP-FA12-BSE-072

Section: C

Presented By

Page 4: Breadth first search

Breadth-first search (BFS) start from the root node and explores all the neighbouring nodes. Then for each of those nearest nodes, it explores their neighbour nodes, and so on, until it finds the goal.

Definition

Page 5: Breadth first search

Breadth-first search can be used to solve many problems in graph theory, for example:

• Finding all nodes within one connected components.• Finding the shortest path between two nodes u and v .• Testing a graph for bipartiteness.

Applications

Page 6: Breadth first search

•  Undiscovered--white colour

• Discovered but not fully explored-grey colour

• Fully explored.-black colour

States

Page 7: Breadth first search

• Pick a source vertex S to start.• Find (or discover) the vertices that are adjacent to S.• Pick each child of S in turn and discover their vertices adjacent to that

child.• Done when all children have been discovered and examined.• The breadth-first search uses a FIFO queue.

How It Works?

Page 8: Breadth first search

Step # 1

Page 9: Breadth first search

Step # 2

Page 10: Breadth first search

Step # 3

Page 11: Breadth first search

Step # 4

Page 12: Breadth first search

Step # 5

Page 13: Breadth first search

Step # 6

Page 14: Breadth first search

Step # 7

Page 15: Breadth first search

Step # 8

Page 16: Breadth first search

Step # 9

Page 17: Breadth first search

Algorithm

Algorithm BFS(s )1. color all the vertices white2. initialize an empty queue Q3. for each neighbor v of s4. insert v in Q ; color [v ] = grey5. output s ; color [s ] = black6. while Q is not empty7. u = top of Q ; remove u from Q8. for each neighbor v of u9. if color [v ] = white10. insert v in Q ; color [v ] = grey11. output u; color [u] = black

Page 18: Breadth first search