ECE 242 Data Structures Lecture 29 Graph Traversal November 23, 2009 ECE242 L29: Graph Traversal Overview ° Problem: How can we efficiently visit all vertices in the graph? ° Depth first search • Quickly follow paths between vertices ° Breadth first search • Visit all neighbors of a vertex ° Compact algorithms available to perform traversals • Note Galaxy example and cheap flights example ° Useful for many applications November 23, 2009 ECE242 L29: Graph Traversal
Graph Traversal ° List out all cities that United Airline can reach from Hartford Airport CHI Hartford SF NYC LA W. DC November 23, 2009 ECE242 L29: Graph Traversal Graph Traversal ° From vertex u, list out all vertices that u can reach via graph G ° Set of nodes to expand ° Each node has a flag to indicate visited or not ° Keep visiting until all vertices are visited ° Some nodes may not be connected November 23, 2009 ECE242 L29: Graph Traversal
Demos For Traversal Algorithm ° Step 1: { Hartford } • find neighbors of Hartford • { Hartford, NYC, CHI } CHI Hartford SF NYC LA W. DC November 23, 2009 ECE242 L29: Graph Traversal Demos For Traversal Algorithm ° Step 2: { Hartford, NYC, CHI } • find neighbors of NYC, CHI • { Hartford, NYC, CHI, LA, SF } CHI Hartford SF NYC LA W. DC November 23, 2009 ECE242 L29: Graph Traversal
Demos For Traversal Algorithm ° Step 3: {Hartford, NYC, CHI, LA, SF } • find neighbors of LA, SF • no other new neighbors CHI Hartford SF NYC LA W. DC November 23, 2009 ECE242 L29: Graph Traversal Demos For Traversal Algorithm ° Finally we get all cities that United Airline can reach from Hartford Airport • {Hartford, NYC, CHI, LA, SF } CHI Hartford SF NYC LA W. DC November 23, 2009 ECE242 L29: Graph Traversal
Algorithm of Graph Traversal 1. Mark all nodes as unvisited 2. Pick a starting vertex u, add u to probing list 3. While ( probing list is not empty) { Remove a node v from probing list Mark node v as visited For each neighbor w of v, if w is unvisited, add w to the probing list } November 23, 2009 ECE242 L29: Graph Traversal Graph Traversal ° Source vertex • We must choose an arbitrary vertex to start from. ° Depth-first traversal • Follows edges until it reaches a dead end, then backtracks to the last branching point. ° Breadth-first • Visits one vertex, then its neighbors, then vertices two edges away, and so on. November 23, 2009 ECE242 L29: Graph Traversal
Graph Traversal ° Depth-first traversal Start Note that all 8 nodes are visited eventually November 23, 2009 ECE242 L29: Graph Traversal Depth First Traversal ° Probing List is implemented as stack (LIFO) ° Example • A’s neighbor: B, C, E • B’s neighbor: A, C, F A • C’s neighbor: A, B, D • D’s neighbor: E, C, F • E’s neighbor: A, D B E C • F’s neighbor: B, D • start from vertex A D F November 23, 2009 ECE242 L29: Graph Traversal
Depth First Traversal (Cont) – A’s neighbor: B C E A – B’s neighbor: A C F – C’s neighbor: A B D – D’s neighbor: E C F B E C – E’s neighbor: A D – F’s neighbor: B D D F ° Initial State • Visited Vertices { } • Probing Vertices { A } • Unvisited Vertices { A, B, C, D, E, F } stack A November 23, 2009 ECE242 L29: Graph Traversal Depth First Traversal (Cont) – A’s neighbor: B C E A – B’s neighbor: A C F – C’s neighbor: A B D – D’s neighbor: E C F B E C – E’s neighbor: A D – F’s neighbor: B D D F ° Peek a vertex from stack, it is A, mark it as visited ° Find A’s first unvisited neighbor, push it into stack • Visited Vertices { A } • Probing vertices { A, B } B • Unvisited Vertices { B, C, D, E, F } A A stack November 23, 2009 ECE242 L29: Graph Traversal
Depth First Traversal (Cont) – A’s neighbor: B C E A – B’s neighbor: A C F – C’s neighbor: A B D – D’s neighbor: E C F B E C – E’s neighbor: A D – F’s neighbor: B D D F ° Peek a vertex from stack, it is B, mark it as visited ° Find B’s first unvisited neighbor, push it in stack C • Visited Vertices { A, B } • Probing Vertices { A, B, C } B B • Unvisited Vertices { C, D, E, F } A A stack November 23, 2009 ECE242 L29: Graph Traversal Depth First Traversal (Cont) – A’s neighbor: B C E A – B’s neighbor: A C F – C’s neighbor: A B D – D’s neighbor: E C F B E C – E’s neighbor: A D – F’s neighbor: B D D F ° Peek a vertex from stack, it is C, mark it as visited ° Find C’s first unvisited neighbor, push it in stack D • Visited Vertices { A, B, C } C C • Probing Vertices { A, B, C, D } B B • Unvisited Vertices { D, E, F } A A stack November 23, 2009 ECE242 L29: Graph Traversal
Depth First Traversal (Cont) – A’s neighbor: B C E A – B’s neighbor: A C F – C’s neighbor: A B D – D’s neighbor: E C F B E C – E’s neighbor: A D – F’s neighbor: B D D F ° Peek a vertex from stack, it is D, mark it as visited E ° Find D’s first unvisited neighbor, push it in stack D D C C • Visited Vertices { A, B, C, D } • Probing Vertices { A, B, C, D, E } B B • Unvisited Vertices { E, F } A A stack November 23, 2009 ECE242 L29: Graph Traversal Depth First Traversal (Cont) – A’s neighbor: B C E A – B’s neighbor: A C F – C’s neighbor: A B D – D’s neighbor: E C F B E C – E’s neighbor: A D – F’s neighbor: B D D F ° Peek a vertex from stack, it is E, mark it as visited E ° Find E’s first unvisited neighbor, no vertex found, Pop E D D C C • Visited Vertices { A, B, C, D, E } • Probing Vertices { A, B, C, D } B B • Unvisited Vertices { F } A A stack November 23, 2009 ECE242 L29: Graph Traversal
Depth First Traversal (Cont) – A’s neighbor: B C E A – B’s neighbor: A C F – C’s neighbor: A B D – D’s neighbor: E C F B E C – E’s neighbor: A D – F’s neighbor: B D D F ° Peek a vertex from stack, it is D, mark it as visited F ° Find D’s first unvisited neighbor, push it in stack D D C C • Visited Vertices { A, B, C, D, E } • Probing Vertices { A, B, C, D, F} B B • Unvisited Vertices { F } A A stack November 23, 2009 ECE242 L29: Graph Traversal Depth First Traversal (Cont) – A’s neighbor: B C E A – B’s neighbor: A C F – C’s neighbor: A B D – D’s neighbor: E C F B E C – E’s neighbor: A D – F’s neighbor: B D D F ° Peek a vertex from stack, it is F, mark it as visited F ° Find F’s first unvisited neighbor, no vertex found, Pop F D D C C • Visited Vertices { A, B, C, D, E, F } • Probing Vertices { A, B, C, D} B B • Unvisited Vertices { } A A stack November 23, 2009 ECE242 L29: Graph Traversal
Depth First Traversal (Cont) – A’s neighbor: B C E A – B’s neighbor: A C F – C’s neighbor: A B D – D’s neighbor: E C F B E C – E’s neighbor: A D – F’s neighbor: B D D F ° Peek a vertex from stack, it is D, mark it as visited ° Find D’s first unvisited neighbor, no vertex found, Pop D D C C • Visited Vertices { A, B, C, D, E, F } • Probing Vertices { A, B, C } B B • Unvisited Vertices { } A A stack November 23, 2009 ECE242 L29: Graph Traversal Depth First Traversal (Cont) – A’s neighbor: B C E A – B’s neighbor: A C F – C’s neighbor: A B D – D’s neighbor: E C F B E C – E’s neighbor: A D – F’s neighbor: B D D F ° Peek a vertex from stack, it is C, mark it as visited ° Find C’s first unvisited neighbor, no vertex found, Pop C C • Visited Vertices { A, B, C, D, E, F } • Probing Vertices { A, B } B B • Unvisited Vertices { } A A stack November 23, 2009 ECE242 L29: Graph Traversal
Depth First Traversal (Cont) – A’s neighbor: B C E A – B’s neighbor: A C F – C’s neighbor: A B D – D’s neighbor: E C F B E C – E’s neighbor: A D – F’s neighbor: B D D F ° Peek a vertex from stack, it is B, mark it as visited ° Find B’s first unvisited neighbor, no vertex found, Pop B • Visited Vertices { A, B, C, D, E, F } • Probing Vertices { A } B • Unvisited Vertices { } A A stack November 23, 2009 ECE242 L29: Graph Traversal Depth First Traversal (Cont) – A’s neighbor: B C E A – B’s neighbor: A C F – C’s neighbor: A B D – D’s neighbor: E C F B E C – E’s neighbor: A D – F’s neighbor: B D D F ° Peek a vertex from stack, it is A, mark it as visited ° Find A’s first unvisited neighbor, no vertex found, Pop A • Visited Vertices { A, B, C, D, E, F } • Probing Vertices { } • Unvisited Vertices { } A stack November 23, 2009 ECE242 L29: Graph Traversal
Depth First Traversal (Cont) – A’s neighbor: B C E A – B’s neighbor: A C F – C’s neighbor: A B D – D’s neighbor: E C F B E C – E’s neighbor: A D – F’s neighbor: B D D F ° Now probing list is empty ° End of Depth First Traversal • Visited Vertices { A, B, C, D, E, F } • Probing Vertices { } • Unvisited Vertices { } stack November 23, 2009 ECE242 L29: Graph Traversal Graph Traversal ° Breadth-first traversal Start Visit ALL neighbors at each step November 23, 2009 ECE242 L29: Graph Traversal
Recommend
More recommend