Unit 1 of 1 · BCA Sem 4

Unit 1: Algorithm implementation and analysis

Design and Analysis of Algorithm Laboratory notes · PTU syllabus (UGCC2520)

3 min read3 topics6 exam questions
On this page
  1. Unit summary
  2. 0/1 knapsack: greedy approach
  3. 0/1 knapsack: dynamic programming
  4. Matrix chain multiplication
  5. Key terms
  6. Quick revision
  7. Important questions

Unit summary

This lab compares two strategies on the same problem — 0/1 knapsack by a greedy approach and by dynamic programming — and implements matrix chain multiplication with DP, analysing the complexity of each.

After this unit you can

  • Implement a greedy heuristic for 0/1 knapsack and see why it can fail
  • Implement the DP solution for 0/1 knapsack
  • Implement matrix chain multiplication with DP
  • Analyse and compare time complexities

PTU syllabus topics

0/1 Knapsack via Greedy Approach and via Dynamic Programming, Matrix Chain Multiplication implementation and complexity analysis

Comparison0/1 knapsack: greedy vs DP
Greedy by value/weight
Dynamic programming

Guarantee

May miss the best answer

Always optimal

Time

O(n log n)

O(nW)

Space

O(1) extra

O(nW) table

Lesson

Fast but risky for 0/1

Correct but uses more memory

1

Topic 1

0/1 knapsack: greedy approach

Sort items by profit/weight ratio and take whole items while they fit. It is fast — O(n log n) — but not always optimal for 0/1 knapsack.

Example

W = 50; items (w, p): (10, 60), (20, 100), (30, 120). Greedy by ratio takes items 1 and 2 (profit 160). The optimal answer takes items 2 and 3 (profit 220).

2

Topic 2

0/1 knapsack: dynamic programming

cint knapsack(int W, int wt[], int val[], int n) {
    int K[n + 1][W + 1];
    for (int i = 0; i <= n; i++)
        for (int w = 0; w <= W; w++) {
            if (i == 0 || w == 0) K[i][w] = 0;
            else if (wt[i - 1] <= w)
                K[i][w] = (val[i - 1] + K[i - 1][w - wt[i - 1]] > K[i - 1][w])
                          ? val[i - 1] + K[i - 1][w - wt[i - 1]] : K[i - 1][w];
            else K[i][w] = K[i - 1][w];
        }
    return K[n][W];
}

Time and space: O(nW).

3

Topic 3

Matrix chain multiplication

Find the order of multiplying matrices A₁A₂…Aₙ (dimensions p₀ × p₁, p₁ × p₂ …) that needs the fewest scalar multiplications. m[i][j] = min over k of (m[i][k] + m[k + 1][j] + pᵢ₋₁ × pₖ × pⱼ), with m[i][i] = 0.

Example

A (10 × 30), B (30 × 5), C (5 × 60): (AB)C costs 10·30·5 + 10·5·60 = 4,500; A(BC) costs 30·5·60 + 10·30·60 = 27,000. So (AB)C is better.

Complexity: O(n³) time, O(n²) space.

ComparisonGreedy vs DP on 0/1 knapsack
Greedy
Dynamic programming

Optimal?

Not guaranteed

Always

Time

O(n log n)

O(nW)

Memory

O(1) extra

O(nW) table

Key terms

Heuristic
A fast rule that usually but not always gives a good answer
DP table
Stores answers to subproblems
Matrix chain order
Parenthesisation minimising scalar multiplications
Scalar multiplications
Number of multiply operations in matrix products

Quick revision

  • Greedy fails on 0/1 knapsack; DP is optimal.
  • Knapsack DP O(nW); matrix chain DP O(n³).
  • Matrix order changes cost but not the result.

Important exam questions

Practice questions written to the PTU exam pattern for this unit's syllabus: short answers (Section A style) and long answers (Sections B and C style).

Short-answer questions

  1. Q1.Why can the greedy approach fail for 0/1 knapsack?
  2. Q2.What is the time complexity of DP knapsack?
  3. Q3.Write the recurrence for matrix chain multiplication.
  4. Q4.Why does the order of matrix multiplication matter?

Long-answer questions

  1. Q1.Implement 0/1 knapsack using greedy and DP and compare results on the same input.
  2. Q2.Implement matrix chain multiplication and find the optimal parenthesisation for four matrices.

Stuck on this unit?

Message SBS on WhatsApp for help with Design and Analysis of Algorithm Laboratory, or to ask about studying BCA at Synetic.

WhatsApp us