Breadth first search.

2,581 views
2,312 views

Published on

Published in: Education
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,581
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
143
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Breadth first search.

  1. 1. Breadth First-Search
  2. 2. Lecturer: Miss Sana Rizwan
  3. 3. Presented By
  4. 4. Definition 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.
  5. 5. Applications 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.
  6. 6. States • Undiscovered--white colour • Discovered but not fully explored-grey colour • Fully explored.-black colour
  7. 7. How It Works? • • • • • 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.
  8. 8. Step # 1
  9. 9. Step # 2
  10. 10. Step # 3
  11. 11. Step # 4
  12. 12. Step # 5
  13. 13. Step # 6
  14. 14. Step # 7
  15. 15. Step # 8
  16. 16. Step # 9
  17. 17. Algorithm Algorithm BFS(s ) 1. color all the vertices white 2. initialize an empty queue Q 3. for each neighbor v of s 4. insert v in Q ; color [v ] = grey 5. output s ; color [s ] = black 6. while Q is not empty 7. u = top of Q ; remove u from Q 8. for each neighbor v of u 9. if color [v ] = white 10. insert v in Q ; color [v ] = grey 11. output u; color [u] = black

×