Data Structures & Algorithms Shortest Paths Richard Newman based on book by R. Sedgewick and slides by S. Sahni.

Slides:



Advertisements
Similar presentations
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10.
Advertisements

Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.
Chapter 23 Minimum Spanning Trees
Data Structures & Algorithms Graph Search Richard Newman based on book by R. Sedgewick and slides by S. Sahni.
Tirgul 12 Algorithm for Single-Source-Shortest-Paths (s-s-s-p) Problem Application of s-s-s-p for Solving a System of Difference Constraints.
Jim Anderson Comp 122, Fall 2003 Single-source SPs - 1 Chapter 24: Single-Source Shortest Paths Given: A single source vertex in a weighted, directed graph.
CS 311 Graph Algorithms. Definitions A Graph G = (V, E) where V is a set of vertices and E is a set of edges, An edge is a pair (u,v) where u,v  V. If.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Fall, 2001 Lectures 3 Tuesday, 9/25/01 Graph Algorithms: Part 1 Shortest.
Shortest Paths Definitions Single Source Algorithms –Bellman Ford –DAG shortest path algorithm –Dijkstra All Pairs Algorithms –Using Single Source Algorithms.
Graph Algorithms: Part 1
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 15 Shortest paths algorithms Properties of shortest paths Bellman-Ford algorithm.
Shortest Path Problems Directed weighted graph. Path length is sum of weights of edges on path. The vertex at which the path begins is the source vertex.
Shortest Paths Definitions Single Source Algorithms
Tirgul 13. Unweighted Graphs Wishful Thinking – you decide to go to work on your sun-tan in ‘ Hatzuk ’ beach in Tel-Aviv. Therefore, you take your swimming.
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
More Graph Algorithms Weiss ch Exercise: MST idea from yesterday Alternative minimum spanning tree algorithm idea Idea: Look at smallest edge not.
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE 15 Shortest paths algorithms Properties of shortest paths Bellman-Ford algorithm.
Dijkstra's algorithm.
Shortest Path Algorithms. Kruskal’s Algorithm We construct a set of edges A satisfying the following invariant:  A is a subset of some MST We start with.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Graph Algorithms: Minimum.
1 Shortest Path Algorithms Andreas Klappenecker [based on slides by Prof. Welch]
Jim Anderson Comp 122, Fall 2003 Single-source SPs - 1 Chapter 24: Single-Source Shortest Paths Given: A single source vertex in a weighted, directed graph.
5/27/03CSE Shortest Paths CSE Algorithms Shortest Paths Problems.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
COSC 2007 Data Structures II Chapter 14 Graphs III.
Algorithm Course Dr. Aref Rashad February Algorithms Course..... Dr. Aref Rashad Part: 6 Shortest Path Algorithms.
Parallel graph algorithms Antonio-Gabriel Sturzu, SCPD Adela Diana Almasi, SCPD Adela Diana Almasi, SCPD Iulia Alexandra Floroiu, ISI Iulia Alexandra Floroiu,
Chapter 24: Single-Source Shortest Paths Given: A single source vertex in a weighted, directed graph. Want to compute a shortest path for each possible.
Introduction to Algorithms Jiafen Liu Sept
Graphs. Definitions A graph is two sets. A graph is two sets. –A set of nodes or vertices V –A set of edges E Edges connect nodes. Edges connect nodes.
Shortest Path in Weighted Graph : Dijkstra’s Algorithm.
Minimum spanning trees (MST) Def: A spanning tree of a graph G is an acyclic subset of edges of G connecting all vertices in G. A sub-forest of G is an.
CSE 2331 / 5331 Topic 12: Shortest Path Basics Dijkstra Algorithm Relaxation Bellman-Ford Alg.
Nattee Niparnan. Dijkstra’s Algorithm Graph with Length.
Shortest Paths CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
Weighted Graphs Computing 2 COMP s1 Sedgewick Part 5: Chapter
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 22c:31 Algorithms Minimum-cost Spanning Tree (MST)
Contest Algorithms January 2016 Describe shortest path trees, SSSP, APSP, and three algorithms: Dijkstra, Bellman-Ford, Floyd-Warshall 9. Shortest Paths.
CSE 373: Data Structures and Algorithms Lecture 21: Graphs V 1.
Data Structures & Algorithms Graphs Richard Newman based on book by R. Sedgewick and slides by S. Sahni.
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.
Shortest Paths.
Shortest Paths and Minimum Spanning Trees
Single-Source Shortest Paths
Minimum Spanning Trees
Shortest Path Problems
Shortest Path Problems
Three Graph Algorithms
Short paths and spanning trees
Data Structures & Algorithms Graphs
Minimum-Cost Spanning Tree
Graphs & Graph Algorithms 2
Shortest Paths.
Minimum-Cost Spanning Tree
Chapter 13 Graph Algorithms
Minimum-Cost Spanning Tree
Shortest Path Problems
Shortest Paths and Minimum Spanning Trees
Chapter 15 Graphs © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Algorithms: Design and Analysis
Single-Source Shortest Paths
Shortest Path Problems
Shortest Path Problems
Shortest Paths.
Graphs: Shortest path and mst
Graph Algorithm.
Directed Graphs (Part II)
Minimum-Cost Spanning Tree
Presentation transcript:

Data Structures & Algorithms Shortest Paths Richard Newman based on book by R. Sedgewick and slides by S. Sahni

Minimum Cost Path Weighted, digraph G, or network Directed simple path p from s to t Cost(p) = sum of edge weights on p Minimum cost path p from s to t in G such that no other path p' from s to t in G has cost(p') < cost(p)

