Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC 213 – Large Scale Programming. Today’s Goals  Discuss what is meant by weighted graphs  Where weights placed within Graph  How to use Graph ’s.

Similar presentations


Presentation on theme: "CSC 213 – Large Scale Programming. Today’s Goals  Discuss what is meant by weighted graphs  Where weights placed within Graph  How to use Graph ’s."— Presentation transcript:

1 CSC 213 – Large Scale Programming

2 Today’s Goals  Discuss what is meant by weighted graphs  Where weights placed within Graph  How to use Graph ’s weights to model problems  How to solve problems once Graph is set up  Learn about myth & legend of Edsgar Dijkstra  Who was he? Why should we care? How is it related?  What was his largest contribution to graph theory?  How does Dijkstra’s algorithm find smallest path?

3 Weighted Graphs  Edge’s weight is cost of using edge  Distance, cost, travel time, &c. usable as the weight  Weights below are distance in miles ORD PVD MIA DFW SFO LAX LGA HNL 849 802 1387 1743 1843 1099 1120 1233 337 2555 142

4 Cheapest Path Problem  Find path with min. weight between 2 vertices  Sum of edge weights is the path weight  Consider the cheapest path from PVD to HNL  None of edges is cheapest in this example ORD PVD MIA DFW SFO LAX LGA HNL 849 802 1387 1743 1843 1099 1120 1233 337 2555 142

5 Cheapest Path Problem  Subpath on shortest path is shortest path also  Otherwise we would use shorter subpath  Tree made by all shortest paths from vertex  Consider all shortest paths from PVD ORD PVD MIA DFW SFO LAX LGA HNL 849 802 1387 1743 1843 1099 1120 1233 337 2555 142

6 Dijkstra’s Algorithm  Finds cheapest paths from single vertex  Normally, computes cheapest path to all vertices  Stop once vertex computed for single target vertex  Makes several fundamental assumptions  Connected graph needed when targeting all vertices edge weights must be nonnegative  Only works if edge weights must be nonnegative

7 Dijkstra’s Algorithm  Grows cloud of vertices as it goes  Cloud starts with source vetex  Add vertex to cloud with each step  Tracks distances to each vertex not in cloud  For each vertex, considers only cheapest path  Only uses 1 edge from cloud to vertex not in cloud  Each step uses vertex with smallest distance  Adds this vertex to cloud, if not done yet  Checks if creates smaller path to any vertices

8 Edge Relaxation  Consider e from u to z  When u added to cloud  Check adjacent vertices  Assume z not in cloud  Found faster path!  Update via relaxation  New minimum selected: d( z ) = 75 z s u d( u ) = 50 10 e

9 Edge Relaxation  Consider e from u to z  When u added to cloud  Check adjacent vertices  Assume z not in cloud  Found faster path!  Update via relaxation  New minimum selected: d( z ) = 75 z s u d( u ) = 50 10 e d( z ) = 60

10 Edge Relaxation  Consider e from u to z  When u added to cloud  Check adjacent vertices  Assume z not in cloud  Found faster path!  Update via relaxation  New minimum selected: d( z ) = 75 z s u d( u ) = 50 10 e d( z ) = 60

11 Edge Relaxation  Consider e from u to z  When u added to cloud  Check adjacent vertices  Assume z not in cloud  Found faster path!  Update via relaxation  New minimum selected: z s u d( u ) = 50 10 e d( z ) = 60

12 Dijkstra Example CB A E D F 0 428   4 8 7 1 2 5 2 3 9

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

14 Dijkstra Example C B A E D F 0 328 5 8 4 8 7 1 2 5 2 3 9

15 C B A E D F 0 327 5 8 4 8 7 1 2 5 2 3 9

16 CB A E D F 0 327 5 8 4 8 7 1 2 5 2 3 9

17 CB A E D F 0 327 5 8 4 8 7 1 2 5 2 3 9

18 Why Dijkstra’s Algorithm Works  Ultimately, Dijkstra was smart  Smarter than me, if that is possible

19 Why Dijkstra’s Algorithm Works  Ultimately, Dijkstra was smart  Smarter than me, if that is possible

20 Why Dijkstra’s Algorithm Works  Ultimately, Dijkstra was smart  Smarter than me, if that is possible  Example of a greedy algorithm  Takes best choice at each point in time  Vertices added in increasing distance  Brings vertices closer at each step  Stops when vertex cannot move closer

21 Why No Negative-Weight Edges?  Assume edge has negative weight  Greedily chose vertex before finding edge  Cloud will include only one endpoint  Negative weight changes everything, however  Vertices not added in order  Negative weight cycles?  Repeat cycle to optimize CB A E D F 0 4 5 7 5 9 4 8 7 1 2 5 6 0 -8

22 Why No Negative-Weight Edges?  Assume edge has negative weight  Greedily chose vertex before finding edge  Cloud will include only one endpoint  Negative weight changes everything, however  Vertices not added in order  Negative weight cycles?  Repeat cycle to optimize C added when distance was 5, but cheapest distance is 1! CB A E D F 0 4 5 7 5 9 4 8 7 1 2 5 6 0 -8

23 Spanning Tree spanning subgraph tree  Subgraph that is both spanning subgraph & tree  Contains all vertices in graph  spanning subgraph  Tree  connected without any cycles Graph

24 Spanning Tree spanning subgraph tree  Subgraph that is both spanning subgraph & tree  Contains all vertices in graph  spanning subgraph  Tree  connected without any cycles Tree

25 Spanning Tree spanning subgraph tree  Subgraph that is both spanning subgraph & tree  Contains all vertices in graph  spanning subgraph  Tree  connected without any cycles Spanning subgraph

26 Spanning Tree spanning subgraph tree  Subgraph that is both spanning subgraph & tree  Contains all vertices in graph  spanning subgraph  Tree  connected without any cycles Spanning tree

27 Prim-Jarnik’s Algorithm  Similar to Dijkstra’s algorithm but for MST  Processing must start with some vertex s  Grow MST using “cloud” of vertices  Label vertices with least Edge weight to cloud  At each step:  Find and add vertex closest to cloud  Update adjacent vertices to vertex just added

28 Prim-Jarnik’s Algorithm  Priority queue stores vertices outside of cloud  You all should be reminded of Dijkstra's algorithm  Three decorations used for each Vertex  Distance from cloud  Edge connecting vertex to cloud  Entry for Vertex in the priority queue

29 Prim-Jarnik Example B D C A F E 7 4 2 8 5 7 3 9 8 0 7 2 8  

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

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

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

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

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

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

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

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

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

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

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

41 Prim-Jarnik’s Analysis  Each connected vertex is:  Decorated O (deg( v )) times going through algorithm  Priority queue will have added & removed once  Takes O (( n + m ) log n ) time using adjacency list  Each operation on priority queue takes O (log n ) time  Takes O (log n ) time to decorate Vertex each time

42 For Next Lecture  Weekly assignment available on Angel before next Monday’s quiz  Due at special time: before next Monday’s quiz  Programming assignment #3 designs due Friday  Reading on more cheap paths for Friday  Why does everything need to be connected?  Algorithms for the uptight who do not want to relax?


Download ppt "CSC 213 – Large Scale Programming. Today’s Goals  Discuss what is meant by weighted graphs  Where weights placed within Graph  How to use Graph ’s."

Similar presentations


Ads by Google