Lecture #39. Initial grading run Wednesday. GUI files up soon. Today: Dynamic programming and memoization.

Similar documents
Public-Service Announcement

Public-Service Announcement

Data Structures and Algorithms CSE 465

CS61B Lecture #2. Public Service Announcements:

CS2 Algorithms and Data Structures Note 6

Practice Problems for the Final

CS173 Longest Increasing Substrings. Tandy Warnow

ALGORITHM DESIGN DYNAMIC PROGRAMMING. University of Waterloo

CS 380 ALGORITHM DESIGN AND ANALYSIS

Search and Optimization

1 Dynamic Programming

CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall Office hours:

memoization or iteration over subproblems the direct iterative algorithm a basic outline of dynamic programming

Design and Analysis of Algorithms Prof. Madhavan Mukund Chennai Mathematical Institute. Module 02 Lecture - 45 Memoization

Data Structures and Algorithms CMPSC 465

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

CS2 Algorithms and Data Structures Note 6

CS 4349 Lecture September 13th, 2017

CSCI 136 Data Structures & Advanced Programming. Fall 2018 Instructors Bill Lenhart & Bill Jannen

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

CS61B Lectures # Purposes of Sorting. Some Definitions. Classifications. Sorting supports searching Binary search standard example

Recursive Functions. Biostatistics 615 Lecture 5

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

CMPSCI 250: Introduction to Computation. Lecture #1: Things, Sets and Strings David Mix Barrington 22 January 2014

LECTURE 17. Array Searching and Sorting

Algorithms. Ch.15 Dynamic Programming

CmpSci 187: Programming with Data Structures Spring 2015

APCS Semester #1 Final Exam Practice Problems

Shell s sort. CS61B Lecture #25. Sorting by Selection: Heapsort. Example of Shell s Sort

CS 3410 Ch 7 Recursion

CSCI-1200 Data Structures Spring 2018 Lecture 7 Order Notation & Basic Recursion

CS 173, Running Time Analysis, Counting, and Dynamic Programming. Tandy Warnow

CS S-17 Recursion IV 1. ArrayLists give some extra functionality to arrays (automatic resizing, code for inserting, etc)

1 Dynamic Programming

Lecture 22: Dynamic Programming

Welcome to... CS113: Introduction to C

CSC 284/484 Advanced Algorithms - applied homework 0 due: January 29th, 11:59pm EST

Question 2. [5 points] State a big-o upper bound on the worst case running time of the given

Dynamic Programming Homework Problems

Recursive Definitions

Backtracking. See Section 7.7 p of Weiss

Maintain binary search tree property nodes to the left are less than the current node, nodes to the right are greater

CS61B Lecture #3. Homework: Please see Homework #1 on the lab page. Last modified: Wed Sep 10 20:34: CS61B: Lecture #3 1

Algorithm Discovery and Design. Why are Algorithms Important? Representing Algorithms. Chapter 2 Topics: What language to use?

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

Lecture 12: Dynamic Programming Part 1 10:00 AM, Feb 21, 2018

Dynamic Programming 1

1 Dynamic Programming

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

Public-Service Announcements

Instructions PLEASE READ (notice bold and underlined phrases)

introduction to Programming in C Department of Computer Science and Engineering Lecture No. #40 Recursion Linear Recursion

CSCI-1200 Data Structures Fall 2017 Lecture 7 Order Notation & Basic Recursion

CSE 3101 Design and Analysis of Algorithms Solutions for Practice Test for Unit 5 Dynamic Programming

Recall from Last Time: Big-Oh Notation

ECE 2574: Data Structures and Algorithms - Recursion Part I. C. L. Wyatt

APCS-AB: Java. Recursion in Java December 12, week14 1

DESIGN AND ANALYSIS OF ALGORITHMS. Unit 1 Chapter 4 ITERATIVE ALGORITHM DESIGN ISSUES

ICS 491: Competitive Programming. Lecture 6: Dynamic Programming II

On the Complexity of the Policy Improvement Algorithm. for Markov Decision Processes

Signed umbers. Sign/Magnitude otation

CSCI 136 Data Structures & Advanced Programming. Lecture 9 Fall 2018 Instructors: Bills

Recursion. CSCI 112: Programming in C

CS61B Lecture #35. [The Lecture #32 notes covered lectures #33 and #34.] Today: Enumerated types, backtracking searches, game trees.

Dynamic Programming Algorithms

Unit 1 Chapter 4 ITERATIVE ALGORITHM DESIGN ISSUES

CMPSCI 187: Programming With Data Structures. Lecture #15: Thinking Recursively David Mix Barrington 10 October 2012

- Main approach is recursive, but holds answers to subproblems in a table so that can be used again without re-computing

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

COMP 182: Algorithmic Thinking Dynamic Programming

Introduction to Programming

Main approach: always make the choice that looks best at the moment.

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Data Structures Lecture 3 Order Notation and Recursion

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

CS61A Notes Disc 11: Streams Streaming Along

Representation of Non Negative Integers

CS61C : Machine Structures

Dynamic Programming Algorithms

Recursion. Let s start by looking at some problems that are nicely solved using recursion. First, let s look at generating The Fibonacci series.

CS 201, Fall 2016 Dec 2nd Exam 3

CS 310 Advanced Data Structures and Algorithms


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

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger.

Algorithm Design Techniques part I

Data Structures and Algorithms (CSCI 340)

EECE.3170: Microprocessor Systems Design I Summer 2017 Homework 4 Solution

DO NOT. In the following, long chains of states with a single child are condensed into an edge showing all the letters along the way.

Theory of control structures

School of Informatics, University of Edinburgh

CS 491 CAP Intermediate Dynamic Programming

