chapter

    Dynamic Programming, Greedy Algorithms and Optimization Notes for Gate Cs

    Dynamic Programming, Greedy Algorithms and Optimization notes for GATE CS: 39 study cards covering concepts, formulas, shortcuts and exam traps, plus solved p

    Dynamic Programming Greedy Algorithms and Optimization notes

    Chapter Roadmap: Counting and Combinatorics

    Chapter Roadmap

    Counting & Combinatorics

    School Level Mathematics · Data Science

    1
    Selection Intuition
    Understand when order does not matter.
    You are here
    2
    Core Tool: The Choose Formula
    Count unordered groups without listing them.
    3
    Exam Methods
    Apply the formula to multiple groups, categories, and conditions.
    4
    Constraints & Traps
    Handle "at least", "at most", fixed items, and overcounting.
    5
    Exam Readiness
    Final checklist to choose the correct method quickly.
    By the end: You will count unordered selections from distinct groups, apply multiplication and addition rules correctly, and solve constrained selection problems confidently.

    What is a Combination?

    Concept · Level 1

    What is a Combination?

    In counting problems, most questions ask you to either arrange objects or select objects.

    Selection
    Only who or what is in the group matters.
    Arrangement
    Both the members and their order matter.
    The Swap Test
    Ask: "If I swap two chosen items, does the outcome change?"
    No → Combination   |   Yes → Arrangement
    Example
    Selecting 3 questions out of 5 to answer on a paper: choosing questions 1, 2, 3 is the same as choosing 3, 2, 1. The set of attempted questions is unchanged. This is a combination.

    Problem Definition and Boundary

    The Unbounded Partition

    The rod cutting problem asks us to determine the maximum revenue obtainable by cutting a rod of length into integer-length pieces and selling them. We are given a price array , where is the direct market value of a piece of length . The physical reality is simple: you have a fixed total length, and you must partition it into segments whose lengths sum to .

    The boundary of this topic is strictly one-dimensional sequential partitioning. Unlike the zero-one knapsack problem where each item can be used at most once, here you can cut multiple pieces of the exact same length. This unbounded reuse of piece lengths is the defining characteristic that separates rod cutting from subset selection problems.

    Explain this more simply

    Imagine you have a 4-meter copper pipe. The scrap yard pays 1 dollar for a 1-meter piece, 5 dollars for 2 meters, 8 dollars for 3 meters, and 9 dollars for the full 4 meters. If you sell it whole, you get 9 dollars. If you cut it into two 2-meter pieces, you get 5 plus 5, which is 10 dollars. The problem is simply finding the combination of lengths that yields the highest total payout.

    Go one level deeper

    Mathematically, this is an unbounded knapsack problem where the weight of item is exactly and its value is , with a total capacity of . The optimal substructure holds because the optimal solution for a rod of length must contain within it the optimal solutions for the remaining lengths after the first cut is made. Recognizing this equivalence allows you to instantly import all unbounded knapsack optimizations.

    36 more cards in this chapter

    Try a question

    Answer it here to see how it works. Nothing is recorded until you sign in.

    Question 1
    Level 1: Warm-up

    In the rod cutting problem with rod length and price array , the correct recurrence for maximum revenue is

    Question 2
    Level 1: Warm-up

    Consider a directed acyclic graph with vertices and edge weights: (weight 5), (weight 2), (weight 1), (weight 3), (weight 4). Using the recurrence with , the minimum cost to reach vertex 4 is

    Question 3
    Level 1: Warm-up
    Consider a DP table with recurrence for , with base cases for all . Assertion: . Reason: .
    Question 4
    Level 1: Warm-up

    For , the maximum value of over all non-negative integers is

    Question 5
    Level 1: Warm-up

    Consider the rod cutting recurrence with . Given , the value of is

    Question 6
    Level 1: Warm-up

    A message consists of 20 symbols from an alphabet with frequencies A:8, B:6, C:4, D:2. Using Huffman coding, the minimum total encoded length in bits is

    Question 7
    Level 1: Warm-up

    In a directed acyclic graph with source and vertices , the edge weights are: (weight 2), (weight 1), (weight 3), (weight 4), (weight 2). Using the recurrence with , the maximum quality-score to reach is

    Question 8
    Level 1: Warm-up

    Consider the array . The distance of is defined as the minimum number of elements that must be replaced to make the array sorted in non-decreasing order. Which of the following statements are correct?

    Question 9
    Level 1: Warm-up

    Consider an alphabet with symbols A, B, C, D having frequencies 4, 2, 1, 1 respectively. Which of the following statements are correct?

    Question 10
    Level 1: Warm-up

    Consider a DP table with recurrence for , and base cases for all . Which of the following statements are correct?

    Free preview ends here

    Login to view the complete notes

    Creating an account is free. You get the rest of this chapter, step-by-step solutions, and a study plan built around the topics you are actually weak at.

    Explore complete test series, official past papers, and chapter-wise study tools.

    Why MastersUp

    Personalised first. High quality throughout.

    Most platforms hand everyone the same content. Here the content moves with your performance, topic by topic.

    Built around you, not around a syllabus PDF

    Every answer you give moves your topic-level intelligence rate. The next question, the next revision card and tomorrow's plan all change with it.

    Revision that hits your weak spots

    We only revise topics you have actually attempted and are still below the safe bar on — never the same chapter on repeat.

    Questions calibrated to the real exam

    Each question carries a measured toughness. You are served a rung above your current level, so practice keeps stretching you.

    Notes written for recall, not for volume

    Full lesson cards for first study, curated short-note cards for the last mile — with derivations, traps and exam patterns marked.

    One place for everything

    Notes, chapter practice, previous-year questions, test series and full-length papers — all feeding one picture of your preparation.

    Honest progress

    No vanity streaks. Progress here means chapters mastered and accuracy that held up on harder questions.

    Unlock the whole course

    Full notes and short notes, the complete question bank with worked solutions, mock tests, full-length papers, and an adaptive plan that rebuilds itself as you improve.

    Dynamic Programming, Greedy Algorithms and Optimization Notes for GATE CS

    Dynamic Programming, Greedy Algorithms and Optimization notes for GATE CS: 39 study cards covering concepts, formulas, shortcuts and exam traps, plus solved practice questions.

    Chapter Roadmap: Counting and Combinatorics

    Chapter Roadmap

    Counting & Combinatorics

    School Level Mathematics · Data Science

    1
    Selection Intuition
    Understand when order does not matter.
    You are here
    2
    Core Tool: The Choose Formula
    Count unordered groups without listing them.
    3
    Exam Methods
    Apply the formula to multiple groups, categories, and conditions.
    4
    Constraints & Traps
    Handle "at least", "at most", fixed items, and overcounting.
    5
    Exam Readiness
    Final checklist to choose the correct method quickly.
    By the end: You will count unordered selections from distinct groups, apply multiplication and addition rules correctly, and solve constrained selection problems confidently.

    What is a Combination?

    Concept · Level 1

    What is a Combination?

    In counting problems, most questions ask you to either arrange objects or select objects.

    Selection
    Only who or what is in the group matters.
    Arrangement
    Both the members and their order matter.
    The Swap Test
    Ask: "If I swap two chosen items, does the outcome change?"
    No → Combination   |   Yes → Arrangement
    Example
    Selecting 3 questions out of 5 to answer on a paper: choosing questions 1, 2, 3 is the same as choosing 3, 2, 1. The set of attempted questions is unchanged. This is a combination.

    Problem Definition and Boundary

    The Unbounded Partition

    The rod cutting problem asks us to determine the maximum revenue obtainable by cutting a rod of length into integer-length pieces and selling them. We are given a price array , where is the direct market value of a piece of length . The physical reality is simple: you have a fixed total length, and you must partition it into segments whose lengths sum to .

    The boundary of this topic is strictly one-dimensional sequential partitioning. Unlike the zero-one knapsack problem where each item can be used at most once, here you can cut multiple pieces of the exact same length. This unbounded reuse of piece lengths is the defining characteristic that separates rod cutting from subset selection problems.

    Explain this more simply

    Imagine you have a 4-meter copper pipe. The scrap yard pays 1 dollar for a 1-meter piece, 5 dollars for 2 meters, 8 dollars for 3 meters, and 9 dollars for the full 4 meters. If you sell it whole, you get 9 dollars. If you cut it into two 2-meter pieces, you get 5 plus 5, which is 10 dollars. The problem is simply finding the combination of lengths that yields the highest total payout.

    Go one level deeper

    Mathematically, this is an unbounded knapsack problem where the weight of item is exactly and its value is , with a total capacity of . The optimal substructure holds because the optimal solution for a rod of length must contain within it the optimal solutions for the remaining lengths after the first cut is made. Recognizing this equivalence allows you to instantly import all unbounded knapsack optimizations.

    Optimal Substructure and Recurrence

    Concrete Instance

    Consider a physical rod of length 3 on a table. You can saw it into pieces. The mathematical recurrence simply adds up the money for each physical action and picks the largest sum.

    1
    2
    2
    1
    3 (Uncut)

    General Recurrence

    If the rod has length and the first piece has length , we earn the price of plus the best revenue for the remaining length . We maximize this over all valid first cuts.


    Base case:

    Notice that when , the term becomes , which simplifies to . This elegantly includes the option of making zero cuts.

    Explain this more simply

    Think of a physical rod of length 3 on a table. You can saw off a 1-meter chunk and leave 2 meters. You can saw off a 2-meter chunk and leave 1 meter. You can saw off the entire 3 meters and leave nothing. The mathematical recurrence simply adds up the money for each of these three physical actions and picks the largest sum.

    Go one level deeper

    The recurrence implicitly handles the uncut rod because the loop for includes . A common implementation error is to loop only up to , which forces at least one physical cut and destroys the optimal solution if the uncut rod is the most valuable option. The mathematical formulation naturally prevents this trap as long as the upper bound of the maximization is inclusive.

    Dynamic Programming, Greedy Algorithms and Optimization: Solved Questions with Step-by-Step Explanations (10 Problems)

    Question 1 · Algorithms MCQ

    In the rod cutting problem with rod length and price array , the correct recurrence for maximum revenue is

    1. A.

    2. B.

    3. C.

    4. D.

    Correct Answer:

    B

    Step-by-Step Solution

    Key idea: The recurrence must include the option of not cutting the rod at all (selling it whole), which corresponds to .

    Step 1: For a rod of length , we try every possible first cut of length where .

    Step 2: If we cut a piece of length , we earn (price of piece ) plus (optimal revenue for remaining length ).

    Step 3: When , the remaining length is , and . So the term becomes , which represents selling the rod uncut.

    Step 4: The recurrence is:

    with base case .

    Answer: B

    Question 2 · Algorithms MCQ

    Consider a directed acyclic graph with vertices and edge weights: (weight 5), (weight 2), (weight 1), (weight 3), (weight 4). Using the recurrence with , the minimum cost to reach vertex 4 is

    1. A.

      6

    2. B.

      8

    3. C.

      7

    4. D.

      9

    Correct Answer:

    A

    Step-by-Step Solution

    Key idea: This is a minimum cost path problem. Apply the recurrence with addition and take minimum over all incoming paths.

    Step 1: Initialize , all others to .

    Step 2: Process vertex 1:

    Step 3: Process vertex 2:

    Step 4: Process vertex 3:

    Answer: 6

    Question 3 · Algorithms MCQ
    Consider a DP table with recurrence for , with base cases for all . Assertion: . Reason: .
    1. A.

      Both assertion and reason are true, and reason is the correct explanation

    2. B.

      Both assertion and reason are true, but reason is not the correct explanation

    3. C.

      Assertion is true but reason is false

    4. D.

      Both assertion and reason are false

    Correct Answer:

    A

    Step-by-Step Solution

    Key idea: Verify the assertion by computing step by step using the recurrence, then check if the reason correctly explains it.

    Step 1: Compute :

    Step 2: Compute :

    Step 3: Compute :

    Step 4: Compute :

    The assertion is TRUE.

    Step 5: Check the reason:

    The reason states , which matches our calculation exactly.

    The reason is TRUE and correctly explains the assertion.

    Answer: Both assertion and reason are true, and reason is the correct explanation

    Question 4 · Algorithms MCQ

    For , the maximum value of over all non-negative integers is

    1. A.

      924

    2. B.

      792

    3. C.

      1

    4. D.

      12

    Correct Answer:

    A

    Step-by-Step Solution

    Key idea: The binomial coefficient is maximized when is as close to as possible.

    Step 1: For , the maximum occurs at .

    Step 2: Calculate :

    Step 3: Simplify:

    Answer: 924

    Question 5 · Algorithms MCQ

    Consider the rod cutting recurrence with . Given , the value of is

    1. A.

      8

    2. B.

      7

    3. C.

      6

    4. D.

      9

    Correct Answer:

    A

    Step-by-Step Solution

    Key idea: Apply the recurrence systematically, building from up to .

    Step 1: Base case:

    Step 2: Calculate :

    Step 3: Calculate :

    Step 4: Calculate :

    Answer: 8

    Question 6 · Algorithms MCQ

    A message consists of 20 symbols from an alphabet with frequencies A:8, B:6, C:4, D:2. Using Huffman coding, the minimum total encoded length in bits is

    1. A.

      38

    2. B.

      40

    3. C.

      36

    4. D.

      42

    Correct Answer:

    A

    Step-by-Step Solution

    Key idea: Build the Huffman tree, find code lengths, then compute total encoded length as .

    Step 1: Build the tree:

    • Heap: {2, 4, 6, 8}
    • Merge 2 + 4 = 6. Heap: {6, 6, 8}
    • Merge 6 + 6 = 12. Heap: {8, 12}
    • Merge 8 + 12 = 20. Heap: {20}

    Step 2: Determine code lengths:

    • A (freq 8): depth 1
    • B (freq 6): depth 2
    • C (freq 4): depth 3
    • D (freq 2): depth 3

    Step 3: Calculate total encoded length:

    Answer: 38

    Question 7 · Algorithms MCQ

    In a directed acyclic graph with source and vertices , the edge weights are: (weight 2), (weight 1), (weight 3), (weight 4), (weight 2). Using the recurrence with , the maximum quality-score to reach is

    1. A.

      8

    2. B.

      12

    3. C.

      16

    4. D.

      24

    Correct Answer:

    B

    Step-by-Step Solution

    Key idea: Apply the DP recurrence step by step, processing vertices in topological order.

    Step 1: Initialize , all others to 0.

    Step 2: Process :

    Step 3: Process :

    Step 4: Process :

    Step 5: Process :

    Answer: 12

    Question 8 · Algorithms MSQ

    Consider the array . The distance of is defined as the minimum number of elements that must be replaced to make the array sorted in non-decreasing order. Which of the following statements are correct?

    1. A.

      The length of the longest non-decreasing subsequence of is 5

    2. B.

      The distance of is 6

    3. C.

      The length of the longest strictly increasing subsequence of is 5

    4. D.

      is a valid longest non-decreasing subsequence of

    Correct Answer:

    ["A","B","D"]

    Step-by-Step Solution

    Key idea: The distance equals minus the length of the longest non-decreasing subsequence (LNDS). This is a complement-counting pattern: instead of counting replacements directly, find what you keep.

    Step 1: Compute LNDS length using DP. Let be the LNDS length ending at index .

    • (3)
    • (1)
    • (1,4 or 3,4)
    • (1,1)
    • (1,1,5 or 1,4,5)
    • (1,1,5,9)
    • (1,1,2)
    • (1,1,2,6)
    • (1,1,2,5)
    • (1,1,2,3)
    • (1,1,2,3,5)

    Maximum value is 5, so LNDS length = 5. Option A is correct.

    Step 2: Distance = . Option B is correct.

    Step 3: For longest strictly increasing subsequence (LIS), equal elements cannot be used. The LIS is or , length 4, not 5. Option C is incorrect.

    Step 4: Verify : indices 1, 3, 6, 9, 10 (values 1, 1, 2, 3, 5). Indices are increasing, values are non-decreasing. This is a valid LNDS of length 5. Option D is correct.

    Answer: A, B, D

    Question 9 · Algorithms MSQ

    Consider an alphabet with symbols A, B, C, D having frequencies 4, 2, 1, 1 respectively. Which of the following statements are correct?

    1. A.

      The entropy is 1.75 bits/symbol

    2. B.

      The Huffman expected code length is 1.75 bits/symbol

    3. C.

      The minimum total encoded length for a message of 10 symbols is 17.5 bits

    4. D.

      The minimum total encoded length for a message of 10 symbols is 14 bits

    Correct Answer:

    ["A","B","C"]

    Step-by-Step Solution

    Key idea: For Huffman coding, the expected length satisfies . The total encoded length is where is the message size.

    Step 1: Compute probabilities. Total frequency = .

    Step 2: Compute entropy .

    • bits/symbol. Option A is correct.

    Step 3: Build Huffman tree.

    • Heap: C(1), D(1), B(2), A(4)
    • Merge C + D = CD(2). Heap: B(2), CD(2), A(4)
    • Merge B + CD = BCD(4). Heap: A(4), BCD(4)
    • Merge A + BCD = Root(8)
    • Code lengths: A = 1, B = 2, C = 3, D = 3

    Step 4: Compute expected length .

    • bits/symbol. Option B is correct.

    Step 5: Total encoded length for 10 symbols = bits. Option C is correct, Option D is incorrect.

    Answer: A, B, C

    Question 10 · Algorithms MSQ

    Consider a DP table with recurrence for , and base cases for all . Which of the following statements are correct?

    1. A.

    2. B.

    3. C.

    4. D.

      T[n][n] = inom{2n}{n}

    Correct Answer:

    ["A","B","C","D"]

    Step-by-Step Solution

    Key idea: This recurrence computes the Delannoy numbers (or central binomial coefficients in this case). We can verify by direct computation and recognize the pattern.

    Step 1: Compute .

    • . Option A is correct.

    Step 2: Compute and .

    Step 3: Compute .

    • . Option B is correct.

    Step 4: Compute , , and .

    • . First, . So .
    • . First, . So .
    • . Option C is correct.

    Step 5: Recognize the pattern. for gives .

    • ✓
    • ✓
    • ✓

    The pattern is . Option D is correct.

    Answer: A, B, C, D

    More notes in this unit