Chapter 2: Greedy Algorithms

Chapter 2: Greedy Algorithms · Study Questions
Greedy approachMaking changeActivity selectionJob sequencingFractional knapsackMST and the cut propertyHuffman coding
Sercan KÜLCÜ · Giresun University · Computer Engineering

Multiple Choice · Greedy Approach

Question 1: The Greedy-Choice Property

What does it mean for a problem to have the greedy-choice property?

B: The first greedy choice never leads "down the wrong path"; this is usually proved by an exchange argument. A defines optimal substructure; for a greedy method to be correct both are needed together. D is a property of dynamic programming.

Output Question · Making Change

Question 2: Output of Making Change

The making-change method was changed to print every coin it picks. What does the program print? Is this result optimal for this coin system?

static int makeChange(int[] coins, int amount) {
  int count = 0;
  for (int c : coins) {
    while (amount >= c) {
      amount -= c;
      count++;
      System.out.print(c + " ");
    }
  }
  return count;
}
// main:
int[] coins = {50, 25, 10, 5, 1};
int count = makeChange(coins, 87);
System.out.println();
System.out.println(count);
Show answer

Output: 50 25 10 1 1 and on the next line 5.

  • 87 → 50 (37 left) → 25 (12) → 10 (2) → 5 does not fit → 1, 1 (0).
  • {50, 25, 10, 5, 1} is a canonical coin system: the greedy result is optimal for every amount (dynamic programming also finds 5).
  • Time O(k + count): k = 5 coin types, 5 coins picked.

Multiple Choice · Making Change

Question 3: A Non-Canonical Coin System

The coins are {10, 7, 1} and the amount is 15. How many coins does the greedy making-change algorithm give, and how many coins does the best solution use?

A: Greedy: 10 + 1 + 1 + 1 + 1 + 1 = 6 coins. Best: 7 + 7 + 1 = 3 coins. The first choice (10) misses the global optimum; this coin system does not satisfy the greedy-choice property. In general dynamic programming is needed: minCoins[t] = 1 + min minCoins[t − p].

Step-by-Step Trace · Activity Selection

Question 4: Tracing Activity Selection

Activities (s, f): (1,4), (3,5), (0,6), (5,7), (3,9), (5,9), (6,10), (8,11), (8,12), (2,14), (12,16); ids 1–11. What does select(e) print?

02468101214161234567891011

Already sorted by finish time. #1 [1, 4) finishes first and is selected; the dashed line is the last finish time.

02468101214161234567891011

#2 [3, 5): 3 < 4 → overlaps, skipped.

02468101214161234567891011

#3 [0, 6): 0 < 4 → overlaps, skipped.

02468101214161234567891011

#4 [5, 7): 5 ≥ 4 → selected, i = 3.

02468101214161234567891011

#5 [3, 9): 3 < 7 → overlaps, skipped.

02468101214161234567891011

#6 [5, 9): 5 < 7 → overlaps, skipped.

02468101214161234567891011

#7 [6, 10): 6 < 7 → overlaps, skipped.

02468101214161234567891011

#8 [8, 11): 8 ≥ 7 → selected, i = 7.

02468101214161234567891011

#9 [8, 12): 8 < 11 → overlaps, skipped.

02468101214161234567891011

#10 [2, 14): 2 < 11 → overlaps, skipped.

02468101214161234567891011

#11 [12, 16): 12 ≥ 11 → selected, i = 10. Result: 4 activities.

static void select(Activity[] e) {
  Arrays.sort(e, (x, y) ->
              x.finish - y.finish);
  int i = 0;              // last selected
  System.out.print(e[i].id + " ");
  for (int j = 1; j < e.length; j++) {
    if (e[j].start >= e[i].finish) {
      System.out.print(e[j].id + " ");
      i = j;
    }
  }
}

Multiple Choice · Activity Selection

Question 5: The Right Criterion

Which greedy criterion for selecting the maximum number of mutually non-overlapping activities in a single room always gives the optimal result?

D: The earliest-finishing activity leaves the most remaining time; an exchange argument shows that some optimal solution contains this choice. The earliest-starting one may pick a long activity and block many short ones; the shortest and fewest-overlaps criteria also have counterexamples.

Classic Question · Activity Selection

Question 6: The Exchange Argument

Prove with an exchange argument that the rule "pick the earliest-finishing activity" gives an optimal result in activity selection.

Show answer
  1. Let a₁ be the earliest-finishing activity and O = {b₁, b₂, …, bk} an optimal solution sorted by finish time.
  2. If b₁ = a₁ the proof is done. Otherwise form O' = O − {b₁} ∪ {a₁}.
  3. Since f(a₁) ≤ f(b₁) ≤ s(b₂), a₁ does not overlap b₂ … bk : O' is feasible and |O'| = |O| = k, so O' is also optimal.
  4. So there is an optimal solution containing a₁ (the greedy-choice property). After removing a₁ and everything that overlaps it, the remaining problem is of the same kind (optimal substructure); by induction the choice at every step is correct.

