Page 620 Single-Source Shortest Paths

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
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 Graphs Single-Source Shortest Paths (Dijkstra’s Algorithm) PLSD210(ii)
chapter Single-Source Shortest Paths Problem Definition Shortest paths and Relaxation Dijkstra’s algorithm (can be viewed as a greedy algorithm)
1 Greedy 2 Jose Rolim University of Geneva. Algorithmique Greedy 2Jose Rolim2 Examples Greedy  Minimum Spanning Trees  Shortest Paths Dijkstra.
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.
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
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.
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.
CS 253: Algorithms Chapter 24 Shortest Paths Credit: Dr. George Bebis.
Theory of Computing Lecture 7 MAS 714 Hartmut Klauck.
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.
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.
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)
Lecture 13 Algorithm Analysis
Single Source Shortest Paths Chapter 24 CSc 4520/6520 Fall 2013 Slides adapted from George Bebis, University of Reno, Nevada.
David Luebke 1 11/21/2016 CS 332: Algorithms Minimum Spanning Tree Shortest Paths.
CSC317 Shortest path algorithms
Single-Source Shortest Path
Algorithms and Data Structures Lecture XIII
Minimum Spanning Tree Shortest Paths
Introduction to Graphs
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.
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
Basic Graph Algorithms
2018/12/27 chapter25.
Advanced Algorithms Analysis and Design
2-optimal Euler path problem Euler circuit in a directed graph:
Lecture 11 Topics Application of BFS Shortest Path
Algorithms and Data Structures Lecture XIII
Lecture 13 Algorithm Analysis
Lecture 13 Algorithm Analysis
Minimum Spanning Tree Algorithms
CSC 413/513: Intro to Algorithms
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
Review of the second half
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.
Chapter 24: Single-Source Shortest-Path
Single-Source Shortest Path & Minimum Spanning Trees
Chapter 9 Graph algorithms
Topological Sorting Minimum Spanning Trees Shortest Path
Presentation transcript:

Page 620 Single-Source Shortest Paths Problem Definition Shortest paths and Relaxation Dijkstra’s algorithm (can be viewed as a greedy algorithm) 2018/12/4 chapter25

Find a shortest path from station A to station B. -need serious thinking to get a correct algorithm. 2018/12/4 chapter25

Adjacency-list representation Let G=(V, E) be a graph. V– set of nodes (vertices) E– set of edges. For each uV, the adjacency list Adj[u] contains all nodes in V that are adjacent to u. 2 1 5 4 3 / (a) (b) 2018/12/4 chapter25

Adjacency-matrix representation Assume that the nodes are numbered 1, 2, …, n. The adjacency-matrix consists of a |V||V| matrix A=(aij) such that aij= 1 if (i,j) E, otherwise aij= 0. 2 1 5 4 3 (a) 0 1 0 0 1 1 0 1 1 1 0 1 0 1 0 0 1 1 0 1 1 1 0 1 0 1 2 3 4 5 (c) 2018/12/4 chapter25

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 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. 2018/12/4 chapter25

The cost of the shortest path from s to v is denoted as (s, v). The weight of path p=<v0,v1,…,vk > 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). 2018/12/4 chapter25

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 = - . 2018/12/4 chapter25

a b -4 h i 3 -1 2 4 3 c d 6 8 5 -8 3 5 11 g s -3 e 3 f 2 7 j -6 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. 2018/12/4 chapter25

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) 2018/12/4 chapter25

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? 2018/12/4 chapter25

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 2018/12/4 chapter25

u v u v 2 2 5 9 5 6 RELAX(u,v) RELAX(u,v) u v u v 2 2 5 7 5 6 (a) (b) 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. 2018/12/4 chapter25

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 2018/12/4 chapter25

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} D[u]= (s, u) at this moment! Why? (b) for each node v adjacent to u do RELAX(u, v, w). Repeat step (a) and (b) until S=V. 2018/12/4 chapter25

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) 2018/12/4 chapter25

