Professional Documents
Culture Documents
Assuming that T (n)∈ Θ(1) for all n ≤ n0 for a suitable constant n 0, solve each of the following
recurrences to obtain an asymptotic bound on their complexity as a closed form. Make your
bounds as tight as possible.
Check regularity: a f (n /b)=6∗(n2 /16)=(3/8)n2 ≤ c f (n) for 0< c<1 if c=3 /8, therefore T (n)
is regular.
Therefore, case 2 of the Master Method applies, and T (n)∈ Θ(f ( n)lg(n))=Θ(l g 2 n).
Question 2 [25 marks]
Vertices with no incoming edges are referred to as inlets, and vertices with no outgoing
edges are referred to as outlets. The rainwater drainage system can have multiple inlets and
outlets.
Since weights in the graph represent fractions, they must be non-negative. For each vertex u
that is not an outlet we also require the fractions on its outgoing edges to sum to one.
If the rainwater drainage system is initially empty and there is a deluge of rain that causes,
for each inlet vi , x (v i )liters of water to flow into vi , we can use the drainage system graph to
calculate, for any outlet v o, the total amount of deluge water that will flow into v o.
Let G be a rainwater drainage system graph, and x be a mapping from the inlets of G to the
number of liters of water that flows into each of those inlets from the deluge.
a. [5 marks] What is a topological sort of a directed acyclic graph, and how can you
efficiently compute one?
A topological sort of a dag sorts the vertices based on the edges between them such that all
vertices with directed edges pointing to a given vertex v appear before v in the sorted list.
A topological sort can be computed by performing a Depth First Search on the dag, keeping
timestamps of when each vertex is first visited and completed, and then add the vertices to
the front of a linked list as they're finished.
b. [5 marks] Define a recurrence M (G , x , v )that describes, for any arbitrary vertex vofG , the
total number of liters of deluge water that will flow into vertex v. Be sure to define the base
cases of the recurrence as well as the more general cases.
c. [10 marks] Given any outlet v o ∈ G, write an efficient algorithm MAXIMUM-FLOW(G, x, vo)
in pseudocode, that calculates M (G , x , v o ). You may assume the existence of an efficient
algorithm that can perform a topological sort of your graph.
MAXIMUM-FLOW(G, x, v0):
L = TOPOLOGICAL-SORT(G)
M = empty map
for i = 0 to |G.V| - 1 do
v = L[i]
if v.numIncoming() == 0 then
M.put(v, x(v))
else
sum = 0
for u in’ince top. sorted, all u will have already been put in the map
sum += G.w(u,v) * M.get(u)
M.put(v, sum)
return M.get(v0)
d. [5 marks] What is the time complexity of your algorithm? State any assumptions that you
make and briefly justify your answer.
Assumptions:
● the incoming edges are stored with each vertex - i.e. v.numIncoming() is constant
time and iterating over v.incoming() is O(deg (v , incoming))
● The map used is implemented such that M.put() and M.get() are constant time (e.g.
HashMap)
MAXIMUM-FLOW then iterates over all vertices, and for each it iterates over every incoming
❑
edge. Since ∑ deg (v , incoming)=¿ E∨¿ ¿ (because every edge points to exactly one
v ∈G . V
vertex), this leads to this portion of the algorithm being O ¿.
Consider a data structure that keeps track of the inventory in a warehouse that has two
operations, BUY(s) that adds one item of stock swith price s . key to the inventory, and
SELL(m) that removes the cheapest m items of stock in the inventory and returns the cost of
purchasing those m cheapest items. The concrete implementation below uses a min-priority
queue, called inventory, that stores one element for each item of stock in the inventory. The
key associated with each element of stock, s, in the inventory is its price s . key . The min-
priority queue is implemented using a binary heap.
a. [4 marks] What is the actual cost of the BUY and SELL operations? Answer in terms of
the size, N , of the inventory, letting the actual cost of the priority-queue INSERT operation
be ⌊ lo g2 ( N +1) ⌋ +1, EXTRACT-MIN be ⌊ lo g2 N ⌋ +1, and IS-EMPTY be 1.
BUY(s) costs c BUY =⌊ lo g2 (N +1)⌋ +1 , since it is just the priority queue INSERT operation.
The number of iterations is m' , where m'=min(m , N ).Therefore, the overall cost is
c SELL =2+m ' ⋅( 4+ ⌊ lo g2 N ⌋ +1)=2+ m' ⋅( ⌊ lo g 2 N ⌋ +5)
b. [8 marks] Give the amortised cost for BUY and SELL for use in the accounting method.
Argue that the total amortised costs minus the total actual costs is never negative for any
sequence of m operations.
Each element on the priority queue is inserted once and removed once. For each element
inserted on the priority queue, at most one iteration of the SELL while loop will be performed.
Therefore, let the amortised costs be as follows:
Since the extraction of the stock item on sale is 'paid for' upfront when it is bought, the sum
of amortised costs will never be lower after any sequence of operations than the actual
costs, and therefore the amortised costs may be used.
In any sequence of operations starting from an empty priority queue, at most half of the
operations may be SELL operations (since you can't sell more than you have bought) -
proportionally half or more operations are BUY operations.
c. [8 marks] Give a potential function Φ for the warehouse data structure implementation, as
would be used in analysing complexity with the potential method, and show that the value of
the potential function after any sequence of m operations, starting from an empty inventory, is
bound below by the initial value of the potential function. Calculate the amortised cost of
operations BUY and SELL from Φ (show your working).
Since each item requires ( ⌊ lo g2 N ⌋ +5)cost to eventually be decreased let the potential
function for the structure be Φ=N ×(⌊ lo g 2 N ⌋ +5).
When the structure is empty, we have Φ 0=0 (while lo g 2 0 is undefined, it is irrelevant since
you cannot take an item off of an empty priority queue). Then, trivially, Φ i ≥ Φ0 for i>0 since
N ×( ⌊ lo g 2 N ⌋ +5)will never be non-negative for a positive value of N . The change in
potential for a BUY operation is ( ⌊ lo g2 N ⌋ +5) since it adds one item, and the change in
potential for a SELL(m) operation is −m ' ×( ⌊ lo g 2 N ⌋ +5) using the same definition of m'
from part a.
a. [15 marks] This problem can be solved by dynamic programming. Let A(i , j)be the length
of the longest subsequence of⟨ xi , x i+1 ,... , x j ⟩ that is also a palindrome. The solution we
seek is A(1 , m) . Give a recurrence defining A(i , j) for1 ≤i ≤m and 0 ≤ j ≤m .
You do NOT have to give a dynamic programming solution, just the recurrence.
Be sure to define the base cases of the recurrence as well as the more general cases.
If i > j then the sequence ⟨ xi , x i+1 ,... , x j ⟩is empty, so the length of the LPS is 0.
If i= j then there is only one character to consider which is by definition a palindrome, so the
length of the LPS is 1.
If the characters at i and j are not the same, then there can't be any longer palindromic
subsequences than existed in the two subproblems it is built from: i.e. where the sequence
was one shorter because i was greater, and where the sequence was shorter because j was
greater. Therefore in this case the length of the LPS is the maximum of the length of the LPS
for the subproblems (i+1 , j) and (i , j−1).
However, if the characters at i and j are the same, then, any palindrome that existed in the
middle of them (i.e. in the subproblem (i+1 , j−1)) is increased in length by two (both ends).
b. [10 marks] For the dynamic programming solution indicate in what order the elements of
the matrix A corresponding to the recurrence should be calculated. As part of answering this
question you could either give pseudocode indicating the evaluation order or draw a table
and indicate the dependencies of typical elements and which elements have no
dependencies.
If we consider the table A as a matrix of size m× m, where the columns represent i values
and the rows represent j values, then the diagonal of the matrix (i.e. i= j) will contain 1s,
and everything above and to the right (i.e. the top right half of the matrix) is filled with 0s (i> j
), as shown below:
1 0 0 0 0
1 0 0 0
1 0 0
1 0
For the cells in the lower-right half of the matrix, they may depend on the element above
them and the element to the right (the third recurrence case), or the element diagonally
above-right (the fourth recurrence case).
Therefore, the table should be filled in top-to-bottom, right-to-left, so that when a subproblem
is calculated, all subproblems it depends on are already calculated.
The subset-sum problem: Given a set A of n distinct positive integers and an integer C , the
❑
subset-sum decision problem is to decide if there is a subset Bof A such that ∑ b=C. This
b ∈B
problem is known to be NP-complete (assuming a standard encoding of inputs).
The multiple-partition problem: Given a set A of n distinct positive integers and m integers
C 1 , C2 , ... ,C m, the multiple-partition decision problem is to decide if A can be partitioned into
❑
m disjoint subsets B1 , B2 ,... , B mof A such that for each i in1,2 ,... , m, ∑ b=C i .
b ∈B i
The subset-sum problem is known NP-Complete, and can be reduced to the multiple-
partition problem by converting its input as follows:
● The parameter A to multiple-partition is the same as is passed to subset-sum
● The sequence C input to multiple-partition is set such that m=1, with C 1=C from
subset-sum
Then, the multiple-partition problem will determine if there is m=1disjoint subsets B1such
❑
that ∑ b=C 1, where C 1 is C from sub-set sum, which is equivalent to determining whether
b ∈B 1
❑
there is 1 disjoint subset B such that ∑ b=C - i.e. with this input, multiple-partition is
b ∈B
equivalent to subset-sum.
VERIFY-MULTIPLE-PARTITION(A, C, B)
// Check the subsets B are disjoint
for i = 1 to m do
for j = i to m do
if Bi ∩ B j ≠ ∅then
return false
// Check the subsets B partition A
D=∅
for i = 1 to m do
D = D∪ Bi
if D ≠ A then
return false
// Check that each subset B sums to its equivalent sum value in C
for i = 1 to m do
sum = 0
for b ∈ Bi do
sum += b
if sum ≠ Ci then
return false
return true
Assuming that checking for set intersection, union and equality can be performed in
polynomial time (as in all practical implementations), this algorithm verifies a solution B to
the multiple-partition problem in polynomial time, and therefore multiple-partition is in NP.
NP-Complete problems are problems such that if one NP-Complete problem could be solved
in polynomial time, all problems in NP could be solved in polynomial time and hence P=NP .
In practice, since no mathematician has thus far been able to find a polynomial time solution
to any NP-Complete problem, it is reasonable to assume that if a problem you are
attempting to solve can be shown to be NP-Complete, you are unlikely to be able to find
such a solution. Therefore, if your problem is NP-Complete it is reasonable to instead either
give up, or attempt to approximate a solution or solve a special case instead.