CSC 373: Algorithm Design and Analysis Lecture 8

Similar documents
Chapter 6. Dynamic Programming

Algorithms IV. Dynamic Programming. Guoqiang Li. School of Software, Shanghai Jiao Tong University

CS 231: Algorithmic Problem Solving

CSC 373 Lecture # 3 Instructor: Milad Eftekhar

Memoization/Dynamic Programming. The String reconstruction problem. CS124 Lecture 11 Spring 2018

Unit-5 Dynamic Programming 2016

CSC 373: Algorithm Design and Analysis Lecture 4

Analysis of Algorithms, I

A4B33ALG 2015/10 ALG 10. Dynamic programming. abbreviation: DP. Sources, overviews, examples see.

CSC2420 Spring 2015: Lecture 2

Algorithm Design Techniques part I

CSC2420 Fall 2012: Algorithm Design, Analysis and Theory

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

CSE 417 Network Flows (pt 4) Min Cost Flows

Artificial Intelligence

1 Dynamic Programming

Algorithms for Data Science

Lecture 18 Solving Shortest Path Problem: Dijkstra s Algorithm. October 23, 2009

CSE 417 Dynamic Programming (pt 4) Sub-problems on Trees

Algorithms for Data Science

CAD Algorithms. Categorizing Algorithms

Dijkstra s Algorithm Last time we saw two methods to solve the all-pairs shortest path problem: Min-plus matrix powering in O(n 3 log n) time and the

15.Dynamic Programming

Module 27: Chained Matrix Multiplication and Bellman-Ford Shortest Path Algorithm

CS 491 CAP Intermediate Dynamic Programming

1 Variations of the Traveling Salesman Problem

Computer Sciences Department 1

Shortest path problems

6. Algorithm Design Techniques

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.)

CSC 373: Algorithm Design and Analysis Lecture 3

Chapter 9 Graph Algorithms

CSE 421: Intro Algorithms. Winter 2012 W. L. Ruzzo Dynamic Programming, I Intro: Fibonacci & Stamps

Dynamic Programming II

1 Format. 2 Topics Covered. 2.1 Minimal Spanning Trees. 2.2 Union Find. 2.3 Greedy. CS 124 Quiz 2 Review 3/25/18

Chapter 9 Graph Algorithms

Final Exam Solutions

Greedy Algorithms. Informal Definition A greedy algorithm makes its next step based only on the current state and simple calculations on the input.

Longest Common Subsequence, Knapsack, Independent Set Scribe: Wilbur Yang (2016), Mary Wootters (2017) Date: November 6, 2017

Dynamic Programming Intro

IN101: Algorithmic techniques Vladimir-Alexandru Paun ENSTA ParisTech

Design and Analysis of Algorithms 演算法設計與分析. Lecture 7 April 6, 2016 洪國寶

Advanced Algorithms Class Notes for Monday, November 10, 2014

Design and Analysis of Algorithms 演算法設計與分析. Lecture 7 April 15, 2015 洪國寶

Dynamic Programming Shabsi Walfish NYU - Fundamental Algorithms Summer 2006

1 More on the Bellman-Ford Algorithm

Advanced Operations Research Prof. G. Srinivasan Department of Management Studies Indian Institute of Technology, Madras

Lecture 8. Dynamic Programming

Department of Computer Applications. MCA 312: Design and Analysis of Algorithms. [Part I : Medium Answer Type Questions] UNIT I

Lecture 8: The Traveling Salesman Problem

Question Points Score TOTAL 55 TOTAL (Calibrated) 50

Part I Basic Concepts 1

Framework for Design of Dynamic Programming Algorithms

Last week: Breadth-First Search

Combinatorial Search; Monte Carlo Methods

6.231 DYNAMIC PROGRAMMING LECTURE 15 LECTURE OUTLINE

8/19/13. Computational problems. Introduction to Algorithm

Introduction to Algorithms / Algorithms I Lecturer: Michael Dinitz Topic: Dynamic Programming I Date: 10/6/16

CSE 521: Algorithms. Dynamic Programming, I Intro: Fibonacci & Stamps. W. L. Ruzzo

