PDF - 2020-05-02T010524.258
PDF - 2020-05-02T010524.258
PDF - 2020-05-02T010524.258
Q1 (out of 1)
Q2 (out of 4)
Q3 (out of 6)
m
Q4 (out of 9)
er as
co
Q5 (out of 10)
eH w
o.
Q6 (out of 5)
rs e
ou urc
Total (out of 30+5)
o
aC s
vi y re
ed d
ar stu
sh is
Th
https://www.coursehero.com/file/21311105/TCSS543A-midterm1-simulation/
1
Name: _______________________________________
2. (4 points) Sort the following functions in order of increasing asymptotic complexity. You must justify
each adjacent pair in your ordered list (use the limit test). Since the list has 5 functions, this means
you need to use the limit test for justification 4 times (1 point each).
𝑓1 (𝑛) = 𝑎𝑛 , where 0 < 𝑎 < 1 is some constant.
𝑓2 (𝑛) = 𝑛𝑛 .
𝑓3 (𝑛) = 𝑛 𝑟√𝑛, where 𝑟 > 1 is some integer constant.
𝑓4 (𝑛) = 𝑛2 / ln𝑐 𝑛, where 𝑐 > 0 is some integer constant.
𝑓5 (𝑛) = 𝑛 ln𝑘 𝑛, where 𝑘 > 0 is some integer constant.
Solution: given the ordering is 𝑓1 < 𝑓5 < 𝑓3 < 𝑓4 < 𝑓2. Proof (applying L’Hôpital’s rule whenever needed
in the limit tests):
m
𝑓1 (𝑛) 𝑎𝑛
er as
lim = lim = 0 because 𝑎 < 1.
𝑛→∞ 𝑓5 (𝑛) 𝑛→∞ 𝑛 ln𝑘 𝑛
co
𝑓 (𝑛) 𝑛 ln𝑘 𝑛 ln𝑘 𝑛 𝑘 ln𝑘−1 𝑛/𝑛 𝑟𝑘 ln𝑘−1 𝑛 𝑟𝑘(𝑘−1) ln𝑘−2 𝑛/𝑛
lim 5 = lim 𝑛 𝑟 𝑛 = lim = lim = lim = lim =
eH w
1 1 1 1
𝑛→∞ 𝑓3 (𝑛) 𝑛→∞ √ 𝑛→∞ 𝑛𝑟 𝑛→∞ 1
( )𝑛𝑟
−1 𝑛→∞ 𝑛𝑟 𝑛→∞ 1
( )𝑛𝑟
−1
𝑟 𝑟
o.
𝑟 2 𝑘(𝑘−1) ln𝑘−2 𝑛 𝑟 𝑘 𝑘!
lim = ⋯ lim = 0.
𝑛→∞
rs e
1
𝑛𝑟
𝑛 𝑟√𝑛
𝑛→∞ 𝑛𝑟
1
𝑟
ou urc
𝑓3 (𝑛) √𝑛 ln𝑐 𝑛 ln𝑐 𝑛 𝑐!
lim = lim = lim = lim 1 = ⋯ = lim 1 = 0.
𝑛→∞ 𝑓4 (𝑛) 𝑛→∞ 𝑛2 / ln𝑐 𝑛 𝑛→∞ 𝑛 𝑛→∞ 𝑛1−𝑟 𝑛→∞ (1−1)𝑐 𝑛1−𝑟
𝑟
𝑓4 (𝑛) 𝑛2 / ln𝑐 𝑛 1
lim = lim = lim = 0.
𝑛→∞ 𝑓2 (𝑛) 𝑛𝑛 𝑛→∞ 𝑛𝑛−2 ln𝑐 𝑛
o
𝑛→∞
aC s
3. (6 points) Solve the following recurrences, where 𝑏 > 1 is some constant, using the Master Theorem.
vi y re
Indicate which case applies, what the value of 𝜀 is, and check the regularity condition as needed.
𝑇(𝑛) = 𝑏 2 𝑇(𝑛/𝑏) + 𝑛.
𝑇(𝑛) = 𝑏 2 𝑇(𝑛/𝑏) + 𝑛2.
ed d
𝑏
𝑇(𝑛) = 𝑇( √𝑛) + 1.
𝑏
𝑇(𝑛) = 𝑏𝑇( √𝑛) + lg 𝑛.
𝑇(𝑛) = 𝑇(𝑛 − lg 𝑏) + 𝑏.
sh is
Th
Solution:
https://www.coursehero.com/file/21311105/TCSS543A-midterm1-simulation/
2
Name: _______________________________________
Define 𝑚 ≔ 2𝑛 (i.e. 𝑛 = lg 𝑚) and 𝑆(𝑚) ≔ 𝑇(lg 𝑚). Thus 𝑆(𝑚) = 𝑇(𝑛) = 𝑇(𝑛 − lg 𝑏) + 𝑏 =
𝑇(lg 𝑚 − lg 𝑏) + 𝑏 = 𝑇(lg(𝑚/𝑏)) + 𝑏 = 𝑆(𝑚/𝑏) + 𝑏, hence 𝑆(𝑚) ∈ Θ(lg 𝑚) by the 2nd case,
i.e. 𝑇(𝑛) ∈ Θ(𝑛).
4. (9 points, 3 points each) Let 𝐴[1 … 𝑛][1 … 𝑛] be an 𝑛 × 𝑛 real matrix. Denote by 𝐴[1 … 𝑖 − 1, 𝑖 +
1 … 𝑛][1 … 𝑗 − 1, 𝑗 + 1 … 𝑛] the (𝑛 − 1) × (𝑛 − 1) submatrix of 𝐴 obtained by deleting the 𝑖-th row
and the 𝑗-th column of 𝐴. The determinant of that submatrix is called the (𝑖, 𝑗)-minor of 𝐴, denoted
𝑀𝑖𝑗 , and the value 𝐶𝑖𝑗 = (−1)𝑖+𝑗 𝑀𝑖𝑗 is called the (𝑖, 𝑗)-cofactor of 𝐴.
The Laplace expansion computes the determinant of 𝐴 as det 𝐴 = 𝑑 if 𝐴 = [𝑑], i.e. if 𝑛 = 1, and as
det 𝐴 = ∑𝑛𝑘=1 𝐴𝑖𝑘 ⋅ 𝐶𝑖𝑘 for any fixed choice of 1 ≤ 𝑖 ≤ 𝑛 if 𝑛 > 1.
a. Express the Laplace expansion in pseudo-code as a recursive algorithm to compute
det 𝐴 from a given matrix 𝐴.
m
Solution:
er as
Laplace(𝐴[1 … 𝑛][1 … 𝑛])
co
If 𝑛 = 1 then
eH w
Return 𝐴[1,1]
o.
End if
rs e 𝑠=0
ou urc
For 𝑘 = 1 to 𝑛 do
// take 𝑖 = 1 for instance
o
𝑠 = 𝑠 + 𝐴[1, 𝑘] ∗ (−1)1+𝑘 ∗ 𝑀
vi y re
End for
Return 𝑠.
End Laplace
ed d
Solution:
𝑐 if 𝑛 = 1
𝑇(𝑛) = {
𝑛𝑇(𝑛 − 1) + 𝑑𝑛 if 𝑛 > 1
sh is
where 𝑑 stands for the time needed to compute and update the summation inside the
Th
inner loop body (a larger cost might arise according to how the input to the recursive
call is prepared, but all this is actually small compared to the complexity of the recursive
calls themselves).
Here we assume 𝑓(𝑛) = 𝑑𝑛, which means constant time to execute the body of the
loop on 𝑘 (except for the recursive call), which is reasonable because the update of 𝑠 is
clearly linear, and the preparation of 𝐴[2 … 𝑛][1 … 𝑘 − 1, 𝑘 + 1 … 𝑛] can be carried out by
simply indicating which columns are omitted from 𝐴. However, the cost is never worse than
quadratic even if one uses a full copy of the elements on 𝐴[2 … 𝑛][1 … 𝑘 − 1, 𝑘 + 1 … 𝑛], and the
resulting complexity (item c below) will not be substantially affected.
https://www.coursehero.com/file/21311105/TCSS543A-midterm1-simulation/
3
Name: _______________________________________
c. State (by repeated substitution) and prove (by induction) a tight (big-Theta) asymptotic
𝑥𝑘
expression for 𝑇(𝑛). Hint: from the Taylor series 𝑒 𝑥 = ∑∞
𝑘=1 𝑘! we have the
1𝑘 1 1 1 1
approximation 𝑒 = 𝑒 1 = ∑∞
𝑘=1 𝑘! ≈ 1! + 2! + 3 ! + ⋯ + (𝑛−1)! for large 𝑛.
Solution:
By repeated substitution,
𝑇(𝑛) = 𝑛𝑇(𝑛 − 1) + 𝑑𝑛 = 𝑛((𝑛 − 1)𝑇(𝑛 − 2) + 𝑑(𝑛 − 1)) + 𝑑𝑛 =
𝑛(𝑛 − 1)𝑇(𝑛 − 2) + 𝑑𝑛(𝑛 − 1) + 𝑑𝑛 = 𝑛(𝑛 − 1)((𝑛 − 2)𝑇(𝑛 − 3) + 𝑑(𝑛 − 2)) +
𝑑𝑛(𝑛 − 1) + 𝑑𝑛 = 𝑛(𝑛 − 1)(𝑛 − 2)𝑇(𝑛 − 3) + 𝑑𝑛(𝑛 − 1)(𝑛 − 2) + 𝑑𝑛(𝑛 − 1) +
𝑑𝑛 = ⋯ = 𝑛(𝑛 − 1)(𝑛 − 2) … (𝑛 − 𝑘 + 3)(𝑛 − 𝑘 + 2)𝑇(𝑛 − 𝑘 + 1) +
𝑑𝑛(𝑛 − 1)(𝑛 − 2) … (𝑛 − 𝑘 + 3) + ⋯ + 𝑑𝑛(𝑛 − 1) + 𝑑𝑛 = ⋯ = 𝑛(𝑛 − 1)(𝑛 −
2) … (3)(2)𝑇(1) + 𝑑𝑛(𝑛 − 1)(𝑛 − 2) … (3) + ⋯ + 𝑑𝑛(𝑛 − 1) + 𝑑𝑛 = ⋯ = 𝑛! 𝑐 +
𝑑𝑛(𝑛 − 1)(𝑛 − 2) … (3)(2)/(2) + 𝑑𝑛(𝑛 − 1)(𝑛 − 2) … (3)(2)/(3)(2) + ⋯ +
𝑑𝑛(𝑛 − 1)(𝑛 − 2) … (3)(2)/(𝑛 − 1) … (3)(2) = 𝑛! 𝑐 + 𝑑𝑛!/2! + 𝑑𝑛!/3! + ⋯ +
m
er as
𝑑𝑛!/(𝑛 − 1)! = 𝑛! (𝑐 + 𝑑(1/2! + 1/3! + ⋯ + 1/(𝑛 − 1)!)) = 𝑛! (𝑐 − 𝑑 +
co
𝑑(1/1! + 1/2! + 1/3! + ⋯ + 1/(𝑛 − 1)!)) ≈ 𝑛! (𝑐 − 𝑑 + 𝑑𝑒).
eH w
Hence we guess that 𝑇(𝑛) ∈ Θ(𝑛!).
o.
rs e
Proof:
ou urc
Upper bound:
The naïve approach that 𝑇(𝑛) ≤ 𝑏 ⋅ 𝑛! will not work for 𝑛 > 1: 𝑇(𝑛) = 𝑛𝑇(𝑛 − 1) +
𝑑𝑛 ≤ 𝑛𝑏 ⋅ (𝑛 − 1)! + 𝑑(𝑛 − 1) ≤ 𝑏 ⋅ 𝑛! + 𝑑𝑛, but this fails to be ≤ 𝑏 ⋅ 𝑛!. Hence we
o
any choice of ℎ.
Inductive hypothesis: 𝑇(𝑘) ≤ 𝑏 ⋅ 𝑘! + ℎ for any 1 ≤ 𝑘 < 𝑛.
Inductive step: for 𝑛 > 1: 𝑇(𝑛) = 𝑛𝑇(𝑛 − 1) + 𝑑𝑛 ≤ 𝑛(𝑏 ⋅ (𝑛 − 1)! + ℎ) + 𝑑(𝑛 − 1) =
𝑏 ⋅ 𝑛! + ℎ𝑛 + 𝑑(𝑛 − 1) ≤ 𝑏 ⋅ 𝑛! + ℎ. The last inequality holds as long as ℎ𝑛 + 𝑑𝑛 − 𝑑 ≤
ed d
−𝑑, and the upper bound 𝑇(𝑛) ≤ 𝑐 ⋅ 𝑛! − 𝑑 holds for all cases. □
Lower bound:
sh is
If we used a pessimistic 𝑓(𝑛) = 𝑑𝑛2 cost to prepare the recursive call instead, the guess
would become 𝑇(𝑛) ≈ 𝑛! (𝑐 + 𝑑(2𝑒 − 1)) − 𝑑𝑛 instead, which is still Θ(𝑛!).
5. (10 points) A connected, undirected graph 𝐺 = (𝑉, 𝐸) is called bipartite if 𝑉 can be split into two
disjoint sets 𝑉1 and 𝑉2 such that every edge in 𝐸 connects a vertex in 𝑉1 to a vertex in 𝑉2 . Consider
the following algorithm to test if a graph is bipartite: it returns a pair (𝑉1 , 𝑉2 ) such that 𝑉1 ∪ 𝑉2 = 𝑉
and 𝑉1 ∩ 𝑉2 = ∅ if 𝐺 is bipartite, and (∅, ∅) otherwise.
https://www.coursehero.com/file/21311105/TCSS543A-midterm1-simulation/
4
Name: _______________________________________
Bipartite(𝐺)
If #𝑉 ≤ 1 then
Return (𝑉1 , ∅)
End if
Remove a vertex 𝑢 from 𝑉.
Let 𝑊 = ∅
For all edges 𝑒 ∈ 𝐸 such that 𝑒 = (𝑢, 𝑣) for some 𝑣 do
Add 𝑣 to 𝑊
Remove 𝑒 from 𝐸
End for
Let 𝐺′ = (𝑉′, 𝐸′) be the resulting graph.
(𝑉1 , 𝑉2 ) = Bipartite(𝐺′)
If 𝑊 ⊆ 𝑉1 then
Return (𝑉1 , 𝑉2 ∪ {𝑢})
m
er as
Else if 𝑊 ⊆ 𝑉2 then
Return (𝑉1 ∪ {𝑢}, 𝑉2 )
co
eH w
Else
Return (∅, ∅)
o.
End if
End Bipartite rs e
ou urc
a. (5 points) Prove that this algorithm is correct. Hint: try proving by induction instead of
comparing the algorithm’s output with an oracle’s output.
o
aC s
Base case: If the graph has only one vertex 𝑢, that vertex is assigned to 𝑉1, no vertex is assigned to
𝑉2 , and the algorithm correctly returns ({𝑢}, ∅) which means “bipartite”.
Induction hypothesis: Assume that the algorithm is correct when presented with a graph with 1 ≤
𝑘 < 𝑛 vertices.
ed d
Inductive step: Let 𝑢 be the vertex removed from 𝐺 for the recursive call. The algorithm yields the
ar stu
correct answer (𝑉1 , 𝑉2 ) when applied to 𝐺′ because the number of nodes of 𝐺′ is less than 𝑛.
If 𝐺′ is bipartite, then 𝑉1 ∪ 𝑉2 = 𝑉 and 𝑉1 ∩ 𝑉2 = ∅. In that case, if the set 𝑊 of nodes that are
adjacent to 𝑢 is entirely contained in 𝑉1, then assigning 𝑢 to 𝑉2 preserves the bipartition and the
correct solution for 𝐺 is (𝑉1 , 𝑉2 ∪ {𝑢}), and if the set 𝑊 of nodes that are adjacent to 𝑢 is entirely
sh is
contained in 𝑉2 , then assigning 𝑢 to 𝑉1 preserves the bipartition and the correct solution for 𝐺 is
(𝑉1 ∪ {𝑢}, 𝑉2 ).
Th
Now if 𝐺′ is bipartite but the set 𝑊 of nodes adjacent to 𝑢 is not entirely contained in either 𝑉1 or
𝑉2 , then 𝑢 cannot be added to either 𝑉1 or 𝑉2 without violating the bipartition conditions, and hence
𝐺 is not bipartite. Finally, if 𝐺′ is not bipartite, then there is a vertex already in 𝐺′ that is adjacent to
any two sets 𝑉1 , 𝑉2 such that 𝑉1 ∪ 𝑉2 = 𝑉 and 𝑉1 ∩ 𝑉2 = ∅. Hence, adding back 𝑢 will not make the
result 𝐺 bipartite. □
b. (5 points) Find a big-Oh bound for the running time of this algorithm as a function of the
number of vertices 𝑛 and the number of edges 𝑚.
https://www.coursehero.com/file/21311105/TCSS543A-midterm1-simulation/
5
Name: _______________________________________
6. (bonus question: 5 points) Let 𝑛 be a power of 3, i.e. 𝑛 = 3𝑘 for some 𝑘, let 𝜔 be a primitive 𝑛-th
root of unity, i.e. 𝜔𝑛 = 1 (but 𝜔𝑘 ≠ 1 for any 1 ≤ 𝑘 < 𝑛), and let 𝜁 be a primitive cubic root of
unity. Consider the following ternary version of the Cooley-Tukey algorithm for the FFT: split
polynomial
𝑎(𝑥) = 𝑎0 + 𝑎1 𝑥 + 𝑎2 𝑥 2 + ⋯ + 𝑎𝑛−1 𝑥 𝑛−1
into three parts as
𝑎(𝑥) = 𝑎[0] (𝑥 3 ) + 𝑥 ⋅ 𝑎[1] (𝑥 3 ) + 𝑥 2 ⋅ 𝑎[2] (𝑥 3 ),
m
er as
where
𝑎[0] (𝑥) = 𝑎0 + 𝑎3 𝑥 + 𝑎6 𝑥 2 + ⋯ + 𝑎𝑛−3 𝑥 𝑛−3 ,
co
eH w
𝑎[1] (𝑥) = 𝑎1 + 𝑎4 𝑥 + 𝑎7 𝑥 2 + ⋯ + 𝑎𝑛−2 𝑥 𝑛−2 ,
𝑎[2] (𝑥) = 𝑎2 + 𝑎5 𝑥 + 𝑎8 𝑥 2 + ⋯ + 𝑎𝑛−1 𝑥 𝑛−1 .
o.
Notice that 𝑎[0] (𝑥), 𝑎[1] (𝑥), 𝑎[2] (𝑥) have only 𝑛/3 coefficients each. Then compute these parts
rs e
at points (𝜔0 )3 , (𝜔1 )3 , (𝜔2 )3 , … (𝜔𝑛−1 )3 recursively, and combine the results as 𝑎(𝜔𝑘 ) =
ou urc
𝑎[0] (𝜔3 ) + 𝜔𝑘 ⋅ 𝑎[1] (𝜔3 ) + 𝜔2𝑘 ⋅ 𝑎[2] (𝜔3 ) , where the final combination of values takes linear
time. This yields the following algorithm stated in pseudo-code:
FFT3(𝑎[0 … 𝑛 − 1])
o
If 𝑛 = 1 then
aC s
Return 𝑎
vi y re
End if
𝑣 [0] =FFT3(𝑎[0] )
𝑣 [1] =FFT3(𝑎[1] )
ed d
𝑣 [2] =FFT3(𝑎[2] )
For 𝑘 = 0 to 𝑛/3 − 1 do
ar stu
End for
Return 𝑣.
Th
End FFT3
Find and prove a tight bound for the complexity of this algorithm as a function of 𝑛.
https://www.coursehero.com/file/21311105/TCSS543A-midterm1-simulation/
6