© 2004 Goodrich, Tamassia Shortest Paths1 Shortest Paths (§ 13.6) Given a weighted graph and two vertices u and v, we want to find a path of minimum total.

Slides:



Advertisements
Similar presentations
Spring 2007Shortest Paths1 Minimum Spanning Trees JFK BOS MIA ORD LAX DFW SFO BWI PVD
Advertisements

Shortest Paths and Dijkstra's Algorithm CS 110: Data Structures and Algorithms First Semester,
1 prepared from lecture material © 2004 Goodrich & Tamassia COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material.
Shortest Paths1 C B A E D F
CSC 213 Lecture 23: Shortest Path Algorithms. Weighted Graphs Each edge in weighted graph has numerical weight Weights can be distances, building costs,
1 Graphs ORD DFW SFO LAX Many slides taken from Goodrich, Tamassia 2004.
Minimum Spanning Trees1 JFK BOS MIA ORD LAX DFW SFO BWI PVD
Minimum Spanning Trees1 JFK BOS MIA ORD LAX DFW SFO BWI PVD
CSC311: Data Structures 1 Chapter 13: Graphs II Objectives: Graph ADT: Operations Graph Implementation: Data structures Graph Traversals: DFS and BFS Directed.
Shortest Paths1 C B A E D F
© 2004 Goodrich, Tamassia Shortest Paths1 C B A E D F
1 prepared from lecture material © 2004 Goodrich & Tamassia COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material.
CSC 213 Lecture 24: Minimum Spanning Trees. Announcements Final exam is: Thurs. 5/11 from 8:30-10:30AM in Old Main 205.
1 Minimum Spanning Trees Longin Jan Latecki Temple University based on slides by David Matuszek, UPenn, Rose Hoberman, CMU, Bing Liu, U. of Illinois, Boting.
1 Minimum Spanning Trees Longin Jan Latecki Temple University based on slides by David Matuszek, UPenn, Rose Hoberman, CMU, Bing Liu, U. of Illinois, Boting.
Lecture20: Graph IV Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
Graphs – Shortest Path (Weighted Graph) ORD DFW SFO LAX
Weighted Graphs In a weighted graph, each edge has an associated numerical value, called the weight of the edge Edge weights may represent, distances,
© 2010 Goodrich, Tamassia Shortest Paths1 C B A E D F
1 Shortest Path Problem Topic 11 ITS033 – Programming & Algorithms C B A E D F Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program,
Shortest Paths C B A E D F
Graphs Part 2. Shortest Paths C B A E D F
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.
Graphs. Data Structure for Graphs. Graph Traversals. Directed Graphs. Shortest Paths. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013.
Introduction DATA STRUCTURE – Graph electronic circuits networks (roads, flights, communications) CS16 LAX JFK LAX DFW STL HNL FTL.
Minimum-Cost Spanning Tree CS 110: Data Structures and Algorithms First Semester,
Shortest Paths 1 Chapter 7 Shortest Paths C B A E D F
Lecture 16. Shortest Path Algorithms
Shortest Paths and Dijkstra’s Algorithm CS 105. SSSP Slide 2 Single-source shortest paths Given a weighted graph G and a source vertex v in G, determine.
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.
© 2010 Goodrich, Tamassia Minimum Spanning Trees1 JFK BOS MIA ORD LAX DFW SFO BWI PVD
1 Prim’s algorithm. 2 Minimum Spanning Tree Given a weighted undirected graph G, find a tree T that spans all the vertices of G and minimizes the sum.
1 Weighted Graphs CSC401 – Analysis of Algorithms Lecture Notes 16 Weighted Graphs Objectives: Introduce weighted graphs Present shortest path problems,
Data Structures and Algorithms1 Data Structures and algorithms (IS ZC361) Weighted Graphs – Shortest path algorithms – MST S.P.Vimal BITS-Pilani
Graphs Quebec Toronto Montreal Ottawa 449 km 255 km 200 km 545 km Winnipeg 2075 km 2048 km New York 596 km 790 km 709 km.
CHAPTER 13 GRAPH ALGORITHMS ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA.
© 2010 Goodrich, Tamassia Shortest Paths1 C B A E D F
© 2010 Goodrich, Tamassia Minimum Spanning Trees1 JFK BOS MIA ORD LAX DFW SFO BWI PVD
1 COMP9024: Data Structures and Algorithms Week Twelve: Graphs (II) Hui Wu Session 1, 2014
Graphs ORD SFO LAX DFW Graphs 1 Graphs Graphs
Graphs 10/24/2017 6:47 AM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and.
Shortest Paths C B A E D F Shortest Paths
Graphs Part 2.
14 Graph Algorithms Hongfei Yan June 8, 2016.
COMP9024: Data Structures and Algorithms
Graphs 5/11/2018 8:05 PM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and.
Minimum Spanning Trees
Shortest Paths C B A E D F Shortest Paths
Minimum Spanning Trees
Shortest Path 6/18/2018 4:22 PM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Shortest Paths C B A E D F Shortest Paths 1
Minimum Spanning Trees
Shortest Paths C B A E D F Shortest Paths
Minimum Spanning Tree 11/3/ :04 AM Minimum Spanning Tree
Shortest Paths C B A E D F Shortest Paths
Shortest Paths C B A E D F
Graphs.
Minimum Spanning Trees
Shortest Paths C B A E D F Shortest Paths
Shortest Paths C B A E D F Shortest Paths
Minimum Spanning Trees
Chapter 13 Graph Algorithms
Graphs Part 2.
Graph Algorithms shortest paths, minimum spanning trees, etc.
Minimum Spanning Trees
Single-source shortest paths
Copyright © Aiman Hanna All rights reserved
Shortest Paths.
Minimum Spanning Trees
Weighted Graphs C B A E D F Sequences
Presentation transcript:

