CSE 202 - Shortest Paths1 Midterm page 1 The worst-case complexity of an algorithm is... a function from N (or size of instance) to N.  (n lg n) is...

Slides:



Advertisements
Similar presentations
Greedy Algorithms Greed is good. (Some of the time)
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.
Design and Analysis of Algorithms Single-source shortest paths, all-pairs shortest paths Haidong Xue Summer 2012, at GSU.
1 Graphs: Traversal Searching/Traversing a graph = visiting the vertices of a graph by following the edges in a systematic way Example: Given a highway.
Chapter 25: All-Pairs Shortest-Paths
More Graph Algorithms Minimum Spanning Trees, Shortest Path Algorithms.
The Shortest Path Problem. Shortest-Path Algorithms Find the “shortest” path from point A to point B “Shortest” in time, distance, cost, … Numerous.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 21: Graphs.
1 Greedy 2 Jose Rolim University of Geneva. Algorithmique Greedy 2Jose Rolim2 Examples Greedy  Minimum Spanning Trees  Shortest Paths Dijkstra.
Chapter 23 Minimum Spanning Trees
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.
CPSC 411, Fall 2008: Set 9 1 CPSC 411 Design and Analysis of Algorithms Set 9: More Graph Algorithms Prof. Jennifer Welch Fall 2008.
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.
CS 311 Graph Algorithms. Definitions A Graph G = (V, E) where V is a set of vertices and E is a set of edges, An edge is a pair (u,v) where u,v  V. If.
Shortest Path Problems
Shortest Paths Definitions Single Source Algorithms –Bellman Ford –DAG shortest path algorithm –Dijkstra All Pairs Algorithms –Using Single Source Algorithms.
1 8a-ShortestPathsMore Shortest Paths in a Graph (cont’d) Fundamental Algorithms.
Chapter 9: Greedy Algorithms The Design and Analysis of Algorithms.
12/3CSE NP-complete CSE Algorithms NP Completeness Approximations.
1 8-ShortestPaths Shortest Paths in a Graph Fundamental Algorithms.
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
1 Advanced Algorithms All-pairs SPs DP algorithm Floyd-Warshall alg.
Shortest Paths Definitions Single Source Algorithms
CSE 780 Algorithms Advanced Algorithms SSSP Dijkstra’s algorithm SSSP in DAGs.
CSE Algorithms Minimum Spanning Trees Union-Find Algorithm
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.
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
All-Pairs Shortest Paths
More Graph Algorithms Weiss ch Exercise: MST idea from yesterday Alternative minimum spanning tree algorithm idea Idea: Look at smallest edge not.
10/31/02CSE Greedy Algorithms CSE Algorithms Greedy Algorithms.
11/26/02CSE FFT,etc CSE Algorithms Polynomial Representations, Fourier Transfer, and other goodies. (Chapters 28-30)
CS 473 All Pairs Shortest Paths1 CS473 – Algorithms I All Pairs Shortest Paths.
10/31/02CSE Greedy Algorithms CSE Algorithms Greedy 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.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Graph Algorithms Shortest-Path.
1 Shortest Path Algorithms Andreas Klappenecker [based on slides by Prof. Welch]
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.
5/27/03CSE Shortest Paths CSE Algorithms Shortest Paths Problems.
1 WEEK 9-10 Graphs II Unweighted Shortest Paths Dijkstra’s Algorithm Graphs with negative costs Acyclic Graphs Izmir University of Economics.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Dijkstras Algorithm Named after its discoverer, Dutch computer scientist Edsger Dijkstra, is an algorithm that solves the single-source shortest path problem.
Minimum Spanning Trees CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
Algorithm Course Dr. Aref Rashad February Algorithms Course..... Dr. Aref Rashad Part: 6 Shortest Path Algorithms.
Single Source Shortest-Path: The General Case (with negative edges) Bellman-Ford algorithm. Iteratively relax all edges |V|-1 times Running time? O(VE).
All-Pairs Shortest Paths & Essential Subgraph 01/25/2005 Jinil Han.
Parallel graph algorithms Antonio-Gabriel Sturzu, SCPD Adela Diana Almasi, SCPD Adela Diana Almasi, SCPD Iulia Alexandra Floroiu, ISI Iulia Alexandra Floroiu,
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.
All-pairs Shortest Paths. p2. The structure of a shortest path: All subpaths of a shortest path are shortest paths. p : a shortest path from vertex i.
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.
Data Structures & Algorithms Shortest Paths Richard Newman based on book by R. Sedgewick and slides by S. Sahni.
1 Prim’s algorithm. 2 Minimum Spanning Tree Given a weighted undirected graph G, find a tree T that spans all the vertices of G and minimizes the sum.
Lecture 13 Algorithm Analysis
1 Greedy Technique Constructs a solution to an optimization problem piece by piece through a sequence of choices that are: b feasible b locally optimal.
CS 361 – Chapter 14 Weighted graph –How would you represent? Shortest path algorithms from 1 vertex to any other: –General –Handling negative weight –Acyclic.
Introduction to Algorithms All-Pairs Shortest Paths My T. UF.
Suppose G = (V, E) is a directed network. Each edge (i,j) in E has an associated ‘length’ c ij (cost, time, distance, …). Determine a path of shortest.
CSCE 411 Design and Analysis of Algorithms Set 9: More Graph Algorithms Prof. Jennifer Welch Spring 2012 CSCE 411, Spring 2012: Set 9 1.
Single Source Shortest Paths Chapter 24 CSc 4520/6520 Fall 2013 Slides adapted from George Bebis, University of Reno, Nevada.
11/21/02CSE Max Flow CSE Algorithms Max Flow Problems.
1 GRAPHS – Definitions A graph G = (V, E) consists of –a set of vertices, V, and –a set of edges, E, where each edge is a pair (v,w) s.t. v,w  V Vertices.
CSE 373: Data Structures and Algorithms Lecture 21: Graphs V 1.
CSE Algorithms Max Flow Problems 11/21/02 CSE Max Flow.
Lecture 6 Shortest Path Problem.
CSE373: Data Structures & Algorithms Lecture 18: Dijkstra’s Algorithm
Algorithms and Data Structures Lecture XIII
CSE 373: Data Structures and Algorithms
Presentation transcript:

