Lecture 13 Algorithm Analysis

Slides:



Advertisements
Similar presentations
Advanced Algorithm Design and Analysis Jiaheng Lu Renmin University of China
Advertisements

Single Source Shortest Paths
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.
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.
Shortest Paths Algorithm Design and Analysis Week 7 Bibliography: [CLRS] – chap 24 [CLRS] – chap 25.
Introduction to Algorithms 6.046J/18.401J/SMA5503
CSE 780 Algorithms Advanced Algorithms Shortest path Shortest path tree Relaxation Bellman-Ford Alg.
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.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 15 Shortest paths algorithms Properties of shortest paths Bellman-Ford algorithm.
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.
SINGLE-SOURCE SHORTEST PATHS
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.
Graphs – Shortest Path (Weighted Graph) ORD DFW SFO LAX
David Luebke 1 9/10/2015 ITCS 6114 Single-Source Shortest Path.
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.
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.
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
Greedy Algorithms Z. GuoUNC Chapel Hill CLRS CH. 16, 23, & 24.
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.
Single Source Shortest Paths Chapter 24 CSc 4520/6520 Fall 2013 Slides adapted from George Bebis, University of Reno, Nevada.
Shortest paths Given: A single source vertex (given s) in a weighted, directed graph. Want to compute a shortest path for each possible destination. (Single.
Lecture 12 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
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])
David Luebke 1 11/21/2016 CS 332: Algorithms Minimum Spanning Tree Shortest Paths.
Algorithms and Data Structures Lecture XIII
Minimum Spanning Tree Shortest Paths
CS200: Algorithm Analysis
Lecture 12 Algorithm Analysis
Graph Search Algorithms
Minimum Spanning Trees
Lecture 10 Algorithm Analysis
SINGLE-SOURCE SHORTEST PATHS
Algorithms (2IL15) – Lecture 5 SINGLE-SOURCE SHORTEST PATHS
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
CS 583 Analysis of Algorithms
Advanced Algorithms Analysis and Design
Algorithms and Data Structures Lecture XIII
Lecture 13 Algorithm Analysis
Lecture 13 Algorithm Analysis
Lecture 12 Algorithm Analysis
CSC 413/513: Intro to Algorithms
Advanced Algorithms Analysis and Design
Lecture 13 Algorithm Analysis
Shortest Path Problems
Chapter 24: Single-Source Shortest Paths
Advanced Algorithms Analysis and Design
Chapter 24: Single-Source Shortest Paths
Lecture 12 Algorithm Analysis
CS 3013: DS & Algorithms Shortest Paths.
Chapter 24: Single-Source Shortest-Path
Topological Sorting Minimum Spanning Trees Shortest Path
Advanced Algorithms Analysis and Design
Presentation transcript:

Lecture 13 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea

Single Source Shortest Paths

Shortest Paths Problem: How to find the shortest route between two points on a map? Input: Directed graph G = (V, E) Weight function w : E → R Weight of path p = <v0, v1, . . . , vk> : = sum of edge weights on path p Algorithm Analysis

Shortest Paths (cont.) Shortest-path weight u to v:| Shortest path u to v is any path p such that w(p) = δ(u, v). Algorithm Analysis

Example Shortest paths from s Example shows that the shortest path might not be unique. It also shows that when we look at shortest paths from one vertex to all other vertices, the shortest paths are organized as a tree. Algorithm Analysis

Variants of Shortest Path Problem Single-source: Find shortest paths from a given source vertex s ∈ V to every vertex v ∈ V. Single-destination: Find shortest paths to a given destination vertex. Single-pair: Find shortest path from u to v. No way known that’s better in worst case than solving single-source. All-pairs: Find shortest path from u to v for all u, v ∈ V. Algorithm Analysis

Negative-Weight Edges OK if the negative-weight cycle is not reachable from the source If we have a negative-weight cycle, we can just keep going around it, and get w(s, v) = −∞ for all v on the cycle Some algorithms work only if there are no negative-weight edges in the graph. Algorithm Analysis

Optimal Substructure Lemma: Any subpath of a shortest path is a shortest path. Proof Cut-and-paste technique: Shortest path form u to v: We show that the assumption, that there is a shorter path form x to y, leads to a contradiction. Algorithm Analysis

Cycles Shortest paths can not contain cycles: Already ruled out negative-weight cycles. Positive-weight ⇒ we can get a shorter path by omitting the cycle. Zero-weight: no reason to use them ⇒ assume that our solutions won’t use them. Algorithm Analysis

Output of Single-Source Shortest-Path Algorithm For each vertex v ∈ V: d[v] = δ(s, v). Initially, d[v]=∞. Reduces as algorithms progress. But always maintain d[v] ≥ δ(s, v). Call d[v] a shortest-path estimate. π[v] = predecessor of v on a shortest path from s. If no predecessor, π[v] = NIL. π induces a tree. shortest-path tree. Algorithm Analysis

Initialization All the shortest-paths algorithms start with INIT-SINGLE-SOURCE: Algorithm Analysis

Relaxing an edge (u, v) Algorithm Analysis

Relaxing an edge (cont.) All the single-source shortest-paths algorithms will start by calling INIT-SINGLE-SOURCE and then relax edges. The algorithms differ in the order and how many times they relax each edge. Algorithm Analysis

Shortest-Paths Properties Triangle inequality For all (u, v) ∈ E, we have δ(s, v) ≤ δ(s, u) + w(u, v). Proof Weight of shortest path from s to v is ≤ weight of any path from s to v. Path from s to u and then v is a path from s to v, and if we use a shortest path from s to u, its weight is δ(s, u) + w(u, v). Algorithm Analysis