© 2004 Goodrich, Tamassia Shortest Paths1 Shortest Paths (§ 13.6) Given a weighted graph and two vertices u and v, we want to find a path of minimum total weight between u and v. Length of a path is the sum of the weights of its edges. Example: Shortest path between Providence and Honolulu Applications Internet packet routing Flight reservations Driving directions ORD PVD MIA DFW SFO LAX LGA HNL

© 2004 Goodrich, Tamassia Shortest Paths2 Dijkstra’s Algorithm (§ ) The distance of a vertex v from a vertex s is the length of a shortest path between s and v Dijkstra’s algorithm computes the distances of all the vertices from a given start vertex s Assumptions: the graph is connected the edges are undirected the edge weights are nonnegative We grow a “cloud” of vertices, beginning with s and eventually covering all the vertices We store with each vertex v a label d(v) representing the distance of v from s in the subgraph consisting of the cloud and its adjacent vertices At each step We add to the cloud the vertex u outside the cloud with the smallest distance label, d(u) We update the labels of the vertices adjacent to u

© 2004 Goodrich, Tamassia Shortest Paths3 Example CB A E D F  C B A E D F C B A E D F C B A E D F

© 2004 Goodrich, Tamassia Shortest Paths4 Example (cont.) CB A E D F CB A E D F

© 2004 Goodrich, Tamassia Shortest Paths5 Dijkstra’s Algorithm A priority queue stores the vertices outside the cloud Key: distance Element: vertex Locator-based methods insert(k,e) returns a locator replaceKey(l,k) changes the key of an item We store two labels with each vertex: Distance (d(v) label) locator in priority queue Algorithm DijkstraDistances(G, s) Q  new heap-based priority queue for all v  G.vertices() if v  s setDistance(v, 0) else setDistance(v,  ) l  Q.insert(getDistance(v), v) setLocator(v,l) while  Q.isEmpty() u  Q.removeMin() for all e  G.incidentEdges(u) { relax edge e } z  G.opposite(u,e) r  getDistance(u)  weight(e) if r  getDistance(z) setDistance(z,r) Q.replaceKey(getLocator(z),r)

© 2004 Goodrich, Tamassia Shortest Paths6 Analysis of Dijkstra’s Algorithm Graph operations Method incidentEdges is called once for each vertex Label operations We set/get the distance and locator labels of vertex z O(deg(z)) times Setting/getting a label takes O(1) time Priority queue operations Each vertex is inserted once into and removed once from the priority queue, where each insertion or removal takes O(log n) time The key of a vertex in the priority queue is modified at most deg(w) times, where each key change takes O(log n) time Dijkstra’s algorithm runs in O((n  m) log n) time provided the graph is represented by the adjacency list structure Recall that  v deg(v)  2m The running time can also be expressed as O(m log n) since the graph is connected

© 2004 Goodrich, Tamassia Shortest Paths7 Why Dijkstra’s Algorithm Works Dijkstra’s algorithm is based on the greedy method. It adds vertices by increasing distance. CB A E D F Suppose it didn’t find all shortest distances. Let F be the first wrong vertex the algorithm processed. When the previous node, D, on the true shortest path was considered, its distance was correct. But the edge (D,F) was relaxed at that time! Thus, so long as d(F)>d(D), F’s distance cannot be wrong. That is, there is no wrong vertex.