Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 14 Weighted Graphs © John Urrutia 2014, All Rights Reserved1.

Similar presentations


Presentation on theme: "Chapter 14 Weighted Graphs © John Urrutia 2014, All Rights Reserved1."— Presentation transcript:

1 Chapter 14 Weighted Graphs © John Urrutia 2014, All Rights Reserved1

2 Weighted Graphs Similar to directional Graphs Added attribute called “weight” to the edges Used to designate a preference between edges Helps answer questions like What is the minimum spanning tree for a weighted graph? What is the shortest (or cheapest) distance from one vertex to another? © John Urrutia 2014, All Rights Reserved2

3 Minimum Spanning Tree with weighted graphs Weight changes everything Each vertex edge must be compared based on their weight The lightest weight wins Given 6 cities where the weight between each city is the cost ($millions)of building an internet cable What is/are the least expensive route © John Urrutia 2014, All Rights Reserved3

4 Minimum Spanning Trees © John Urrutia 2014, All Rights Reserved4 Colina Danza Flor Ajo Bordo Erizo 10 6 7 124 7 7 8 5 6 Repeat until no more Vertices to add Pick a vertex Add vertex to MSP Identify the lightest Edge from this vertex New Vertex in MSP Identify the next lightest Edge from this vertex No

5 Minimum Spanning Trees © John Urrutia 2014, All Rights Reserved5 Colina Danza Flor Ajo Bordo Erizo 10 6 7 124 7 7 8 5 6 ErizoColinaFlorBordoAjoDanza 56764

6 Minimum Spanning Trees © John Urrutia 2014, All Rights Reserved6 Colina Danza Flor Ajo Bordo Erizo 10 6 7 124 7 7 8 5 6 FlorColinaErizoBordoAjoDanza 65764

7 Minimum Spanning Trees © John Urrutia 2014, All Rights Reserved7 Colina Danza Flor Ajo Bordo Erizo 10 6 7 124 7 7 8 5 6 ColinaErizoFlorBordoAjoDanza 56764

8 Minimum Spanning Trees © John Urrutia 2014, All Rights Reserved8 Colina Danza Flor Ajo Bordo Erizo 10 6 7 124 7 7 8 5 6 BordoAjoDanzaErizoColinaFlor 64756

9 Minimum Spanning Trees © John Urrutia 2014, All Rights Reserved9 Colina Danza Flor Ajo Bordo Erizo 10 6 7 124 7 7 8 5 6 AjoDanzaBordoErizoColinaFlor 46756

10 Minimum Spanning Trees © John Urrutia 2014, All Rights Reserved10 Colina Danza Flor Ajo Bordo Erizo 10 6 7 124 7 7 8 5 6 DanzaAjoBordoErizoColinaFlor 46756

11 Minimum Spanning Tree The Algorithm Select a current vertex to start the tree Find all edges from the current vertex to vertices not already in the tree. Place them into a priority queue. Select the lowest priority edge from the queue add it and the destination vertex to the tree. Repeat until no more vertices to add. © John Urrutia 2014, All Rights Reserved11

12 Priority Queue Vertex List Minimum Spanning Trees © John Urrutia 2014, All Rights Reserved12 Colina Danza Flor Ajo Bordo Erizo 10 6 7 124 7 7 8 5 6 A D4 B6 E7 C5 F6 AjoDanzaBordoErizoColinaFlor 46756 B6 D4 E12 C8 B7 B6 C10 E7 F7 C5

13 Shortest Path Problem With the Minimum Spanning Tree we answer the question: Can we get there from here With the Shortest Path we answer the questions: What is the shortest way to get there from here © John Urrutia 2014, All Rights Reserved13

14 Dijkstra’s Algorithm Developed in 1959 by Edsger Dijkstra Based on an adjacency matrix for a directed graph Finds the shortest path between one vertex to another Finds the shortest paths between a vertex and all other vertices Similar to Warshall’s algorithm but updates a path when it is shorter than a previous one © John Urrutia 2014, All Rights Reserved14

