Breadth first search.
-
Upload
mutasadiq-iqbal -
Category
Education
-
view
1.283 -
download
2
Transcript of Breadth first search.
Breadth First-Search
Lecturer: Miss Sana Rizwan
Muhammad Arslan KhanDDP-FA12-BSE-072
Mutasadiq IqbalDDP-FA12-BSE-072
Section: C
Presented By
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
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
• Undiscovered--white colour
• Discovered but not fully explored-grey colour
• Fully explored.-black colour
States
• 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?
Step # 1
Step # 2
Step # 3
Step # 4
Step # 5
Step # 6
Step # 7
Step # 8
Step # 9
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