Data Structures and Algorithms Week 8

Similar documents
ECE250: Algorithms and Data Structures Dynamic Programming Part B

So far... Finished looking at lower bounds and linear sorts.

Algorithm Design Techniques part I

Introduction to Algorithms

Dynamic Programming II

CS473-Algorithms I. Lecture 10. Dynamic Programming. Cevdet Aykanat - Bilkent University Computer Engineering Department

Dynamic Programming (Part #2)

Lecture 8. Dynamic Programming

Dynamic Programming part 2

We ve done. Now. Next

Dynamic Programming. Design and Analysis of Algorithms. Entwurf und Analyse von Algorithmen. Irene Parada. Design and Analysis of Algorithms

Chapter 3 Dynamic programming

F(0)=0 F(1)=1 F(n)=F(n-1)+F(n-2)

Lecture 4: Dynamic programming I

CS 231: Algorithmic Problem Solving

Unit-5 Dynamic Programming 2016

We augment RBTs to support operations on dynamic sets of intervals A closed interval is an ordered pair of real

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

Partha Sarathi Manal

Homework3: Dynamic Programming - Answers

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

Dynamic Programming. Nothing to do with dynamic and nothing to do with programming.

Elements of Dynamic Programming. COSC 3101A - Design and Analysis of Algorithms 8. Discovering Optimal Substructure. Optimal Substructure - Examples

CS 380 ALGORITHM DESIGN AND ANALYSIS

Computer Sciences Department 1

y j LCS-Length(X,Y) Running time: O(st) set c[i,0] s and c[0,j] s to 0 for i=1 to s for j=1 to t if x i =y j then else if

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

CS Algorithms. Dynamic programming and memoization. (Based on slides by Luebke, Lim, Wenbin)

Dynamic Programming Shabsi Walfish NYU - Fundamental Algorithms Summer 2006

Dynamic Programming Matrix-chain Multiplication

Dynamic Programming. An Enumeration Approach. Matrix Chain-Products. Matrix Chain-Products (not in book)

CMPS 2200 Fall Dynamic Programming. Carola Wenk. Slides courtesy of Charles Leiserson with changes and additions by Carola Wenk

Presentation for use with the textbook, Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, Dynamic Programming

15.Dynamic Programming

Lecture 13: Chain Matrix Multiplication

10/24/ Rotations. 2. // s left subtree s right subtree 3. if // link s parent to elseif == else 11. // put x on s left

ECE608 - Chapter 15 answers

Lecture 57 Dynamic Programming. (Refer Slide Time: 00:31)

IN101: Algorithmic techniques Vladimir-Alexandru Paun ENSTA ParisTech

CSE 101, Winter Design and Analysis of Algorithms. Lecture 11: Dynamic Programming, Part 2

Algorithm classification

Algorithms. Ch.15 Dynamic Programming

Tutorial 6-7. Dynamic Programming and Greedy

Dynamic Programming. December 15, CMPE 250 Dynamic Programming December 15, / 60

Merge Sort Goodrich, Tamassia Merge Sort 1

Divide-and-Conquer. Divide-and conquer is a general algorithm design paradigm:

Dynamic Programming Group Exercises

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

Introduction to Algorithms

(Feodor F. Dragan) Department of Computer Science Kent State University. Advanced Algorithms, Feodor F. Dragan, Kent State University 1

Dynamic Programmming: Activity Selection

Chain Matrix Multiplication

Merge Sort fi fi fi 4 9. Merge Sort Goodrich, Tamassia

15.4 Longest common subsequence

15.4 Longest common subsequence

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

DESIGN AND ANALYSIS OF ALGORITHMS (DAA 2017)

UC Berkeley CS 170: Efficient Algorithms and Intractable Problems Handout 15 Lecturer: Michael Jordan October 26, Notes 15 for CS 170

=1, and F n. for all n " 2. The recursive definition of Fibonacci numbers immediately gives us a recursive algorithm for computing them:

/463 Algorithms - Fall 2013 Solution to Assignment 3

Dynamic Programming. Outline and Reading. Computing Fibonacci

CSC 505, Spring 2005 Week 6 Lectures page 1 of 9

Divide and Conquer Strategy. (Page#27)

17/05/2018. Outline. Outline. Divide and Conquer. Control Abstraction for Divide &Conquer. Outline. Module 2: Divide and Conquer

Dynamic Programming in Haskell

L.J. Institute of Engineering & Technology Semester: VIII (2016)

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

Dynamic Programming. Lecture Overview Introduction

Algorithm Analysis and Design

Unit 4: Dynamic Programming

Algorithm Design and Analysis

More Dynamic Programming

Lecture 22: Dynamic Programming

Ensures that no such path is more than twice as long as any other, so that the tree is approximately balanced

Dynamic Programming CS 445. Example: Floyd Warshll Algorithm: Computing all pairs shortest paths

Last week: Breadth-First Search

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

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

Virtual University of Pakistan

Greedy Algorithms Huffman Coding

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

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

Attendance (2) Performance (3) Oral (5) Total (10) Dated Sign of Subject Teacher

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

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

CSci 231 Final Review

Algorithms: COMP3121/3821/9101/9801

Longest Common Subsequence. Definitions

CSE 101- Winter 18 Discussion Section Week 6

Data Structures, Algorithms & Data Science Platforms

Longest Common Subsequences and Substrings

( ) + n. ( ) = n "1) + n. ( ) = T n 2. ( ) = 2T n 2. ( ) = T( n 2 ) +1

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

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)

Parallel Algorithms CSE /22/2015. Outline of this lecture: 1 Implementation of cilk for. 2. Parallel Matrix Multiplication

CS Final - Review material

Dynamic Programming. An Introduction to DP

Subsequence Definition. CS 461, Lecture 8. Today s Outline. Example. Assume given sequence X = x 1, x 2,..., x m. Jared Saia University of New Mexico

O(n): printing a list of n items to the screen, looking at each item once.

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

Transcription:

Data Structures and Algorithms Week 8 Dynamic programming Fibonacci numbers Optimization problems Matrix multiplication optimization Principles of dynamic programming Longest Common Subsequence

Algorithm design techniques Algorithm design techniques so far: Iterative (brute-force) algorithms For example, insertion sort Algorithms that use efficient data structures For example, heap sort Divide-and-conquer algorithms Binary search, merge sort, quick sort 2

Divide and Conquer Divide and conquer method for algorithm design: Divide: If the input size is too large to deal with in a simple manner, divide the problem into two or more disjoint subproblems Conquer: Use divide and conquer recursively to solve the subproblems Combine: Take the solutions to the subproblems and merge these solutions into a solution for the original problem 3

Divide and Conquer/2 For example, MergeSort The subproblems are independent and non-overlapping Merge-Sort(A, l, l, r) r) if if l < r then then m := := (l+r)/2 Merge-Sort(A, l, l, m) m) Merge-Sort(A, m+1, m+1, r) r) Merge(A, l, l, m, m, r) r) 17 24 31 45 50 63 85 96 4

