TTIC 31010 and CMSC 37000 Algorithms
Winter 2017
Homework set 1
Due: Thu Jan 19, noon
• Please read the homework policy on the course webpage before preparing your homework.
• An extra 5/100 points will be added to your grade if the solution is completely typed.
• You can rely on statements in sub-questions (even if you cannot prove them) when solving
subsequent sub-questions.
• Please do not write code. A clear description of algorithms in plain English, or in pseudo-code,
is sufficient.
Question 1 (15 points) We are given a set J of n jobs. Every job j is specified by an integral
arrival time sj , and needs to be executed immediately upon arrival. The execution time is 1 time unit.
There are k processors available for rental. We can rent a processor for exactly t consecutive time
units at a time, and each such rental costs $1. The goal is to rent enough processors so that all jobs
can be executed (as usual, a processor can execute at most one job at a time), while minimizing the
total payment. We assume that we know the set of jobs and their arrival times in advance. Design
an efficient algorithm that computes an optimal solution or correctly reports that no feasible solution
exists, prove its correctness and analyze its running time.
Question 2 (15 points) We are given 2n equally-spaced points on a line of length 2n-1. Half the
points are black and half are white. Our goal is to connect every black point to a distinct white
point with a wire, while minimizing the total wire length. Below are several suggestions for greedy
algorithms, that may or may not solve the problem correctly. For each suggested algorithm, if it is
correct, prove its correctness. If it is incorrect, prove it by showing an input on which the algorithm
does not find an optimal solution. Each of the suggested algorithms performs n iterations. In every
iteration, it selects a single pair of points to connect and then deletes these two points from the line.
In order to define each algorithm it is now enough to specify the greedy rule for selecting the pair of
points to connect.
a. Greedy rule 1: find any pair p, p0 of points, where p is white and p0 is black, and no other point
lies between the two. Connect p to p0 and delete both points from the line.
b. Greedy rule 2: Let p be the leftmost black point and let p0 be the leftmost white point. Connect
p to p0 and delete both points from the line.
Question 3 (25 points) In this question we study graphs with a prescribed degree sequence.
Suppose we are given a list of n positive integers d1 , d2 , . . . , dn . We would like to determine whether
there exists a graph G = (V, E), whose vertex set is V = {v1 , . . . , vn }, and for each 1 ≤ i ≤ n,
the degree of vi in G is exactly di . Graph G is not allowed to contain self-loops (edges whose both
endpoints are the same vertex) or parallel edges (more than one edge connecting the same pair of
vertices).
1
a. Give an example of a sequence of four positive integers d1 , d2 , d3 , d4 , where di ≤ 3 for all i and
d1 + d2 + d3 + d4 is even, but no graph with degree sequence (d1 , d2 , d3 , d4 ) exists.
b. Assume that d1 ≥ d2 ≥ . . . ≥ dn , and there exists a graph G = (V, E) with the degree sequence
(d1 , d2 , . . . , dn ). Our goal is to prove that there exists a graph G0 with the same degree sequence,
such that the neighbors of v1 in G0 are v2 , v3 , . . . , vd1 +1 .
(i) Assume that in graph G, the set of neighbors of v1 is not {v2 , v3 , . . . , vd1 +1 }. Prove that there
are two indices 1 < i < j ≤ n, and a vertex u 6∈ {v1 , vi , vj }, such that (v1 , vi ), (u, vj ) 6∈ E,
but (v1 , vj ), (u, vi ) ∈ E (see Figure 1).
vi
vj
v1
u
Figure 1: Illustration to Question 3 b (i)
(ii) Show how to modify graph G so that its degree sequence remains the same, but v1 is
connected to vi .
(iii) Prove that there exists a graph G0 with the same degree sequence as G, such that the
neighbors of v1 in G0 are v2 , v3 , . . . , vd1 +1 .
c. Describe an algorithm, that, given a (possibly unsorted) sequence d1 , . . . , dn of positive integers
determines whether there is a graph with this degree sequence, and if so, constructs the graph.
Prove the algorithm’s correctness and analyze its running time.
Question 4 (25 points) The following statements may be correct or incorrect. For each statement,
if it is correct, prove its correctness; otherwise, provide a counterexample proving its incorrectness. In
all questions we assume that we are given an undirected connected graph G = (V, E) with positive
edge weights. No parallel edges or self-loops are allowed. Assume that all edge weights are distinct.
a. If G has more than |V | − 1 edges, then the heaviest edge cannot belong to a minimum spanning
tree (MST).
b. If e is the lightest edge in the graph, then it must be a part of every MST.
c. Given a path in G, define its length to be the total weight of all edges on the path. Let u, v be
any pair of vertices of G, and let P be the shortest path connecting u to v in G. Then some
MST contains all edges of P .
d. Kruskal’s algorithm works correctly when edge weights can be positive or negative (that is, the
algorithm still finds a spanning tree of minimum total weight).
2
Question 5 (20 points)
Suppose we have an alphabet Σ, and a frequency fc for each character c.
a. Assume that some character has frequency strictly greater than 2/5. Prove that the Huffman
code must have a codeword of length 1. (Partial credit will be given if you solve the question
assuming some character has frequency strictly greater than 1/2).
b. Assume that every character has frequency strictly less than 1/3. Prove that no codeword in
the Huffman encoding will have length 1.
Question 6 (15 points extra credit) A string w of parentheses ’(’ and ’)’ is balanced if it satisfies
one of the following conditions:
• w is an empty string.
• w = (x) for some balanced string x
• w = xy for some balanced strings x and y.
For example, the string w = ((())()())(()())() is balanced, but string w0 = ((()()) is not. Assume that
your input is an array w[1, . . . , n], where for each i, either w[i] =’(’, or w[i] =’)’.
a. Describe and analyze an algorithm to determine whether a given string of parentheses is balanced.
The running time of the algorithm should be O(n).
b. Describe and analyze an algorithm to compute the length of a longest balanced subsequence of
a given string of parentheses. Recall that a string s of length k is a subsequence of a string w,
iff there are k indices i1 < i2 < . . . < ik , such that s[ij ] = w[j] for all 1 ≤ j ≤ k. The running
time of the algorithm should be O(n).
(Only complete formal proofs will be accepted).
3
© Copyright 2026 Paperzz