Module11_intro_slides_Maryam
Module11_intro_slides_Maryam
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?
6
Divide & Conquer vs. Dynamic Programming
What are the similarities and differences between these two algorithmic
paradigms?
7
Divide & Conquer vs. Dynamic Programming
What are the similarities and differences between these two algorithmic
paradigms?
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.
• 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.
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)
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!
16