chapter 13 graph algorithms acknowledgement: these slides are adapted from slides provided with data...

Download CHAPTER 13 GRAPH ALGORITHMS ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA

If you can't read please download the document

Upload: victor-walters

Post on 19-Jan-2018

225 views

Category:

Documents


0 download

DESCRIPTION

REMINDER WEIGHTED GRAPHS In a weighted graph, each edge has an associated numerical value, called the weight of the edge Edge weights may represent, distances, costs, etc. Example: In a flight route graph, the weight of an edge represents the distance in miles between the endpoint airports ORD PVD MIA DFW SFO LAX LGA HNL

TRANSCRIPT

CHAPTER 13 GRAPH ALGORITHMS ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA AND MOUNT (WILEY 2004) AND SLIDES FROM NANCY M. AMATO ORD DFW SFO LAX MINIMUM SPANNING TREES JFK BOS MIA ORD LAX DFW SFO BWI PVD REMINDER WEIGHTED GRAPHS In a weighted graph, each edge has an associated numerical value, called the weight of the edge Edge weights may represent, distances, costs, etc. Example: In a flight route graph, the weight of an edge represents the distance in miles between the endpoint airports ORD PVD MIA DFW SFO LAX LGA HNL MINIMUM SPANNING TREE ORD PIT ATL STL DEN DFW DCA EXERCISE MST Show an MST of the following graph. ORD PVD MIA DFW SFO LAX LGA HNL CYCLE PROPERTY e C f C e f Replacing f with e yields a better spanning tree PARTITION PROPERTY UV e f Replacing f with e yields another MST e f UV PRIM-JARNIKS ALGORITHM EXAMPLE B D C A F E B D C A F E 7 B D C A F E 7 B D C A F E B D C A F E B D C A F E EXERCISE PRIMS MST ALGORITHM Show how Prims MST algorithm works on the following graph, assuming you start with SFO Show how the MST evolves in each iteration (a separate figure for each iteration). ORD PVD MIA DFW SFO LAX LGA HNL ANALYSIS KRUSKALS ALGORITHMS A priority queue stores the edges outside the cloud Key: weight Element: edge At the end of the algorithm We are left with one cloud that encompasses the MST A tree T which is our MST DATA STRUCTURE FOR KRUSKALS ALGORITHM REPRESENTATION OF A PARTITION ANALYSIS EXAMPLE EXERCISE KRUSKALS MST ALGORITHM Show how Kruskals MST algorithm works on the following graph. Show how the MST evolves in each iteration (a separate figure for each iteration). ORD PVD MIA DFW SFO LAX LGA HNL SHORTEST PATHS C B A E D F WEIGHTED GRAPHS In a weighted graph, each edge has an associated numerical value, called the weight of the edge Edge weights may represent, distances, costs, etc. Example: In a flight route graph, the weight of an edge represents the distance in miles between the endpoint airports ORD PVD MIA DFW SFO LAX LGA HNL SHORTEST PATH PROBLEM ORD PVD MIA DFW SFO LAX LGA HNL SHORTEST PATH PROBLEM ORD PVD MIA DFW SFO LAX LGA HNL SHORTEST PATH PROPERTIES Property 1: A subpath of a shortest path is itself a shortest path Property 2: There is a tree of shortest paths from a start vertex to all the other vertices Example: Tree of shortest paths from Providence ORD PVD MIA DFW SFO LAX LGA HNL DIJKSTRAS ALGORITHM EDGE RELAXATION D[z] 75 D[u] z s u D[z] 60 D[u] z s u e e y y D[y] D[y] = 20 55 EXAMPLE CB A E D F C B A E D F C B A E D F C B A E D F Pull in one of the vertices with red labels 2.The relaxation of edges updates the labels of LARGER font size EXAMPLE CB A E D F CB A E D F EXERCISE DIJKSTRAS ALGORITHM ORD PVD MIA DFW SFO LAX LGA HNL DIJKSTRAS ALGORITHM ANALYSIS EXTENSION Using the template method pattern, we can extend Dijkstras algorithm to return a tree of shortest paths from the start vertex to all other vertices We store with each vertex a third label: parent edge in the shortest path tree Parents are all initialized to null In the edge relaxation step, we update the parent label as well WHY DIJKSTRAS ALGORITHM WORKS CB s E D F WHY IT DOESNT WORK FOR NEGATIVE-WEIGHT EDGES Dijkstras algorithm is based on the greedy method. It adds vertices by increasing distance. If a node with a negative incident edge were to be added late to the cloud, it could mess up distances for vertices already in the cloud. CB A E D F C B A E D F BELLMAN-FORD ALGORITHM BELLMAN-FORD EXAMPLE 8 -2 0 4 First round Second round Third round EXERCISE BELLMAN-FORDS ALGORITHM Show how Bellman-Fords algorithm works on the following graph, assuming you start with the top node Show how the labels are updated in each iteration (a separate figure for each iteration). 0 ALL-PAIRS SHORTEST PATHS k j i