Lecture 13: Divide and Conquer (1997) Steven Skiena. skiena

Chapter 9 Graph Algorithms

CS483 Design and Analysis of Algorithms

Dynamic Programming. Introduction, Weighted Interval Scheduling, Knapsack. Tyler Moore. Lecture 15/16

Graphs and Algorithms 2016

11/22/2016. Chapter 9 Graph Algorithms. Introduction. Definitions. Definitions. Definitions. Definitions

Chapter 6. Dynamic Programming. Modified from slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

Monte Carlo Methods; Combinatorial Search

14 Dynamic. Matrix-chain multiplication. P.D. Dr. Alexander Souza. Winter term 11/12

Algorithms for Integer Programming

Scribe: Virginia Williams, Sam Kim (2016), Mary Wootters (2017) Date: May 22, 2017

In this lecture, we ll look at applications of duality to three problems:

Algorithms (I) Introduction. Guoqiang Li. School of Software, Shanghai Jiao Tong University

15-451/651: Design & Analysis of Algorithms January 26, 2015 Dynamic Programming I last changed: January 28, 2015

Theorem 2.9: nearest addition algorithm

Dynamic Programming. Outline and Reading. Computing Fibonacci

Lecture 13. Reading: Weiss, Ch. 9, Ch 8 CSE 100, UCSD: LEC 13. Page 1 of 29

Solutions for the Exam 6 January 2014

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

Algorithms. Algorithms ALGORITHM DESIGN

CSC2420 Fall 2012: Algorithm Design, Analysis and Theory

Lectures by Volker Heun, Daniel Huson and Knut Reinert, in particular last years lectures

Greedy Algorithms 1. For large values of d, brute force search is not feasible because there are 2 d

12 Dynamic Programming (2) Matrix-chain Multiplication Segmented Least Squares

Final. Name: TA: Section Time: Course Login: Person on Left: Person on Right: U.C. Berkeley CS170 : Algorithms, Fall 2013

Algorithm Design and Analysis

Efficient Sequential Algorithms, Comp309. Problems. Part 1: Algorithmic Paradigms

Dynamic Programming Algorithms

CMSC 451: Dynamic Programming

Greedy Algorithms CHAPTER 16

Lecture 22: Dynamic Programming

CMSC 451: Lecture 10 Dynamic Programming: Weighted Interval Scheduling Tuesday, Oct 3, 2017

Design and Analysis of Algorithms - - Assessment

Greedy algorithms is another useful way for solving optimization problems.

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING QUESTION BANK UNIT-III. SUB NAME: DESIGN AND ANALYSIS OF ALGORITHMS SEM/YEAR: III/ II PART A (2 Marks)

CS599: Convex and Combinatorial Optimization Fall 2013 Lecture 14: Combinatorial Problems as Linear Programs I. Instructor: Shaddin Dughmi

Design and Analysis of Algorithms (I)

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

Lecture 13: Chain Matrix Multiplication

Greedy Algorithms. CLRS Chapters Introduction to greedy algorithms. Design of data-compression (Huffman) codes

Sankalchand Patel College of Engineering - Visnagar Department of Computer Engineering and Information Technology. Assignment

Transcription:

CSC 373: Algorithm Design and Analysis Lecture 8 Allan Borodin January 23, 2013 1 / 19

Lecture 8: Announcements and Outline Announcements No lecture (or tutorial) this Friday. Lecture and tutorials as usual on Monday. First Problem Set is now complete and due Friday, February 1. First Term Test in tutorials on Monday, February 4. Today s outline Reflections on the DP algorithm for the least cost paths problem The all pairs least cost problem and an alternative DP The chain matrix product problem The edit distance problem 2 / 19

Review: A DP with a somewhat different style We considered the single source least cost paths problem which is efficiently solved by Dijkstra s greedy algorithm for graphs in which all edge costs are non-negative. The least cost paths problem is still well defined as long as there are no negative cycles; that is, the least cost path is a simple path. There can also be non simple least cost paths when there are zero cost cycles but there will always be a simple path having the least cost and hence we can restrict attention to such simple paths. Basically the same algorithm can be used to compute the least cost paths from all nodes to a single terminal node t (again assuming no negative cycles). Given the Bellman Ford algorithm for shortest paths, it is not difficult to detect when a directed graph has a negative cost cycle (and find such a cycle if it exists). See, for example, Section 6.10 of KT. 3 / 19