Fibonacci Numbers Leonardo Fibonacci (1202): A rabbit starts producing in the 2nd year after its birth and produces one child each generation. How many rabbits will there be after n generations? F(1)=1 F(2)=1 F(3)=2 F(4)=3 F(5)=5 F(6)=8 5

Fibonacci Numbers/2 F(n)= F(n-1)+ F(n-2) F(0) =0, F(1) =1 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 FibonacciR(n) 01 01 if if n 1 then return n 02 02 else return FibonacciR(n-1) + FibonacciR(n-2) Straightforward recursive procedure is slow! 6

Fibonacci Numbers/3 We keep calculating the same value over and over! Subproblems are overlapping they share subsubproblems F(6) = 8 F(5) F(4) F(4) F(3) F(3) F(2) F(3) F(2) F(2) F(1) F(2) F(1) F(1) F(0) F(2) F(1) F(1) F(0) F(1) F(0) F(1) F(0) F(1) F(0) 7

Fibonacci Numbers/4 How many summations are there S(n)? S(n) = S(n 1) + S(n 2) + 1 S(n) 2S(n 2) +1 and S(1) = S(0) = 0 Solving the recurrence we get S(n) 2 n/2 1 1.4 n Running time is exponential! 8

Fibonacci Numbers/5 We can calculate F(n) in linear time by remembering solutions of solved sub-problems (= dynamic programming). Compute solution in a bottom-up fashion Trade space for time! Fibonacci(n) 01 01 F[0] := := 0 02 02 F[1] := := 1 03 03 for for i := := 2 to to n do do 04 04 F[i] := := F[i-1] + F[i-2] 05 05 return F[n] 9

