Analysis of Algorithms, I

Similar documents
Algorithms for Data Science

Algorithms for Data Science

Lecture 11: Analysis of Algorithms (CS ) 1

Shortest Path Problem

Minimum Spanning Trees

Week 11: Minimum Spanning trees

Shortest path problems

CSC 8301 Design & Analysis of Algorithms: Warshall s, Floyd s, and Prim s algorithms

CIS 121 Data Structures and Algorithms Minimum Spanning Trees

Breadth-First Search, 1. Slides for CIS 675 DPV Chapter 4. Breadth-First Search, 3. Breadth-First Search, 2

22 Elementary Graph Algorithms. There are two standard ways to represent a

The Shortest Path Problem. The Shortest Path Problem. Mathematical Model. Integer Programming Formulation

2.1 Greedy Algorithms. 2.2 Minimum Spanning Trees. CS125 Lecture 2 Fall 2016

CS 4349 Lecture October 23rd, 2017

6.1 Minimum Spanning Trees

CS 341: Algorithms. Douglas R. Stinson. David R. Cheriton School of Computer Science University of Waterloo. February 26, 2019

1 Shortest Paths. 1.1 Breadth First Search (BFS) CS 124 Section #3 Shortest Paths and MSTs 2/13/2018

Minimum Spanning Trees

22 Elementary Graph Algorithms. There are two standard ways to represent a

CS161 - Minimum Spanning Trees and Single Source Shortest Paths

CSC 373 Lecture # 3 Instructor: Milad Eftekhar

Introduction to Algorithms / Algorithms I Lecturer: Michael Dinitz Topic: Shortest Paths Date: 10/13/15

Algorithms and Data Structures: Minimum Spanning Trees I and II - Prim s Algorithm. ADS: lects 14 & 15 slide 1

Dijkstra s algorithm for shortest paths when no edges have negative weight.

Week 12: Minimum Spanning trees and Shortest Paths

1 Dijkstra s Algorithm

Single Source Shortest Path (SSSP) Problem

from notes written mostly by Dr. Carla Savage: All Rights Reserved

Algorithm Design and Analysis

Algorithms (IX) Yijia Chen Shanghai Jiaotong University

Algorithms (V) Path in Graphs. Guoqiang Li. School of Software, Shanghai Jiao Tong University

Minimum Spanning Trees My T. UF

Greedy Algorithms. At each step in the algorithm, one of several choices can be made.

CSE 502 Class 23. Jeremy Buhler Steve Cole. April BFS solves unweighted shortest path problem. Every edge traversed adds one to path length

1 Shortest Paths. 1.1 Breadth First Search (BFS) CS 124 Section #3 Shortest Paths and MSTs 2/13/2018

CS420/520 Algorithm Analysis Spring 2009 Lecture 14

UC Berkeley CS 170: Efficient Algorithms and Intractable Problems Handout 7 Lecturer: David Wagner February 13, Notes 7 for CS 170

Introduction to Algorithms

Single Source Shortest Path

CS 561, Lecture 10. Jared Saia University of New Mexico

Introduction to Algorithms

Lecture #7. 1 Introduction. 2 Dijkstra s Correctness. COMPSCI 330: Design and Analysis of Algorithms 9/16/2014

CSC 8301 Design & Analysis of Algorithms: Kruskal s and Dijkstra s Algorithms

The Shortest Path Problem

Minimum Spanning Trees and Prim s Algorithm

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

Introduction to Algorithms

Minimum Spanning Trees

Fundamental Algorithms CSCI-GA /Summer Solution to Homework 8

MA 252: Data Structures and Algorithms Lecture 36. Partha Sarathi Mandal. Dept. of Mathematics, IIT Guwahati

2 A Template for Minimum Spanning Tree Algorithms

Dijkstra s Algorithm. Dijkstra s algorithm is a natural, greedy approach towards

CS 561, Lecture 10. Jared Saia University of New Mexico

CS 161 Lecture 11 BFS, Dijkstra s algorithm Jessica Su (some parts copied from CLRS) 1 Review

Undirected Graphs. DSA - lecture 6 - T.U.Cluj-Napoca - M. Joldos 1

CSE 101. Algorithm Design and Analysis Miles Jones Office 4208 CSE Building Lecture 8: Negative Edges

Solution. violating the triangle inequality. - Initialize U = {s} - Add vertex v to U whenever DIST[v] decreases.

Dr. Alexander Souza. Winter term 11/12

UC Berkeley CS 170: Efficient Algorithms and Intractable Problems Handout 8 Lecturer: David Wagner February 20, Notes 8 for CS 170

