Algo-CH-4 Dynamic Programming
Algo-CH-4 Dynamic Programming
Programming
By: Elsay M.
Subtopics in Dynamic Programming
(6hr)
4.1. Introduction to Dynamic Programming
• For a graph:
(4) (2)
• A -----> B -----> C
\ \
(1) (5)
\ \
D -----> E
(3)
• Using Dijkstra from node A, the shortest paths to all other
nodes are found by exploring each neighboring node and
updating their distances.
4.4. 0/1 Knapsack Problem
• The 0/1 Knapsack Problem is a classical dynamic programming
problem. You are given a set of items, each with a weight and
value, and a knapsack that can carry up to a certain weight. The
goal is to maximize the value of items in the knapsack without
exceeding its weight limit. The "0/1" means you either include an
item in the knapsack or exclude it (no partial items allowed).
How it works:
• Build a table where each cell dp[i][j] represents the maximum
value that can be achieved with the first i items and a knapsack of
weight j.
• For each item, you decide whether to include it (if it fits) or exclude
it.
• If you include the item, add its value to the total and reduce the
remaining capacity accordingly.
• Example: Let’s say we have 3 items:
A ---- B ---- C
| |
D ---- E
• Starting at node A:
• First visit A.
• Move to B, then to C (as far as possible).
• Backtrack to B, then visit E, and finally D.
• The DFS traversal would be: A → B → C → E → D.
Summary of Chapter 4 :
• Dynamic Programming is about breaking problems into
overlapping subproblems and using stored solutions to avoid
redundant calculations.
• Floyd-Warshall solves the all-pairs shortest path problem for graphs
with positive and negative weights.
• Dijkstra’s Algorithm finds the shortest path from a single source
node, with non-negative weights.
• The 0/1 Knapsack Problem is a combinatorial optimization problem
where we maximize value without exceeding a weight limit.
• Depth First Search (DFS) is a graph traversal technique that
explores each path as deeply as possible before backtracking.
• These algorithms are key tools for solving optimization problems in
fields like networking, resource allocation, and decision-making.