Fibonacci Numbers/6 In fact, only two values need to be remembered at any time! FibonacciImproved(n) 01 01 if if n 1 then return n 02 02 Fim2 := := 0 03 03 Fim1 := := 1 04 04 for for i := := 2 to to n do do 05 05 Fi Fi := := Fim1 + Fim2 06 06 Fim2 := := Fim1 07 07 Fim1 := := Fi Fi 05 05 return Fi Fi 10

History Dynamic programming Invented in the 1950s by Richard Bellman as a general method for optimizing multistage decision processes The term programming refers to a tabular method. Often used for optimization problems. 11

Optimization Problems We have to choose one solution out of many. We want the solution with the optimal (minimum or maximum) value. Structure of the solution: It consists of a sequence of choices that were made. What choices have to be made to arrive at an optimal solution? An algorithm should compute the optimal value plus, if needed, an optimal solution. 12

Multiplying Matrices Two matrices, A n m matrix and B m k matrix, can be multiplied to get C with dimensions n k, using nmk scalar multiplications a1 1a1 2... b1 1b1 2 b13 a2 1 a 2 2 2 2 c...... b2 1b2 2 b 23 a3 1a 3 2... i, j i l, l j, l 1 Problem: Compute a product of many matrices efficiently Matrix multiplication is associative: (AB)C = A(BC) 13 m c a b

Multiplying Matrices/2 The parenthesization matters Consider A B C D, where A is 30 1, B is 1 40, C is 40 10, D is 10 25 Costs: (AB)C)D 1200 + 12000 + 7500 = 20700 (AB)(CD) 1200 + 10000 + 30000 = 41200 A((BC)D) 400 + 250 + 750 = 1400 We need to optimally parenthesize A 1 x A 2 x A n where A i is a d i-1 x d i matrix 14

Multiplying Matrices/3 Let M(i,j) be the minimum number of multiplications necessary to compute A i..j = A 1 x x A n Key observations The outermost parenthesis partitions the chain of matrices (i,j) at some k, (i k<j): (A i A k )(A k+1 A j ) The optimal parenthesization of matrices (i,j) has optimal parenthesizations on either side of k: for matrices (i,k) and (k+1,j) 15

Multiplying Matrices/4 We try out all possible k: M ( i, i ) 0 M (, i ) j mm i n i k(, M ) ( k 1, j ) d d d i k j i k j A direct recursive implementation is exponential there is a lot of duplicated work. But there are only few different subproblems (i,j): one solution for each choice of i and j (i<j). 1 16

Multiplying Matrices/5 Idea: store the optimal cost M(i,j) for each subproblem in a 2d array M[1..n,1..n] Trivially M(i,i) = 0, 1 i n To compute M(i,j), where i j = L, we need only values of M for subproblems of length < L. Thus we have to solve subproblems in the increasing length of subproblems: first subproblems of length 2, then of length 3 and so on. To reconstruct an optimal parenthesization for each pair (i,j) we record in c[i, j]=k the optimal split into two subproblems (i, k) and (k+1, j) 17

Multiplying Matrices/6 DynamicMM 01 01 for for i := := 1 to to n do do 02 02 M[i,i] := := 03 03 for for L := := 1 to to n-1 n-1 do do 04 04 for for i := := 1 to to n-l n-l do do 05 05 j := := i+l i+l 06 06 M[i,j] := := 07 07 for for k := := i to to j-1 j-1 do do 08 08 q := := M[i,k]+ M[k+1,j]+ d i-1 d i-1 k d k j j 09 09 if if q < M[i,j] then then 1 M[i,j] := := q 2 c[i,j] := := k 12 12 return M, M, c 18

Multiplying Matrices/7 After the execution: M [1,n] contains the value of an optimal solution and c contains optimal subdivisions (choices of k) of any subproblem into two subsubproblems Let us run the algorithm on the four matrices: A 1 is a 2x10 matrix, A 2 is a 10x3 matrix, A 3 is a 3x5 matrix, A 4 is a 5x8 matrix. 19

Multiplying Matrices/8 Running time It is easy to see that it is O(n 3 ) (three nested loops) It turns out it is also Ω(n 3 ) Thus, a reduction from exponential time to polynomial time. 20

