1. Consider the problem of sorting an array A[1,...,n] of integers. We

1. Consider the problem of sorting an array A[1, . . . , n] of integers.
We presented an O(n log n)-time algorithm in class and, also, proved
a lower bound of O(n log n) for any comparison-based algorithm.
(a)Give an efficient sorting algorithm for a boolean array B[1, . . . , n].
(3P oints)
Solution:
Algorithm 1 Two Way Count Sort
1: procedure Count Sort(B[1, . . . n])
2:
counttrue ← 0, countf alse ← 0
3:
for i ← 1 to n do
4:
if B[i] = true then
5:
counttrue ← counttrue + 1
6:
else
7:
countf alse ← countf alse + 1
8:
initialize ret[counttrue + countf lase]
9:
for i ← 1 to counttrue do
10:
ret[i] ← true
11:
for i ← 1 to countf alse do
12:
ret[i + counttrue] ← f alse
13:
return ret
time complexity: O(n) space complexity: O(log n) for the counter
(b) Give an efficient sorting algorithm for an array C[1, . . . , n] whose
elements are taken from the set {1, 2, 3, 4, 5} (4P oints)
Solution:
Algorithm 2 Five Way Count Sort
1: procedure Count Sort(C[1, . . . n])
2:
initialize count[5] with all 0
3:
for i ← 1 to n do
4:
count[C[i]] ← count[C[i]] + 1
5:
initialize ret[1, . . . , n]
6:
cur ← 1
7:
for i ← 1 to 5 do
8:
for j ← 1 to count[i] do
9:
ret[cur] ← i
10:
cur ← cur + 1
11:
return ret
1
time complexity: O(n) space complexity: O(log n) for the counter
(c) Give an efficient sorting algorithm for an array E[1, . . . , n] whose
elements are distinct (E[i] 6= E[j], for every i 6= j ∈ {1, . . . , n}); and are
taken from the set {1, 2, . . . , 2n}. (10P oints)
Solution:
Algorithm 3 n Way Count Sort
1: procedure Count Sort(E[1, . . . n])
2:
initialize boolean array count[2n] with all f alse
3:
for i ← 1 to n do
4:
count[D[i]] ← true
5:
initialize ret[1, . . . , n]
6:
cur ← 1
7:
for i ← 1 to 2n do
8:
if count[i] is true then
9:
ret[cur] ← i
10:
cur ← cur + 1
11:
return ret
time complexity: O(n) space complexity: O(n)
(d)In case you designed linear-time sorting algorithms for (a-c) above,
does it mean that the lower bound for sorting of Ω(n log n) is wrong?
Explain. (3P oints)
Solution: refer to https://en.wikipedia.org/wiki/Counting_sort
In computer science, counting sort is an algorithm for sorting a collection of
objects according to keys that are small integers; that is, it is an integer sorting
algorithm. It operates by counting the number of objects that have each distinct
key value, and using arithmetic on those counts to determine the positions of
each key value in the output sequence. Its running time is linear in the number
of items and the difference between the maximum and minimum key values, so
it is only suitable for direct use in situations where the variation in keys is not
significantly greater than the number of items. However, it is often used as a
subroutine in another sorting algorithm, radix sort, that can handle larger keys
more efficiently.Because counting sort uses key values as indexes into an array,
it is not a comparison sort, and the (n log n) lower bound for comparison sorting
does not apply to it.
2.A Fibonacci number is a number that appears in the Fibonacci
Sequence 1, 1, 2, 3, 5, 8, 13, . . . . The next number is found by
2
adding the two numbers before it. Formally, F0 = 1, F1 = 1 and
Fn+1 = Fn + Fn−1 , for n ≥ 2. Define the set FIBO = {n|n is Fibonacci
number}. Give an efficient algorithm for checking if a given number
n is a Fibonacci number (i.e.,an algorithm that return 1, if n ∈ FIBO
and 0, otherwise). Analyze the correctness and the running time of
your algorithm. (15P oints)
Solution 1: refer to Yiwen Zhu’s solution
To check if a given number is a Fibonacci number, we can simply build a Fibonacci list. If we found a Fibonacci number that is equal to the given number,
we can conclude this number is a Fibonacci number. Otherwise, we continue
to grow the Fibonacci list until the number in the list is larger than our input
number n.
Algorithm 4 Judge Fibbonacci
1: procedure initialize FIBO
2:
initialize list F IBO
3:
F IBO1 ← 1
4:
F IBO2 ← 1
5:
lastindex ← 2
6: procedure is Fibbonacci(n)
7:
if F IBO doesn’t exist then
8:
initialize F IBO
9:
if F IBO has n then
10:
return true
11:
else if F IBOl astindex > n then
12:
return f alse
13:
i ← lastindex
14:
while F IBOi < n do
15:
F IBOi+1 = F IBOi + F IBOi−1
16:
i←i+1
17:
lastindex ← i
18:
return F IBOi = n
Analysis: First two numbers were initialized according to the definition of Fibonacci numbers. The loop generates a number sequence by adding previous
two numbers The time depends on the given number x. In particular, the growth
rate of the fibonacci numbers is φn , so we will calculate O(log n) fibonacci numbers before reaching or exceeding n. To be more efficient for query multiple
times. We keep the last item of the FIBO, so that if the query number n has
already in the set, or the query number is not in the set but the last item is
larger than n, we can return in constant time.
Solution 2: refer to Yiwen Zhu’s solution
3
To check whether a number is Fibonacci number is to check whether 5n2 + 4
or 5n2 − 4 is perfect square. Generally, we can use a binary search to look up
the square root of a Natrual number.(check http://www.geeksforgeeks.org/
square-root-of-an-integer/ and for practice check https://leetcode.com/
problems/sqrtx/). In particular, the best complexity is O(log n).
3. Graph Coloring. A vertex coloring is an assignment of colors
(or labels) to each vertex of a graph such that no edge connects two
identically colored vertices. More formally, a proper vertex coloring is
an assignment c : V → S such that c(v) 6= c(u), for every edge (u, v) ∈ E.
The elements of S are called colors; the vertices of one color form
a color class. If |S| = k, we say that c is a k-coloring (often we use
S = {1, . . . , k}). A graph is k-colorable if it has a proper k-coloring.
The chromatic number χ(G) is the smallest value of k such that G is
k-colorable.
(a) Let G be a graph where no node has degree larger than ∆. Prove
that χ(G) ≤ ∆ + 1. (Hint: design a ∆ + 1 coloring algorithm and prove
its correctness.) (5P oints)
Solution: refer to Kaichen Zhang’s solution and http://www.geeksforgeeks.
org/graph-coloring-set-2-greedy-algorithm/
Algorithm 5 Greedy Graph Color
1: procedure color graph(G(V, E), v, S)
2:
color V1 with S1
3:
for all v ∈ V do
4:
if no legal color then return f alse
5:
else
6:
assign the lowest legal color to v
return true
we just need to prove that if every node in n node graph G has degree no more
than d(≤ d), then the this basic algorithm will use at most d+1 colors for G.
We can prove it by induction: Induction Hypothesis: P (n) and induct on node n
Proof. Base Case:
n = 1, there is 0 edge, and degree d = 0, algorithm use 1 color, where c = 1 =
0 + 1 = d + 1, the hypothesis is true in base case.
Inductive Step:
Assume P(n) holds for induction. Let G = (V, E) be any (n + 1) nodes graph.
Let d = max degree in G. 1. Order the nodes v1 , v2 , , vn+1 2. Color first vn :
Remove vn+1 from G, to create G = (V , E) with n nodes. G has max degree
¡= d and n nodes,so P (n) says the basic algorithm uses d + 1 colors at most
for nodes v1 , v2 , . . . , vn . 3. Color vn+1 : As every node in G has at most degree
d, and thus, vn+1 has no more than d neighbors in G, Assume u1 , u2 , . . . , ud
4
are vn + 1s possible adjacent neighbors in G. As vn+1 has at most d adjacent
neighbors to other node, there exists at least 1 color in d + 1 color set c1, c2, ,
cn+1 , that is not used by any neighbor, and give vn+1 that color Thus, basic
algorithm uses no more than d + 1 colors on G P(n + 1) proved.
(b) Planar graphs are graphs that can be drawn on the plane such
that no edges cross each other. Planar graphs are known to satisfy
certain properties. For example, the number of edges in a connected
planar graph will be proportional to the number of nodes. In particular, it is known that P
in a planar graph there are at most 3n edges
(|E| < 3n) and hence
v∈V deg(v) < 6n Give an efficient 6-coloring
algorithm for planar graphs. Prove its correctness and analyze its
running time. (10P oints)
Solution: refer to Yiwen Zhu’s solution and http://www.geeksforgeeks.
org/backttracking-set-5-m-coloring-problem/
Algorithm 6 6-Color graph
1: procedure colorgraph(G(V, E))
2:
initialize heap H(d, v) with the degree as the key
3:
for all v ∈ V do
4:
add v to H
5:
initialize set D
6:
colorgraphHelper(G, H, D)
7: procedure colorgraphHelper((G(V, E), H, D)
8:
if only one vertex v in G then
9:
color v with legal color
10:
dmin , v ← getmin(H)
11:
add v to D
12:
for all (u, v) ∈ E do
13:
update degree of u with newd
14:
update H(newd, u)
15:
colorgraphHelper(G, H, D)
16:
add v back to H and update G
17:
color v with the legal color
18:
delete v from D
Proof of correctness:
Based on the average degree being ¡ 6, you can find a node with degree ≤ 5. If
you color this node last, by removing it and inductively coloring the rest of the
graph first, then when you add it back you know at least one color will be free
for it since we are allowed 6 colors, and only 5 are ruled out by its neighbors.
Give an efficient algorithm that takes as input a graph G(V, E) and
returns a proper 2-coloring assignment if G is 2-colorable; otherwise
returning the message G is not 2-colorable. Prove the correctness
5
and analyze the running time of your algorithm. (5P oints)
Complexity Analysis:
n insert operations to construct the initial priority queue (heap).
m = O(n) updates and n extract-min operations to find the sequence.
Handling the priority queue (heap) is O(nlogn) because the complexity of each
operation is O(logn).
Finding a free color is O(1) for a total of O(n) to color all the vertices.
There is no need to update the adjacency lists.
The total complexity is O(n log n).
(c) Give an efficient algorithm that takes as input a graph G(V, E) and
returns a proper 2-coloring assignment if G is 2-colorable; otherwise
returning the message G is not 2-colorable. Prove the correctness
and analyze the running time of your algorithm. (5P oints)
Algorithm 7 2-color
1: procedure color graph(G(V, E))
2:
Run BFS on the graph, label the level of each node
3:
Color even level nodes with one color and the odd level with another.
4:
if there exists (u, v) ∈ E and Cu = Cv then
5:
return f alse
6:
else
7:
done
Proof of correctness:
In a BF S there are no forward and backward edges and a cross edge connects
level γ with level γ 0 only if |γ − γ 0 | ≤ 1.
If γ = γ 0 + 1 or γ = γ 0 − 1 then the cross edge connects vertices with different
colors.
If γ = γ 0 and the two vertices is colored differently, then all the edges connect
vertices with different colors.
Time complexity:
O(n + m)-time complexity using adjacency lists.
4. Prove that in any tree, there exists a node which if removed breaks
the tree into connected components such that no connected component contains more than half the original nodes. (5 Points)
Solution: refer to Yiwen Zhu group’s answer
Algorithm:
1. Separate the graph using any node u. If the new connected components are
all with less than or equal to half of the nodes, then terminate.
2. If not, go to the component with size larger than n/2 (there can be only one,
component A). Using the node v connected with u in A as the new separation
node.
6
3. If the new connected components are all with less than or equal to half of
the nodes, then terminate. Otherwise go to step 2.
4. By moving to the node in the largest component as separation node, the
components other than A (the largest component) will possibly merge together
and add the separation node into its weight (add one more). However, even after
the merge, because component A takes more than a half weight, all the other
nodes all together will have weights less than or equal to a half. Therefore the
merged tree will not have weights larger than a half. However, by using the node
u in component A will decrease the weight of A by at least 1. By continuing
finding the node connected with the separation node in the largest connected
component, the size of the largest component will monotonically decrease while
keep all the other connected components with weights less than or equal to one.
The loop will also terminate because in the tree structure, the node in the component A connected with the current separation node will eventually reach the
leave, considering no cycles in the structure. Therefore, the solution that with
all connected components with less or equal than a half nodes will be reached.
5. For a given graph G(V, E). The distance dG (v, u) between v and
u is the length in hops of the shortest path between v and u. The
diameter DG of a graph G is the maximum distance among all pairs
(of nodes) in V , i.e., DG = max{dG (u, v)|v, uV }.
Let a, bV be two nodes such that dG (a, b) = DG . Decide whether each
of the following statements are correct and give a proof for each part.
(a) For every node rV either a or b is a leaf in a BF S tree of G rooted
at r. (4 Points)
(b) Node a is a leaf in any BF S tree of G rooted at b. (3 Points)
(c) The depth of every BF S tree of G is at least DG /2. (The depth of
a tree is the depth of its deepest leaf ) (3 Points)
Solution: refer to Yiwen Zhu group’s answer
(a) False.
figure1 gives a counter example: Root on f, a and b are neither leaves.
Figure 1: a
7
(b) Correct.
We have know dG (a, b) = DG which means dG (a, b) is the longest path of G.
Run BF S on node b in the graph, a leaf of BF S tree is the last found node.
Moreover, in BF S nodes are found in distance order. Closer to the root a node
is, sooner it can be found, and vice verse. Since we know a is the furthest node
from b, it must be discovered last. Thus it is a leaf. On the contrary, If a is not
a leaf, it must have children nodes. Then the last discovered node cannot be
a but its children. So a is not the furthest node from b, which means dG (a, b)
is not the longest path. The conclusion contradicts with its condition. Overall,
the statement holds.
(c)Correct.
Assume there exists one tree root on node r with depth less than DG /2. The
distance for any two node (a, b) will be: dG (a, b) ≤ dG (a, r) + dG (r, b) ≤
depth + depth < DG /2 + DG /2 = DG
Which means the distance between every two nodes is less than DG . Therefore
the diameter of this graph should be less than DG as well. The conclusion contradicts with its assumption. This tree does not exist.
8