Single source least cost paths for graphs with no negative cycles Following the DP paradigm, we consider the nature of an optimal solution and how it is composed of optimal solutions to subproblems. Consider an optimal simple path P from source s to some node v. This path could be just an edge. But if the path P has length greater than 1, then there is some node u which immediately proceeds v in P. If P is an optimal path to v, then the path leading to u must also be an optimal path. s P v u 4 / 19

Single source least cost paths for graphs with no negative cycles s P v u This leads to the following semantic array: C[i, v] = the minimum cost of a simple path with path length at most i from source s to v. (If there is no such path then this cost is.) The desired answer is then the single dimensional array derived by setting i = n 1. (Any simple path has path length at most n 1.) 5 / 19

How to construct the computational array? We can construct C [i, v] from C [i 1,...] as follows: s C [i 1, v] v c(u, v) C [i 1, u] u Let C [i, v] be the minimum value among C [i 1, v] C [i 1, u] + c(u, v) for all (u, v) E. 6 / 19

Corresponding computational array The computational array is defined as: 0 if i = 0 and v = s C [i, v] = if i = 0 and v s min{a, B} otherwise A = C [i 1, v] { } B = min C [i 1, u] + c(u, v) : (u, v) E Why is this slightly different from before? Namely, showing the equivalence between the semantic and computationally defined arrays is not an induction on the indices of the input items in the solution. But it is based on some other parameter (i.e. the path length) of the solution. Time complexity: n 2 entries O(n) per entry = O(n 3 ) in total. 7 / 19

Computing maximum cost path using the same DP? To define this problem properly we want to say maximum cost simple path since cycles will add to the cost of a path. (For least cost we did not have to specify that the path is simple once we assumed no negative cycles.) Suppose we just replace min by max in the least cost DP. Namely, M[i, v] = the maximum cost of a simple path with path length at most i from source s to v. (If there is no such path then this cost is.) The corresponding computational array would be 0 if i = 0 and v = s M [i, v] = if i = 0 and v s max{a, B} otherwise A = M [i 1, v] { } B = max M [i 1, u] + c(u, v) : (u, v) E Is this correct? 8 / 19

What goes wrong? The problem calls for a maximum simple path but the recursion { } B = max M [i 1, u] + c(u, v) : (u, v) E does not guarantee that the path through u will be a simple path as v might occur in the path to u. Algorithm would work for a DAG. In fact, determining the maximum cost of a simple path is NP-hard. A special case of this problem is the Hamiltonian path problem: does a graph G = (V, E) have a simple path of length V 1? The Hamiltonian path problem is a variant of the notorious (NP-hard) traveling salesman problem (TSP). See Section 6.6 of DPV for how to use DP to reduce the complexity from the naive O(n!) to O(n 2 2 n ). Stirling s approximation n! 2πn ( ) n n e 9 / 19

The all pairs least cost problem We now wish to compute the least cost path for all pairs (u, v) in an edge weighted directed graph (with no negative cycles). We can repeat the single source DP for each possible source node: complexity O(n 4 ) We can reduce the complexity to O(n 3 logn) using the DP based on the semantic array E[j, u, v] = cost of shortest path of path length at most 2 j from u to v. What is corresponding computational array? 10 / 19

Another DP for all pairs (DPV section 6.6) Let s assume (without loss of generality) that V = {1, 2,..., n}. We now define the semantic array G[k, u, v] = the least cost of a (simple) path π from u to v such that the internal nodes in the path π are in the subset {1, 2,..., k}. The computational array is 0 if u = v G [0, u, v] = c(u, v) if (u, v) is an edge otherwise. G [k + 1, u, v] = min{a, B} where A = G [k, u, v] and B = G [k, u, k + 1] + G [k, k + 1, v]. Like the recursion for the previous array E [j, u, v], the recursion here uses two recursive calls for each entry. Time complexity: n 3 entries O(1) per entry = O(n 3 ) in total. 11 / 19

