CSC 172 DATA STRUCTURES Traversing graphs DepthFirst Search

  • Slides: 22
Download presentation
CSC 172 DATA STRUCTURES

CSC 172 DATA STRUCTURES

Traversing graphs Depth-First Search like a post-order traversal of a tree Breath-First Search Less

Traversing graphs Depth-First Search like a post-order traversal of a tree Breath-First Search Less like tree traversal

Traversing graphs Depth-First Search like a post-order traversal of a tree Breath-First Search Less

Traversing graphs Depth-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)

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

Example A B C D E F G H I J K L M N O P

Example 0 A B C D E F G H I J K L

Example 0 A B C D E F G H I J K L M N O P

Example 0 A B C D E F G H I J K L

Example 0 A B C D E F G H I J K L M N O P

Example 0 1 A B C D E F G H I J K

Example 0 1 A B C D E F G H I J K L M N O P

Example 0 1 A B C D E F G H I J K

Example 0 1 A B C D E F G H I J K L M N O P

Example 0 1 2 A B C D E F G H I J

Example 0 1 2 A B C D E F G H I J K L M N O P

Example 0 1 2 A B C D E F G H I J

Example 0 1 2 A B C D E F G H I J K L M N O P

Example 0 1 2 3 A B C D E F G H I

Example 0 1 2 3 A B C D E F G H I J K L M N O P

Example 0 1 2 3 A B C D E F G H I

Example 0 1 2 3 A B C D E F G H I J K L M N O P

Example 0 1 2 3 A B C D E F G H I

Example 0 1 2 3 A B C D E F G H I J K M N O 4 L P

Example 0 1 2 3 A B C D E F G H I

Example 0 1 2 3 A B C D E F G H I J K M N O 4 L P

Example 0 1 2 3 A B C D E F G H I

Example 0 1 2 3 A B C D E F G H I J K M N O 4 L P

Example 0 1 2 3 A B C D E F G H I

Example 0 1 2 3 A B C D E F G H I J K M N O 4 L P

Example 0 1 2 3 A B C D E F G H I

Example 0 1 2 3 A B C D E F G H I J K M N O L 4 5 P

Example 0 1 2 3 A B C D E F G H I

Example 0 1 2 3 A B C D E F G H I J K M N O L 4 5 P

BFS Tree 0 1 2 3 A B C D E F G H

BFS Tree 0 1 2 3 A B C D E F G H I J K M N O L 4 5 P

Dijkstra's Algorithm // for each Vertex v { v. dist = infinity ; v.

Dijkstra's Algorithm // for each Vertex v { v. dist = infinity ; v. known = false ; }

Dijkstra's Algorithm while (there is an unknown vertex) { v = del. Min() ;

Dijkstra's Algorithm while (there is an unknown vertex) { v = del. Min() ; // get the next one off the queue v. known = true ; for each Vertex w adjacent to v { if (!w. known) { if (v. dist + dist_v 2 w < w. dist){ w. dist = vdist + dist_v 2 w ; enqueue(w)