Lecture 14: Shortest Paths Steven Skiena

Artificial Intelligence

CSci 231 Final Review

tree follows. Game Trees

Algorithms for Minimum Spanning Trees

Shortest Paths. Nishant Mehta Lectures 10 and 11

Title. Ferienakademie im Sarntal Course 2 Distance Problems: Theory and Praxis. Nesrine Damak. Fakultät für Informatik TU München. 20.

Dist(Vertex u, Vertex v, Graph preprocessed) return u.dist v.dist

CSC 1700 Analysis of Algorithms: Minimum Spanning Tree

Shortest Paths. Nishant Mehta Lectures 10 and 11

Minimum-Spanning-Tree problem. Minimum Spanning Trees (Forests) Minimum-Spanning-Tree problem

Dijkstra s Shortest Path Algorithm

Algorithms (VII) Yijia Chen Shanghai Jiaotong University

Lecture 10: Analysis of Algorithms (CS ) 1

Lecture 6 Basic Graph Algorithms

Theory of Computing. Lecture 10 MAS 714 Hartmut Klauck

CSE 101. Algorithm Design and Analysis Miles Jones Office 4208 CSE Building Lecture 6: BFS and Dijkstra s

CS 561, Lecture 9. Jared Saia University of New Mexico

Algorithms (VII) Yijia Chen Shanghai Jiaotong University

Minimum Spanning Trees

Chapter 4. Greedy Algorithms. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

2pt 0em. Computer Science & Engineering 423/823 Design and Analysis of Algorithms. Lecture 04 Minimum-Weight Spanning Trees (Chapter 23)

CSE 431/531: Algorithm Analysis and Design (Spring 2018) Greedy Algorithms. Lecturer: Shi Li

Representations of Weighted Graphs (as Matrices) Algorithms and Data Structures: Minimum Spanning Trees. Weighted Graphs

Announcements Problem Set 5 is out (today)!

Decreasing a key FIB-HEAP-DECREASE-KEY(,, ) 3.. NIL. 2. error new key is greater than current key 6. CASCADING-CUT(, )

CS60020: Foundations of Algorithm Design and Machine Learning. Sourangshu Bhattacharya

Algorithms (VI) Greedy Algorithms. Guoqiang Li. School of Software, Shanghai Jiao Tong University

Introduction to Algorithms. Lecture 11

Unit 2: Algorithmic Graph Theory

CS521 \ Notes for the Final Exam

Lecture 4: Graph Algorithms

2.3 Optimal paths. E. Amaldi Foundations of Operations Research Politecnico di Milano 1

Minimum Spanning Trees Shortest Paths

(Refer Slide Time: 00:18)

Parallel Graph Algorithms

Algorithms. All-Pairs Shortest Paths. Dong Kyue Kim Hanyang University

Minimum Spanning Trees

2.3 Optimal paths. Optimal (shortest or longest) paths have a wide range of applications:

1 i n (p i + r n i ) (Note that by allowing i to be n, we handle the case where the rod is not cut at all.)

CSE331 Introduction to Algorithms Lecture 15 Minimum Spanning Trees

Transcription:

Analysis of Algorithms, I CSOR W4231.002 Eleni Drinea Computer Science Department Columbia University Thursday, March 8, 2016

Outline 1 Recap Single-source shortest paths in graphs with real edge weights: a DP solution (Bellman-Ford) 2 An alternative formulation of the Bellman-Ford algorithm 3 All-pairs shortest paths (real weights): Floyd-Warshall 4 Minimum Spanning Trees (MSTs) Prim s algorithm

Today 1 Recap Single-source shortest paths in graphs with real edge weights: a DP solution (Bellman-Ford) 2 An alternative formulation of the Bellman-Ford algorithm 3 All-pairs shortest paths (real weights): Floyd-Warshall 4 Minimum Spanning Trees (MSTs) Prim s algorithm

Review of the last lecture Input: weighted directed graph G = (V, E, w), w : E R; a source (origin) vertex s V Output: Decide if G has a negative cycle If no negative cycles 1. find the costs (weights) of shortest s-v paths for all v V 2. reconstruct shortest s-v paths Why Dijsktra s algorithm fails s 5 2 a b 1-4 c

Bigger problems in graphs with negative edges? s 5 2 a b 1-4 1 c dist(a) =?

Bigger problems in graphs with negative edges? s 5 2 a b 1-4 1 c 1. dist(v) goes to for every v on the cycle 2. no solution to shortest paths when negative cycles need to detect negative cycles