Minimum Cost Path s = 1, t = 10 What is minimum cost path?

A Spanning Tree path cost = 28 Is there a cheaper path?

A Spanning Tree path cost = 25 Is there a cheaper path?

A Spanning Tree path cost = 24 Is there a cheaper path?

Shortest Path Problems No negative weight edges allowed! s-t shortest path – Single source, destination – Fastest route to Epcot Single Source Shortest Path – Best routes from A to anywhere All Pairs Shortest Paths – Routing tables in network nodes

Shortest Path Algorithms Dijkstra's Algorithm Floyd's Algorithm Bellman-Ford Algorithm

Shortest Path Algorithms Dijkstra's Algorithm Floyd's Algorithm Bellman-Ford Algorithm

Dijkstra's Algorithm Very similar to Prim's algorithm Differences are that – Paths form a rooted tree (whereas MST is not rooted) – Graph is directed (not undirected) Grow known shortest paths from source one edge at a time Priority of edge is different

Dijkstra's Algorithm Set Known nodes K = {s} Set dist(s) = 0, dist(u) = ∞ for all other nodes u in G Set pred(s) = s, pred(u) = NULL for all other nodes u in G Set Seen nodes S = {neighbors of s} Set pred(u) = s for all nodes in S

Dijkstra's Algorithm While |K| < V Find nearest node v in S Add v to K For each edge (v,w) in E If dist(v) + cost(v,w) < dist(w) Add w to S Pred(w) = v dist(w) = dist(v) + cost(v,w)

Dijkstra's Algorithm s = 1 Grow SPT by adding node nearest to s Update best distances

Dijkstra's Algorithm Correct Builds shortest path tree (SPT) Always adds nearest seen node v Path takes into account all nodes in K No other node x not in K can be closer than v Hence no path through x could be shorter than the path we have to v

Dijkstra's Algorithm Complexity Initialization – O(V) Select next node – O(V) linear list Update dist, pred – O(E) total Selection done V-1 times Total time – O(V 2 + E) = O(V 2 ) Using linear list for S

Dijkstra's Algorithm Complexity MinHeap – O((V+E) lg V) V removals, E changes to S What if G is dense – E is O(V 2 )? Worse!! Fibonacci Heap – O(V lg V + E) Even better!

Dijkstra's Algorithm Solves single source shortest path Builds SPT For s-t shortest path, Just stop when t is added to K Also works for sink-trees (take edges in reverse direction)

Dijkstra's Algorithm Does NOT work with negative edge weights! (Violates assumption needed by greedy method) Can be used to solve all pairs shortest path Build SPT from each node Complexity of SSSP times V

Shortest Path Algorithms Dijkstra's Algorithm Floyd's Algorithm Bellman-Ford Algorithm

Shortest Path Algorithms Floyd's Algorithm Single source shortest paths Works like Warshall’s algorithm for reachability Except takes path costs into account

Floyd’s Algorithm Recall Warshall's Algorithm For each intermediate node i For each source s For each destination t s reaches t if s already reaches t or if s reaches i and i reaches t

Floyd’s Algorithm Now just track distances For each intermediate node i For each source s For each destination t cost(s,t) = lesser of cost(s,t) and cost(s,i) + cost(i,t)

Floyd’s Algorithm Complexity of Floyd’s algorithm: O(V 3 ) Dynamic Programming and Relaxation Build estimates Improve estimates (node relax) Converge

Floyd's Algorithm Consider paths through nodes numbered <1, <2, <3, etc

Floyd's Algorithm Paths through nodes numbered < better, worse

Floyd's Algorithm Paths through nodes numbered < = 61 >

Floyd's Algorithm Paths through nodes numbered <

Floyd's Algorithm Paths through nodes numbered <

Floyd's Algorithm Paths through nodes numbered <

Floyd's Algorithm Paths through nodes numbered <

Floyd’s Algorithm Update successor node when updating minimum cost path Method of choice for APSP for dense graphs Works even with negative weights But not with negative cycles! (can detect at least one)

Shortest Path Algorithms Dijkstra's Algorithm Floyd's Algorithm Bellman-Ford Algorithm

Shortest Path Algorithms Bellman-Ford Algorithm SSSP Compute best cost path by edge relaxation – checking all edges Essentially considers paths of increasing potential length

Bellman-Ford Algorithm Initialize: dist[t] = {0 if t==s, else infinity} pred[v] = NULL for all v For (i = 1 to V-1) For each edge e=(u,v) in E If (dist[u] + cost[e] < dist[v]) dist[v] = dist[u] + cost[e] pred[v] = u

Bellman-Ford Algorithm Consider paths of length <2, <3, etc. Adjacency matrix is paths of length <

Bellman-Ford Algorithm Edge (0,1) Enter here Leave here

Bellman-Ford Algorithm Edge (0,5) doesn’t help Edge (1,2)

Bellman-Ford Algorithm Edge (2,3)

Bellman-Ford Algorithm Edge (3,0)

Bellman-Ford Algorithm Edge (3,5)

Bellman-Ford Algorithm Edge (4,2)

Bellman-Ford Algorithm Edge (4,3)

Bellman-Ford Algorithm Edge (5,1)

Bellman-Ford Algorithm Edge (5,4)

Bellman-Ford Algorithm Well, that was ONE pass – need to do that V times!

Shortest Path Algorithms Dijkstra's Algorithm Floyd's Algorithm Bellman-Ford Algorithm