The same pattern is used in the Huffman and Kruskal proofs: modify an optimal solution so that it contains the greedy choice without making it worse.

Step-by-Step Trace · Job Sequencing

Question 7: Tracing Job Sequencing

Jobs (id, deadline, profit): (1, 2, 100), (2, 1, 19), (3, 2, 27), (4, 1, 25), (5, 3, 15). jobSequencing(jobs, 5) fills the slots how, and what does it return?

−1slot 1−1slot 2−1slot 3−1slot 4−1slot 5

Descending profit order: Job 1 (100), Job 3 (27), Job 4 (25), Job 2 (19), Job 5 (15). n = 5 slots, all empty (−1).

−1slot 1Job 1slot 2−1slot 3−1slot 4−1slot 5

Job 1 (deadline 2, profit 100): the latest feasible slot 2 is empty → placed. total = 100.

Job 3slot 1Job 1slot 2−1slot 3−1slot 4−1slot 5

Job 3 (deadline 2, profit 27): slot 2 is taken, so look one slot earlier; slot 1 is empty → placed. total = 127.

Job 3slot 1Job 1slot 2−1slot 3−1slot 4−1slot 5

Job 4 (deadline 1, profit 25): everything up to slot 1 is taken → the job is skipped.

Job 3slot 1Job 1slot 2−1slot 3−1slot 4−1slot 5

Job 2 (deadline 1, profit 19): everything up to slot 1 is taken → the job is skipped.

Job 3slot 1Job 1slot 2Job 5slot 3−1slot 4−1slot 5

Job 5 (deadline 3, profit 15): the latest feasible slot 3 is empty → placed. total = 142.

Job 3slot 1Job 1slot 2Job 5slot 3−1slot 4−1slot 5

Result: Job 3, Job 1, Job 5 are done in this order; total profit 142. Jobs 4 and 2 miss their deadlines.

static int jobSequencing(Job[] jobs, int n) {
  Arrays.sort(jobs, (a, b) -> b.profit - a.profit);
  int[] slot = new int[n];
  Arrays.fill(slot, -1); int total = 0;
  for (int i = 0; i < n; i++) {
    int j = Math.min(n, jobs[i].deadline) - 1;
    for (; j >= 0; j--) {
      if (slot[j] == -1) {
        slot[j] = jobs[i].id;
        total += jobs[i].profit;
        break;
      }
    }
  }
  return total;
}

Multiple Choice · Job Sequencing

Question 8: The Latest Free Slot

The job sequencing algorithm puts each job into the latest free slot before its deadline. Why?

B: Putting a job into an earlier slot than necessary may block the place of a later job with an earlier deadline. Choosing the latest free slot preserves flexibility. The slot search still takes O(dmax) (O(n²) in the worst case); with Union-Find it drops to nearly O(n).

Output Question · Job Sequencing

Question 9: Profit of Greedy Job Sequencing

Jobs (profit, deadline): a(90, 2), b(20, 1), c(40, 2), d(30, 1), e(10, 3). jobSequencing finds which order and total profit? What would happen if the jobs were sorted by deadline instead of profit?

Show answer

Profit order a, c, d, b, e:

  • a → j = 1; c → j = 1 taken → j = 0.
  • d and b (deadline 1): the only slot j = 0 is taken → skipped.
  • e → j = 2.

Order c, a, e, total profit 140 (optimal). With deadline order (b, d, a, c, e): b → j = 0, d is skipped, a → j = 1, c is skipped, e → j = 2: only 20 + 90 + 10 = 120. The greedy rule is profit order; the deadline is used only to choose the slot.

Output Question · Knapsack

Question 10: Fractional Knapsack

Items (profit, weight): (24, 4), (18, 6), (30, 5), (10, 5), (12, 3); capacity W = 15. What does the fractional knapsack method print?

static double fractional(int[][] n, int W) {
  // n[i] = {profit, weight}; ratio descending
  Arrays.sort(n, (a, b) -> Double.compare(
    (double) b[0] / b[1], (double) a[0] / a[1]));
  double value = 0;
  for (int[] x : n) {
    if (W == 0) break;
    int take = Math.min(x[1], W);
    value += (double) x[0] * take / x[1];
    W -= take;
  }
  return value;
}
// main: n = {{24,4},{18,6},{30,5},{10,5},{12,3}}
System.out.println(fractional(n, 15));
Show answer

Output: 75.0

item(24, 4)(30, 5)(12, 3)(18, 6)(10, 5)
ratio66432
taken4 (all)5 (all)3 (all)3/6—
profit2430129—

