ALGORITHM DESIGN DYNAMIC PROGRAMMING. University of Waterloo

Similar documents
Chapter 4. Divide-and-Conquer. Copyright 2007 Pearson Addison-Wesley. All rights reserved.

Algorithms: Efficiency, Analysis, techniques for solving problems using computer approach or methodology but not programming

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

CS141: Intermediate Data Structures and Algorithms Dynamic Programming

How many leaves on the decision tree? There are n! leaves, because every permutation appears at least once.

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

Last week: Breadth-First Search

Divide and Conquer 4-0

Algorithm Design Techniques part I

CSE 230 Intermediate Programming in C and C++ Recursion

Midterm solutions. n f 3 (n) = 3

CSC Design and Analysis of Algorithms

Quicksort. Repeat the process recursively for the left- and rightsub-blocks.

CSC Design and Analysis of Algorithms. Lecture 6. Divide and Conquer Algorithm Design Technique. Divide-and-Conquer

recursive algorithms 1

Divide-and-Conquer. Dr. Yingwu Zhu

Announcements. Biostatistics 615/815 Lecture 8: Hash Tables, and Dynamic Programming. Recap: Example of a linked list

Dynamic Programming Homework Problems

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

CSC148-Section:L0301

CS115 INTRODUCTION TO COMPUTER SCIENCE 1. Additional Notes Module 5

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

Last week. Another example. More recursive examples. How about these functions? Recursive programs. CSC148 Intro. to Computer Science

Chapter 17 Recursion

Introduction to Algorithms

Computer Algorithms. Introduction to Algorithm

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

CS 380 ALGORITHM DESIGN AND ANALYSIS

Practice Problems for the Final

Two Approaches to Algorithms An Example (1) Iteration (2) Recursion

Recursion. COMS W1007 Introduction to Computer Science. Christopher Conway 26 June 2003

Divide-and-Conquer. The most-well known algorithm design strategy: smaller instances. combining these solutions

Data Structures and Algorithms Chapter 4

CS Algorithms and Complexity

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

Analysis of Algorithms

Recursion CSCI 136: Fundamentals of Computer Science II Keith Vertanen Copyright 2011

Dynamic Programming Homework Problems

You should know the first sum above. The rest will be given if you ever need them. However, you should remember that,, and.

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

Dynamic Programming. An Introduction to DP

Course goals. exposure to another language. knowledge of specific data structures. impact of DS design & implementation on program performance

Topics. Trees Vojislav Kecman. Which graphs are trees? Terminology. Terminology Trees as Models Some Tree Theorems Applications of Trees CMSC 302

Computer Science Foundation Exam. Dec. 19, 2003 COMPUTER SCIENCE I. Section I A. No Calculators! KEY

Recap: Hash Tables. Recap : Open hash. Recap: Illustration of ChainedHash. Key features. Key components. Probing strategies.

Chapter 4: Sorting. Spring 2014 Sorting Fun 1

Data Structures and Algorithms CSE 465

Lecture 10: Recursion vs Iteration

Divide and Conquer. Algorithm Fall Semester

Data Structures and Algorithms Week 8

Trees and Graphs Shabsi Walfish NYU - Fundamental Algorithms Summer 2006

Algorithm classification

CSE 202: Design and Analysis of Algorithms Lecture 5

def F a c t o r i a l ( n ) : i f n == 1 : return 1 else : return n F a c t o r i a l ( n 1) def main ( ) : print ( F a c t o r i a l ( 4 ) )

Introduction to Multithreaded Algorithms

Multithreaded Algorithms Part 1. Dept. of Computer Science & Eng University of Moratuwa

1 Greedy algorithms and dynamic programming

CS583 Lecture 01. Jana Kosecka. some materials here are based on Profs. E. Demaine, D. Luebke A.Shehu, J-M. Lien and Prof. Wang s past lecture notes

Normal Order (Lazy) Evaluation SICP. Applicative Order vs. Normal (Lazy) Order. Applicative vs. Normal? How can we implement lazy evaluation?

Recursion. Fundamentals of Computer Science

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

Data Structure Lecture#17: Internal Sorting 2 (Chapter 7) U Kang Seoul National University

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

The Limits of Sorting Divide-and-Conquer Comparison Sorts II

Chapter 17 Recursion

Recursive Functions. Biostatistics 615 Lecture 5

CAD Algorithms. Categorizing Algorithms

Functional Programming. Overview. Topics. Definition n-th Fibonacci Number. Graph

Lecture 19 Sorting Goodrich, Tamassia

Lecture Outline. Code Generation. Lecture 30. Example of a Stack Machine Program. Stack Machines

CS171 Final Practice Exam

Data Structures and Algorithms Week 4

Sorting and Selection

Algorithms Dr. Haim Levkowitz

Objectives. Recursion. One Possible Way. How do you look up a name in the phone book? Recursive Methods Must Eventually Terminate.

CS 206 Introduction to Computer Science II

CSCE 411 Design and Analysis of Algorithms

Problem Set 7 CMSC 426 Assigned Tuesday April 27, Due Tuesday, May 11

CS173 Longest Increasing Substrings. Tandy Warnow

Code Generation. Lecture 30

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

Chapter 15-1 : Dynamic Programming I

CS473 - Algorithms I

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

CS 206 Introduction to Computer Science II

Subset sum problem and dynamic programming

Presentation for use with the textbook, Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, Merge Sort & Quick Sort

! Greed. O(n log n) interval scheduling. ! Divide-and-conquer. O(n log n) FFT. ! Dynamic programming. O(n 2 ) edit distance.