Shortest-Paths Properties (cont.) Upper-bound property (for Relaxing edges) We always have d[v] ≥ δ(s, v) for all v. Once d[v] = δ(s, v), it never changes. No-path property (Corollary of Upper-bound prop.) If δ(s, v)=∞, then always d[v]=∞ Convergence property If the path from s to u and finally v is a shortest path, d[u] = δ(s, u), and we call RELAX(u,v,w), then d[v] = δ(s, v) afterward. Path relaxation property Let p = <v0, v1, . . . , vk> be a shortest path from s = v0 to vk . If we relax, in order, (v0, v1), (v1, v2), . . . , (vk−1, vk), even intermixed with other relaxations, then d[vk ] = δ(s, vk ). Algorithm Analysis

Bellman-Ford Algorithm Allows negative-weight edges. Computes d[v] and π[v] for all v ∈ V. Returns TRUE if no negative-weight cycles reachable from s, FALSE otherwise Algorithm Analysis

Bellman-Ford Algorithm Core: The first for loop relaxes all edges |V| − 1 times. Time: Θ(VE). Algorithm Analysis

Bellman-Ford Algorithm / Example Values you get on each pass and how quickly it converges depends on order of relaxation. But guaranteed to converge after |V| − 1 passes, assuming no negative-weight cycles. Algorithm Analysis

Correctness of Bellman-Ford Algorithm Proof Use path-relaxation property. Let v be reachable from s, and let p = <v0, v1, . . . , vk> be a shortest path from s to v, where v0 = s and vk = v. Since p is acyclic, it has ≤ |V| − 1 edges, so k ≤ |V| − 1. Each iteration of the for loop relaxes all edges: First iteration relaxes (v0, v1). Second iteration relaxes (v1, v2). kth iteration relaxes (vk−1, vk). By the path-relaxation property, d[v] = d[vk ] = δ(s, vk ) = δ(s, v). Algorithm Analysis

Single-Source Shortest Paths in a Directed Acyclic Graph Since a dag, there are no negative-weight cycles Algorithm Analysis

Single-Source Shortest Paths in a DAG / Example Time: Θ(V + E). Algorithm Analysis

Single-Source Shortest Paths in a DAG / Correctness Because we process vertices in topologically sorted order, edges of any path must be relaxed in order of appearance in the path. ⇒ Edges on any shortest path are relaxed in order. ⇒ By path-relaxation property, correct. Algorithm Analysis

Dijkstra’s Algorithm

Dijkstra’s Algorithm / Basics No negative-weight edges. Essentially a weighted version of breadth-first search Instead of a FIFO queue, uses a priority queue Keys are shortest-path weights (d[v]) Have two sets of vertices: S = vertices whose final shortest-path weights are determined, Q = priority queue = V − S Algorithm Analysis

Dijkstra’s Algorithm / Pseudocode (Includes a call of DECREASE-KEY) Algorithm Analysis

Dijkstra’s Algorithm / Example Algorithm Analysis

Correctness Loop Invariant: At the start of each iteration of the while loop, d[v] = δ(s, v) for all v ∈ S. Initialization: Initially, S = ∅, so trivially true. Termination: At end, Q = ∅⇒S = V ⇒d[v] = δ(s, v) for all v ∈ V. Algorithm Analysis

Correctness / Maintenance (loop invariant) Need to show that d[u] = δ(s, u) when u is added to S in each iteration. Suppose there exists u such that d[u] ≠ δ(s, u). Without loss of generality, let u be the first vertex for which d[u] ≠ δ(s, u) when u is added to S. Observations: • u ≠ s, since d[s] = δ(s, s) = 0. • Therefore, s ∈ S, so S ≠ ∅. • There must be some path from s to u, since otherwise d[u] = δ(s, u) = ∞ by no-path property. Algorithm Analysis

Correctness (cont.) / Maintenance (loop invariant) So, there is a shortest path p from s to u. Just before u is added to S, path p connects a vertex in S (i.e., s) to a vertex in V − S (i.e., u). Let y be first vertex along p that is in V − S, and let x ∈ S be y’s predecessor. Algorithm Analysis

Correctness (cont.) / Maintenance (loop invariant) Claim: d[y] = δ(s, y) when u is added to S. Proof: x ∈ S and u is the first vertex such that d[u] ≠ δ(s, u) when u is added to S ⇒d[x] = δ(s, x) when x is added to S. Relaxed (x, y) at that time, so by the convergence property, d[y] = δ(s, y). Algorithm Analysis

Correctness (cont.) / Maintenance (loop invariant) Now can get a contradiction to d[u] ≠ δ(s, u): y is on shortest path from s to u, and all edge weights are nonnegative ⇒ δ(s, y) ≤ δ(s, u) ⇒ d[y] = δ(s, y) ≤ δ(s, u) ≤ d[u] (upper-bound property) Also, both y and u were in Q when we chose u, so d[u] ≤ d[y] ⇒ d[u] = d[y] Therefore, d[y] = δ(s, y) = δ(s, u) = d[u]. Contradicts assumption that d[u] ≠ δ(s, u). Hence, Dijkstra’s algorithm is correct. Algorithm Analysis

Analysis Like Prim’s algorithm, depends on implementation of priority queue. If binary heap, each operation (EXTRACT-MIN, DECREASE-KEY) takes O(lg V) time⇒ O((E+V) lg V) = O(E lg V). If a Fibonacci heap: Each EXTRACT-MIN takes O(1) amortized time. There are O(V) other operations, taking O(lg V) amortized time each. Therefore, time is O(V lg V + E). Algorithm Analysis