Algorithm Analysis & Design (CST306) - Module 4 Study Guide
Dynamic Programming: Matrix Chain Multiplication
[SURE SHOT | ↻ REPEATED PYQ]
Section A: What is this? (The Concept)
Imagine you are a chef in a high-pressure kitchen. You have three tasks: peeling potatoes, chopping them, and frying them. Depending on which tools you use and which task you do first, the total effort changes. If you chop then peel, it takes forever. If you peel first then chop, it’s efficient.
The Analogy: Matrix Chain Multiplication is like finding the most efficient “recipe” for multiplying a long string of matrices. Multiplying matrices is expensive (takes many scalar multiplications). Because matrix multiplication is associative, gives the same result as , but one way might take 10 times more “effort” (scalar multiplications) than the other. We use Dynamic Programming to find the absolute cheapest way to parenthesize the chain.
Section B: Exam-Ready Theory (The Rigor)
1. Problem Statement: Given a chain of matrices , where matrix has dimensions . The goal is to fully parenthesize the product in a way that minimizes the total number of scalar multiplications.
2. Cost Calculation: Multiplying a matrix by a matrix takes scalar multiplications.
3. Recursive Property: Let be the minimum number of scalar multiplications needed to compute the matrix .
4. Complexity:
- Time Complexity:
- Space Complexity:
Section C: Step-by-Step Worked Example (The Application)
Example: Matrix Chain Multiplication (April 2025 University Question) Chain of 4 matrices: . Dimensions .
- table initialization:
- .
- (Length 2 chains):
- .
- .
- .
- (Length 3 chains):
- .
- (Final Chain):
- .
Final Result: Minimum scalar multiplications = 640. Optimal Order: .
Section D: How to Write This in the Exam (The Strategy)
- Start With: State the Principle of Optimality and the cost formula.
- Body: Draw the triangular table. Show the split values clearly.
- Traps: Do not forget to state the final parenthesized order.
- Close With: Time complexity .
Dynamic Programming: Floyd-Warshall (All Pairs)
[SURE SHOT | ↻ REPEATED PYQ]
Section A: What is this? (The Concept)
Imagine you are designing a high-speed train network. You need a master table that shows the fastest way between every possible pair of cities simultaneously.
The Analogy: Floyd-Warshall is the “Master Schedule”. It starts with direct routes. Then it asks: “What if we allow a stopover in City 1? Can we find a shortcut?” It does this for every city until the table is perfectly optimized.
Section B: Exam-Ready Theory (The Rigor)
- Recurrence: .
- Complexity: .
- Property: Handles negative edge weights but not negative cycles.
Section C: Step-by-Step Worked Example (The Application)
Example: Floyd-Warshall Trace (University Question) Initial Matrix (4 nodes):
1 2 3 4
1 [ 0, 9, -4, α]
2 [ 6, 0, α, 2]
3 [ α, 5, 0, α]
4 [ α, α, 1, 0]
- (Via 1): .
- (Via 2): .
- Final : Shortest paths for all pairs.
Section D: How to Write This in the Exam (The Strategy)
- Start With: Write the 3-nested-loop algorithm.
- Body: Show every matrix.
- Close With: Final optimized distance matrix.
Backtracking / Branch & Bound: TSP and N-Queens
[HIGH PROB | ↻ REPEATED PYQ]
Section A: What is this? (The Concept)
The Analogy: Backtracking is like walking into a tunnel, hitting a dead end, and turning back. Branch & Bound is like sending friends with walkie-talkies. They explore level-by-level and “kill” expensive paths immediately.
Section B: Exam-Ready Theory (The Rigor)
- N-Queens: Uses Backtracking (DFS).
- TSP: Uses Branch and Bound (BFS/LC-Search) with Matrix Reduction.
Section C: Step-by-Step Worked Example (The Application)
Example 1: 4-Queens Problem
- Place Q1 at (1,1).
- Place Q2 at (2,3).
- Backtrack if no valid spot for Q3.
- Final Solution: .
Example 2: TSP Matrix Reduction Cost(Root) = sum of row mins + sum of column mins. Branch to City : Set Row and Col to .
Section D: How to Write This in the Exam (The Strategy)
- Start With: Define State Space Tree.
- Body: Draw the pruned tree for 4-Queens. Show reduction steps for TSP.
- Close With: Final tour path and min cost.