CSE Shortest Paths1 Midterm page 1 The worst-case complexity of an algorithm is... a function from N (or size of instance) to N.  (n lg n) is... a set of functions. A random variable is... a function from elementary event to real number. Polyhash is... a set of (hash) functions. T(n) = 3 T(n/2) + n T(n) is  (n lg3 ) T(n) = 3 T(n/3) + 5 T(n) is  (n) T(n) = 3 T(n/4) + n lg n T(n) is  (n lg n) False A heap of height n can have 2 n+1 –1 nodes False A tree of height k might only have k+1 nodes True A red-black can implement a priority queue efficiently

CSE Shortest Paths2 Midterm page 2 Yes, you can implement “Decrease-Key(S,x,k)” in a max heap efficiently. Go to node x (which you can do since you have a pointer to it), change its priority to k, apply Heapify (which “bubbles downward”). Given a red-black tree... in black subtree –Every non-leaf has 2, 3, or 4 children (by P3). –Every leaf has depth k (by P4). –Thus # black nodes  2 k+1 – 1 (obvious, or by induction) In full red-black tree, N  # black nodes  2 k+1 – 1 –So lg N  lg (2 k+1 – 1)  lg 2 k = k; that is, k < lg N. –Longest path  2 k (by P4). –So height tree = longest path  2 lg N. NOTE: Just saying “Tree is balanced” is insufficient – we never even really defined what “balanced” means.

CSE Shortest Paths3 Midterm page 3 If keys are more or less uniformly randomly distributed (so Bucket Sort take O(N) time) and K is large (or grows with N, so KN isn’t O(N)), then Bucket Sort will be faster than Radix Sort. If the keys are very skewed, and K is small, Radix Sort will be faster (actually, all you really need is that K is very small, that is, K << lg N). Greedy change-making can be arbitrarily bad. Given any c, let D 2 = 2c, D 3 = 2c+1, and N = 4c. Then greedy will give one D 3 and 2c-1 D 1 ’s, or 2c coins. But optimal is to give two D 2 ’s, or 2 coins. So greedy is c times worse.

CSE Shortest Paths4 Midterm page 5 Let A(i) = best sum of numbers in the first i columns that doesn’t use any entry in column i. B(i) = best sum [of...] that uses only the top entry in column i C(i) = best sum that uses middle entry in column i D(i) = best sum that uses only the bottom entry in column i E(i) = best sum that uses top and bottom entry of column i. I claim, given A(i),..., E(i), we can compute A(i+1),..., E(i+1) in the “obvious” way. This will take  (N) time. E.g. A(i+1) = max(A(i), B(i), C(i), D(i), E(i)) B(i+1) = T(1,i+1) + max(A(i), C(i), D(i)), etc.

CSE Shortest Paths5 Midterm, page 6 Basic idea: use divide an conquer. ktile(A,N,K) { /* print the K-tiles of A[1],..., A[N] */ Let M = select(A, N, K/2); /* takes O(N) time */ Split A by M into Big and Small (each of size N/2); if (K>1) call ktile (Big, N/2, K/2); print M; if (K>1) call ktile (Small, N/2, K/2); } Complexity: T(N,K) = 2T(N/2, K/2) + cN Recursion tree has lg K levels, each requires cN time. So complexity is c N lg K.

11/12/02CSE Shortest Paths CSE Algorithms Shortest Paths Problems