Memoization If we prefer recursion we can structure our algorithm as a recursive algorithm: MemoMM(i,j) 1. 1. if if i = j then then return 0 2. 2. else else if if M[i,j] < then then return M[i,j] 3. 3. else else for for k := := i to to j-1 j-1 do do 4. 4. q := := MemoMM(i,k)+ MemoMM(k+1,j) + d i-1 d i-1 k d k j j 5. 5. if if q < M[i,j] then then 6. 6. M[i,j] := := q 7. 7. return return M[i,j] M[i,j] Initialize all elements to and call MemoMM(i,j) 21

Memoization/2 Memoization: Solve the problem in a top-down fashion, but record the solutions to subproblems in a table. Pros and cons: Recursion is usually slower than loops and uses stack space (not a relevant disadvantage) Easier to understand If not all subproblems need to be solved, you are sure that only the necessary ones are solved 22

Dynamic Programming In general, to apply dynamic programming, we have to address a number of issues: Show optimal substructure an optimal solution to the problem contains optimal solutions to sub-problems Solution to a problem: Making a choice out of a number of possibilities (look what possible choices there can be) Solving one or more sub-problems that are the result of a choice (characterize the space of sub-problems) Show that solutions to sub-problems must themselves be optimal for the whole solution to be optimal. 23

Dynamic Programming/2 Write a recursive solution for the value of an optimal solution Mopt = Min over all choices k {(Combination of M opt of all sub-problems resulting from choice k) + (the cost associated with making the choice k)} Show that the number of different instances of sub-problems is bounded by a polynomial 24

Dynamic Programming/3 Compute the value of an optimal solution in a bottom-up fashion, so that you always have the necessary sub-results pre-computed (or use memoization) Check if it is possible to reduce the space requirements, by forgetting solutions to sub-problems that will not be used any more Construct an optimal solution from computed information (which records a sequence of choices made that lead to an optimal solution) 25

Longest Common Subsequence Two text strings are given: X and Y There is a need to quantify how similar they are: Comparing DNA sequences in studies of evolution of different species Spell checkers One of the measures of similarity is the length of a Longest Common Subsequence (LCS) 26

LCS: Definition Z is a subsequence of X if it is possible to generate Z by skipping some (possibly none) characters from X For example: X = ACGGTTA, Y= CGTAT, LCS(X,Y) = CGTA or CGTT To solve LCS problem we have to find skips that generate LCS(X,Y) from X and skips that generate LCS(X,Y) from Y 27

LCS: Optimal Substructure We make Z to be empty and proceed from the ends of X m = x 1 x 2 x m and Y n = y 1 y 2 y n If x m =y n, append this symbol to the beginning of Z, and find optimally LCS(X m-1, Y n-1 ) If x m y n, Skip either a letter from X or a letter from Y Decide which decision to do by comparing LCS(Xm, Y n-1 ) and LCS(X m-1, Y n ) Starting from beginning is equivalent. 28

LCS: Recurrence The algorithm can be extended by allowing more editing operations in addition to copying and skipping (e.g., changing a letter) Let c[i,j] = LCS(Xi, Y j ) 0 if i 0 or j 0 c[ i, j ] [ c1, i 1 ] j 1 i j if x, y0 and i m a x c{ i[, j1 ], c[ 1 i, ] j} if i, j 0 xand y i Note that the conditions in the problem restrict sub-problems (if xi = yi we consider xi-1 and yi-1, etc) j j 29

LCS: Algorithm LCS-Length(X, Y, Y, m, m, n) n) 1 for for i := := 1 to to m do do c[i,0] := := 0 2 for for j := := 0 to to n do do c[0,j] := := 0 3 for for i := := 1 to to m do do 4 for for j := := 1 to to n do do 5 if if x i = i y j then j c[i,j] := := c[i-1,j-1]+1 6 b[i,j] := := copy 7 else if if c[i-1,j] c[i,j-1] then 8 c[i,j] := := c[i-1,j] 9 b[i,j] := := skipx 10 10 else c[i,j] := := c[i,j-1] 11 11 b[i,j] := := skipy 12 12 return c, c, b 30

LCS: Example Lets run: X = GGTTCAT, Y= GTATCT What is the running time and space requirements of the algorithm? How much can we reduce our space requirements, if we do not need to reconstruct an LCS? 31

Next Week Graphs: Representation in memory Breadth-first search Depth-first search Topological sort 32