Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

COT5405 - Analysis of Algorithms (Fall 2021)

Final Exam
Name: Dhanush Pakanati, UFID: 28079405, dpakanati@ufl.edu

December 9, 2021

Problem 1:
Dynamic programming [35]: A thief is robbing a store that has n items. The ith item is worth Vi dollars,
weighs wi pounds and can be grabbed in ti time, where , and are integers. He wants to take as valuable
a load as possible, but he can carry at most W pounds in his knapsack, and can only spend T time
before the police arrive. Which items should he take to maximize the total value? All values involved
are integers.

1. Bellmann Equation:
(
dp[i − 1][j][k]
dp[i][j][k] = (1)
dp[i − 1][j − w[i − 1]][k − t[i − 1]] + dp[i − 1][j][k]

2. Design a correct algorithm and show it in pseudo-code.

Algorithm 1 Dynamic Programming Algorithm


Knapsack():
Initialize dp[n+1][w+1][t+1]
Initialize W, T, w[], t[]
for i in 0 to n do
for j in 0 to W do
for k in 0 to T do
if i == 0 or j ==0 or k ==0 then
dp[i][j][k] = 0
else if wt[i − 1] <=w[i] and t[i − 1] <= t[i] then
dp[i][j][k] = Max(dp[i-1][j][k],v[i-1]+dp[i-1][j-w[i-1]][k-t[i-1]])
else
dp[i][j][k] = dp[i-1][j][k]
end if
end for
end for
end for
return dp[n][W][T]

1
3. Provide proof of the algorithm’s correctness.

Claim: The dp[i][j][k] value is calculated correctly for every pair (x,y,z) that occurs
before the set (a,b,c)
Assumption: dp[i][0][0] = dp[0][j][0] = dp [0][0][k] = 0.
Proof: In the algorithm when we consider a item we only have two options. We either
choose it or we do not. Whatever is the case we always take the maximum value from
dp[i-1][w][t] and dp[i-1][j-wi][k-ti]. So, the algorithm always calculate the correct value
of dp[i][j][k].
Proof of Termination: The algorithm is looping over tha values of count(n), weight and
time. Hence, it terminates as soon as every possible index is checked.

4. Find and prove the algorithm’s running time.

The algorithm iterates in 3 for loops over the number of values, weights and times. So
the overall time complexity is in the order of n*W*T.
Time Complexity = O(nWT) where n is number of values, W is maximum weight and
T is maximum time.

2
Problem 2:
Network flow [35]: Given an undirected graph , determine the minimum number of edges that can be
disconnected to partition the graph in such a way that two given vertices are in separate partitions.
Hint: reduce the problem to max flow or one of the variants discussed in class. Make sure you provide
a complete algorithm and indicate a possibly specialized version of max flow that can be used. Indicate
the complexity of this specialized algorithm.

1. Design a correct algorithm and show it in pseudo-code.

Algorithm 2 Algorithm to find the minum number of cuts


Initialize graph G
Initialize source S and sink T
MinCut():
initialize cuts ← 0
Path = DPS(G,S,T) ▷ graph traversal to find path between source and sink
for s-t path in Path do
Let b = bottleneck(Path,f)
for each edge e(u,v) in path do
if e = (u,v) is a forward edge then
increase f(e) by b
else
decrease f(e) by b
end if
end for
if e(u,v) or e(v,u) in Path then
f = f(e)+1
end if
Update graph
end for
For each s-t path in the algorithm remove one edge.

2. Provide proof of the algorithm’s correctness.

Claim: If C is the number of minimum cuts then the algorithm terminates after finding
at most of C augmenting paths.
Proof of Termination: For every augmentation, as the capacity is 1, the flow increases
by 1. So the maximum number of iterations is equal to the maximum number of paths
found or min number of cuts needed.
Claim: For an un directed graph the minimum number of cuts needed between s and t
is equal to the maximum number of disjoint s-t paths
Proof: In this algorithm the value of maximum s-t flow is equal to the maximum number
of edge-disjoint paths. The removal a set x (subset of E) edges separate s and t then
each s-t path will have at least one edge in the set x. So the number of edge-disjoint
paths are no more than x.
If x be the set of edges that are in the path of A and B with each edge having a capacity
of 1, so the max value of x = v, where v is the value of maximum s-t flow. Hence
removing v edges from G separates and t.

3
3. Find and prove the algorithm’s running time [10].

The running time of augmented graph calculation is O(mc) and the overall time com-
plexity of the ford fulkerson is O(mn). The space complexity of the algorithm is O(m+n).
Where m and n edges and vertices.

4
Problem 3:
Complexity [30]: Problem to consider – given a set of integers , determine if there is a way to partition
the set into two parts in such a way that the sum of the values for each subset is the same. Provide a
complete proof that the problem is NP-complete. Hint: use a reduction to Subset-Sum; make sure you
have all the elements of the proof in place.

Steps to show that Set partition is NP complete:


To prove that a given problem A is NP Complete it has to satisfy the following steps:
1. There exists a NP time algorithm that solves A. A belongs to the NP Class.
2. There exists an NP complete problem that can be reduced to the given problem in poly-
nomial time
3. The solution to given problem exists if and only if the reducible problem has a solution.
Now to prove that set partition is NP:
1. We have to find x1,x2 of two sets such that all the elements of the set are included.
2. For every element x1, x2 we add that value to the result- R1 and R2
3. We verify that R1 and R2 are equal. This algorithm runs in linear time of set size.
Reduce a NP Complete problem to set partition: For this lets choose subset sum problem
1. For the subset sum problem let S be the set of numbers and t be the target sum.
2. Now our problem is to find subset I that is a part of S and has the same target sum -t.
3. If s is the sum of all the number in the given set. We choose I = S U s-2t as the input
for set partition problem.
4. WE have to prove that for any instance (a,t) belongs to set partition if and only if a
belongs to the subset sum. Here the sum of members in I is s+s-2t=2(s-t) and subset
partition divides them into set of target sum t,s-t i.e in other words dividing into two
halves t and s-t. This is the solution for our set partition problem when the target sum
is half of total sum. If the first solution to subset sum does-not exist that implies the
solution to set partition does-not exist.
5. So, there exists a partition t,s-t for a particular set of numbers - A with sum -s that can
be reduced to equal partitions for set partition problem.
6. Therefore, the subset sum problem is now reduced to set partition problem.
From this we can prove that set partition problem is also NP Complete

You might also like