0% found this document useful (0 votes)
6 views29 pages

10 Dynamic Programing DP

The document provides an overview of Dynamic Programming (DP) as an optimization technique that solves complex problems by breaking them into interdependent subproblems and caching results. It outlines key concepts such as overlapping subproblems and optimal substructure, along with design steps for DP problems, including state definition, recurrence relations, and computation methods. Several examples, including the Fibonacci sequence, 0-1 Knapsack problem, and flight scheduling, illustrate the application of DP in various scenarios.

Uploaded by

gihod10765
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)
6 views29 pages

10 Dynamic Programing DP

The document provides an overview of Dynamic Programming (DP) as an optimization technique that solves complex problems by breaking them into interdependent subproblems and caching results. It outlines key concepts such as overlapping subproblems and optimal substructure, along with design steps for DP problems, including state definition, recurrence relations, and computation methods. Several examples, including the Fibonacci sequence, 0-1 Knapsack problem, and flight scheduling, illustrate the application of DP in various scenarios.

Uploaded by

gihod10765
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/ 29

ANALYSIS AND DESIGN

OF ALGORITHMS

Instructor: Dr. Mert Büyükdede

1
Dynamic Programing
Dynamic Programming (DP) is an optimization technique that breaks a large, complex
problem into interdependent subproblems, computes each subproblem exactly once, and
caches their results for reuse in subsequent computations.

Core Ideas

•Overlapping Subproblems: If the same subproblem would be solved multiple times,


we store its result (via memoization or tabulation) to avoid redundant work.

•Optimal Substructure: If an optimal solution to the overall problem can be composed


from optimal solutions of its subproblems, the problem exhibits optimal substructure and
is flexible to DP.
Comparison

Dynamic
Feature Brute-Force Divide & Conquer
Programming
Subproblem sharing None Rare Heavy (cached)
Time complexity Exponential Often exponential Typically polynomial
Overlapping
Typical use cases Tiny inputs Independent parts
subproblems
DP Design Steps

When solving any DP problem, follow these five general stages:

1. State Definition
•Identify the parameters that define each subproblem.
•Example (Knapsack):
•dp[i][j]=maximum value using the first i items with capacity j

2. Recurrence Relation

•Express each state in terms of smaller states.


•Example (Knapsack):
3. Base Cases

•Define the solution for the smallest subproblems.


•Example:
•dp[0][∗]=0, dp[∗][0]=0.

4. Computation Order & Method

Top-down (Memoization): Recursively compute subproblems, storing answers in a cache


(map or array).

Bottom-up (Tabulation): Iteratively fill a table (array) according to increasing subproblem


size.

5. Reading the Final Answer


Extract the solution from the DP table (e.g. 𝑑𝑝[𝑛][𝑊]) or from the memorized cache.
There are a lot of example in Dynamic Programing Approach.

• Fibonacci Sequance
• 0-1 Knapsack Problem
• Longest Common Subsequence (LCS)
Fibonacci Sequence

Let’s examine the Fibonacci example step by step, illustrating both how and why
Dynamic Programming (DP) improves over naïve recursion, and exploring
optimizations in time and space.

Problem Statement
Compute the 𝑛th Fibonacci number, where

𝐹(0)=0, 𝐹(1)=1, 𝐹(𝑛)=𝐹(𝑛−1)+𝐹(𝑛−2) (𝑛≥2).

Fibonacci numbers grow exponentially if computed by the naïve recursive definition,


because each call to F(n) spawns two subcalls (F(n-1) and F(n-2)), and these overlap
heavily.
Bottom-up Pseudocode
Top-down (Recursive with Memoization) Pseudocode
0-1 Knapsack Problem

Given items with weights wi and values vi, and a capacity W, the 0-1 Knapsack asks for the
maximum total value of a subset whose weights sum to at most W. DP defines dp[i][j] as
the best value using the first iii items within capacity j. Filling this table takes O(nW) time,
and space can be cut to O(W) by using a single array in reverse. It’s widely applied in
resource allocation, budgeting, and cargo loading where discrete choices must be optimized
under a limit.

Longest Common Subsequence (LCS)

LCS finds the longest sequence appearing (not necessarily contiguously) in both strings A
and B. Define dp[i][j] as the LCS length of prefixes A[1..i], B[1..j]B. If A[i]=B[j], take
dp[i−1][j−1]+1; otherwise take the max of dropping one character. The table fills in O(mn)
time and O(mn) space. LCS is fundamental in diff/merge tools, version control, and
bioinformatics for identifying shared patterns.
Coin Collecting Problem

Problem Statement:

Several coins are placed in cells of an n x m board, no more than one coin per cell.

A robot, located in the upper left cell of the board, needs to collect as many of the coins as
possible and bring them to the bottom right cell.

On each step, the robot can move either one cell to the right or one cell down from its
current location. When the robot visits a cell with a coin, it always picks up that coin.