Recursion. Overview. Mathematical induction. Hello recursion. Recursion. Example applications. Goal: Compute factorial N! = 1 * 2 * 3...

V Advanced Data Structures

Algorithms and Data Structures. Marcin Sydow. Introduction. QuickSort. Sorting 2. Partition. Limit. CountSort. RadixSort. Summary

CSC-140 Assignment 5

COMP Analysis of Algorithms & Data Structures

Dynamic Programming Version 1.0

CS61A Notes 02b Fake Plastic Trees. 2. (cons ((1 a) (2 o)) (3 g)) 3. (list ((1 a) (2 o)) (3 g)) 4. (append ((1 a) (2 o)) (3 g))

COMP 251 Winter 2017 Online quizzes with answers

CSC Design and Analysis of Algorithms. Lecture 6. Divide and Conquer Algorithm Design Technique. Divide-and-Conquer

Analysis of Algorithms & Big-O. CS16: Introduction to Algorithms & Data Structures Spring 2018

TREES AND ORDERS OF GROWTH 7

algorithm evaluation Performance

Transcription:

ALGORITHM DESIGN DYNAMIC PROGRAMMING University of Waterloo

LIST OF SLIDES 1-1 List of Slides 1 2 Dynamic Programming Approach 3 Fibonacci Sequence (cont.) 4 Fibonacci Sequence (cont.) 5 Bottom-Up vs. Top-Down 6 Optimal Binary Search Tree 7 Optimal BST: Examples 8 Optimal BST (cont.) 9 Optimal BST (cont.) 10 All-points Shortest Path 11 Principle of Optimality 12 Summary

Dynamic Programming: 2 Dynamic Programming Approach IDEA: Use recursive (e.g., divide-and-conquer) approach but be careful not to recompute the same values Example: Fibonacci numbers Fibonacci(n) = if n < 2 then return n else return Fibonacci(n 1)+Fibonacci(n 2) Running time: Θ (( 1 + ) n ) 5 2

Dynamic Programming: 3 Fibonacci Sequence (cont.) Why is it so bad? Trace of Fibonacci(k) calls for n = 5: f(5)-f(4)-f(3)-f(2)-f(1) \f(0) \f(1) \f(2)-f(1) \f(0) \f(3)-f(2)-f(1) \f(0) \f(1)

Dynamic Programming: 4 Fibonacci Sequence (cont.) IDEA: compute bottom-up : new-fibonacci(n) = if n < 2 then return n a := 1 // Fib(0) b := 1 // Fib(1) for i := 3 to n a, b := b, a + b // Fib(n),Fib(n+1) return b runs in O(n)

Dynamic Programming: 5 Bottom-Up vs. Top-Down GENERAL IDEA: Remember call-return pairs for recursive functions keep (necessary) results in a table bottom-up technique construct results from small to large most common top-down technique remember calls made short-circuit calls by known results

Dynamic Programming: 6 Optimal Binary Search Tree Problem: given values w 1,...,w N (in increasing order) and fixed probabilities p 1,...,p N Goal: construct optimal search tree (that minimizes expected search time w.r.t. the given probabilities): min N p i (1 + d i ) i=1 where d i is the depth of the value w i.

Dynamic Programming: 7 Optimal BST: Examples Consider a BST storing k 1 < k 2 < k 3 frequency of access of the key k 1 (k 2, k 3 ) is p (q, r), respectively. All BST s for k 1, k 2, k 3 : k 3 k 2 k 1 k 3 k 1 k 2 k 2 k 1 k 3 k 1 k 3 k 2 k 1 k 2 3p + 2q + r 2p + 3q + r 2p + q + 2r p + 3q + 2r p + 2q + 3r... with access costs w.r.t. p, q, r. k 3

Dynamic Programming: 8 Optimal BST (cont.) Recursive definition of cost of tree for words w L,...,w R : i T 1 T 2 w L...w i 1 w i+1...w R C L,R = min L i R C L,i 1 + C i+1,r + R j=l p j with the base case C L,L = 0.

Dynamic Programming: 9 Optimal BST (cont.) Recursive (top-down) solution: recomputes the cost of small trees many times T(n) = O(n) + 2 n 1 i=1 T(i) Dynamic programming (bottom-up) solution: computes the costs for optimal small trees and stores the resulting C i,j in an O(n 2 )-sized table time complexity Θ(n 3 ) (can be reduced to O(n 2 ))

Dynamic Programming: 10 All-points Shortest Path Input: A directed graph G = (V, E) with positive integer edge weights d i,j. Output: For each ordered pair (u, v) V 2, the length of the shortest path from u to v. Let D k (i, j) be the length of the shortest path from v i to v j whose intermediate nodes come from {v 1,...,v k }. D k (i, j) = d i,j k = 0 min{d k 1 (i, j), D k 1 (i, k) + D k 1 (k, j)} k > 0 Time complexity Θ(n 3 ).

Dynamic Programming: 11 Principle of Optimality The principle of optimality holds for an optimization problem if for every optimal sequence of decisions, every subsequence is also optimal. Example: If p is a shortest path from u to v and p contains w, then: The initial portion of p from u to w is a shortest path from u to w; and The final portion of p from w to v is a shortest path from w to v. However: If p is a longest simple path from u to v that contains w, then it is not necessarily the case that the initial portion of p from u to w is the longest simple path from u to w.

Dynamic Programming: 12 Summary Very powerful method avoids unnecessary reevaluation Two main principles: 1. memoing of call-return pairs (e.g., in a table) make sure to evaluate in the right order 2. the optimality principle