Implementation: a adaptable priority queue Q stores vertices in V-S, keyed by their d[] values. the graph G is represented by adjacency lists so that it takes O(1) time to find an edge (u, v) in (b). 2018/12/4 chapter25

u v 10 5 2 1 3 4 6 9 7 8 s Single Source Shortest Path Problem x y (a) 2018/12/4 chapter25

u v 1 10 8 10 9 s 2 3 4 6 7 5 5 8 2 x y (b) (s,x) is the shortest path using one edge. It is also the shortest path from s to x. 2018/12/4 chapter25

Assume EXTRACT -MIN(Q)=x. (s,x) is the shortest path using one edge. Why? Since (s, x) is the shortest among all edges starting from s. It is also the shortest path from s to x. Proof: (1) Suppose that path P: s->u…->x is the shortest path. Then w (s,u) w(s, x). (2) Since edges have non-negative weight, the total weight of path P is at least w(s,u) w(s, x). (3) So, the edge (s, x) is the shortest path from s to x. 2018/12/4 chapter25

u v 1 8 14 10 9 s 2 3 4 6 7 5 5 7 2 x y (c) 2018/12/4 chapter25

Statement: Suppose S={s, x} and d[y]=min d(v). ……(1) vV-S Then d[y] is the cost of the shortest, i.e., either s->x->y or (s->y) is the shortest path from s to y. Why? If (s, y) is the shortest path, d[y] is correct. Consider that case: s->x->y is the shortest path for y. Proof by contradiction. Assume that s->x->y is not the shortest path and that P1: s->yy->…->y is a shortest path and yyS. (At this moment, we already tried the case for yy=s and yy=x in the alg.) Thus, w(P1)< w(s->x->y). (from assumption: s->x-Y is not the shortest) Since w(e)0 for any e, w(s->yy)<w(P1)<w(s->x->y). Therefore, d[yy]<d[y] and (1) is not true. 2018/12/4 chapter25

u v 1 8 13 10 9 s 2 3 4 6 7 5 5 7 2 x y (d) 2018/12/4 chapter25

7 9 5 8 10 2 1 3 4 6 s u v x y (e) 2018/12/4 chapter25

u v 1 8 9 10 9 s 2 3 4 6 7 5 5 7 2 x y (f) 2018/12/4 chapter25

Theorem: Let S be the set in algorithm and d[y]=min d(v). ……(1) vV-S Then d[y] is the cost of the shortest. (hard part) Proof: Assume that (1) for any v in S, y[v] is the cost of the shortest path. We want to show that d[y] is also the cost of the shortest path after execution of Step (a) If the shortest path from s to v contains vertices in S ONLY, then d[v] is the length of the shortest. Assume that (2) d[y] is NOT the cost of the shortest path from s to y and that P1: s…->yy->…->y is a shortest path and yyS is the 1st node in P1 not in S. Thus, w(P1)<d[y]. So, w(s…->yy)<w(P1). (weight of edge >=0) Thus, w(s…->yy)<w(P1)<d[y]. From (1) and (2), after execution of step (a) d[yy]w(s…->yy). Therefore, d[yy]<d[y] and (1) is not true. 2018/12/4 chapter25

Time complexity of Dijkstra’s Algorithm: Time complexity depends on implementation of the adaptable priority. 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 adaptable priority queue is implemented as a heap. It takes O(log |V|) time to do EXTRACT-MIN(Q) and O(log |V|) time for each RELAX(u, v, w)’s. The total running time is O((|V|+|E|)log |V|)=O(|E|log |V|) assuming the graph is connected. When |E| is O(|V|) the second implementation is better. If |E|=O(|V|2), then the first implementation is better. 2018/12/4 chapter25

Method 3: The priority queue is implemented as a Fibonacci heap Method 3: The priority queue is implemented as a Fibonacci heap. It takes O(log |V|) time to do EXTRACT-MIN(Q) and O(1) time to decrease the key value of an entry. The total running time is O(|V|log |V|+|E|). (not required) 2018/12/4 chapter25