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

Module11_intro_slides_Maryam

The document outlines the agenda for Module 11 of CS 5008, focusing on Dynamic Programming and its comparison with Divide & Conquer. It covers various topics including Fibonacci number implementations, the Fractional and 0/1 knapsack problems, and includes exercises and discussions for classification of problems. Key takeaways include the differences in problem-solving approaches and the complexities associated with dynamic programming solutions.

Uploaded by

chen.sizhi
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)
3 views

Module11_intro_slides_Maryam

The document outlines the agenda for Module 11 of CS 5008, focusing on Dynamic Programming and its comparison with Divide & Conquer. It covers various topics including Fibonacci number implementations, the Fractional and 0/1 knapsack problems, and includes exercises and discussions for classification of problems. Key takeaways include the differences in problem-solving approaches and the complexities associated with dynamic programming solutions.

Uploaded by

chen.sizhi
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/ 15

CS 5008

Module 11: Dynamic Programming


3
Agenda
1. Divide & Conquer vs. Dynamic Programming
2. Fibonacci Numbers (different implementations)
3. Fractional knapsack problem
4. 0/1 knapsack problem (dynamic programming)
5. Classify a problem: exercise and discussion
6. Menti quiz! (if time allows)

4
Divide & Conquer vs. Dynamic Programming
What are the similarities and differences between these two algorithmic
paradigms?

5
Divide & Conquer vs. Dynamic Programming
What are the similarities and differences between these two algorithmic
paradigms?

Both break a problem into subproblems…

6
Divide & Conquer vs. Dynamic Programming
What are the similarities and differences between these two algorithmic
paradigms?

Both break a problem into subproblems…

D&C: Each subproblem is solved independently. Examples?

7
Divide & Conquer vs. Dynamic Programming
What are the similarities and differences between these two algorithmic
paradigms?

Both break a problem into subproblems…

D&C: Each subproblem is solved independently. Example? Sorting

DP: Subproblems overlap, solve each once, and store their solutions to
avoid recomputation (memoization or tabulation). Example? LCS (longest
Common Substring), 0/1 Knapsack problem
Every dynamic programming solution involves a grid/table.
8
Fibonacci
Three ways to implement Fibinacci
- Recursive naïve (Time: O(2n), space: O(n))
- Dynamic programming (O(n) for time and space)
- Non-recursive, space-efficient (O(1))

9
Exercise 1: Fractional Knapsack
Please work in groups 2 or 3. (10 minutes)

10
Fractional Knapsack – Takeawys
• Greedy algorithm to solve the problem:
➢ You’re choosing the item with the most value per unit of weight at every step.

➢ This local optimum choice leads to a global optimum.

• For the Fractional Knapsack Problem, the greedy algorithm always gives
the optimal solution.

11
Exercise 2: 0/1 Knapsack Problem
Please work in groups 2 or 3. (20 minutes)

12
0/1 Knapsack Problem – Takeways (1)
• Each dp[i][w] holds the maximum value you can achieve using the first i
items and a knapsack of size w.

• The classic DP solution (table-based) uses a 2D array dp[i][w] where i is


the item index and w is the current weight capacity.

• Since arrays require integer indexing, this approach assumes integer


weights.

• Memoization the technique that caches function results to speed up


repeated calls with the same inputs.

13
0/1 Knapsack Problem – Takeways (2)
• Time complexity of the dynamic programming solution to the 0/1
knapsack problem (N items, capacity is W): O(N*W)

• Space complexity of the dynamic programming solution to the 0/1


knapsack problem (N items, capacity is W): O(N*W)

14
Exercise 3: Classifying Problems
Please work in groups 2 or 3. (15 minutes)

15
Your feedback is super important
TRACE survey is open now!

Any extra feedback?


Please email me!
Only 5008 is
needed.
5009 is optional

16

You might also like