Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "CHAPTER 13 GRAPH ALGORITHMS ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA."— Presentation transcript:

1 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 802 1743 1843 1233 337

2 MINIMUM SPANNING TREES JFK BOS MIA ORD LAX DFW SFO BWI PVD 867 2704 187 1258 849 144 740 1391 184 946 1090 1121 2342 1846 621 802 1464 1235 337

3 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 849 802 1387 1743 1843 1099 1120 1233 337 2555 142 1205

4 MINIMUM SPANNING TREE ORD PIT ATL STL DEN DFW DCA 10 1 9 8 6 3 2 5 7 4

5 EXERCISE MST Show an MST of the following graph. ORD PVD MIA DFW SFO LAX LGA HNL 849 802 1387 1743 1843 1099 1120 1233 337 2555 142 1205

6 CYCLE PROPERTY 8 4 2 3 6 7 7 9 8 e C f 8 4 2 3 6 7 7 9 8 C e f Replacing f with e yields a better spanning tree

7 PARTITION PROPERTY UV 7 4 2 8 5 7 3 9 8 e f Replacing f with e yields another MST 7 4 2 8 5 7 3 9 8 e f UV

8 PRIM-JARNIK’S ALGORITHM

9

10 EXAMPLE B D C A F E 7 4 2 8 5 7 3 9 8 0 7 2 8   B D C A F E 7 4 2 8 5 7 3 9 8 0 7 2 5  7 B D C A F E 7 4 2 8 5 7 3 9 8 0 7 2 5  7 B D C A F E 7 4 2 8 5 7 3 9 8 0 7 2 5 4 7

11 B D C A F E 7 4 2 8 5 7 3 9 8 0 3 2 5 4 7 B D C A F E 7 4 2 8 5 7 3 9 8 0 3 2 5 4 7

12 EXERCISE PRIM’S MST ALGORITHM Show how Prim’s 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 849 802 1387 1743 1843 1099 1120 1233 337 2555 142 1205

13 ANALYSIS

14 KRUSKAL’S 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

15 DATA STRUCTURE FOR KRUSKAL’S ALGORITHM

16 REPRESENTATION OF A PARTITION

17 ANALYSIS

18 EXAMPLE

19

20

21

22

23

24

25

26

27

28

29

30

31

32 EXERCISE KRUSKAL’S MST ALGORITHM Show how Kruskal’s 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 849 802 1387 1743 1843 1099 1120 1233 337 2555 142 1205

33 SHORTEST PATHS C B A E D F 0 328 58 4 8 71 25 2 39

34 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 849 802 1387 1743 1843 1099 1120 1233 337 2555 142 1205

35 SHORTEST PATH PROBLEM ORD PVD MIA DFW SFO LAX LGA HNL 849 802 1387 1743 1843 1099 1120 1233 337 2555 142 1205

36 SHORTEST PATH PROBLEM ORD PVD MIA DFW SFO LAX LGA HNL 849 -802 1387 -1743 1099 1120 -1233 2555 142 1205

37 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 849 802 1387 1743 1843 1099 1120 1233 337 2555 142 1205

38 DIJKSTRA’S ALGORITHM

39 EDGE RELAXATION D[z]  75 D[u]  50 10 z s u D[z]  60 D[u]  50 10 z s u e e y y D[y]  20 55 D[y] = 20 55

40 EXAMPLE CB A E D F 0 42 8  4 8 71 25 2 39 C B A E D F 0 3 28 511 4 8 71 25 2 39 C B A E D F 0 328 5 8 4 8 71 25 2 39 C B A E D F 0 32 7 58 4 8 71 25 2 39 55 1.Pull in one of the vertices with red labels 2.The relaxation of edges updates the labels of LARGER font size

41 EXAMPLE CB A E D F 0 327 58 4 8 71 25 2 39 CB A E D F 0 327 58 4 8 71 25 2 39

42 EXERCISE DIJKSTRA’S ALGORITHM ORD PVD MIA DFW SFO LAX LGA HNL 849 802 1387 1743 1843 1099 1120 1233 337 2555 142 1205

43 DIJKSTRA’S ALGORITHM

44 ANALYSIS

45 EXTENSION Using the template method pattern, we can extend Dijkstra’s 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

46 WHY DIJKSTRA’S ALGORITHM WORKS CB s E D F 0 327 5 8 4 8 71 25 2 39

47 WHY IT DOESN’T WORK FOR NEGATIVE-WEIGHT EDGES Dijkstra’s 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 0 42 8  4 8 7-3 25 2 39 C B A E D F 0 0 28 511 4 8 7-3 25 2 39

48 BELLMAN-FORD ALGORITHM

49 BELLMAN-FORD EXAMPLE 8 -2  0    4 8 71 5 39 0  4  4 8 71 5 39 5 0 9 4 8 71 -25 39 1 5 0 1 4 4 8 71 -25 39 First round Second round Third round

50 EXERCISE BELLMAN-FORD’S ALGORITHM Show how Bellman-Ford’s 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    4 8 71 -55 -2 39

51 ALL-PAIRS SHORTEST PATHS k j i


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

Similar presentations


Ads by Google