Introduction to Algorithms Jiafen Liu Sept. 2013.

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
Advanced Algorithm Design and Analysis (Lecture 7) SW5 fall 2004 Simonas Šaltenis E1-215b
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)
Design and Analysis of Algorithms Single-source shortest paths, all-pairs shortest paths Haidong Xue Summer 2012, at GSU.
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
The Shortest Path Problem. Shortest-Path Algorithms Find the “shortest” path from point A to point B “Shortest” in time, distance, cost, … Numerous.
CSCI 3160 Design and Analysis of Algorithms Tutorial 2 Chengyu Lin.
1 Discrete Structures & Algorithms Graphs and Trees: II EECE 320.
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
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 Path Algorithms
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.
1 Shortest Path Algorithms. 2 Routing Algorithms Shortest path routing What is a shortest path? –Minimum number of hops? –Minimum distance? There is a.
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE 15 Shortest paths algorithms Properties of shortest paths Bellman-Ford algorithm.
Minimum Spanning Trees. Subgraph A graph G is a subgraph of graph H if –The vertices of G are a subset of the vertices of H, and –The edges of G are a.
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
David Luebke 1 9/10/2015 ITCS 6114 Single-Source Shortest Path.
David Luebke 1 9/13/2015 CS 332: Algorithms S-S Shortest Path: Dijkstra’s Algorithm Disjoint-Set Union Amortized Analysis.
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.
Algorithms: Design and Analysis Summer School 2013 at VIASM: Random Structures and Algorithms Lecture 3: Greedy algorithms Phan Th ị Hà D ươ ng 1.
Graphs, BFS, DFS and More…
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.
Master Method (4. 3) Recurrent formula T(n) = a  T(n/b) + f(n) 1) if for some  > 0 then 2) if then 3) if for some  > 0 and a  f(n/b)  c  f(n) for.
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)
Algorithms LECTURE 14 Shortest Paths II Bellman-Ford algorithm
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
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.
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 All-Pairs Shortest Paths My T. UF.
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.
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
Algorithms (2IL15) – Lecture 5 SINGLE-SOURCE SHORTEST PATHS
Advanced Algorithms Analysis and Design
Algorithms and Data Structures Lecture XIII
Lecture 13 Algorithm Analysis
Lecture 13 Algorithm Analysis
Lecture 13 Algorithm Analysis
Lecture 13 Algorithm Analysis
CS 3013: DS & Algorithms Shortest Paths.
Presentation transcript:

Introduction to Algorithms Jiafen Liu Sept. 2013

Today’s Tasks Shortest Paths –Properties of shortest paths –Dijkstra’s algorithm –Correctness –Analysis –Breadth-first search

Paths in graphs Consider a digraph G= (V, E) with edge- weight function w: E→R. The weight of path p = v 1 →v 2 →…→v k is defined to be Example: w(p)=?

Shortest paths A shortest path from u to v is a path of minimum weight from u to v. The shortest path weight from u to v is defined as δ(u, v) = min{w(p): p is a path from u to v}. In what case there is no shortest path? –Note: δ(u, v) = ∞ if no path from u to v exists. –There is a negative cycle. In other cases, usually a finite set.

Well-definedness of shortest paths If a graph G contains a negative-weight cycle, then some shortest paths may not exist. Example:

Thought of solving Dynamic Programming Greedy Algorithms Characteristics : –Optimal Substructure –Overlapping Subproblems –Greedy choice property

Optimal substructure What is the optimal substructure for this problem? Theorem. A subpath of a shortest path is a shortest path. How to prove? –Cut and paste

What’s the subproblems ? Shortest path going from u to v, maybe involves computing shortest path from u to some intermediate point x, and then from x to v. Different choosing of x 1,x 2 …x n consists overlapping subproblems. How many subproblems are there? |v| 2

Triangle inequality Important for us to make greedy choice Theorem: For all u, v, x ∈ V, we have Figure Illustration:

Single-source shortest paths Problem. From a given source vertex s ∈ V, find the shortest-path weights δ(s, v) for all v ∈ V. If all edge weights w(u, v) are nonnegative, all shortest-path weights must exist. IDEA: Greedy. 1.Maintain a set S of vertices whose shortest-path distances from s are known. (s ∈ S) 2.At each step add the vertex v ∈ V–S whose distance estimate from s is minimal to S. 3.Update the distance estimates of vertices adjacent to v.