24 + 30 + 12 + 9 = 75. Sorting is O(n log n), the single pass O(n). With equal ratios, (24, 4) and (30, 5), the result is the same whichever is taken first.

Multiple Choice · Minimum Spanning Tree

Question 11: The False Statement About MSTs

Which of these statements about the minimum spanning tree (MST) is false?

D: If all weights are distinct, the MST is unique; Prim and Kruskal find the same tree. With equal weights they may find different trees, but the total weight is the same. A is true: every spanning tree has V − 1 edges, all sums increase by the same amount, and the order of the edges does not change. B is true: in Kruskal it is enough to reverse the sorting direction.

Multiple Choice · Minimum Spanning Tree

Question 12: The Cut Property

The nodes are split into two sets, S and V − S. According to the cut property, which statement is correct (weights are distinct)?

C: The lightest edge crossing the cut is in the MST; Borůvka (the lightest edge leaving each component), Kruskal (the lightest edge joining two components) and Prim use this property. The heaviest edge crossing the cut is in the MST if it is the only edge crossing the cut, so A is false. B is false: the lightest edge of the graph is always in the MST.

Multiple Choice · Minimum Spanning Tree

Question 13: Three MST Algorithms

Which statement is correct for Kruskal's, Prim's and Borůvka's algorithms?

B: In Borůvka each component chooses the lightest edge of its own cut and the chosen edges are added together; since the choices are independent, it suits parallel execution. A describes Kruskal and C describes Prim: Prim adds the lightest edge leaving the tree, while Kruskal merges a forest. D is false: only Kruskal sorts the edges; Prim uses a priority queue, and Borůvka scans the edges once per round.

Step-by-Step Trace · Huffman Coding

Question 14: Tracing the Huffman Tree

Frequencies a:5, b:9, c:12, d:13, e:16, f:45. huffman(ch, freq) performs which merges? What are the codes and the total number of bits?

a5b9c12d13e16f45

Each character was added to the min-heap as a leaf with its frequency: a5, b9, c12, d13, e16, f45.

c12d13(14)e16f45

poll() twice: a5 and b9 → new internal node 14 = 5 + 9, added to the queue.

(14)e16(25)f45

poll() twice: c12 and d13 → new internal node 25 = 12 + 13, added to the queue.

(25)(30)f45

poll() twice: (14) and e16 → new internal node 30 = 14 + 16, added to the queue.

f45(55)

poll() twice: (25) and (30) → new internal node 55 = 25 + 30, added to the queue.

(100)

poll() twice: f45 and (55) → new internal node 100 = 45 + 55, added to the queue.

(100)

The root (100) is returned. Left 0, right 1: f = 0, c = 100, d = 101, a = 1100, b = 1101, e = 111. Total 45·1 + 12·3 + 13·3 + 5·4 + 9·4 + 16·3 = 224 bits (300 with a fixed 3 bits).

static Node huffman(char[] ch, int[] freq) {
  PriorityQueue<Node> queue =
    new PriorityQueue<>((a, b) -> a.freq - b.freq);
  for (int k = 0; k < ch.length; k++)
    queue.add(new Node(ch[k], freq[k]));
  while (queue.size() > 1) {
    Node left = queue.poll();
    Node right = queue.poll();
    Node parent =
      new Node('\0', left.freq + right.freq);
    parent.left = left;
    parent.right = right;
    queue.add(parent);
  }
  return queue.poll();
}

Output Question · Huffman Coding

Question 15: Decoding with Huffman

In the Huffman tree the left edge is 0 and the right edge is 1: F = 000, O = 001, E = 01, T = 10, G = 110, R = 111. 0110111001000 Which text does this bit string decode to? How many bits would the same text take with a fixed 3-bit code?

0101010101E5T7F2O3G4R4
Show answer

Start at the root; when you reach a leaf, write the character and return to the root:

01 | 10 | 111 | 001 | 000 → ETROF

  • Huffman: 13 bits; fixed code: 5 × 3 = 15 bits.
  • No separator is needed: the codes are prefix-free (each character sits at a leaf), so the moment a code ends is known for certain.
  • The decoder must have the tree (or the code table); the tree is sent along with the compressed data.

Multiple Choice · Huffman Coding

Question 16: Prefix-Free Codes

Which of these code sets is prefix-free (no code is a prefix of another)?

C: {0, 10, 110, 111}: none is the beginning of another; this corresponds to the four leaves of a binary tree. In A, 0 is a prefix of 01 ("01" → "ab" or "b"?). In B, 1 is a prefix of 10; in D, 0 is a prefix of 00 and 01.

The End

Chapter 2: Greedy Algorithms: Study Questions
Lecture slides: Chapter 2: Greedy Algorithms · Lecture notes
1 / 1 Sercan KÜLCÜ. All rights reserved.