Shortest Paths.

Slides:



Advertisements
Similar presentations
1 Minimum Spanning Tree Prim-Jarnik algorithm Kruskal algorithm.
Advertisements

Spring 2007Shortest Paths1 Minimum Spanning Trees JFK BOS MIA ORD LAX DFW SFO BWI PVD
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
© 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.
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
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.
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.
Paths in a Graph : A Brief Tutorial Krishna.V.Palem Kenneth and Audrey Kennedy Professor of Computing Department of Computer Science, Rice University 1.
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
1 COMP9024: Data Structures and Algorithms Week Twelve: Graphs (II) Hui Wu Session 1, 2014
Shortest Path -Prim’s -Djikstra’s. PRIM’s - Minimum Spanning Tree -A spanning tree of a graph is a tree that has all the vertices of the graph connected.
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.
COMP9024: Data Structures and Algorithms
Minimum Spanning Trees
Shortest Paths C B A E D F Shortest Paths
Minimum Spanning Trees
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
Shortest Paths C B A E D F Shortest Paths
Shortest Paths C B A E D F
Minimum Spanning Trees
Shortest Paths C B A E D F Shortest Paths
Shortest Paths C B A E D F Shortest Paths
Shortest Paths.
Minimum Spanning Trees
Chapter 13 Graph Algorithms
Advanced Algorithms Analysis and Design
Graphs Part 2.
Graph Algorithms shortest paths, minimum spanning trees, etc.
Minimum Spanning Trees
Single-source shortest paths
Copyright © Aiman Hanna All rights reserved
Lecture 14 Shortest Path (cont’d) Minimum Spanning Tree
Algorithms: Design and Analysis
Dijkstra’s Shortest Path Algorithm
Minimum Spanning Trees
Weighted Graphs C B A E D F Sequences
CSE 417: Algorithms and Computational Complexity
Lecture 13 Shortest Path (cont’d) Minimum Spanning Tree
Data Structures and Algorithm Analysis Lecture 8
Presentation transcript:

Shortest Paths

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: A vertex represents an airport and stores the three-letter airport code An edge represents a flight route between two airports and stores the mileage of the route ORD PVD MIA DFW SFO LAX LGA HNL 849 802 1387 1743 1843 1099 1120 1233 337 2555 142 Los Angeles Chicago Providence San Francisco Honolulu Dallas Miami New York

Shortest Paths The problem: Weight of the path P sum of the weights of all the edges on the path P Shortest path path with smallest weight The problem: Given a weighted graph and two vertices u and v, we want to find a path of minimum total weight (shortest path) between u and v.

Shortest Path Properties Property 1: A sub-path 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 Providence Los Angeles Chicago San Francisco Honolulu Dallas Miami New York

The Distance of a Shortest Path Case 1: The graph may have negative edges but no negative cycles. The shortest distance from s to t can be computed. A B s t -3 1 8 d(s,t)=6 Case 2: The graph contains negative weight cycles, and a path from s to t includes an edge on a negative weight cycle. The shortest path distance is -. 1 s 1 8 t d(s,t)=-  A B -3

Shortest Path Algorithms Dijkstra’s algorithm does NOT allow negative edges. Uses a greedy heuristic. undirected/directed graph Bellman-Ford’s and Floyd’s algorithms work correctly for any graph and can detect negative cycles. Must be directed graph if negative edges are included.

Shortest Path Algorithms Dijkstra’s algorithm does NOT allow negative edges. Uses a greedy heuristic. undirected/directed graph Bellman-Ford’s and Floyd’s algorithms work correctly for any graph and can detect negative cycles. Must be directed graph if negative edges are included. We will study Dijkstra’s algorithm only. The others are beyond the scope of this course

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 (pronounced dyke-stra) computes the distances of all the vertices from a given start vertex s Assumptions: the graph is connected the edge weights are nonnegative

Dijkstra’s Algorithm d(u) We grow a “cloud” of vertices, beginning with s and eventually covering all the vertices We store with each vertex u a label d(u) representing the distance of u 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 d(.) of the vertices adjacent to u s s d(u) 8 A 4 8 A 4 2 2 2 8 7 2 1 4 8 7 1 3 B C D B C D 9  3 9 5  3 11 2 2 E F 5 E F 5

Dijkstra’s Algorithm - Edge Relaxation d(u) = 50 Consider an edge e = (u,z) such that u is the vertex most recently added to the cloud z is not in the cloud The relaxation of edge e updates distance d(z) as follows: d(z) := min{d(z),d(u) + weight(e)} 10 d(z) = 75 e u s z d(u) = 50 10 d(z) = 60 u e s z

Example C B A E D F 4 2 8  7 1 5 3 9 11

Example C B A E D F 3 2 7 5 8 4 1 9

Dijkstra’s Algorithm A priority queue stores the vertices outside the cloud (Key: distance, Element: vertex) Algorithm ShortestPath(G, v) Input A weighted graph G with nonnegative edge weights, and a start vertex v of G Output a label D[u] for each u of G , so that D[u] is the length of a shortest path from v to u in G Initialize D[v] := 0 , and D[u] :=  for every vertex u ≠ v Let a priority queue Q contain all the vertices of G using the D label as a key while Q is not empty do {pull a new vertex u into the cloud} u := Q.removeMin() for each vertex z adjacent to u such that z is in Q do {perform the relaxation on edge (u,z)} if D[u] + w((u,z)) < D[z] then D[z] := D[u] + w((u,z)) change to D[z] the key of vertex z in Q return the label D[u] of each vertex u

Analysis of Dijkstra’s Algorithm Inserting all the vertices in Q with their initial key value can be done in O(n log n) At each iteration of the while loop, we spend O(log n) time to remove vertex u from Q and O(degree (v) log n) time to perform the relaxation procedure on each edge //note that it takes log n to update D[z] in the heap The overall running time for the entire while loop is O(m log n) remember Sv degree(v) = 2m Dijkstra’s algorithm runs in O((n + m) log n) time provided the graph is represented by the adjacency list structure // the use of adjacency list allows us to step through the vertices adjacent to u during the relaxation step in time proportional to their number

Why It Doesn’t Work for Negative-Weight Edges Dijkstra’s algorithm is based on the idea that 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. A 8 4 6 7 5 4 B 7 C 1 D -8 5 9 2 5 E F C’s true distance is 1, but it is already in the cloud with d(C)=5!