A similar DP (using 2 recursive calls) The chain matrix product problem (DPV section 6.5 We are given n matrices (say over some field) M 1,..., M n with M i having dimension d i 1 d i. Goal: compute the matrix product M 1 M 2... M n using a given subroutine for computing a single matrix product A B. We recall that matrix multiplication is associative; that is, (A B) C = A (B C). But the number of operations for computing A B C generally depends on the order in which the pairwise multiplications are carried out. 12 / 19

The matrix chain product problem continued Let us assume that we are using classical matrix multiplication and say that the scalar complexity for a (p q) times (q r) matrix multilication is pqr. For example say the dimensions of A, B and C are (respectively) 5 10, 10 100 and 100 50. Then using (A B) C costs 5000 + 25000 = 30000 scalar operations whereas A (B C) costs 50000 + 2500 = 52500 scalar ops. Note: For this problem the input is these dimensions and not the actual matrix entries. 13 / 19

Parse tree for the product chain The matrix product problem then is to determine the parse tree that describes the order of pairwise products. At the leaves of this parse tree are the individual matrices and each internal node represents a pairwise matrix multiplication. Once we think of this parse tree, the DP is reasonably suggestive: The root of the optimal tree is the last pairwise multiplication and the subtrees are subproblems that must must be computed optimally. 14 / 19

The DP array for the matrix chain product problem The semantic array: C[i, j] = the cost of an optimal parse of M i... M j for 1 i j n. The recursive computationally array: { C 0 if i = j [i, j] = min { C [i, k] + C [k + 1, j] + d i 1 d k d j : i k < j } if i < j This same style DP algorithm (called DP over intervals) is also used in the RNA folding problem (in Section 6.5 of KT) as well as in computing optimal binary search trees (see section 15.5 in CLRS). Essentially in all these cases we are computing an optimal parse tree. 15 / 19

The sequence alignment (edit distance) problem The edit distance problem Given two strings X = x 1 x 2... x m and Y = y 1 y 2... y n over some finite alphabet S. Goal: find the best way to match these two strings. Variants of this problem occur often in bio-informatics as well as in spell checking. Sometimes this is cast as a maximization problem. We will view it as a minimization problem by defining different distance measures and matching symbols so as to minimize this distance. 16 / 19

A simple distance measure Suppose we can delete symbols and match symbols. We can have a cost d(a) to delete a symbol a in S, and a cost m(a, b) to match symbol a with symbol b (where we would normally assume m(a, a) = 0). As in any DP we consider an optimal solution and let s consider whether or not we will match the rightmost symbols of X and Y or delete a symbol. 17 / 19

The DP arrays The semantic array: E[i, j] = the cost of an optimal match of x 1... x i and y 1... y j. The computational array: 0 if i = j = 0 E d(y j ) + E [i, j 1] if i = 0 and j > 0 [i, j] = d(x i ) + E [i 1, j] if i > 0 and j = 0 min{a, B, C} otherwise where A = m(x i, y j ) + E [i 1, j 1], B = d(x i ) + E [i 1, j], and C = d(y j ) + E [i, j 1]. As a simple variation of edit distance we consider the maximization problem where each match of compatible a and b has profit 1 (resp. v(a, b)) and all deletions and mismatches have 0 profit. This is a special case of unweighted (resp. weighted) bipartite graph matching where edges cannot cross. 18 / 19

DP concluding remarks In DP algorithms one usually has to first generalize the problem (as we did more or less to some extent for all problems considered). Sometimes this generalization is not at all obvious. What is the difference between divide and conquer and DP? In divide and conquer the recursion tree never encounters a subproblem more than once. In DP, we need memoization (or an iterative implementation) as a given subproblem can be encountered many times leading to exponential time complexity if done without memoization. See also the comment on page 169 of DPV as to why in some cases memoization pays off since we do not necessaily have to compute every possible subproblem. (Recall also the comment by Dai Tri Man Le in Lecture 6.) 19 / 19