# Problem Set 2

Suppose we are given a directed graph in which every edge has a distinct positive edge weight. A directed graph is acyclic if it has no directed cycle. Suppose that we want to compute the maximum-weight acyclic subgraph of (where the weight of a subgraph is the sum of its edges’ weights). Assume that is weakly connected, meaning that there is no cut with no edges crossing it in either direction.

Here is an analog of Prim’s algorithm for directed graphs. Start from an arbitrary vertex , initialize and . While , find the maximum-weight edge with one endpoint in and one endpoint in . Add this edge to , and add the appropriate endpoint to .

Here is an analog of Kruskal’s algorithm. Sort the edges from highest to lowest weight. Initialize . Scan through the edges; at each iteration, add the current edge to if and only if it does not create a directed cycle.

Which of the following is true?

- Both algorithms might fail to compute a maximum-weight acyclic subgraph.
- Only the modification of Kruskal’s algorithm always computes a maximum-weight acyclic subgraph.
- Only the modification of Prim’s algorithm always computes a maximum-weight acyclic subgraph.
- Both algorithms always compute a maximum-weight acyclic subgraph.

**ANSWER:** Consider the two-node graph (yes, two edges from to ; nothing in the question precludes that). Analog of Prim’s creates a cycle no matter which vertex it chooses first. Analog of Kruskal’s chooses and then halt. However, the maximum-weight acyclic subgraph consists of the two edges which either algorithms fails to find. Thus, option 1 is correct.

Consider a connected undirected graph with edge costs that are not necessarily distinct. Suppose we replace each edge cost with ; call this new graph . Consider running either Kruskal’s or Prim’s minimum spanning tree algorithm on , with ties between edge costs broken arbitrarily, and possibly differently, in each algorithm. Which of the following is true?

- Both algorithms compute the same maximum-cost spanning tree of .
- Both algorithms compute a maximum-cost spanning tree of , but they might compute different ones.
- Prim’s algorithm computes a maximum-cost spanning tree of but Kruskal’s algorithm might not.
- Kruskal’s algorithm computes a maximum-cost spanning tree of but Prim’s algorithm might not.

**ANSWER:** Option 2 is correct; different tie-breaking rules may yield different spanning trees.

Consider the following algorithm that attempts to compute a minimum spanning tree of a connected undirected graph with distinct edge costs. First, sort the edges in decreasing cost order (i.e., the opposite of Kruskal’s algorithm). Initialize to be all edges of . Scan through the edges (in the sorted order), and remove the current edge from if and only if it lies on a cycle of .

Which of the following statements is true?

- The output of the algorithm will never have a cycle, but it might not be connected.
- The algorithm always outputs a spanning tree, but it might not be a minimum cost spanning tree.
- The output of the algorithm will always be connected, but it might have cycles.
- The algorithm always outputs a minimum spanning tree.

**ANSWER:** During the iteration in which an edge is removed, it was on a cycle of . By the sorted ordering, it must be the maximum-cost edge of . By an exchange argument, it cannot be a member of any MST. Since every edge deleted by the algorithm belongs to no MST, and its output is a spanning tree (no cycles by construction, connected by the Lonely Cut Corollary), its output must be the (unique) MST. Therefore, option 4 is correct.

You are given a connected undirected graph with distinct edge costs, in adjacency list representation. You are also given the edges of a minimum spanning tree of . This question asks how quickly you can recompute the MST if we change the cost of a single edge. Which of the following are true? [RECALL: It is not known how to deterministically compute an MST from scratch in time, where is the number of edges of .] [Check all that apply.]

- Suppose and we increase the cost of . Then, the new MST can be recomputed in deterministic time.
- Suppose and we increase the cost of . Then, the new MST can be recomputed in deterministic time.
- Suppose and we decrease the cost of . Then, the new MST can be recomputed in deterministic time.
- Suppose and we decrease the cost of . Then, the new MST can be recomputed in deterministic time.

**ANSWER:** Option 1: Let be the two connected components of . Edge no longer belongs to the new MST if and only if it is no longer the cheapest edge crossing the cut (this can be checked in time). If is the new cheapest edge crossing , then the new MST is .

Option 2 and 3: The MST does not change, so no re-computation is needed.

Option 4: Let C be the cycle of . Edge belongs to the new MST if and only if it is no longer the most expensive edge of (this can be checked in time). If is the new most expensive edge of , then the new MST is .

Thus, all four options are correct.

Consider an alphabet with five letters, , and suppose we know the frequencies . What is the expected number of bits used by Huffman’s coding scheme to encode a 1000-letter document?

- 3450
- 2400
- 3000
- 2230

**ANSWER:** Recall from lecture video 8.3, the average encoding length is given by . A binary tree for the alphabet in question is given below.

Therefore, for a 1000-letter document, the expected number of bits is . Option 4 is correct.

Which of the following statements holds for Huffman’s coding scheme?

- If the most frequent letter has frequency less than , then all letters will be coded with more than one bit.
- A letter with frequency at least might get encoded with two or more bits.
- If a letter’s frequency is at least , then the letter will certainly be coded with only one bit.
- If the most frequent letter has frequency less than , then all letters will be coded with at least two bits.

**ANSWER:** Option 1 is incorrect; counterexample: Frequencies 0.49, 0.48, and 0.3.

Option 2 is incorrect. A letter with frequency at least is going to be the last one to be merged, because there cannot be any other letter with higher frequency.

Option 3 is incorrect; counterexample: Frequencies 0.4, 0.4, and 0.2.

Option 4 is correct. Such a letter will endure a merge in at least two iterations: the last one (which involves all letters), and at least one previous iteration. In the penultimate iteration, if the letter has not yet endured a merge, at least one of the two other remaining subtrees has cumulative frequency at least , so the letter will get merged in this iteration.

Under a Huffman encoding of symbols, how long (in terms of number of bits) can a codeword possibly be?

- \(ln \ n\)
- \(n - 1\)
- \(n\)
- \(\log_2 n\)

**ANSWER:** Consider the frequencies (powers of two). Clearly, (and ) participates in every merge, of which, there are three (in general, ). Therefore, option 2 is correct.

Option 1 is the best-case scenario.

This problem is not unique to Huffman encoding, it’s the case of an unbalanced Binary Tree devolving into a linked list.