Dijkstra’s algorithm

Example

Correctness —Part I Lemma. Initializing d[s]←0 and d[v]←∞ for all v ∈ V–{s} establishes d[v] ≥ δ(s,v) for all v ∈ V, and this invariant is maintained over any sequence of relaxation steps. Any suggestions about how to prove? –Induction Proof. Base case: check the initializing term Induction: suppose that this fails to hold at some point.

Correctness —Part I Suppose not. Let v be the first vertex for which d[v] < δ(s, v), and let u be the vertex that caused d[v] to change: d[v] = d[u] + w(u, v). Then how to induct a contradition? d[v]< δ(s, v) ≤ δ(s, u) + δ(u, v) ≤ δ(s,u) + w(u, v) d[v] = d[u] + w(u, v) Contradiction. supposition triangle inequality sh. path ≤ specific path v is first violation

Correctness —Part II Lemma. Let u be v’s predecessor on a shortest path from s to v: s→…u→v. if d[u]=δ(s, u) and if we relax edge(u, v), Then, we have d[v] = δ(s, v) after the relaxation. Proof. s→…u→v is the shortest path from s to v, so δ(s, v) = w(s→… →u)+ w(u, v) = δ(s, u)+ w(u, v).

Correctness —Part II Suppose that d[v] > δ(s, v) before the relaxation. –By Correctness I, d[v] ≥ δ(s,v) –Otherwise d[v] = δ(s,v), we’re done. Then test d[v] > d[u] + w(u, v) succeeds –because d[v] > δ(s, v) = δ(s, u)+ w(u, v) = d[u] + w(u, v) Then the algorithm sets d[v] = d[u]+ w(u,v) = δ(s, v).

Correctness —Part III Theorem. Dijkstra’s algorithm terminates with d[v] = δ(s, v) for all v ∈ V. Something to be addressed: –Once we add a vertex to S, we never change its weight Proof. It suffices to show that d[v] = δ(s, v) for every v ∈ V when v is added to S.

Correctness —Part III Suppose for contradiction: Suppose u is the first vertex added to S for which d[u] ≠ δ(s, u). –d[u] > δ(s, u). Let p be a shortest path from s to u. Now look at the first place here where the path p exits S

Correctness —Part III Let y be the first vertex in V–S along a shortest path from s to u, and let x be its predecessor. Since u is the first vertex violating the claimed invariant, we have d[x] = δ(s, x).

Correctness —Part III When x was added to S, the edge (x, y) was relaxed, which implies that d[y] = δ(s, y) ≤ δ(s, u) < d[u] But, d[u] ≤ d[y] by our choice of u. Contradiction.

Analysis of Dijkstra

Note: Same formula as in the analysis of Prim’s minimum spanning tree algorithm.

Unweighted graphs Suppose that w(u, v) =1 for all (u, v) ∈ E. Can Dijkstra’s algorithm be improved? –Use a simple FIFO queue instead of a priority queue. –Analysis: Time = O(V+ E).

Example

Correctness of BFS IDEA: The FIFO Q in breadth-first search mimics the priority queue Q in Dijkstra. Invariant: v comes after u in Q implies that d[v] = d[u] or d[v] = d[u] + 1.

Step Further I

Bellman-Ford algorithm

Example of Bellman-Ford

Step Further II All-pairs shortest paths Input: Digraph G= (V, E), where V= {1, 2, …, n}, with edge-weight function w: E→R. Output: n ×nmatrix of shortest-path lengths δ(i, j)for all i, j ∈ V. Floyd alglrithm

Any idea of this problem? Dynamic programming again! Consider the n×n adjacency matrix A= (a ij ) of the digraph, and define d ij (m) =weight of a shortest path from i to j that uses at most m edges Claim: We have and for m= 1, 2, …, n–1,

Proof of Claim

Matrix multiplication Compute C= A·B, where C, A, and B are n×n matrices Time = Θ(n 3 ) using the standard algorithm. What if we map “+”→“min” and “·”→“+”?