Multi-Topic Algorithm Problems Explained: Greedy, DP and Complexity in One Worked Example
Use one four-item 0/1 knapsack instance to separate modelling, greedy output, DP optimality, proof and pseudo-polynomial complexity.
KnowledgeGate Team
Exam prep & CS education

A mixed Algorithms question may ask for one method's output, another method's optimum, a correctness judgement and a time bound from the same small input. Isolated definitions are not enough when the model, invariant and complexity measure get mixed together. One 0/1 knapsack instance yields every value, exposes ratio-greedy's failure, gives the dynamic programming optimum, and reveals the cost difference. GATE CS Exam Preparation is the broader subject route for related study.
Multi-topic algorithm problems: separate the five layers in one stem
A mixed-question label describes the question format, not the name of an algorithm. Such a question can test five distinct layers, and an answer may be correct at one layer but wrong at another.
Layer | Question to ask | Result for this instance |
|---|---|---|
Model | What choices are legal? | 0/1 choices with capacity 8 |
Objective | What must be optimised? | Maximum total value |
Paradigm | Which methods are compared? | Ratio-greedy and DP |
Correctness | Why trust or reject a method? | Counterexample versus include/exclude recurrence |
Complexity | What resource is counted? |
|
Use the same reading order: lock the qualifiers, trace the rule, test optimality, then compare costs. A faster method is useless if it solves a different problem or lacks an optimality proof.
Cross-concept modelling: lock the 0/1 knapsack instance
Let the capacity be W = 8, with four indivisible items:
Item | Weight | Value | Value/weight ratio |
|---|---|---|---|
A | 2 | 3 |
|
B | 3 | 4 |
|
C | 4 | 5 |
|
D | 5 | 8 |
|
Each item may be taken at most once. Fractions are forbidden, total weight cannot exceed 8, and the objective is maximum value, not maximum item count or filled weight. Allowing fractions would create a different optimisation problem.
The four results to calculate are the ratio-greedy output, its optimality, the DP optimum and the running-time comparison. Keep the model fixed while answering all four.
Ratio-greedy: compute 11, then disprove optimality
Sort by decreasing value/weight ratio: D(1.60), A(1.50), B(about 1.33), C(1.25). Apply the take-if-it-fits rule step by step:
Take D. Weight is 5 and value is 8.
Take A. Weight is
5 + 2 = 7and value is8 + 3 = 11.Remaining capacity is 1, so neither B nor C fits.
Greedy therefore returns (D, A) with weight 7 and value 11. The order looks plausible, but a sorted order is not a proof. Test a rival legal set: (D, B) has weight 5 + 3 = 8 and value 8 + 4 = 12. Since 12 is greater than 11, this single counterexample rejects ratio-greedy as an exact algorithm for this 0/1 instance.
It does not show that every greedy rule fails, and it does not contradict the ratio rule for fractional knapsack. An exact greedy method needs a safe-choice or exchange argument. Greedy Algorithms: Strategy and Problems develops that distinction.

Dynamic programming: compute the optimum value 12
Define DP[i][c] as the maximum value using the first i items with capacity c. The base values are DP[0][c] = 0 and DP[i][0] = 0. For an item of weight w_i and value v_i, copy the value above when w_i > c. Otherwise choose between excluding and including it:
DP[i][c] = max(DP[i-1][c], v_i + DP[i-1][c-w_i]).
Items available | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
|---|---|---|---|---|---|---|---|---|---|
No items | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
A | 0 | 0 | 3 | 3 | 3 | 3 | 3 | 3 | 3 |
A, B | 0 | 0 | 3 | 4 | 4 | 7 | 7 | 7 | 7 |
A, B, C | 0 | 0 | 3 | 4 | 5 | 7 | 8 | 9 | 9 |
A, B, C, D | 0 | 0 | 3 | 4 | 5 | 8 | 8 | 11 | 12 |
Representative cells make the recurrence concrete:
DP[2][5] = max(3, 4 + 3) = 7.DP[3][7] = max(7, 5 + 4) = 9.DP[4][8] = max(9, 8 + 4) = 12.
Backtrack from the final cell. DP[4][8] = 12 differs from DP[3][8] = 9, so select D and reduce capacity to 3. C is not selected because DP[3][3] = DP[2][3] = 4. B is selected because DP[2][3] = 4 differs from DP[1][3] = 3. The recovered set is (D, B), with weight 8 and value 12. See Dynamic Programming Explained: 0/1 Knapsack for the method in isolation.

