CSC 172 DATA STRUCTURES
Traversing graphs Depth-First Search Breath-First Search like a post-order traversal of a tree Breath-First Search Less like tree traversal
Traversing graphs Depth-First Search Breath-First Search like a post-order traversal of a tree Breath-First Search Less like tree traversal Priority-First Search Good for Shortest Path Dijkstra's Algorithm
Breadth-First Search Unweighted Shortest Path Starting vertex has level 0 (anchor vertex) Visit (mark) all vertices that are only one edge away mark each vertex with its “level” One edge away from level 0 is level 1 One edge away from level 1 is level 2 Etc. . . .
Example A B C D E F G H I J K L M N O P
Example A B C D E F G H I J K L M N O P
Example A B C D E F G H I J K L M N O P
Example 1 A B C D E F G H I J K L M N O P
Example 1 A B C D E F G H I J K L M N O P
Example 2 1 A B C D E F G H I J K L M N O P
Example 2 1 A B C D E F G H I J K L M N O P
Example 2 1 3 A B C D E F G H I J K L M N O P
Example 2 1 3 A B C D E F G H I J K L M N O P
Example 2 1 3 A B C D E F G H I J K 4 L M N O P
Example 2 1 3 A B C D E F G H I J K 4 L M N O P
Example 2 1 3 A B C D E F G H I J K 4 L M N O P
Example 2 1 3 A B C D E F G H I J K 4 L M N O P
Example 2 1 3 A B C D E F G H I J K 4 L 5 M N O P
Example 2 1 3 A B C D E F G H I J K 4 L 5 M N O P
BFS Tree 2 1 3 A B C D E F G H I J K 4 L 5 M N O P
Dijkstra's Algorithm // for each Vertex v { v.dist = infinity ; v.known = false ; }
Dijkstra's Algorithm while (there is an unknown vertex) { v = delMin() ; // get the next one off the queue v.known = true ; for each Vertex w adjacent to v { if (!w.known) { if (v.dist + dist_v2w < w.dist){ w.dist = vdist + dist_v2w ; enqueue(w) ]