Properties of shortest paths Suppose the problem has a solution for an input graph. Can there be negative cycles in the graph? Can there be positive cycles in the graph? Can the shortest paths contain positive cycles? Consider a shortest s-t path; are its subpaths shortest?

Towards a DP solution Key observation: if there are no negative cycles, a path cannot become cheaper by traversing a cycle. Fact 1. If G has no negative cycles, then there is a shortest s-v path that is simple, thus has at most n 1 edges. Fact 2. The shortest paths problem exhibits optimal substructure. Facts 1 and 2 suggest a DP solution.

Subproblems and recurrence Let OP T (i, v) = cost of a shortest s-v path using at most i edges Then OP T (i, v) = 0, if i = 0, v = s, { if i = 0, v s OP T (i 1, v) min min {OP T (i 1, x) + w(x, v)}, if i > 0 x:(x,v) E

Pseudocode n n dynamic programming table M storing OP T (i, v) Bellman-Ford(G = (V, E, w), s V ) for v V do M[0, v] = end for M[0, s] = 0 for i = 1,..., n 1 do for v V (in any order) do end for end for M[i, v] = min M[i 1, v] min x:(x,v) E { M[i 1, x] + w(x, v) }

Running time & Space Running time: O(nm) Space: Θ(n 2 ) can be improved (coming up) To reconstruct actual shortest paths: keep array prev of size n such that prev[v] = predecessor of v in current shortest s-v path.

Improving space requirements Only need two rows of M. Actually, only need one! (see Remark 1) Thus drop the index i from M[i, v] and only use it as a counter for #repetitions. { { } } M[v] = min M[v], min M[x] + w(x, v) x:(x,v) E Remark 1. Throughout the algorithm, M[v] is the cost of some s-v path. After i repetitions, M[v] is no larger than the cost of the current shortest s-v path with at most i edges. Early termination condition: if at some iteration i no value in M changed, stop. (why?)

Today 1 Recap Single-source shortest paths in graphs with real edge weights: a DP solution (Bellman-Ford) 2 An alternative formulation of the Bellman-Ford algorithm 3 All-pairs shortest paths (real weights): Floyd-Warshall 4 Minimum Spanning Trees (MSTs) Prim s algorithm

An alternative way to view Bellman-Ford s v1 v2 vk-1 v Let P = (s = v 0, v 1, v 2,..., v k = v) be a shortest s-v path. Then P can contain at most n 1 edges. How can we correctly compute dist[v] on this path?

Key observations about subroutine Update(u, v) Recall subroutine Update from Dijkstra s algorithm: Fact 3. Update(u, v) : dist[v] = min{dist[v], dist[u] + w(u, v)} Suppose u is the last node on the shortest s-v path, and suppose dist[u] has been correctly set. The call Update(u, v) returns the correct value for dist[v]. Fact 4. No matter how many times Update(u, v) is performed, it will never make dist[v] too small. That is, Update is a safe operation: performing few extra updates can t hurt.

Example of Bellman-Ford Compute shortest s-v paths in the graph below, for all v V. 4 a 1 s -1 2 c 5 b -1

Main idea Suppose we update the edges on the shortest path P in the order they appear on the path (though not necessarily consecutively). Hence we update (s, v 1 ), (v 1, v 2 ), (v 2, v 3 ),..., (v k 1, v) This sequence of updates correctly computes dist[v 1 ], dist[v 2 ],..., dist[v] (by induction and Fact 3). How can we guarantee that updates will happen in this order?

Bellman-Ford algorithm Update all m edges in the graph, n 1 times in a row. By Fact 4, it is ok to update an edge several times in between. All we care for is that the edges on the path are updated in this particular order. This is guaranteed if we update all edges n 1 times in a row.

Pseudocode We will use Initialize and Update from Dijkstra s algorithm. Initialize(G, s) for v V do dist[v] = prev[v] = NIL end for dist[s] = 0 Update(u, v) if dist[v] > dist[u] + w(u, v) then dist[v] = dist[u] + w(u, v) prev[v] = u end if

Bellman-Ford Bellman-Ford(G = (V, E, w), s) Initialize(G, s) for i = 1,..., n 1 do for (u, v) E do Update(u, v) end for end for Running time? Space?

Detecting negative cycles s 5 2 a b 1-4 1 c 1. dist(v) goes to for every v on the cycle. 2. Any shortest s-v path can have at most n 1 edges. 3. Update every edge n times (instead of n 1): if dist(v) changes for any v V, there is a negative cycle.

Today 1 Recap Single-source shortest paths in graphs with real edge weights: a DP solution (Bellman-Ford) 2 An alternative formulation of the Bellman-Ford algorithm 3 All-pairs shortest paths (real weights): Floyd-Warshall 4 Minimum Spanning Trees (MSTs) Prim s algorithm

All pairs shortest-paths Input: a directed graph G with real edge weights Output: an n n matrix D such that D[i, j] = weight of shortest path from i to j

Solving all pairs shortest-paths 1. Straightforward solution: run Bellman Ford once for every vertex (O(n 2 m) time). 2. Improved solution: a dynamic programming algorithm for generating shortest paths (Floyd Warshall, O(n 3 ) time).

Towards a DP formulation Consider a shortest path P from s to t. This path uses some intermediate vertices: that is, if P = (s, v 1, v 2,..., v k, t), then v 1,..., v k are intermediate vertices. For simplicity, let V = {1, 2, 3,..., n} and consider a shortest path from i to j when intermediate vertices may only be from {1, 2,..., k}. Goal: find the shortest i-j path using {1, 2,..., n} as intermediate vertices.

Example 4 a 1 s -1 2 c 5 b -1 Rename {s, a, b, c} as {1, 2, 3, 4} 4 2 1 1 5-1 3 2-1 4

Examples of shortest paths 2 Shortest (1, 2)-path may use: P 1 (i) no intermediate nodes (P); or 4 P1 (ii) node 3 (P1) 1 5 4 2 P1-1 3 Shortest (1, 3)-path may use: (i) no intermediate nodes (P); or (ii) node 2 (P1) 1 4 2 P1 P1 5-1 1 4 P 3

A shortest i-j path using nodes from {1,..., k} Consider a shortest path P from i to j where intermediate nodes may only be from the set of nodes {1, 2,..., k}. i j Fact: any subpath of P must be shortest itself.

A useful observation Focus on the last node k from this set. Either 1. P completely avoids k: then a shortest i-j path with intermediate nodes from {1,..., k} is the same as a shortest i-j path with intermediate nodes from {1,..., k 1}. 2. Or, k is an intermediate node of P. k i j Decompose P into an i-k subpath P 1 and a k-j subpath P 2. i. P 1, P 2 are shortest subpaths themselves. ii. All intermediate nodes of P 1, P 2 are from {1,..., k 1}.

Subproblems Let OP T k (i, j) = cost of shortest i-j path P using {1,..., k} as intermediate vertices 1. Either k does not appear in P, hence OP T k (i, j) = OP T k 1 (i, j) 2. Or, k appears in P, hence OP T k (i, j) = OP T k 1 (i, k) + OP T k 1 (k, j)

Recurrence Hence w(i, j), if k = 0 OP T k (i, j) = { OP Tk 1 (i, j) min, if k 1 OP T k 1 (i, k) + OP T k 1 (k, j) We want OP T n (i, j). Time/space requirements?

Floyd-Warshall on example graph D 0 = 0 4 5 0-1 1 2 0-1 0 D 1 = 0 4 5 0-1 1 2 0-1 0 D 2 = 0 4 3 5 0-1 1 2 0-1 0 D 3 = 0 4 3 2 0-1 -2 2 0-1 0

Space requirements A single n n dynamic programming table D, initialized to w(i, j) (the adjacency matrix of G). Let {1,..., k} be the set of intermediate nodes that may be used for the shortest i-j path. After the k-th iteration, D[i, j] contains the cost of an i-j path that is no larger than the cost of the shortest i-j path using {1,..., k} as intermediate nodes.

The Floyd-Warshall algorithm Floyd-Warshall(G = (V, E, w)) for k = 1 to n do for i = 1 to n do for j = 1 to n do D[i, j] = min(d[i, j], D[i, k] + D[k, j]) end for end for end for Running time: O(n 3 ) Space: Θ(n 2 )

Today 1 Recap Single-source shortest paths in graphs with real edge weights: a DP solution (Bellman-Ford) 2 An alternative formulation of the Bellman-Ford algorithm 3 All-pairs shortest paths (real weights): Floyd-Warshall 4 Minimum Spanning Trees (MSTs) Prim s algorithm

The problem Motivation: build the cheapest communication network over a set of locations. Input: a weighted, undirected graph G = (V, E, w) Output: a subset of edges E T E such that 1. the graph T = (V, E T ) is connected; 2. w(e) is minimal. e E T

Minimum weight Spanning Trees (MST) Remark 2. The graph T = (V, E T ) is a tree: if there is a cycle, remove any edge from the cycle and obtain a connected graph with less cost. Definition 5 (Spanning tree of a graph G = (V, E)). A tree that spans all the nodes in V. Output (restated): a minimum weight spanning tree of G. Remarks Brute-force won t work: even simple graphs have many spanning trees how many in a simple cycle? #spanning trees in the complete graph on n vertices: n n 2

The cut property Definition 6 (Cut). A cut (S, V S) is a bipartition of the vertices. Claim 1 (Cut property). Assume all edge weights are distinct. Let S V (S ). Let e be the minimum-weight edge with one endpoint in S and the other in V S. Then every MST contains e. Remark 3. The assumption of distinct edge weights is just for the purposes of the analysis; we will show how to remove it later.

Proof of the cut property Notation: w(t ) = e E T w(e) We will derive a contradiction by using an exchange argument. Let T be a minimum-weight spanning tree that does not contain e = (u, v). Then there must be some other path P in T from u to v. Starting at u, follow the vertices of P : since (u, v) crosses from S to V S, there must be some first vertex v V S on P. Let u be the last vertex before it in S. Then e = (u, v ) E T and e crosses between S, V S.

Proof of the cut property (cont d) Exchange e with e to obtain the set of edges T is a spanning tree: E T = E T + {e} {e }. T is connected: any path in T that used e = (u, v ) is rerouted to follow P from u to u, (u, v) and P from v to v. T is acyclic (why?). Since both e and e cross between S and V S but e is the lightest edge with this property, w(e) < w(e ). Thus w(t ) < w(t ).

Using the cut property to design MST algorithms The cut property says: construct MST greedily by taking the lightest edge across two regions not yet connected. Generic-MST(G = (V, E, w)) E T = // the set of edges that will form our MST while E T n 1 do Pick S V s.t. no edge in E T crosses between S, V S Let e E be a lightest edge that crosses between S, V S E T = E T {e} end while

Prim s algorithm In Prim s algorithm, the edges in E T always form a subtree and S is chosen to be the set of this subtree s vertices. In other words: 1. Start with a root node s. 2. Greedily grow a tree outward from s by adding the node that can be attached as cheaply as possible at every step.

Detailed description of Prim s algorithm 1. E t = 2. Maintain a set S V on which a spanning tree has been constructed so far. Initially, S = {s}. 3. In each iteration, update 3.1 S = S {v}, where v is the vertex in V S that minimizes the attachment cost: 3.2 E T = E T {e} min w(e). e=(u,v):u S

Example graph 8 3 1 3 6 6 2 11 5 12 10 8 5 7 4 1 4 7 9 2 9

Prim s MST for example graph (letters indicate the order in which edges were added) 8 3 1 3 6 e f 6 a g 5 2 5 8 7 b d 4 h c 4 2 7 9 1

Prim s: correctness Follows directly from the Cut property: at every iteration an edge (u, v) is added such that u S, v V S (recall that Prim s algorithm sets S to be the set of vertices on which a partial MST has been constructed); (u, v) is the lightest edge that crosses between S and V S.

Implementing Prim s algorithm Similarly to Dijkstra s algorithm, store every node v V S in a priority queue Q, e.g., implemented as a binary min-heap (key= weight of the lightest edge between some node in S and v). Initially, S = {s}. maintain two arrays dist[v]: stores the weight of the lightest edge between v and any vertex in S (in Dijkstra, it stored a conservative overestimate of the distance of v from the source s) prev[v]: stores the node responsible for adding v to S

Pseudocode: how does this compute T = (V, E T )? Prim(G = (V, E, w), s) for u V do dist[v] = ; prev[v] = NIL end for dist[s] = 0 Q = {V ; dist} S = while Q do u = ExtractMin(Q) S = S {u} for (u, v) E and v V S do if dist[v] > w(u, v) then dist[v] = w(u, v) prev[v] = u DecreaseKey(Q, v) end if end for end while

Further implementations of Prim s algorithm Notation: V = n, E = m Insert/ Implementation ExtractMin DecreaseKey Time Array O(n) O(1) O(n 2 ) Binary heap O(log n) O(log n) O((n + m) log n) d-ary heap O(log n) O(log n) O((nd + m) log n log d ) Fibonacci heap O(log n) O(1) amortized O(n log n + m) Optimal choice for d m/n (the average degree of the graph) d-ary heap works well for both sparse and dense graphs If m = n 1+x, what is the running time of Prim s algorithm using a d-ary heap? Amortized analysis: coming up in the next lecture