CSE Shortest Paths7 Shortest Paths Problems Given a weighted directed graph, is a path of weight 29 from u to z. is another path from u to z; it has weight 16 and is the shortest path from u to z. u z t y w v x

CSE Shortest Paths8 Variants of Shortest Paths Problems A. Single pair shortest path problem Given s and d, find shortest path from s to d. B. Single source shortest paths problem Given s, for each d find shortest path from s to d. C. All-pairs shortest paths problem For each ordered pair s,d, find shortest path. (1)and (2) seem to have same asymptotic complexity. (3) takes longer, but not as long as repeating (2) for each s.

CSE Shortest Paths9 More Shortest Paths Variants 1.All weights are non-negative. 2.Weights may be negative, but no negative cycles (A cycle is a path from a vertex to itself.) 3.Negative cycles allowed. Algorithm reports “-  ” if there is a negative cycle on path from source to destination (2) and (3) seem to be harder than (1). u w v uvw u058 v-303 w-60 u w v uvw u0-- -- v  -- -- w  -- --

CSE Shortest Paths10 Single Source Shortest Paths Non-negative weights (problem B-1): From source, construct tree of shortest paths. Why is this a tree? Is this a Minimum Spanning Tree? Basic approach: label nodes with shortest path found so far. Relaxation step: choose any edge (u,v). If it gives a shorter path to v, update v ’s label s s relaxing this edge improves this node

CSE Shortest Paths11 Single Source Shortest Paths Simplest strategy: –Keep cycling through all edges –When all edges are relaxed, you’re done. What is upper bound on work? Improvement: We mark nodes when we’re sure we have best path. Key insight: if you relax all edges out of marked nodes, the unmarked node that is closest to source can be marked. This gives Dijkstra’s algorithm Relax edge (u,v) only once - just after u is marked. Keep nodes on min-priority queue – need E Decrease-Key’s. Gives O(E lg V) (or O(E + V lg V) using Fibonacci heap.) There’s a simple O(V 2 ) implementation – when is it fastest?

CSE Shortest Paths12 Single Source Shortest Paths Negative weights allowed (problem B-2 and B-3) Why can’t we just add a constant to each weight? Why doesn’t Dijkstra’s algorithm work for B-2 ? Bellman-Ford: Cycle through edges V-1 times. O(VE) time. PERT chart: Arbitrary weights, graph is acyclic: Is there a better algorithm than Bellman-Ford?

CSE Shortest Paths13 All-pairs Shortest Paths Naïve #1: Solve V single-source problems. –O(V 2 E) for general problem. –O(V 3 ) or O(V E lg V) for non-negative weights. Naïve #2: Let d k (i,j) = shortest path from i to j involving  k edges. d 1 (i,j) = is original weight matrix. Compute d k+1 ’s from d k ’s by seeing if adding edge helps: d k+1 (i,j) = Min { d k (i,m) + d 1 (m,j) } Hmmm...this looks a lot like matrix multiplication If there are no negative cycles, d V-1 (i,j) is solution. If there is a negative cycle, then d V-1  d V Complexity is O(V 4 )

CSE Shortest Paths14 All-pairs Shortest Paths No negative cycles – Divide and Conquer says: “Shortest path from a to b with at most 2 k+1 hops is a shortest path from a to c with at most 2 k hops followed by one from c to b with at most 2 k hops.” T(k+1) = T(k) + V 3 so T(lg V) is O(V 3 lg V). This also looks like matrix multiplication, using d 2k = d k x d k instead of d k+1 = d k x d 1

CSE Shortest Paths15 All-pairs Shortest Paths No negative cycles – Dynamic Programming says: “Number cities 1 to V. Shortest path from a to b that only uses first cities 1 to k+1 as intermediate points is a path that goes from a to k+1 using only cities 1 to k, followed by a path going from k+1 to b (or shortest from a to b avoiding k+1 altogether).” T(k+1)= T(k) + cV 2, and T(0) is 0. Thus, T(V) is O(V 3 ). This is the Floyd-Warshall algorithm

CSE Shortest Paths16 Johnson’s All-Pairs Shortest Paths Motivation: for sparse non-negative weights, “O(V E lg V)” is better than “O(V 3 )” We’ll convert “arbitrary weights” (C3) to “non- negative” (C1) problem via reweighting. Dijkstra V times Floyd-Warshall Starting bonus or finishing penalty

CSE Shortest Paths17 Johnson’s Algorithm Reweighting can make all edges non-negative Make node-weight(x) = shortest path to x from anywhere. Sounds like all-pairs problem Slick trick use Bellman-Ford (only O(VE) time) s

CSE Shortest Paths18 Glossary (in case symbols are weird)            subset  element of  infinity  empty set  for all  there exists  intersection  union  big theta  big omega  summation  >=  <=  about equal  not equal  natural numbers(N)  reals(R)  rationals(Q)  integers(Z)