Correctness across concepts: prove the recurrence, reject the heuristic
The DP invariant is: after row i is complete, every DP[i][c] is the best value among all subsets of the first i items with total weight at most c.
For the inductive step, every feasible subset falls into exactly one of two cases. It excludes item i, so its best possible value is DP[i-1][c]. Or it includes item i, contributing v_i and leaving capacity c-w_i for earlier items. Taking the maximum is exhaustive because these cases cover every legal 0/1 solution.
Ratio-greedy instead commits to one local order and discards alternatives. (D, B) proves that choosing A after D blocks a better completion. Value 12 is the answer for this instance; the invariant explains why the recurrence works for every valid instance. One counterexample defeats a universal greedy-optimality claim, while one successful trace could never prove it.
Complexity: compare 16 subsets, 36 item-row cells and one sort
Exhaustive search inspects up to 2^4 = 16 subsets. Ratio-greedy computes four ratios, sorts them, then scans once, so its general running time is O(n log n). The two-dimensional DP stores (n+1)(W+1) = 5 x 9 = 45 cells. Its four item rows contain 36 cells, including four capacity-zero base cells, so 32 item-row cells are non-base transition states. It uses O(nW) time and O(nW) space.
One capacity array from 0 to 8 can reproduce the final DP row in O(W) space, but capacities must be updated downward for each item. An upward scan could reuse the current item, silently changing 0/1 knapsack into an unbounded-choice computation.
O(nW) is pseudo-polynomial because it is polynomial in the numeric value of W, not necessarily in the bit length used to encode W. Time Complexity & Asymptotic Notation: Big-O explains the wider analysis vocabulary. The greedy trace is faster, but its answer is still wrong for the locked model.
How cross-concept algorithm material can be tested
Question shapes include tracing a supplied rule, choosing a recurrence, identifying the paradigm after a qualifier changes, judging optimality through a counterexample, and matching an output with its resource bound. In a matching prompt, "fractional choice" points towards ratio-greedy, while "0/1 choice with capacity W" points towards include/exclude DP.
Trap | What goes wrong | Repair |
|---|---|---|
Treating the taxonomy label as an algorithm | An invented definition replaces analysis | Describe the mixed-question shape |
Missing "at most once" | The wrong model is solved | Write qualifiers first |
Assuming ratio order proves optimality | Value 11 is accepted | Seek an exchange proof or counterexample |
Updating 1D DP upward | An item is reused | Scan capacity downward |
Calling | Capacity encoding is ignored | Call it pseudo-polynomial |
A 45-second check is practical: write the objective and qualifiers, trace the stated algorithm, try one rival feasible set, state the invariant or failure witness, then count states and transitions. Use the Algorithms practice subtopic to drill this sequence without assuming a fixed question pattern.
Multi-topic algorithm problems in the short version
Model first.
Compute the proposed rule.
Challenge optimality.
Prove the exact method.
Analyse the method that actually solves the stated model.
Here, ratio-greedy selects D+A for value 11, while DP selects D+B for value 12. Output, correctness and complexity remain three separate answers even when one stem asks for all of them. For a structured route through broader Algorithms study, GATE Guidance by Sanchit Sir is an optional next step.
Keep learning
Pipelining Cross-Concept Problems: CPI, Hazards and Cache Stalls
Build one reliable cycle ledger for a five-stage pipeline, then use it to combine load-use stalls, branch penalties and cache misses without mixing units.
Data Link Layer Flow Control: Stop-and-Wait, Sliding Windows and Worked Numericals
Build a reliable method for DLL flow-control problems by tracing one link from frame timing to windows, receiver credit, sequence-space limits and ACK policy.
Ethernet Cross-Concept MCQs: 12 Solved Data Link Layer Questions
Solve 12 Ethernet questions spanning devices, MAC structure, CSMA/CD, STP, VLANs, collision domains, switch port accounting and frame payload conversion.
Multi-Topic General Hindi for Teaching Exams: Cross-Concept Notes and Worked Set
Learn to separate meaning, word formation, usage, and comprehension when one Hindi passage supports several question types. Then practise the method on a fully solved set.