15 Dijkstra’s Algorithm For each unvisited vertex in the weighted graph Select the vertex with the smallest total distance Calculate the distance through this vertex to each unvisited neighboring vertex Update the neighboring vertex distance if smaller. Mark the source vertex as visited. © John Urrutia 2014, All Rights Reserved15

16 Priority Queue Vertex List Shortest Path Array © John Urrutia 2014, All Rights Reserved16 Colina Danza Flor Ajo Bordo Erizo $10 $6 $7 $12$4 $7 $8 $3 $6 A D4 B6 E7 C5 F6 AjoDanzaBordoErizoColinaFlor 46756 B6 D4 E12 C8 B7 B6 C10 E7 F7 C5 ~ ~ ~ ~ ~ 0 4 6 16 12 18 6 + 10 = 16 !< 12 NoChg 12 + 3 = 15 < 16 Update 15 6 + 7 = 13 !< 4 NoChg

17 All-pairs Shortest Path Weighted Adjacency Matrix © John Urrutia 2014, All Rights Reserved17  From / To  AjoBordoColinaDanzaErizoFlor Ajoinf$6inf$4inf Bordoinf $10$7inf Colinainf $3$6 Danzainf $8inf$12inf Erizoinf$7inf Florinf $7inf

18 All-pairs Shortest Path All-Pairs Table © John Urrutia 2014, All Rights Reserved18  From / To  AjoBordoColinaDanzaErizoFlor Ajoinf$6$12$4$16$18 Bordoinf $10$7$13$16 Colinainf$10inf$17$3$6 Danzainf$18$8inf$12$14 Erizoinf$7$17$14inf$23 Florinf$14$24$21$7inf

19 Floyd’s Algorithm Dijkstra’s algorithm for producing the All-Pairs Shortest Path Table runs in O(N 2 ) Floyds algorithm for the All-Pairs Shortest Path Table is more efficient because it uses a variation on Warshall’s algorithm. © John Urrutia 2014, All Rights Reserved19

20 Floyd’s Algorithm Process each row and column © John Urrutia 2014, All Rights Reserved20 Adjacency Matrix AjoBordoColinaDansaErizoFlor Ajo6 4 Bordo10 7 Colina36 Dansa8 12 Erizo 7 Flor7 Ajo goes to Bordo – Bordo goes to? 1 1 1 6 + 7 = 13 !< 4 No Chg MSP goes to Nothing SAN goes to LGA,MSP,SFO,STL,TPA – what goes to SAN Nothing? SFO goes to MSP – what goes to SFO TPA goes to LGA – what goes to TPA Nothing goes to Ajo SFO goes to STL – what goes to SFO TPA goes to MSP – what goes to TPA TPA goes to STL – what goes to TPA 16 4 + 8 = 12 < 16 Update 12

21 Summary In a weighted graph, edges have an associated number called the weight, which might represent distances, costs, times, or other quantities. The minimum spanning tree in a weighted graph minimizes the weights of the edges necessary to connect all the vertices. An algorithm using a priority queue can be used to find the minimum spanning tree of a weighted graph. The minimum spanning tree of a weighted graph models real-world situations such as installing utility cables between cities. © John Urrutia 2014, All Rights Reserved21

22 Summary The shortest-path problem in a non-weighted graph involves finding the minimum number of edges between two vertices. Solving the shortest-path problem for weighted graphs yields the path with the minimum total edge weight. The shortest-path problem for weighted graphs can be solved with Dijkstra’s algorithm. The algorithms for large, sparse graphs generally run much faster if the adja- cency-list representation of the graph is used rather than the adjacency matrix. © John Urrutia 2014, All Rights Reserved22

23 Summary The all-pairs shortest-path problem is to find the total weight of the edges between every pair of vertices in a graph. Floyd’s algorithm can be used to solve this problem. Some graph algorithms take exponential time and are therefore not practical for graphs with more than a few vertices. © John Urrutia 2014, All Rights Reserved23

24 Directional Weighted Graph © John Urrutia 2014, All Rights Reserved24 Colina Danza Flor Ajo Bordo Erizo $10 $6 $7 $12$4 $7 $8 $3 $6


Download ppt "Chapter 14 Weighted Graphs © John Urrutia 2014, All Rights Reserved1."

Similar presentations


Ads by Google