-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDSA2_9.html
More file actions
94 lines (86 loc) · 5.82 KB
/
Copy pathDSA2_9.html
File metadata and controls
94 lines (86 loc) · 5.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<article>
<h1>DSA II: Page 9 - Dynamic Programming</h1>
<section>
<h2>Solving Problems Efficiently</h2>
<p><strong>Dynamic Programming (DP)</strong> is not a specific algorithm, but a <em>method</em> for solving complex problems by breaking them down into simpler <strong>overlapping subproblems</strong>. The core idea is simple: if you have already solved a subproblem once, why solve it again? By storing the results (caching) of subproblems, you can turn an exponentially slow algorithm into a fast, linear one. This is the classic computer science trade-off: <strong>trading extra memory for faster execution time</strong>.</p>
</section>
<section>
<h2>1. The Two Pillars of DP</h2>
<p>A problem can only be solved with DP if it has these two properties:</p>
<ul>
<li><strong>Overlapping Subproblems:</strong> The problem can be broken down into subproblems which are reused several times.</li>
<li><strong>Optimal Substructure:</strong> The optimal solution to the problem can be constructed from optimal solutions to its subproblems.</li>
</ul>
<h3>Approaches:</h3>
<ul>
<li><strong>Memoization (Top-Down):</strong> Recursively break the problem down, storing results in a table (e.g., a dictionary/map) as you go.</li>
<li><strong>Tabulation (Bottom-Up):</strong> Start from the smallest subproblems and build a table up to the main problem iteratively.</li>
</ul>
<pre><code class="language-python">
# DP (Memoization) example: Fibonacci
def fib(n, memo={}):
if n in memo: return memo[n]
if n <= 1: return n
memo[n] = fib(n-1, memo) + fib(n-2, memo)
return memo[n]
</code></pre>
</section>
<section>
<h2>2. Real-World DP Applications</h2>
<p>DP is used everywhere from financial modeling to bioinformatics:</p>
<ul>
<li><strong>Knapsack Problem:</strong> Optimizing item selection based on weights and values.</li>
<li><strong>Longest Common Subsequence:</strong> Comparing DNA strands or text diffing.</li>
<li><strong>Shortest Path (e.g., Bellman-Ford):</strong> Finding paths in graphs with negative weights.</li>
</ul>
<div class="interactive-sim">
<h3>DP Simulator</h3>
<button class="sim-button" onclick="
const out = document.getElementById('sim-output-ds2-9');
out.innerHTML = 'Calculating Fibonacci(5)...<br>';
setTimeout(() => out.innerHTML += '>> Fibonacci(5) is 5', 800);
">Run Simulation</button>
<div id="sim-output-ds2-9" class="sim-output"></div>
</div>
</section>
<section>
<h2>Visual Learning: Video Tutorials</h2>
<p>Master dynamic programming with these three videos:</p>
<div style="display: flex; gap: 20px; flex-wrap: wrap; margin-top: 20px;">
<div style="flex: 1; min-width: 250px; background: #eee; padding: 15px; border-radius: 8px;">
<strong>1. Dynamic Programming Explained</strong><br>
<a href="https://www.youtube.com/watch?v=BBpAmxU_NQo" target="_blank">Watch on YouTube →</a>
<p><small>The concepts of overlapping subproblems.</small></p>
</div>
<div style="flex: 1; min-width: 250px; background: #eee; padding: 15px; border-radius: 8px;">
<strong>2. Memoization vs. Tabulation</strong><br>
<a href="https://www.youtube.com/watch?v=8hly31xKli0" target="_blank">Watch on YouTube →</a>
<p><small>Top-down vs. Bottom-up approaches.</small></p>
</div>
<div style="flex: 1; min-width: 250px; background: #eee; padding: 15px; border-radius: 8px;">
<strong>3. Solving the Knapsack Problem</strong><br>
<a href="https://www.youtube.com/watch?v=zg9ih6SVACc" target="_blank">Watch on YouTube →</a>
<p><small>Classic DP application walkthrough.</small></p>
</div>
</div>
</section>
<section>
<h2>Real-World Relationship: The Compound Interest Calculator</h2>
<p>Think of <strong>Dynamic Programming</strong> like calculating <strong>Compound Interest</strong> over 30 years. To calculate the interest for year 30, you need the balance from year 29. To get year 29, you need year 28. If you calculate year 1, then year 2, then year 3... and store each intermediate result in a list (Tabulation), you can calculate year 30 instantly. If you tried to calculate year 30 without knowing year 29 (the naive recursive approach), you would have to recalculate years 1–28 millions of times, making the calculation take thousands of years instead of milliseconds.</p>
</section>
<section>
<h2>References & Additional Learning</h2>
<ul>
<li><a href="https://en.wikipedia.org/wiki/Dynamic_programming" target="_blank">Wikipedia: DP Overview</a></li>
<li><a href="https://www.geeksforgeeks.org/dynamic-programming/" target="_blank">GeeksforGeeks: DP Basics</a></li>
<li><a href="https://www.w3schools.com/dsa/dsa_dynamicprogramming.php" target="_blank">W3Schools: DSA DP</a></li>
<li><a href="https://visualgo.net/en/dp" target="_blank">Tool: DP Visualization</a></li>
</ul>
</section>
<footer style="margin-top: 40px; padding: 20px; background: #f8f9fa; border-top: 1px solid #dee2e6;">
<div style="display: flex; justify-content: space-between;">
<a href="#" data-file="DSA2_8.html" style="text-decoration: none; color: #6c757d;">← Previous: Graph Traversal</a>
<a href="#" data-file="DSA2_10.html" style="font-weight: bold; text-decoration: none; color: #007bff;">Next: Optimization Problems →</a>
</div>
</footer>
</article>