0% found this document useful (0 votes)
4 views

Week8_DynamicProgramming

The document discusses dynamic programming, emphasizing its efficiency in solving optimization problems by storing solutions to overlapping subproblems. It provides examples such as the rod-cutting problem and assembly line scheduling, detailing methods like memoization and bottom-up approaches. The document also contrasts stable and unstable sorting algorithms, highlighting their relevance in specific contexts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Week8_DynamicProgramming

The document discusses dynamic programming, emphasizing its efficiency in solving optimization problems by storing solutions to overlapping subproblems. It provides examples such as the rod-cutting problem and assembly line scheduling, detailing methods like memoization and bottom-up approaches. The document also contrasts stable and unstable sorting algorithms, highlighting their relevance in specific contexts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 42

BMI2224 Algorithms

Week 8:Dynamic Programming

Asst. Prof. Dr. Arzum Karataş

2025-Spring
Recall: Last Lecture
• A sorting algorithm is stable when numbers with the same values
appear in the output array in the same order as they do in the
input array.

2
Q. Do we care for simple arrays like the array of integers?
A. No!

Q. When do we care about stability of a sorting algorithm?


A. Stability of sorting algorithms is important when keys with the same value
exist!

Stable Sorting Algorithms


◦ Merge Sort,
◦ Counting Sort,
◦ Insertion Sort,
◦ Bubble Sort.
◦ Insertion Sort
◦ Radix Sort

Unstable Sorting Algorithms


◦ Quicksort,
◦ Heapsort
◦ Selection Sort
◦ Shell Sort
◦ Bucket Sort
3
Radix sort example

Bucket sort example

4
Agenda
• Dynamic Programming
• Example: Rod Cutting
• Example: Assembly Line Scheduling

5
Dynamic Programming
Dynamic programming, like the divide-and-conquer method, solves problems by
combining the solutions of subproblems.

In dynamic programming applies when the subproblems overlap—that is, when


subproblems share common subsubproblems.
A divide-and-conquer algorithm does more work than necessary,
repeatedly solving the common subsubproblems.
A dynamic-programming algorithm solves each subsubproblem just once and then
saves its answer in a table, thereby avoiding the work of recomputing the answer
every time it solves each subsubproblem.

In divide-and-conquer algorithms; partition the problem into disjoint subproblems,


solve the subproblems recursively, and then combine their solutions to solve the
original problem.
Dynamic programming is applied to optimization problems. Such problems can
have many possible solutions. Each solution has a value, and we wish to find a
solution with the optimal (minimum or maximum) value.
6
Some example problems solved by
Dynamic Programming
•Rod Cutting

•Assembly Line Scheduling

•Longest Common Subsequence

•Matrix Chain Multiplication


8
Example 1: Rod-cutting
Given: n inch steel rod, prices by lenght
Find: maximum amount you can get by cutting and
selling it?

Steel rod Prices without cutting

Example: How should you cut your 4 inch rod to maximize its sale?

9
Example 1: Rod-cutting (cont.)
Example: How should you cut your 4 inch rod to maximize its sale?

Possible combinations:

10
Example 1: Rod-cutting(cont.)

11
Rod-cutting:Recursive implementation

p is the price list, n is the problem size.

12
Rod-cutting:Recursive implementation

The recursion tree showing recursive calls from a


call of CUT-ROD(p,n) when n=4.
T(n)=2n
13
Can we have a better solution?

14
rod-cutting problem : using dynamic
programming

The naïve recursive solution is inefficient, because it solves the same


subproblems repeatedly.
We arrange for each subproblem to be solved only once, saving its solutions;
thus uses additional memory to save computation time; (time-memory trade-
off).
As a result; an exponential-time solution may be transformed into a
polynomial-time solution.
15
Using Dynamic Programming
There are two equivalent ways to implement a dynamic programming
approach:
1. Top-down with memoization; write the solution recursively in a natural
manner, but modified to save the results of each subproblem (usually in
an array or hash table).
2. Bottom-up method; it depends on notion of the “size” of a subproblem,
such that any particular subproblem depends only on solving “smaller”
subproblems. We sort the problems by size and solve them in size order,
the smallest is first, and we have saved their solutions. Each subproblem
is solved only once, and when we first see it, we have already solved all
of its prerequisite subproblems.
These two approach yield algorithms with the same asymptotic running
time, except unusual circumstances.

