COMP108 Algorithmic Foundations Dynamic Programming

Slides:



Advertisements
Similar presentations
Dynamic Programming Introduction Prof. Muhammad Saeed.
Advertisements

Dynamic Programming.
Analysis of Algorithms Dynamic Programming. A dynamic programming algorithm solves every sub problem just once and then Saves its answer in a table (array),
Introduction to Algorithms
Dynamic Programming Part 1: intro and the assembly-line scheduling problem.
Dynamic Programming Lets begin by looking at the Fibonacci sequence.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 11.
CPSC 311, Fall 2009: Dynamic Programming 1 CPSC 311 Analysis of Algorithms Dynamic Programming Prof. Jennifer Welch Fall 2009.
Dynamic Programming Optimization Problems Dynamic Programming Paradigm
CPSC 411 Design and Analysis of Algorithms Set 5: Dynamic Programming Prof. Jennifer Welch Spring 2011 CPSC 411, Spring 2011: Set 5 1.
Chapter 7 (Part 2) Sorting Algorithms Merge Sort.
Dynamic Programming. Well known algorithm design techniques:. –Divide-and-conquer algorithms Another strategy for designing algorithms is dynamic programming.
COSC 3101A - Design and Analysis of Algorithms 7 Dynamic Programming Assembly-Line Scheduling Matrix-Chain Multiplication Elements of DP Many of these.
Dynamic Programming Louis Siu What is Dynamic Programming (DP)? Not a single algorithm A technique for speeding up algorithms (making use of.
Data Structures R e c u r s i o n. Recursive Thinking Recursion is a problem-solving approach that can be used to generate simple solutions to certain.
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Divide and Conquer Prudence Wong
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Algorithm efficiency Prudence Wong.
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Algorithm efficiency Prudence Wong
递归算法的效率分析. When a function is called... A transfer of control occurs from the calling block to the code of the function --It is necessary that there be.
Algorithm Design Techniques, Greedy Method – Knapsack Problem, Job Sequencing, Divide and Conquer Method – Quick Sort, Finding Maximum and Minimum, Dynamic.
Fundamentals of Algorithms MCS - 2 Lecture # 11
Advanced Algorithms Analysis and Design
Greedy algorithms: CSC317
Dynamic Programming Typically applied to optimization problems
Dynamic Programming (DP)
Lecture 12.
All-pairs Shortest paths Transitive Closure
Subject Name: Design and Analysis of Algorithm Subject Code: 10CS43
Design & Analysis of Algorithm Dynamic Programming
CSC 421: Algorithm Design & Analysis
CSC 421: Algorithm Design & Analysis
COMP108 Algorithmic Foundations Greedy methods
Decrease-and-Conquer Approach
COMP108 Algorithmic Foundations Algorithm efficiency
COMP108 Algorithmic Foundations Divide and Conquer
COMP108 Algorithmic Foundations Divide and Conquer
Introduction to Recurrence Relations
CS 3343: Analysis of Algorithms
CS 3343: Analysis of Algorithms
CSC 421: Algorithm Design & Analysis
Advanced Design and Analysis Techniques
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
CSCE 411 Design and Analysis of Algorithms
Dynamic Programming.
Dynamic Programming.
Prepared by Chen & Po-Chuan 2016/03/29
Algorithm An algorithm is a finite set of steps required to solve a problem. An algorithm must have following properties: Input: An algorithm must have.
CS 3343: Analysis of Algorithms
Unit-5 Dynamic Programming
CS201: Data Structures and Discrete Mathematics I
Advanced Algorithms Analysis and Design
Advanced Algorithms Analysis and Design
Dynamic Programming.
Chapter 15-1 : Dynamic Programming I
CSE 2010: Algorithms and Data Structures Algorithms
Analysis of Algorithms CS 477/677
CSC 421: Algorithm Design & Analysis
Trevor Brown DC 2338, Office hour M3-4pm
Bioinformatics Algorithms and Data Structures
Dynamic Programming.
DYNAMIC PROGRAMMING.
Lecture 4 Dynamic Programming
Analysis of Algorithms CS 477/677
CSCI 235, Spring 2019, Lecture 25 Dynamic Programming
CSC 421: Algorithm Design & Analysis
Asst. Prof. Dr. İlker Kocabaş
Dynamic Programming.
Analysis of Algorithms CS 477/677
Time Complexity and the divide and conquer strategy
Data Structures and Algorithms Dynamic Programming
Presentation transcript:

COMP108 Algorithmic Foundations Dynamic Programming Prudence Wong

Dynamic programming an efficient way to implement some divide and conquer algorithms

Learning outcomes Understand the basic idea of dynamic programming Able to apply dynamic programming to compute Fibonacci numbers Able to apply dynamic programming to solve the assembly line scheduling problem

Fibonacci numbers …

Problem with recursive method Fibonacci number F(n) 1 if n = 0 or 1 F(n-1) + F(n-2) if n > 1 F(n) = n 1 2 3 4 5 6 7 8 9 10 F(n) 13 21 34 55 89 Pseudo code for the recursive algorithm: Procedure F(n) if n==0 or n==1 then return 1 else return F(n-1) + F(n-2)

The execution of F(7) F7 F6 F5 F5 F4 F4 F3 F3 F2 F3 F2 F2 F1 F1 F0 F1

Computation of F(2) is repeated 8 times! The execution of F(7) F7 F6 F5 F5 F4 F4 F3 F3 F2 F3 F2 F2 F1 F1 F0 F1 F0 F1 F0 F4 F3 F2 F2 F1 F1 F1 F0 F1 F0 F3 F2 F2 F1 F1 F0 F1 F0 Computation of F(2) is repeated 8 times! F2 F1 F1 F0

Computation of F(3) is also repeated 5 times! The execution of F(7) F7 F6 F5 F5 F4 F4 F3 F3 F2 F3 F2 F2 F1 F1 F0 F1 F0 F1 F0 F4 F3 F2 F2 F1 F1 F1 F0 F1 F0 F3 F2 F2 F1 F1 F0 F1 F0 Computation of F(3) is also repeated 5 times! F2 F1 F1 F0

Many computations are repeated!! The execution of F(7) How long it takes? exponential time (refer to notes on divide & conquer) F7 F6 F5 F5 F4 F4 F3 F3 F2 F3 F2 F2 F1 F1 F0 F1 F0 F1 F0 F4 F3 F2 F2 F1 F1 F1 F0 F1 F0 F3 F2 F2 F1 F1 F0 F1 F0 F2 Many computations are repeated!! F1 F1 F0

Idea for improvement Memorization: Procedure F(n) Store F(i) somewhere after we have computed its value Afterward, we don’t need to re-compute F(i); we can retrieve its value from our memory. Procedure F(n) if (v[n] < 0) then v[n] = F(n-1)+F(n-2) return v[n] Main set v[0] = v[1] = 1 for i = 2 to n do v[i] = -1 output F(n) [ ] refers to array ( ) is parameter for calling a procedure

Look at the execution of F(7) v[0] v[1] v[2] v[3] v[4] v[5] v[6] v[7] 1 -1 F5 F5 F4 F4 F3 F2 F2 F3 F3 F2 F1 F1 F0 F1 F0 F1 F0 F4 F3 F2 F2 F1 F1 F1 F0 F2 F1 F0 F2 F3 F1 F1 F0 F1 F0 F2 F1 F1 F0

Look at the execution of F(7) v[0] v[1] v[2] v[3] v[4] v[5] v[6] v[7] 1 2 -1 F5 F5 F4 F4 F3 F2 F2 F3 F3 F2 F1 F1 F0 F1 F0 F1 F0 F4 F3 F2 F2 F1 F1 F1 F0 F2 F1 F0 F2 F3 F1 F1 F0 F1 F0 F2 F1 F1 F0

Look at the execution of F(7) v[0] v[1] v[2] v[3] v[4] v[5] v[6] v[7] 1 2 3 -1 F5 F5 F4 F4 F3 F2 F2 F3 F3 F2 F1 F1 F0 F1 F0 F1 F0 F4 F3 F2 F2 F1 F1 F1 F0 F2 F1 F0 F2 F3 F1 F1 F0 F1 F0 F2 F1 F1 F0

Look at the execution of F(7) v[0] v[1] v[2] v[3] v[4] v[5] v[6] v[7] 1 2 3 5 -1 F5 F5 F4 F4 F3 F2 F2 F3 F3 F2 F1 F1 F0 F1 F0 F1 F0 F4 F3 F2 F2 F1 F1 F1 F0 F2 F1 F0 F2 F3 F1 F1 F0 F2 F1 F1 F0 no recursive calls F(1) & F(0) after F(2)

Look at the execution of F(7) v[0] v[1] v[2] v[3] v[4] v[5] v[6] v[7] 1 2 3 5 8 -1 F5 F5 F4 F4 F3 F2 F2 F3 F3 F2 F1 F1 F0 F1 F0 F1 F0 F4 F3 F2 F2 F1 F1 F1 F0 F1 F0 F2 F3 F2 F1 F1 F0 no recursive calls F(2) & F(1) after F(3)

Look at the execution of F(7) v[0] v[1] v[2] v[3] v[4] v[5] v[6] v[7] 1 2 3 5 8 13 -1 F5 F5 F4 F4 F3 F2 F3 F2 F1 F1 F0 F1 F0 F4 F3 F2 F1 F1 F0 F2 F3 F2 F1 F1 F0 no recursive calls F(3) & F(2) after F(4)

Look at the execution of F(7) v[0] v[1] v[2] v[3] v[4] v[5] v[6] v[7] 1 2 3 5 8 13 21 F5 F5 F4 F4 F3 F2 F3 F2 F1 F1 F0 no recursive calls F(4) & F(3) after F(5)

This new implementation saves lots of overhead. Can we do even better? Observation The 2nd version still makes many function calls, and each wastes time in parameters passing, dynamic linking, ... In general, to compute F(i), we need F(i-1) & F(i-2) only Idea to further improve Compute the values in bottom-up fashion. That is, compute F(2) (we already know F(0)=F(1)=1), then F(3), then F(4)… This new implementation saves lots of overhead. Procedure F(n) Set A[0] = A[1] = 1 for i = 2 to n do A[i] = A[i-1] + A[i-2] return A[n]

Recursive vs DP approach Recursive version: Procedure F(n) if n==0 or n==1 then return 1 else return F(n-1) + F(n-2) Too Slow! exponential Dynamic Programming version: Procedure F(n) Set A[0] = A[1] = 1 for i = 2 to n do A[i] = A[i-1] + A[i-2] return A[n] Efficient! Time complexity is O(n)

Summary of the methodology Write down a formula that relates a solution of a problem with those of sub-problems. E.g. F(n) = F(n-1) + F(n-2). Index the sub-problems so that they can be stored and retrieved easily in a table (i.e., array) Fill the table in some bottom-up manner; start filling the solution of the smallest problem. This ensures that when we solve a particular sub-problem, the solutions of all the smaller sub-problems that it depends are available. For historical reasons, we call such methodology Dynamic Programming. In the late 40’s (when computers were rare), programming refers to the "tabular method".

Exercise Consider the following function Write a recursive procedure to compute G(n) Draw the execution tree of computing G(6) recursively Using dynamic programming, write a pseudo code to compute G(n) efficiently What is the time complexity of your algorithm? 1 if 0 n2 G(n-1) + G(n-2) + G(n-3) if n > 2 G(n) =

Exercise 1 if 0 n2 G(n) = G(n-1) + G(n-2) + G(n-3) if n > 2 O(n) Recursive version: Procedure G(n) if n==0 or n==1 or n==2 then return 1 else return G(n-1) + G(n-2) + G(n-3) O(n) Dynamic Programming version: Procedure G(n) Set A[0] = A[1] = A[2] = 1 for i = 3 to n do A[i] = A[i-1] + A[i-2] + A[i-3] return A[n]

Assembly line scheduling …

Assembly line scheduling 2 assembly lines, each with n stations (Si,j: line i station j) S1,j and S2,j perform same task but time taken is different S1,1 S1,2 S1,3 S1,4 S1,n-1 S1,n a1,1 a1,2 a1,3 a1,4 a1,n-1 a1,n Line 1 t1,1 t1,2 t1,3 t1,n-1 start end t2,1 t2,2 t2,3 t2,n-1 a2,1 a2,2 a2,3 a2,4 a2,n-1 a2,n Line 2 S2,1 S2,2 S2,3 S2,4 S2,n-1 S2,n Problem: To determine which stations to go in order to minimize the total time through the n stations ai,j: assembly time at Si,j ti,j: transfer time after Si,j

Example (1) S1,1 S1,2 S1,3 S1,4 Line 1 5 5 9 4 2 4 1 start end 1 11 2 15 4 3 7 S2,1 S2,2 S2,3 S2,4 S1,1 S1,2 S2,3 S2,4 stations chosen: time required: 5 5 4 3 7 = 24

Example (2) S1,1 S1,2 S1,3 S1,4 Line 1 5 5 9 4 2 4 1 start end 1 11 2 15 4 3 7 S2,1 S2,2 S2,3 S2,4 S1,1 S1,2 S2,3 S2,4 stations chosen: time required: 5 5 4 3 7 = 24 S2,1 S1,2 S2,3 S1,4 stations chosen: time required: 15 1 5 4 3 2 4 = 34

Example (2) How to determine the best stations to go? There are altogether 2n choices of stations. Should we try them all? Line 1 5 5 9 4 2 4 1 start end 1 11 2 Line 2 15 4 3 7 S2,1 S2,2 S2,3 S2,4 S1,1 S1,2 S2,3 S2,4 stations chosen: time required: 5 5 4 3 7 = 24 S2,1 S1,2 S2,3 S1,4 stations chosen: time required: 15 1 5 4 3 2 4 = 34

All possible choices S1,1 S2,1 S1,2 S1,2 S1,2 S1,2 S1,3 S2,3 S1,3 S2,3

The two subtrees cost the same, only one path is needed. All possible choices The two subtrees cost the same, only one path is needed. S1,1 S2,1 S1,2 S1,2 S1,2 S1,2 S1,3 S2,3 S1,3 S2,3 S1,3 S2,3 S1,3 S2,3 S1,4 S2,4 S1,4 S2,4 S1,4 S2,4 S1,4 S2,4 S1,4 S2,4 S1,4 S2,4 S1,4 S2,4 S1,4 S2,4

All possible choices Similarly, … S1,1 S2,1 S1,2 S1,2 S1,2 S1,2 S1,3

All possible choices S1,1 S2,1 S1,2 S1,2 S1,2 S1,2 S1,3 S2,3 S1,3 S2,3

All possible choices S1,1 S2,1 S1,2 S1,2 S1,2 S1,2 S1,3 S2,3 S1,3 S2,3

Good news: Dynamic Programming We don’t need to try all possible choices. We can make use of dynamic programming: If we can compute the fastest ways to get thro’ station S1,n and S2,n, then the faster of these two ways is the overall fastest way. To compute the fastest ways to get thro’ S1,n (similarly for S2,n), we need to know the fastest way to get thro’ S1,n-1 and S2,n-1 In general, we want to know the fastest way to get thro’ S1,j and S2,j, for all j.

Example again S1,1 S1,2 S1,3 S1,4 Line 1 5 5 9 4 2 4 1 start end 1 11 15 4 3 7 S2,1 S2,2 S2,3 S2,4 S1,1 S1,2 S1,3 S1,4 5 minimum cost: S2,1 S2,2 S2,3 S2,4 15

Example again S1,1 S1,2 S1,3 S1,4 Line 1 5 5 9 4 2 4 1 start end 1 11 15 4 3 7 S2,1 S2,2 S2,3 S2,4 S1,1 S1,2 S1,3 S1,4 5+5 15+1+5 5 minimum cost: S2,1 S2,2 S2,3 S2,4 5+2+4 15+4 15

Example again S1,1 S1,2 S1,3 S1,4 Line 1 5 5 9 4 2 4 1 start end 1 11 15 4 3 7 S2,1 S2,2 S2,3 S2,4 S1,1 S1,2 S1,3 S1,4 5+5=10 15+1+5 10+9 11+11+9 5 minimum cost: S2,1 S2,2 S2,3 S2,4 5+2+4=11 15+4 10+4+3 11+3 15

Example again S1,1 S1,2 S1,3 S1,4 Line 1 5 5 9 4 2 4 1 start end 1 11 15 4 3 7 S2,1 S2,2 S2,3 S2,4 S1,1 S1,2 S1,3 S1,4 5+5=10 15+1+5 10+9=19 11+11+9 19+4 14+2+4 5 20 minimum cost: S2,1 S2,2 S2,3 S2,4 5+2+4=11 15+4 10+4+3 11+3=14 19+1+7 14+7 15 21

Example again S1,1 S1,2 S1,3 S1,4 Line 1 5 5 9 4 2 4 1 start end 1 11 15 4 3 7 S2,1 S2,2 S2,3 S2,4 S1,1 S1,2 S1,3 S1,4 5+5=10 15+1+5 10+9=19 11+11+9 19+4 14+2+4 5 20 minimum cost: S2,1 S2,2 S2,3 S2,4 5+2+4=11 15+4 10+4+3 11+3=14 19+1+7 14+7 15 21

A dynamic programming solution What are the sub-problems? given j, what is the fastest way to get thro' S1,j given j, what is the fastest way to get thro' S2,j Definitions: f1[j] = the fastest time to get thro' S1,j f2[j] = the fastest time to get thro' S2,j The final solution equals to min { f1[n], f2[n] } Task: Starting from f1[1] and f2[1], compute f1[j] and f2[j] incrementally

Solving the sub-problems (1) Q1: what is the fastest way to get thro' S1,j? A: either the fastest way thro' S1,j-1, then directly to S1,j, or the fastest way thro' S2,j-1, a transfer from line 2 to line 1, and then through S1,j S1,1 S1,j-1 S1,j Line 1 start Line 2 S2,1 S2,j-1 S2,j

Solving the sub-problems (1) Q1: what is the fastest way to get thro' S1,j? A: either the fastest way thro' S1,j-1, then directly to S1,j, or the fastest way thro' S2,j-1, a transfer from line 2 to line 1, and then through S1,j S1,1 S1,j-1 S1,j a1,j Line 1 Time required = f1[j-1] + a1,j start Line 2 S2,1 S2,j-1 S2,j

Solving the sub-problems (1) Q1: what is the fastest way to get thro' S1,j? A: either the fastest way thro' S1,j-1, then directly to S1,j, or the fastest way thro' S2,j-1, a transfer from line 2 to line 1, and then through S1,j S1,1 S1,j-1 S1,j a1,j Line 1 Time required = f2[j-1] + t2,j-1 + a1,j start t2,j-1 Line 2 S2,1 S2,j-1 S2,j

Solving the sub-problems (1) Q1: what is the fastest way to get thro' S1,j? A: either the fastest way thro' S1,j-1, then directly to S1,j, or the fastest way thro' S2,j-1, a transfer from line 2 to line 1, and then through S1,j Conclusion: Boundary case: f1[j] = min( f1[j-1]+a1,j , f2[j-1]+t2,j-1+a1,j ) f1[1] = a1,1 start Line 1 Line 2 S1,1 S1,j-1 S2,1 S2,j-1 S2,j S1,j

Solving the sub-problems (2) Q2: what is the fastest way to get thro' S2,j? By exactly the same analysis, we obtain the formula for the fastest way to get thro' S2,j: Boundary case: f2[j] = min( f2[j-1]+a2,j , f1[j-1]+t1,j-1+a2,j ) f2[1] = a2,1 S1,1 S1,j-1 S1,j Line 1 start Line 2 S2,1 S2,j-1 S2,j

Summary j f1[j] f2[j] 1 2 3 4 a1,1 if j=1, min ( f1[j-1]+a1,j , f2[j-1]+t2,j-1+a1,j ) if j>1 f1[j] = a2,1 if j=1, min ( f2[j-1]+a2,j , f1[j-1]+t1,j-1+a2,j ) if j>1 f2[j] = f* = min( f1[n] , f2[n] ) j f1[j] f2[j] 1 2 3 4 Line 1 5 5 9 4 2 4 1 start end 1 11 2 4 3 7 Line 2 15

Summary j f1[j] f2[j] 1 5 15 2 3 4 a1,1 if j=1, min ( f1[j-1]+a1,j , f2[j-1]+t2,j-1+a1,j ) if j>1 f1[j] = a2,1 if j=1, min ( f2[j-1]+a2,j , f1[j-1]+t1,j-1+a2,j ) if j>1 f2[j] = f* = min( f1[n] , f2[n] ) 5 j f1[j] f2[j] 1 5 15 2 3 4 Line 1 5 5 9 4 2 4 1 start end 1 11 2 15 4 3 7 Line 2 15

Summary j f1[j] f2[j] 1 5 15 2 10 11 3 4 a1,1 if j=1, min ( f1[j-1]+a1,j , f2[j-1]+t2,j-1+a1,j ) if j>1 f1[j] = a2,1 if j=1, min ( f2[j-1]+a2,j , f1[j-1]+t1,j-1+a2,j ) if j>1 f2[j] = f* = min( f1[n] , f2[n] ) 5 10 j f1[j] f2[j] 1 5 15 2 10 11 3 4 Line 1 5 5 9 4 21 2 4 1 start end 1 11 2 11 15 4 3 7 Line 2 15 19

Summary j f1[j] f2[j] 1 5 15 2 10 11 3 19 14 4 a1,1 if j=1, min ( f1[j-1]+a1,j , f2[j-1]+t2,j-1+a1,j ) if j>1 f1[j] = a2,1 if j=1, min ( f2[j-1]+a2,j , f1[j-1]+t1,j-1+a2,j ) if j>1 f2[j] = f* = min( f1[n] , f2[n] ) 5 10 19 j f1[j] f2[j] 1 5 15 2 10 11 3 19 14 4 Line 1 5 5 9 4 31 2 4 1 start end 1 11 2 11 17 15 4 3 7 Line 2 15 14

Summary j f1[j] f2[j] 1 5 15 2 10 11 3 19 14 4 20 21 a1,1 if j=1, min ( f1[j-1]+a1,j , f2[j-1]+t2,j-1+a1,j ) if j>1 f1[j] = a2,1 if j=1, min ( f2[j-1]+a2,j , f1[j-1]+t1,j-1+a2,j ) if j>1 f2[j] = f* = min( f1[n] , f2[n] ) 5 10 19 23 j f1[j] f2[j] 1 5 15 2 10 11 3 19 14 4 20 21 Line 1 5 5 9 4 2 4 1 20 start end 1 11 2 11 27 15 4 3 7 Line 2 15 14 21

Summary a1,1 if j=1, min ( f1[j-1]+a1,j , f2[j-1]+t2,j-1+a1,j ) if j>1 f1[j] = a2,1 if j=1, min ( f2[j-1]+a2,j , f1[j-1]+t1,j-1+a2,j ) if j>1 f2[j] = f* = min( f1[n] , f2[n] ) f* = 20 5 10 19 j f1[j] f2[j] 1 5 15 2 10 11 3 19 14 4 20 21 Line 1 5 5 9 4 2 4 1 20 start end 11 1 11 2 15 4 3 7 Line 2 15 14 21

Time complexity is O(n) Pseudo code Time complexity is O(n) set f1[1] = a1,1 set f2[1] = a2,1 for j = 2 to n do begin set f1[j] = min ( f1[j-1]+a1,j , f2[j-1]+t2,j-1+a1,j ) set f2[j] = min ( f2[j-1]+a2,j , f1[j-1]+t1,j-1+a2,j ) end set f* = min (f1[n] , f2[n] )

Exercise j f1[j] f2[j] 1 2 3 4 Line 1 10 20 15 15 2 3 2 start end 1 2

Exercise – solution f* = 54 j f1[j] f2[j] 1 10 2 30 25 3 42 45 4 57 54 Line 1 10 20 15 15 42 31 64 2 3 3 2 f* = 54 start end 1 2 4 54 27 53 j f1[j] f2[j] 1 10 2 30 25 3 42 45 4 57 54 10 15 20 10 Line 2 25 45 55