Algorithm Design And Analysis Asst. Prof. Ali Kadhum Idrees The Sum-of-Subsets Problem Department of Computer Science College of Science for Women

Parallel Programming. Parallel algorithms Combinatorial Search

Assignment #1 Simple C++

Object-Oriented Design Lecture 11 CS 3500 Spring 2010 (Pucella) Tuesday, Feb 16, 2010

Public-Service Announcement

Dynamic Programming Homework Problems

Programming for Experimental Research. Flow Control

Midterm 2. Replace by cover page.

Transcription:

Lecture #39 Initial grading run Wednesday. GUI files up soon. Today: Dynamic programming and memoization. Last modified: Fri Dec 10 12:19:27 2004 CS61B: Lecture #39 1

A puzzle (D. Garcia): Dynamic Programming Start with a list with an even number of non-negative integers. Each player in turn takes either the leftmost number or the rightmost. Idea is to get the largest possible sum. Example: starting with (6, 12, 0, 8), you (as first player) should take the 8. Whatever the second player takes, you also get the 12, for a total of 20. Assuming your opponent plays perfectly (i.e., to get as much as possible), how can you maximize your sum? Can solve this with exhaustive game-tree search. Last modified: Fri Dec 10 12:19:27 2004 CS61B: Lecture #39 2

Recursion makes it easy, again: Obvious Program int bestsum (int[] V) { int total, i, N = V.length; for (i = 0, total = 0; i < N; i += 1) total += V[i]; return bestsum (V, 0, N-1, total); /** The largest sum obtainable by the first player in the choosing * game on the list V[LEFT.. RIGHT], assuming that TOTAL is the * sum of all the elements in V[LEFT.. RIGHT]. */ int bestsum (int[] V, int left, int right, int total) { if (left > right) return 0; else { int L = total - bestsum (V, left+1, right, total-v[left]); int R = total - bestsum (V, left, right-1, total-v[right]); return Math.max (L, R); Time cost is C(0) = 1, C(N) = 2C(N 1); so C(N) Θ(2 N ) Last modified: Fri Dec 10 12:19:27 2004 CS61B: Lecture #39 3

Still Another Idea from CS61A The problem is that we are recomputing intermediate results many times. Solution: memoize the intermediate results. Here, we pass in an N N array (N = V.length) of memoized results, initialized to -1. int bestsum (int[] V, int left, int right, int total, int[][] memo) { if (left > right) return 0; else if (memo[left][right] == -1) { int L = V[left] + total - bestsum (V, left+1, right, total-v[left],memo); int R = V[right] + total - bestsum (V, left, right-1, total-v[right],memo); memo[left][right] = Math.max (L, R); return memo[left][right]; Now the number of recursive calls to bestsum must be O(N 2 ), for N = the length of V, an enormous improvement from Θ(2 N )! Last modified: Fri Dec 10 12:19:27 2004 CS61B: Lecture #39 4

Iterative Version I prefer the recursive version, but the usual presentation of this idea known as dynamic programming is iterative: int bestsum (int[] V) { int[][] memo = new int[v.length][v.length]; int[][] total = new int[v.length][v.length]; for (int i = 0; i < V.length; i += 1) memo[i][i] = total[i][i] = V[i]; for (int k = 1; k < V.length; k += 1) for (int i = 0; i < V.length-k-1; i += 1) { total[i][i+k] = V[i] + total[i+1][i+k]; int L = V[i] + total[i+1][i+k] - memo[i+1][i+k]; int R = V[i+k] + total[i][i+k-1] - memo[i][i+k-1]; memo[i][i+k] = Math.max (L, R); return memo[0][v.length-1]; That is, we figure out ahead of time the order in which the memoized version will fill in memo, and write an explicit loop. Save the time needed to check whether result exists. But I say, why bother? Last modified: Fri Dec 10 12:19:27 2004 CS61B: Lecture #39 5

Longest Common Subsequence Problem: Find length of the longest string that is a subsequence of each of two other strings. Example: Longest common subsequence of sally sells sea shells by the seashore and sarah sold salt sellers at the salt mines is sa sl sa sells the sae (length 23) Similarity testing, for example. Obvious recursive algorithm: /** Length of longest common subsequence of S0[0..k0-1] * and S1[0..k1-1] (pseudo Java) */ int lls (String S0, int k0, String S1, int k1) { if (k0 == 0 k1 == 0) return 0; if (S0[k0-1] == S1[k1-1]) return 1 + lls (S0, k0-1, S1, k1-1); else return Math.max (lls (S0, k0-1, S1, k1), lls (S0, k0, S1, k1-1); Exponential, but obviously memoizable (exercise to reader). How fast will the memoized version be? Last modified: Fri Dec 10 12:19:27 2004 CS61B: Lecture #39 6

Longest Common Subsequence Problem: Find length of the longest string that is a subsequence of each of two other strings. Example: Longest common subsequence of sally sells sea shells by the seashore and sarah sold salt sellers at the salt mines is sa sl sa sells the sae (length 23) Similarity testing, for example. Obvious recursive algorithm: /** Length of longest common subsequence of S0[0..k0-1] * and S1[0..k1-1] (pseudo Java) */ int lls (String S0, int k0, String S1, int k1) { if (k0 == 0 k1 == 0) return 0; if (S0[k0-1] == S1[k1-1]) return 1 + lls (S0, k0-1, S1, k1-1); else return Math.max (lls (S0, k0-1, S1, k1), lls (S0, k0, S1, k1-1); Exponential, but obviously memoizable (exercise to reader). How fast will the memoized version be? Θ(k 0 k 1 ) Last modified: Fri Dec 10 12:19:27 2004 CS61B: Lecture #39 6