Distance0 distance1 distance2 distance3

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: order Finds connected component Finds shortest number of edges between start node and all other nodes Fast/Efficient Example Run Example Run Breadth­First Search from vertex 8. Example Run Example Run Breadth­First Search from vertex 8. Distance 0 Distance 1 Distance 2 Distance 3 Breadth First Search Breadth First Search BFS intuition. Explore outward from s in all possible directions, adding nodes one "layer" at a time. s L1 L2 L n­1 BFS algorithm. – L0 = { s }. – L1 = all neighbors of L0. – L2 = all nodes that do not belong to L0 or L1, and that have an edge to a node in L1. – Li+1 = all nodes that do not belong to an earlier layer, and that have an edge to a node in Li. FACT: For each i, Li consists of all nodes at distance exactly i from s. There is a path from s to t only if t appears in some layer. “Computer’s view” 1: 2,3 2: 1,3,4,5 3: 1,2,5,7,8 4: 2,5 5: 2,3,4,6 6: 5 7: 3,8 8: 3,7 9: 10 10: 9 11: 12 12: 11,13 13: 12 “Computer’s view” Mark 8 as “visited”; put 3 and 7 on the list V={8} L = {3,7} Take next thing on list (3), move to V, and put it’s neighbors on list if not in V or L yet. V = {8, 3} L = {7, 1, 2, 5} Take next thing on list (7), move to V, and put it’s neighbors on list if not in V or L yet V = {8, 3 ,7} L = {1, 2, 5} 1: 2,3 2: 1,3,4,5 3: 1,2,5,7,8 4: 2,5 5: 2,3,4,6 6: 5 7: 3,8 8: 3,7 9: 10 10: 9 11: 12 12: 11,13 13: 12 “Computer’s view” V = {8, 3 ,7} L = {1, 2, 5} Mark 1 as “visited”; no new neighbors V={8, 3, 7, 1} L = {2, 5} Take next thing on list (2), move to V, and put it’s neighbors on list if not in V or L yet. V = {8, 3, 7, 1, 2} L = {5, 4} Take next thing on list (5), move to V, and put it’s neighbors on list if not in V or L yet V = {8, 3, 7, 1, 2, 5} L = {4, 6} 1: 2,3 2: 1,3,4,5 3: 1,2,5,7,8 4: 2,5 5: 2,3,4,6 6: 5 7: 3,8 8: 3,7 9: 10 10: 9 11: 12 12: 11,13 13: 12 “Computer...
View Full Document

This document was uploaded on 04/02/2014.

Ask a homework question - tutors are online