CS 170 DISCUSSION 8 DYNAMIC PROGRAMMING. Raymond Chan raychan3.github.io/cs170/fa17.html UC Berkeley Fall 17

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

CS 473: Fundamental Algorithms, Spring Dynamic Programming. Sariel (UIUC) CS473 1 Spring / 42. Part I. Longest Increasing Subsequence

1 Dynamic Programming

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

1 Dynamic Programming

Dynamic Programming. CS 374: Algorithms & Models of Computation, Fall Lecture 11. October 1, 2015

The Knapsack Problem an Introduction to Dynamic Programming. Slides based on Kevin Wayne / Pearson-Addison Wesley

Lecturers: Sanjam Garg and Prasad Raghavendra March 20, Midterm 2 Solutions

Dynamic Programming Algorithms Greedy Algorithms. Lecture 29 COMP 250 Winter 2018 (Slides from M. Blanchette)

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

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

Dynamic Programming CPE 349. Theresa Migler-VonDollen

Chapter 3 Dynamic programming

We ve done. Now. Next

Algorithm Design Techniques part I

CS Algorithms and Complexity

Dynamic Programmming: Activity Selection

Introduction to Algorithms

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

Chapter 6. Dynamic Programming

Introduction to Algorithms

String Patterns and Algorithms on Strings

IN101: Algorithmic techniques Vladimir-Alexandru Paun ENSTA ParisTech

EDIT DISTANCE. Given two words (strings), how can we define a notion of closeness. For example: Is PELICAN closer to PENGUIN or POLITICIAN?

More Dynamic Programming

Lecture 22: Dynamic Programming

Tutorial 7: Advanced Algorithms. (e) Give an example in which fixed-parameter tractability is useful.

4 Dynamic Programming

Dynamic Programming. CSE 101: Design and Analysis of Algorithms Lecture 19

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

Unit 4: Dynamic Programming

A BRIEF INTRODUCTION TO DYNAMIC PROGRAMMING (DP) by Amarnath Kasibhatla Nanocad Lab University of California, Los Angeles 04/21/2010

Unit-5 Dynamic Programming 2016

Homework3: Dynamic Programming - Answers

CSci 231 Final Review

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

Recursive-Fib(n) if n=1 or n=2 then return 1 else return Recursive-Fib(n-1)+Recursive-Fib(n-2)

Total Points: 60. Duration: 1hr

Dynamic Programming Homework Problems

CIS 121 Data Structures and Algorithms Midterm 3 Review Solution Sketches Fall 2018

CS483 Design and Analysis of Algorithms

Algorithm Design and Analysis

CS Final - Review material

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

CMPS 102 Solutions to Homework 7

CSED233: Data Structures (2017F) Lecture12: Strings and Dynamic Programming

5.1 The String reconstruction problem

CSC 8301 Design and Analysis of Algorithms: Exhaustive Search

ECE608 - Chapter 15 answers

Dynamic Programming II

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

Plotting run-time graphically. Plotting run-time graphically. CS241 Algorithmics - week 1 review. Prefix Averages - Algorithm #1

Practice Problems for the Final

1. (a) O(log n) algorithm for finding the logical AND of n bits with n processors

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

COMP 182: Algorithmic Thinking Dynamic Programming

IV/IV B.Tech (Regular) DEGREE EXAMINATION. Design and Analysis of Algorithms (CS/IT 414) Scheme of Evaluation

CS 380 ALGORITHM DESIGN AND ANALYSIS

Subset sum problem and dynamic programming

CS141: Intermediate Data Structures and Algorithms Greedy Algorithms

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

CMSC 451: Dynamic Programming

CMPSCI 311: Introduction to Algorithms Practice Final Exam

Dynamic Programming. Part I. Checking if string is in L. Dynamic Programming. Algorithms & Models of Computation CS/ECE 374, Fall 2017

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

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

Outline. CS38 Introduction to Algorithms. Fast Fourier Transform (FFT) Fast Fourier Transform (FFT) Fast Fourier Transform (FFT)

Dynamic programming II - The Recursion Strikes Back

Problem Strategies. 320 Greedy Strategies 6

Optimization II: Dynamic Programming

Introduction to Algorithms I

6.006 Final Exam Name 2. Problem 1. True or False [30 points] (10 parts) For each of the following questions, circle either True, False or Unknown.

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

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

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

Chapter 11. Dynamic Programming

