Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

COPM2123

Tutorial 9: Greedy

s1 2023

Warm-up

Problem 1. Construct the Huffman tree of the following phrase: ”data structure”

Problem 2. During the lecture we saw that the Fractional Knapsack algorithm’s cor- rectness depends on which greedy choice we make. We saw that there are instances where always picking the highest benefit” or always picking the ”smallest weight” doesn’t give the optimal solution.

For each of these two strategies, give an infinite class of instances where the algorithm gives the optimal solution, thus showing that you can’t show correctness of an algorithm by using a finite number of example instances.

Problem solving

Problem 3. Given vectors a, b ∈ Rn, consider the problem of finding a permutation a\ and b\ of a and b respectively minimizing their total difference

 |a b |.

1≤i≤n

Prove or disprove the correctness (i.e., that it always returns the optimal solution) of the following algorithm that greedily tries to pair up similar coordinates.

1:  function greedy-matching-v1(a, b)

2:        A multiset of values in a

3:        B multiset of values in b

4:        a\ new empty list

5:        b\ new empty list

6:        while A is not empty do

7:               x, y a pair in (A, B) minimizing |x y|

8:              Append x to a\

9:              Append y to b\

10:              Remove x from A and y from B

11:        return (a\, b\)

Problem 4. Given vectors a, b ∈ Rn, consider the problem of finding a permutation a\ and b\ of a and b respectively that minimizes

 |a b |.

1≤i≤n

Prove or disprove the correctness (i.e., that it always returns the optimal solution) of the following algorithm that greedily tries to pair up similar coordinates.

1:  function greedy-matching-v2(a, b)

2:        A multiset of values in a

3:        B multiset of values in b

4:        anew empty list

5:        bnew empty list

6:        while A is not empty do

7:               x smallest in A

8:              y smallest in B

9:              Append x to a

10:              Append y to b

11:              Remove x from A

12:              Remove y from B

13:        return (a, b)

Problem 5. Suppose we are to construct a Huffman tree for a string over an alphabet C = c1, c2, . . . , ck with frequencies fi  = 2i . Prove that every internal node in T has an external-node child.

Problem 6.  Design a greedy algorithm for the following problem (see Figure 1): Given a set of n points {x1, ..., xn} on the real line, determine the smallest set of unit-length intervals that contains all points.

0                1                2                3                4                5

Figure 1: Covering points with unit intervals.

Problem 7.  Suppose we are to schedule print jobs on a printer. Each job j has an associated weight wj  > 0 (representing how important the job is) and a processing time tj (representing how long the job takes). A schedule σ is an ordering of the jobs that tells the printer in which order to process the jobs. Let Cj(σ) be the completion time of job j under the schedule σ .

Design a greedy algorithm that computes a schedule σ minimizing the sum of weighted completion times, that is, minimizing Cj(σ) .