Design an algorithm to find the maximum number of coins the robot can collect and a path
it needs to follow to do this.
Let’s create Dynamic Programing Table
Example
Let’s create Dynamic Programing Table
Example Problem

Weighted Interval Scheduling (Single-Aircraft Daily Schedule)

An airline must schedule a single aircraft for the day.You are given n flight offers; each
flight j has:
•Departure time sj
•Arrival time fj (fj>sj))
•Profit pj

Since the aircraft cannot operate more than one flight at the same time, select a subset
of non-overlapping flights to maximize total profit.
Data Set

j Flight ID Departure Arrival Profit (USD)


1 F1 05:45 07:30 420
2 F2 06:20 08:10 380
3 F3 07:00 09:15 510
4 F5 09:10 11:20 600
5 F6 10:30 12:05 550
6 F8 12:15 14:00 480
7 F10 14:05 16:30 800
1. Compute “prev” Array
Define

or 0 if no such i exists.
j sj fj prev[j] Reason
1 05:45 07:30 0 —
Flight 1 arrives at 07:30 > 06:20 ⇒
2 06:20 08:10 0
none
3 07:00 09:15 0 F1, F2 arrive after 07:00
F2 (08:10) ≤ 09:10, but F3 arrives
4 09:10 11:20 2
at 09:15 > 09:10
5 10:30 12:05 3 F3 (09:15) ≤ 10:30
6 12:15 14:00 5 F6 (12:05) ≤ 12:15
7 14:05 16:30 6 F8 (14:00) ≤ 14:05
2. DP Definition & Recurrence
3. Fill the DP Table

Include = Exclude =
j prev[j] pj dp[j]
pj+dp[prev[j]] dp[j−1]

0 — — — — 0
1 0 420 420 + 0 = 420 0 420
2 0 380 380 + 0 = 380 420 420
3 0 510 510 + 0 = 510 420 510
4 2 600 600 + 420 = 1020 510 1020
5 3 550 550 + 510 = 1060 1020 1060
6 5 480 480 + 1060 = 1540 1060 1540
7 6 800 800 + 1540 = 2340 1540 2340
4. Result & Backtracking

•Maximum Profit:
•dp[7]=2340 USD..
•Selected Flights:
Backtrack from j=7j=7j=7:
• dp[7] came from “include” ⇒ select Flight 7, jump to j=prev[7]=6
• dp[6] came from “include” ⇒ select Flight 6, jump to j=5
• dp[5] came from “include” ⇒ select Flight 5, jump to j=3
• dp[3] came from “include” ⇒ select Flight 3, jump to j=0

•Chosen: Flights 3, 5, 6, 7

Total Profit: 510+550+480+800=2340 USD.


Let’s try to write Algorithm with PseudoCode for DP.
Activity:

Below you will see a new DP scenario called “Film Festival Theater Planning”. You have a single
theater and movies of different lengths will be shown throughout the day. Each movie has a
start-end time and an expected number of viewers. Your goal is to maximize the total number of
viewers by selecting movies that do not overlap.
j Film Title Start – End Expected Viewers
1 “Dawn of Dreams” 09:00–10:30 200
2 “Midday Melody” 10:00–11:15 150
3 “Noon Noir” 10:45–12:30 300
4 “Afternoon Aura” 12:00–13:30 250
5 “Sunset Sonata” 13:00–14:00 100
6 “Evening Epic” 13:30–15:00 180
7 “Night Narrative” 14:30–16:00 220

The festival runs from 09:00 to 16:00 in a single screening room.


Task 1: Computing prev[j]
For each film 𝑗, find the largest index 𝑖<𝑗 whose end time “does not overlap” with film 𝑗’s start:

j End prev[j] Explanation


1 10:30 0 —
Film 1 ends 10:30 > 10:00
2 11:15 0
(overlaps)
3 12:30 1 Film 1 ends 10:30 ≤ 10:45

4 13:30 2 Film 2 ends 11:15 ≤ 12:00

5 14:00 3 Film 3 ends 12:30 ≤ 13:00

6 15:00 4 Film 4 ends 13:30 ≤ 13:30

7 16:00 5 Film 5 ends 14:00 ≤ 14:30


Task: 2 DP Definition & Recurrence
Task 3: Step-by-Step Calculation

Include = viewers_j +
j prev[j] viewers_j Exclude = dp[j-1] dp[j]
dp[prev[j]]
0 — — — — 0
1 0 200 200 + 0 = 200 0 200
2 0 150 150 + 0 = 150 200 200
3 1 300 300 + 200 = 500 200 500
4 2 250 250 + 200 = 450 500 500
5 3 100 100 + 500 = 600 500 600
6 4 180 180 + 500 = 680 600 680
7 5 220 220 + 600 = 820 680 820
Task 4: Pseudocode
Task 4: Pseudocode
HOMEWORK Task 5: Java Implemention

You might also like