1 Non greedy algorithms (which we should have covered

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

Dynamic Programming (Part #2)

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

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

4.1.2 Merge Sort Sorting Lower Bound Counting Sort Sorting in Practice Solving Problems by Sorting...

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

Dynamic Programming Homework Problems

Greedy Algorithms Huffman Coding

Dynamic Programming. Lecture Overview Introduction

CSE 421: Introduction to Algorithms

Lecture 8. Dynamic Programming

PROGRAM EFFICIENCY & COMPLEXITY ANALYSIS

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

Dynamic Programming. Bjarki Ágúst Guðmundsson Tómas Ken Magnússon. School of Computer Science Reykjavík University

Divide and Conquer. Bioinformatics: Issues and Algorithms. CSE Fall 2007 Lecture 12

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

CLASS: II YEAR / IV SEMESTER CSE CS 6402-DESIGN AND ANALYSIS OF ALGORITHM UNIT I INTRODUCTION

Today: Matrix Subarray (Divide & Conquer) Intro to Dynamic Programming (Rod cutting) COSC 581, Algorithms January 21, 2014

Chapter 17. Dynamic Programming

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

Algorithms and Data Structures 2014 Exercises and Solutions Week 9

CS 373: Combinatorial Algorithms, Spring 1999

Transcription:

CS 170 DISCUSSION 8 DYNAMIC PROGRAMMING Raymond Chan raychan3.github.io/cs170/fa17.html UC Berkeley Fall 17

DYNAMIC PROGRAMMING Recursive problems uses the subproblem(s) solve the current one. Dynamic programming recognizes that such problems have many and overlapping subproblems. Can have one or more starting states.

DYNAMIC PROGRAMMING Define subproblems ex. S(i, j) is some solution of subproblem (i, j) of n Recurrence relation of subproblems How S(i, j) can use subproblems to solve it Base Case(s) ex. S(i, j) = 0 when i = j Most have polynomial complexity. Some are exponential. Better running time than backtracking, brute-force.

DYNAMIC PROGRAMMING Tree Recursion Memoization Recognize that tree recursive calls overlap. Cache return values for subproblems so that you can use later. Top Down Approach. Iterative Dynamic Programming Starts from base case. Expand subproblems until you get to the subproblem you want. Recognizes which subproblems comes first. Bottom Up Approach.

DYNAMIC PROGRAMMING Directed Acyclic Graph underlying structure Each subproblem is a vertex. Directed edges (u, v) represents constraint that we need to solve subproblem u before subproblem v.

LONGEST INCREASING SUBSEQUENCE Problem Given sequence of number a 1, a 2,, a n, find longest increasing sequence of numbers. Subproblem L(i) as longest increasing sequence up to the i-th number. Having a partial solution for your original problem. L(i) needs longest increasing sequence of j, for j < i. Goal is L(n)

EXAMPLES Longest Increasing Subsequence Edit Distance Knapsack with and without repetition note: may not cover any (if at all) of the examples.

LONGEST INCREASING SUBSEQUENCE Base Case L(i) = 1 Recurrence L(i) = max ( L(i), L(j) + 1) ) for all j < i and a[j] < a[i] Either we start new sequence with base case, or add a i to longest prior sequence given still increasing

LONGEST INCREASING SUBSEQUENCE for i = 1..n L(i) = 1 for j = 1..i - 1 if a[j] < a[i]: L(i) = max(l(i), L(j) + 1) Add pointer to actually find sequence. Time complexity: O(n 2 ) for i = 1..n L(i) = 1 prev(i) = i for j = 1..i - 1 if a[j] < a[i]: L(i) = max(l(i), L(j) + 1) if updated: prev(i) = j Space complexity: O(n) Can also think of it as longest DAG of increasing subsequences.

EDIT DISTANCE Problem Given 2 strings x[1..n] and y[1..m], find edit distance between the 2. Subproblem Look at only partial strings. x[1..i] and y[1..j]. E(i, j) is the edit distance of x (up to index i) and (y up to index j). Answer at E(n, m).

EDIT DISTANCE Recurrence Relation E(i, j) = min ( E(i - 1, j) + 1, E(i, j - 1) + 1, E(i - 1, j - 1) + diff(i, j) ) diff(i, j) = 1 if x[i] x[j] else 0 Can delete x[i] and get 1 plus edit distance of all characters before i and at current j. Can insert y[j] and get 1 plus edit distance of all characters at current j and before i. If x[i] = y[j], then we have edit distance of character before i and j. If x[i] y[j], then we substitute y[j] for x[i]. 1 plus above.

EDIT DISTANCE Base cases E(i, 0) = i, E(0, j) = j Time complexity: O(nm) Space complexity: O(nm)

EDIT DISTANCE If stuck, try drawing a 2D matrix. Number of arguments in subproblem definition determines dimension of matrix.

KNAPSACK WITH REPETITION Problem Given items with values and weights, find the highest value items such that total weight is at most W. Can re-use items (multiset). Subproblem At some point, you have a knapsack of items and weight. K(w) as best value knapsack with weight w. Need solutions to K(w ) for w < w. Answer at K(W)

KNAPSACK WITH REPETITION Recurrence Formula K(w) = max i ( K(w - w i )+ v i ) for w - w i 0 Look at item i. If we can still fit in bag, put it in to see if can get better value for weight w with value of this item. Base Case K(0) = 0 Time complexity: O(nW) Space complexity: O(W)

KNAPSACK WITHOUT REPETITION Problem Given items with values and weights, find the highest value items such that total weight is at most W. Cannot re-use items (subset). Subproblem At some point, you have a knapsack of items and weight. But lets say we have only look at items 1..i out of n items. K(w, i) as best value knapsacks with weight w having only considered items 1..i. Find K(W, n).

KNAPSACK WITHOUT REPETITION Recurrence Relation Look at item i. Either we add it to our bag (if it fits) or we don t. K(w, i) = max ( K(w - w i, i - 1) + v i, K(w, i - 1) ) Typical tree recursion choice at each step. Base Case K(0, 0) = 0 Time Complexity: O(nW) Space Complexity: O(nW)

KNAPSACK TIME COMPLEXITY Knapsack runtime: O(nW). Length of an array is the determining factor for most inputs. Processors can handle those 4-8 byte data types easily. Adding one more elements adds 4-8 bytes. In knapsack, I can easily have you find best value bag with weight that is very large (e.x. 1,000,000,000). W is represented as binary. Add 1 more bit to W, double the running time. Looking at the size of input (what number is W?), we have polynomial. But when I look at the length of input (number of bits in W), we have exponential.