Lecture 19: Shortest Paths Shang-Hua Teng. Weighted Directed Graphs Weight on edges for distance 400 2500 1000 1800 800 900.

Slides:



Advertisements
Similar presentations
October 31, Algorithms and Data Structures Lecture XIII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Advertisements

November 14, Algorithms and Data Structures Lecture XIII Simonas Šaltenis Aalborg University
Comp 122, Fall 2004 Elementary Graph Algorithms. graphs Lin / Devi Comp 122, Fall 2004 Graphs  Graph G = (V, E) »V = set of vertices »E = set of.
CS138A Single Source Shortest Paths Peter Schröder.
 2004 SDU Lecture9- Single-Source Shortest Paths 1.Related Notions and Variants of Shortest Paths Problems 2.Properties of Shortest Paths and Relaxation.
Data Structures and Algorithms (AT70.02) Comp. Sc. and Inf. Mgmt. Asian Institute of Technology Instructor: Dr. Sumanta Guha Slide Sources: CLRS “Intro.
1 Graphs Traversals In many graph problems, we need to traverse the vertices of the graph in some order Analogy: Binary tree traversals –Pre-order Traversal.
Design and Analysis of Algorithms Single-source shortest paths, all-pairs shortest paths Haidong Xue Summer 2012, at GSU.
CS 473Lecture 141 CS473-Algorithms I Lecture 14-A Graph Searching: Breadth-First Search.
ALGORITHMS THIRD YEAR BANHA UNIVERSITY FACULTY OF COMPUTERS AND INFORMATIC Lecture eight Dr. Hamdy M. Mousa.
CS 473Lecture 141 CS473-Algorithms I Lecture 14-A Graph Searching: Breadth-First Search.
Length of a Path The weight (length) of a path is the sum of the weights of its edges. adcbe Path p: Edge weights: w(a, b) = 7, w(b, c) = 2, w(c,
Introduction to Algorithms 6.046J/18.401J/SMA5503
CS420 lecture twelve Shortest Paths wim bohm cs csu.
CSE 780 Algorithms Advanced Algorithms Shortest path Shortest path tree Relaxation Bellman-Ford Alg.
Lecture 20: Shortest Paths Shang-Hua Teng. Weighted Directed Graphs Weight on edges for distance
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.
Shortest Path Problems
Lecture 14: Graph Algorithms Shang-Hua Teng. Undirected Graphs A graph G = (V, E) –V: vertices –E : edges, unordered pairs of vertices from V  V –(u,v)
1 8-ShortestPaths Shortest Paths in a Graph Fundamental Algorithms.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 15 Shortest paths algorithms Properties of shortest paths Bellman-Ford algorithm.
Graph Algorithms: Shortest Path We are given a weighted, directed graph G = (V, E), with weight function w: E R mapping.
CSE 780 Algorithms Advanced Algorithms SSSP Dijkstra’s algorithm SSSP in DAGs.
Analysis of Algorithms CS 477/677 Shortest Paths Instructor: George Bebis Chapter 24.
1 Graph Algorithms Single source shortest paths problem Dana Shapira.
CS 253: Algorithms Chapter 24 Shortest Paths Credit: Dr. George Bebis.
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE 15 Shortest paths algorithms Properties of shortest paths Bellman-Ford algorithm.
SINGLE-SOURCE SHORTEST PATHS. Shortest Path Problems Directed weighted graph. Path length is sum of weights of edges on path. The vertex at which the.
Theory of Computing Lecture 7 MAS 714 Hartmut Klauck.
Dijkstra's algorithm.
Graphs – Shortest Path (Weighted Graph) ORD DFW SFO LAX
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.
CISC 235: Topic 11 Shortest Paths Algorithms. CISC 235 Topic 112 Outline Single-Source Shortest Paths Algorithm for Unweighted Graphs Algorithm for Weighted,
David Luebke 1 9/13/2015 CS 332: Algorithms S-S Shortest Path: Dijkstra’s Algorithm Disjoint-Set Union Amortized Analysis.
1 Shortest Path Algorithms Andreas Klappenecker [based on slides by Prof. Welch]
COSC 3101NJ. Elder Assignment 2 Remarking Assignment 2 Marks y = 0.995x R 2 = Old Mark New Mark.
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.
1 Shortest Path Problems How can we find the shortest route between two points on a road map? Model the problem as a graph problem: –Road map is a weighted.
Dijkstras Algorithm Named after its discoverer, Dutch computer scientist Edsger Dijkstra, is an algorithm that solves the single-source shortest path problem.
Spring 2015 Lecture 10: Elementary Graph Algorithms
Sept Elementary Graph Algorithms Graph representation Graph traversal -Breadth-first search -Depth-first search Parenthesis theorem.
Algorithm Course Dr. Aref Rashad February Algorithms Course..... Dr. Aref Rashad Part: 6 Shortest Path Algorithms.
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
The single-source shortest path problem (SSSP) input: a graph G = (V, E) with edge weights, and a specific source node s. goal: find a minimum weight (shortest)
CSE 2331 / 5331 Topic 12: Shortest Path Basics Dijkstra Algorithm Relaxation Bellman-Ford Alg.
Shortest Path Algorithms. Definitions Variants  Single-source shortest-paths problem: Given a graph, finding a shortest path from a given source.
1 Introduction to Algorithms L ECTURE 17 (Chap. 24) Shortest paths I 24.2 Single-source shortest paths 24.3 Dijkstra’s algorithm Edsger Wybe Dijkstra
Lecture 13 Algorithm Analysis
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 20.
1 Weighted Graphs. 2 Outline (single-source) shortest path –Dijkstra (Section 4.4) –Bellman-Ford (Section 4.6) (all-pairs) shortest path –Floyd-Warshall.
Greedy Algorithms Z. GuoUNC Chapel Hill CLRS CH. 16, 23, & 24.
Shahed University Dr. Shahriar Bijani May  A path is a sequence of vertices P = (v 0, v 1, …, v k ) such that, for 1 ≤ i ≤ k, edge (v i – 1, v.
Length of a Path The weight (length) of a path is the sum of the weights of its edges. adcbe Path p: Edge weights: w(a, b) = 7, w(b, c) = 2, w(c,
CSE Graph Search Algorithms. CSE Graph a c b Node ~ city or computer Edge ~ road or data cable Undirected or Directed A surprisingly large.
Chapter 05 Introduction to Graph And Search Algorithms.
Single Source Shortest Paths Chapter 24 CSc 4520/6520 Fall 2013 Slides adapted from George Bebis, University of Reno, Nevada.
Single-Source Shortest Paths (25/24) HW: 25-2 and 25-3 p. 546/24-2 and 24-3 p.615 Given a graph G=(V,E) and w: E   weight of is w(p) =  w(v[i],v[i+1])
Minimum Spanning Tree Shortest Paths
Minimum Spanning Trees
Finding Shortest Paths
Basic Graph Algorithms
Advanced Algorithms Analysis and Design
Lecture 13 Algorithm Analysis
Lecture 13 Algorithm Analysis
Lecture 13 Algorithm Analysis
Lecture 13 Algorithm Analysis
Algorithms Searching in a Graph.
CS 3013: DS & Algorithms Shortest Paths.
Presentation transcript:

Lecture 19: Shortest Paths Shang-Hua Teng

Weighted Directed Graphs Weight on edges for distance

Shortest Paths Given a weighted, directed graph G=(V, E) with weight function w: E  R. The weight of path p= is the sum of the weights of its edges: We define the shortest-path weight from u to v by A shortest path from vertex u to vertex v is any path with w(p)=  (u, v) If there is a path from u to v, Otherwise.

Variants of Shortest Path Problem Single-source shortest paths problem –Finds all the shortest path of vertices reachable from a single source vertex s Single-destination shortest-path problem –By reversing the direction of each edge in the graph, we can reduce this problem to a single-source problem Single-pair shortest-path problem –No algorithm for this problem are known that run asymptotically faster than the best single-source algorithm in the worst case All-pairs shortest-path problem

Applications of Shortest Path Every where!!! –Driving (GPS) –Internet: network routing –Flying: Airline route –Resource allocation –VLSI design: wire routing –Computer Games –Mapquest, Yahoo map program

Optimal Substructure of Shortest-Paths Lemma: (Subpath of shortest paths are shortest paths). Let p= be a shortest path from vertex v 1 to v k, and for any i and j such that 1  i  j  k, let p ij = be the sub-path of p from vertex v i to v j. Then p ij is a shortest path from vertex v i to v j.

Algorithmic Impact Greedy Dynamic Programming

Negative-Weight Edges and Cycles In general, edges might have negative weights What if there is a negative-weight cycle No shortest path can contain a negative cycle Of course, a shortest path cannot contain a positive-weight cycle

Graph with Unit Weights Special case: All weights are 1 The single source shortest path problem can be solved by BFS BFS tree explicit gives a shortest path from a source s to any vertex reachable from s

Breadth-First Search BFS(G, s) 1.For each vertex u in V – {s}, 2.color[u] = white; d[u] = infty;  [u] = NIL 3.color[s] = GRAY; d[s] = 0;  [s] = NIL; Q = {} 4.ENQUEUE(Q,s) // Q is a FIFO queue 5.while (Q not empty) 6. u = DEQUEUE(Q) 7. for each v  Adj[u] 8. if color[v] = WHITE 9. then color[v] = GREY 10. d[v] = d[u] + 1;  [v] = u 11. ENQUEUE(Q, v); 12. color[u] = BLACK;

Breadth-first Search Visited all vertices reachable from the root A spanning tree For any vertex at level i, the spanning tree path from s to i has i edges, and any other path from s to i has at least i edges (shortest path property)

Breadth-First Search: the Color Scheme White vertices have not been discovered –All vertices start out white Grey vertices are discovered but not fully explored –They may be adjacent to white vertices Black vertices are discovered and fully explored –They are adjacent only to black and gray vertices Explore vertices by scanning adjacency list of grey vertices

Graphs with Non-Negative Weights Shortest Path Tree Can we use a similar idea to generate a shortest path tree which represents the shortest path from s to all vertices that are reachable from s?

Dijkstra’s Algorithm Solve the single-source shortest-paths problem on a weighted, directed graph and all edge weights are nonnegative Data structure –S: a set of vertices whose final shortest-path weights have already been determined –Q: a min-priority queue keyed by their d values Idea –Repeatedly select the vertex u  V-S (kept in Q) with the minimum shortest-path estimate, adds u to S, and relaxes all edges leaving u

BFS vs Dijkstra’s Algorithm BFS(G, s) | Dijkstra(G,s) 1.For each vertex u in V – {s}, 2.color[u] = white; d[u] = infty;  [u] = NIL 3.color[s] = GRAY; d[s] = 0;  [s] = NIL; Q = {} 4.ENQUEUE(Q,s) | ENQUEUE(Q,(s,d[s])) 5.while (Q not empty) 6. u = DEQUEUE(Q) | u = EXTRACT-MIN(Q) 7. for each v  Adj[u] 8. if color[v] = WHITE | d[v] > d[u] + w(u,v) 9. then color[v] = GREY 10. d[v] = d[u] + 1;  [v] = u | d[v] = d[u] +w(u,v) 11. ENQUEUE(Q, v); | ENQUEUE(Q, (v,d[v])) 12. color[u] = BLACK;

Relaxation For each vertex v  V, we maintain an attribute d[v], which is an upper bound on the weight of a shortest path from source s to v. We call d[v] a shortest-path estimate. Possible Predecessor of v in the shortest path

Relaxation Relaxing an edge (u, v) consists of testing whether we can improve the shortest path found so far by going through u and, if so, update d[v] and  [v] By Triangle Inequality

Dijkstra’s Algorithm

Properties of Shortest Paths Triangle inequality –For any edge (u,v) in E,  (s,v) <=  (s,u) + w(u,v) Upper bound property –d[v] >=  (s,v) Monotonic property –d[v] never increase No-path property –If v is not reachable then d[v] =  (s,v) = infty

Properties of Shortest Paths Convergence property –If (u,v) is on the shortest path from s to v and if d[u] =  (s,u) at any time prior to relaxing (u,v), then d[v] =  (s,v) at all time afterward Path-relaxation property –If p= is the shortest path from s to v k and edges of p are relaxed in order in the index, then d[v k ] =  (s, v k ). This property holds regardless of any other relaxation steps that occur, even if they are intermixed with relaxations of the edges of p

Properties of Shortest Paths Predecessor-subgraph property –Once d[v] =  (s,v), the predecessor subgraph is a shortest-paths tree rooted at s

Analysis of Dijkstra’s Algorithm Min-priority queue operations –INSERT (line 3) –EXTRACT-MIN( line 5) –DECREASE-KEY(line 8) Time analysis –Line 4-8: while loop  O(V) –Line 7-8: for loop and relaxation  |E| –Running time depends on how to implement min-priority queue Simple array: O(V 2 +E) = O(V 2 ) Binary min-heap: O((V+E)lg V) Fibonacci min-heap: O(VlgV + E)