Memoization: is a technique, consists of recording a value so


that we can look it up later.
16
Rod-cutting: Optimal substructure
To solve the original problem, we solve smaller problems of the same
type. We say, rod-cutting problem exhibits optimal substructure.

17
Example: How should you cut your 8 inch rod to
maximize its sale?

18
We can use memoization!
19
V

20
V

21
V

22
Rod-cutting:Memoized approach

Initializing table

If the problem is previously solved for n

T(n) = Θ(n2).
23
Rod-cutting: Bottom-up Method
The bottom-up version is even This solution uses the natural ordering of
simpler:
the subproblems:
a problem size i is “smaller” than a
BUTTOM-UP-CUT-ROD(p,n) subproblem of size j if i<j. The procedure
1 Let r[0..n] be a new array solves subproblems of sizes j=0,1,...,n, in
that order.
2 r[0]=0
3 for j=1 to n
4 q=-∞ Solve each subproblem of size j in
5 for i=1 to j order of increasing size.
6 q=max(q, p[i]+r[j-i])
7 r[j] = q
In the example, we used this method!
8 return r[n]

24
rod-cutting problem : using dynamic
programming
The running time;
◦ For BOTTOM-UP-CUT-ROD is  ( n 2 ,) due to its doubly-
nested loops.
◦ For top-down algorithm MEMOIZED-CUT-ROD is also  ( n 2 )

A recursive call is used to solve a previously subproblem, but each


subproblem is solved only once. To solve a subproblem of size n,
the for loop of lines 5-6 iterates n times. Thus, total number of
iterations of this for loop, over all recursive calls; forms an
arithmetic series, given a total of  ( n 2 ) iterations.

25
Hence:

26
Reconstructing a Solution
EXTENDED-BOTTOM-UP-CUT-ROD(p,n)
1 let r[0..n] and s[0..n] be a new arrays
2 r[0]=0
3 for j=1 to n
4 q=-∞
5 for i=1 to j
The array s holds optimal size i
6 if q< p[i]+r[j-i] of the first piece to cut off when
7 q= p[i]+r[j-i] solving subproblem of size j.

8 s[j] = i
PRINT-CUT-ROD-SOLUTION(p,n)
9 r[j] = q 1 (r,s)=EXTENDED-BOTTOM-UP-CUT-ROD(p,n)
10 return r[n] 2 while n>0
3 print s[n]
4 n=n-s[n]
27
Reconstructing a Solution
EXTENDED-BOTTOM-UP-CUT-ROD(p,n)
1 let r[0..n] and s[0..n] be a new arrays
2 r[0]=0
3 for j=1 to n PRINT-CUT-ROD-SOLUTION(p,n)
1 (r,s)=EXTENDED-BOTTOM-UP-CUT-ROD(p,n)
4 q=-∞ 2 while n>0
5 for i=1 to j 3 print s[n]
4 n=n-s[n]
6 if q< p[i]+r[j-i]
7 q= p[i]+r[j-i]
8 s[j] = i Examle: call PRINT-CUT-ROD-SOLUTION(p,10)
9 r[j] = q
10 return r[n]

Length 1 2 3 4 5 6 7 8 9 10
i
Price pi 1 5 8 9 10 17 17 20 24 30
28
Example 2: Assembly-Line Scheduling

L2.29
30
31
Assembly-Line Scheduling
Assembly-Line Scheduling
Assembly-Line Scheduling
Generally: An optimal solution to a problem (fastest way through S1,j)
contains within it an optimal solution to subproblems (fastest way through
S1,j-1 or S2,j-1).
Use optimal substructure to construct optimal solution to problem from
optimal solutions to subproblems.

Symmetrically:
Assembly-Line Scheduling : Recursive Solution
Assembly-Line Scheduling : Recursive Solution
Algorithm
40
Trace of the algorithm

41
Next Week
Greedy Algorithms

image source: https://encrypted -


tbn0.gstatic.com/images?q=tbn:ANd9GcQEoYhJmAAgQlHlZa5J8VRYsk8N89wFAUIdJYyO1FVnrStZF4idg_gr-
42
XOs_z0NzCntGsI&usqp=CAU
43

You might also like