2015-5-7chapter251 4.4 Single-Source Shortest Paths Problem Definition Shortest paths and Relaxation Dijkstra’s algorithm (can be viewed as a greedy algorithm)

Slides:



Advertisements
Similar presentations
Single Source Shortest Paths
Advertisements

October 31, Algorithms and Data Structures Lecture XIII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
November 14, Algorithms and Data Structures Lecture XIII Simonas Šaltenis Aalborg University
CS138A Single Source Shortest Paths Peter Schröder.
Shortest-paths. p2. Shortest-paths problems : G=(V,E) : weighted, directed graph w : E  R : weight function P=
 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 Graphs Single-Source Shortest Paths (Dijkstra’s Algorithm) PLSD210(ii)
1 Greedy 2 Jose Rolim University of Geneva. Algorithmique Greedy 2Jose Rolim2 Examples Greedy  Minimum Spanning Trees  Shortest Paths Dijkstra.
Lecture 20: Shortest Paths Shang-Hua Teng. Weighted Directed Graphs Weight on edges for distance
Lecture 19: 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
Shortest Paths Definitions Single Source Algorithms –Bellman Ford –DAG shortest path algorithm –Dijkstra All Pairs Algorithms –Using Single Source Algorithms.
1 8-ShortestPaths Shortest Paths in a Graph Fundamental Algorithms.
Graph Algorithms: Shortest Path We are given a weighted, directed graph G = (V, E), with weight function w: E R mapping.
Shortest Paths Definitions Single Source Algorithms
DAST 2005 Tirgul 12 (and more) sample questions. DAST 2005 Q.We’ve seen that solving the shortest paths problem requires O(VE) time using the Belman-Ford.
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.
Dijkstra’s Algorithm Slide Courtesy: Uwash, UT 1.
CS 253: Algorithms Chapter 24 Shortest Paths Credit: Dr. George Bebis.
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.
Shortest Paths Introduction to Algorithms Shortest Paths CSE 680 Prof. Roger Crawfis.
Theory of Computing Lecture 7 MAS 714 Hartmut Klauck.
Topological Sorting and Least-cost Path Algorithms.
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.
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.
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.
Lecture 13 Algorithm Analysis
Greedy Algorithms Z. GuoUNC Chapel Hill CLRS CH. 16, 23, & 24.
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])
Algorithms and Data Structures Lecture XIII
Minimum Spanning Trees
Challenging Problem 2: Challenging problem 2 will be given on the website (week 4) at 9:30 am on Saturday (Oct 1, 2005). 2. The due time is Sunday (Oct.
SINGLE-SOURCE SHORTEST PATHS
Algorithms (2IL15) – Lecture 5 SINGLE-SOURCE SHORTEST PATHS
Announcement 2: A 2 hour midterm (open book) will be given on March (Tuesday) during the lecture time. 2018/12/4.
Page 620 Single-Source Shortest Paths
2018/12/27 chapter25.
Advanced Algorithms Analysis and Design
2-optimal Euler path problem Euler circuit in a directed graph:
Algorithms and Data Structures Lecture XIII
Lecture 13 Algorithm Analysis
Lecture 13 Algorithm Analysis
Lecture 13 Algorithm Analysis
Lecture 13 Algorithm Analysis
2019/2/25 chapter25.
Shortest Path Problems
Evaluation of the Course (Modified)
Chapter 24: Single-Source Shortest Paths
Advanced Algorithms Analysis and Design
Graph Algorithms: Shortest Path
Chapter 24: Single-Source Shortest Paths
Negative-Weight edges:
Negative-Weight edges:
Negative-Weight edges:
CS 3013: DS & Algorithms Shortest Paths.
Topological Sorting Minimum Spanning Trees Shortest Path
Presentation transcript:

chapter Single-Source Shortest Paths Problem Definition Shortest paths and Relaxation Dijkstra’s algorithm (can be viewed as a greedy algorithm)

chapter252 Problem Definition: Real problem: A motorist wishes to find the shortest possible route from Chicago to Boston.Given a road map of the United States on which the distance between each pair of adjacent intersections is marked, how can we determine this shortest route? Formal definition: Given a directed graph G=(V, E, W), where each edge has a weight, find a shortest path from s to v for some interesting vertices s and v. s—source v—destination.

chapter253 Find a shortest path from station A to station B. -need serious thinking to get a correct algorithm. A B

chapter254 Shortest path: The weight of path p= is the sum of the weights of its constituent edges: The cost of the shortest path from s to v is denoted as  (s, v).

chapter255 Negative-Weight edges: Edge weight may be negative. negative-weight cycles– the total weight in the cycle (circuit) is negative. If no negative-weight cycles reachable from the source s, then for all v  V, the shortest-path weight remains well defined,even if it has a negative value. If there is a negative-weight cycle on some path from s to v, we define = -.

chapter256 Figure1 Negative edge weights in a directed graph.Shown within each vertex is its shortest-path weight from source s.Because vertices e and f form a negative-weight cycle reachable from s,they have shortest-path weights of -. Because vertex g is reachable from a vertex whose shortest path is -,it,too,has a shortest-path weight of -.Vertices such as h, i,and j are not reachable from s,and so their shortest-path weights are, even though they lie on a negative-weight cycle hi j s ab cd e f g

chapter257 Representing shortest paths: we maintain for each vertex v  V, a predecessor [ v] that is the vertex in the shortest path right before v. With the values of, a backtracking process can give the shortest path. (We will discuss that after the algorithm is given)

chapter258 Observation: (basic) Suppose that a shortest path p from a source s to a vertex v can be decomposed into s u v for some vertex u and path p’. Then, the weight of a shortest path from s to v is We do not know what is u for v, but we know u is in V and we can try all nodes in V in O(n) time. Also, if u does not exist, the edge (s, v) is the shortest. Question: how to find (s, u), the first shortest from s to some node?

chapter259 Relaxation: The process of relaxing an edge (u,v) consists of testing whether we can improve the shortest path to v found so far by going through u and,if so,updating d[v] and [v]. RELAX(u,v,w) if d[v]>d[u]+w(u,v) then d[v] d[u]+w(u,v) (based on observation) [v] u

chapter2510 Figure2 Relaxation of an edge (u,v).The shortest-path estimate of each vertex is shown within the vertex. (a)Because d[v]>d[u]+w(u,v) prior to relaxation, the value of d[v] decreases. (b)Here, d[v] d[u]+w(u,v) before the relaxation step,so d[v] is unchanged by relaxation. 59 uv 57 uv 56 uv 56 uv (a) (b) RELAX(u,v)

chapter2511 Initialization: For each vertex v  V, d[v] denotes an upper bound on the weight of a shortest path from source s to v. d[v]– will be  (s, v) after the execution of the algorithm. initialize d[v] and  [v] as follows:. INITIALIZE-SINGLE-SOURCE(G,s) for each vertex v  V[G] do d[v] [v] NIL d[s] 0

chapter2512 Dijkstra’s Algorithm: Dijkstra’s algorithm assumes that w(e)  0 for each e in the graph. maintain a set S of vertices such that –Every vertex v  S, d[v]=  (s, v), i.e., the shortest-path from s to v has been found. (Intial values: S=empty, d[s]=0 and d[v]=  ) (a) select the vertex u  V-S such that d[u]=min {d[x]|x  V-S}. Set S=S  {u} (b) for each node v adjacent to u do RELAX(u, v, w). Repeat step (a) and (b) until S=V.

chapter2513 Continue: DIJKSTRA(G,w,s): INITIALIZE-SINGLE-SOURCE(G,s) S Q V[G] while Q do u EXTRACT -MIN(Q) S S {u} for each vertex v  Adj[u] do RELAX(u,v,w)

chapter2514 Implementation: a priority queue Q stores vertices in V-S, keyed by their d[] values. the graph G is represented by adjacency lists.

chapter s uv x y (a)

chapter /s 10/s s uv x y 8 8 (b) (s,x) is the shortest path using one edge. It is also the shortest path from s to x.

chapter /x 14/x 5/s 8/x s uv x y (c)

chapter /x 13/y 5/s 8/x s uv x y (d)

chapter /x 9/u 5/s 8/x s uv x y (e)

chapter /x 9/u 5/s 8/x s uv x y (f) Backtracking: v-u-x-s

chapter2521 Theorem: Consider the set S at any time in the algorithm’s execution. For each v  S, the path P v is a shortest s-v path. Proof: We prove it by induction on |S|. 1.If |S|=1, then the theorem holds. (Because d[s]=0 and S={s}.) 2.Suppose that the theorem is true for |S|=k for some k>0. 3.Now, we grow S to size k+1 by adding the node v.

chapter2522 Proof: (continue) Now, we grow S to size k+1 by adding the node v. Let (u, v) be the last edge on our s-v path P v, i.e., d[v]=d[u]+w(u, v). Consider any other path from P: s,…,x,y, …, v. (red in the Fig.) Case 1: y is the first node that is not in S and x  S. Since we always select the node with the smallest value d[] in the algorithm, we have d[v]  d[y]. Moreover, the length of each edge is  0. Thus, the length of P  d[y]  d[v]. That is, the length of any path  d[v]. s x y uv Set S Case 2: such a y does not exist. d[v]=d[u]+w(u, v)  d[x]+w(x, v). That is, the length of any path  d[v].

chapter2523 The algorithm does not work if there are negative weight edges in the graph s v u S->v is shorter than s->u, but it is longer than s->u->v.

chapter2524 Time complexity of Dijkstra’s Algorithm: Time complexity depends on implementation of the Queue. Method 1: Use an array to story the Queue EXTRACT -MIN(Q) --takes O(|V|) time. –Totally, there are |V| EXTRACT -MIN(Q)’s. – time for |V| EXTRACT -MIN(Q)’s is O(|V| 2 ). RELAX(u,v,w) --takes O(1) time. –Totally |E| RELAX(u, v, w)’s are required. – time for |E| RELAX(u,v,w)’s is O(|E|). Total time required is O(|V| 2 +|E|)=O(|V| 2 ) Backtracking with  [] gives the shortest path in inverse order. Method 2: The priority queue is implemented as a adaptable heap. It takes O(log n) time to do EXTRACT- MIN(Q) as well as | RELAX(u,v,w). The total running time is O(|E|log |V| ).