0% found this document useful (0 votes)
6 views6 pages

HW3

The document outlines various algorithm design problems and their solutions, including shortest path algorithms, complexity analysis, and dynamic programming techniques. It covers different cases of algorithm complexity, recurrence relations, and provides pseudocode for implementations. The overall complexity for most algorithms discussed is analyzed and presented, emphasizing the efficiency of the solutions.

Uploaded by

丁铭涛
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views6 pages

HW3

The document outlines various algorithm design problems and their solutions, including shortest path algorithms, complexity analysis, and dynamic programming techniques. It covers different cases of algorithm complexity, recurrence relations, and provides pseudocode for implementations. The overall complexity for most algorithms discussed is analyzed and presented, emphasizing the efficiency of the solutions.

Uploaded by

丁铭涛
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

1.

Create an array distance[], where distance[v] represents current shortest distance from the
start vertex to vertex v. All the elements are set to infinity at the beginning.
Initialize w+1 containers, and each corresponds to a possible distance value ranging from 0 to
𝑊 and contains the vertexes whose shortest distance to the start vertex is the value in
container.

Scan the containers from container 0 to the largest. For every vertex v in the container, find all
the neighboring vertexes. For example, for one vertex u that connects v through edge (u,v),
calculate distance=dist(v)+w(u,v), if the distance is smaller than the current dist(u), then update
the dist(u). Place vertex u into the appropriate container according to its updated distance.
Container_index = dist(u) mod (W+1).

The process terminates when all containers have been scanned and processed.

For the complexity:


Initialize an array has a complexity of O(V), Initialize w+1 containers have a complexity of O(W).
And during the iteration, all the vertexes and edges will be visited at most once, so the
complexity is O(V+E)
Therefore, the overall complexity is O(V+E+W). Because W is relatively small integer, the
complexity becomes O(V+E).

2.
a.
Case 1,
Θ(𝑛 log5 9 )= Θ(𝑛1.36)
b.
Case 3
Θ(𝑛 √2024)
c.
Θ(log 𝑛 ∗ log log 𝑛)
d.
Case 2
Θ(𝑛 2 (log 𝑛) ^2)
e.
Case 3
Θ(2𝑛 )

3.
a.
Case 1
Θ(𝑛 log2 7)= Θ(𝑛 2.807)
b.
when a is relatively small, f(n) is dominant. Therefore, it is always case 3, which is Θ(𝑛 2 ∗ log 𝑛),
7
it is always less than Θ(𝑛 2).
7
when a becomes bigger, it becomes case 2, which is Θ(𝑛 2 (log 𝑛) ^2), it is still less than Θ(𝑛2).
when a continues to grow bigger, it becomes case 1, which is Θ(𝑛 log4 𝑎 )
if ALG' is asymptotically faster than ALG, then 𝑛 log4 𝑎 must be smaller than 𝑛 2.807, so a<42.807 =
52.4.
So, the largest possible value of a is 52.4.

4.
algorithm design:
1. traverse the whole singly linked list to retrieve the length of it, n. Then go to the position n/2
to get the mid node.
2. compare the mid node value to target value. If they are equal, return the mid node; if mid
node value is bigger, then recursive search in the first half of the linked list, if target value is
bigger, then recursive search in the second half of the linked list.
3. repeat above steps until the sublist is empty.

𝑛
Its recurrence equation is T(n)=T( 2 )+n
So according to master theorem, it is Case 3, the complexity is O(n).

5.
a.
algorithm design:
1. find the midpoint of the array and split the array into two parts, the left part and the right
part.
2. recursively find the longest subarray in the left part (leftResult) and in the right part
(rightResult).
3. find the longest subarray that spans across the midpoint (midResult). Select the longest
subarray from leftResult, rightResult and midResult as the final result.

Methods to find the longest subarray that spans across the midpoint
1. Start from the midpoint and traverse left from mid to the left most point to get the leftMin
and leftMax.
2. Start from the midpoint and traverse right from mid to the right most point to get the
rightMin and rightMax.
3. Find the Arraymin=min(leftMin, rightMin), Arraymax=max(leftMax, rightMax).
4. Check if the elements between the leftMin and leftMax are all bigger than leftMin and
smaller than leftMax. If it is the case, calculate the length from min to max. If it is greater than
the current maximum length, update midResult.

b.
My algorithm finds the subarray because it recursively finds the longest subarray in both left
part and right part to make sure that no solution is left. Moreover, my algorithm also deals with
the possibility that the solution spans left part and right part. Finally, my algorithm leftResult,
rightResult, and midResult to ensure the final solution is the best overall result.

c.
Its recurrence equation is T(n)=2T(n/2)+O(n^2).
According to Master theorem, it is Case 3, so the complexity is Θ(n^2).

6.
(a).
For any weight j which is smaller or equal to W, the minimum number of packages the workers
need to load.
(b).
The recurrence relation is:
num[j]=min(nums[j], nums[j-w]+1) for any w that is smaller or equal to j
where num[j] represents the minimum number of packages the workers need to load for
weight j.
(c).
def minPackageRequire(W,weights):
initialize num[0...W], for every element in the array, set it to infinity
num[0]=0;
for j = 1 to W:
for weight in weights:
if(weight<=j) && num[j] > num[j-weight]+1:
num[j] = num[j-weight]+1;
return num[W]

(d).
base case is that num[0] is set to 0
the final answer is stored in num[W].

(e).
The runtime complexity is O(Wn). Because the initialization takes O(W), and two for iterations
take O(Wn).

7.
(a).
For any fundraising goal i, which is less or equal to n, the number of distinct combinations of
ticket sales to reach that goal.
(b).
num[n] = num[n-1]+num[n-2].
where num[n] represents the number of distinct combinations of ticket sales for fundraising
goal that is n.
(c).
def numberOfCombination(n):
initialize num[0...n], for every element in the array, set it to 0;
num[0]=1;
for i from 1 to n:
if i-1>=0:
num[i]=num[i-1]+num[i];
if i-2>=0:
num[i]=num[i-2]+num[i];
return num[n];
(d).
base case:
The number of distinct combinations for 1 dollar goal is 1.
The number of distinct combinations for 0 dollars goal is 1 (do not sell any ticket)
final answer is stores in num[n].
(e).
The runtime complexity is O(n). Because the initialization takes O(n), and for iteration takes
O(n).

8.
(a).
find the number of ways for Jack to get the position at (J0,I0)
(b).
num[J,I]=
0, if BadTile(J,I)=1;
num[j,i-1]+num[j-1,i-1]+num[j+1,i-1], if BadTile(J,I)=0 && J=2;
num[j,i-1]+ num[j-1,i-1], if BadTile(J,I)=0 && J=3;
num[j,i-1]+ num[j+1,i-1], if BadTile(J,I)=0 && J=1;
(c).
def numberOfWays(BadTile,3)
initialize nums[4][n+1] to 0
for j from 1 to 3:
if BadTile(j,1)=0:
nums[j][1] =1;
for i from 2 to n:
for j from 1 to 3:
if BadTile(j, i)==0:
if j=2:
nums[j][i]=nums[j],[i-1] + nums[j-1],[i-1] + nums[j+1],[i-1];
if j=3:
nums[j][i]=num[j],[i-1] + num[j-1],[i-1]
if j=1:
nums[j][i]= num[j],[i-1] + num[j+1],[i-1]
totalnum=0;
for j from 1 to 3:
totalnum = totalnum + nums[j][n]
return totalnum.
base case:
nums[1],[1] is 0 if BadTile(1,1) is 1; otherwise nums[1],[1] is 1
nums[2],[1] is 0 if BadTile(2,1) is 1; otherwise nums[2],[1] is 1
nums[3],[1] is 0 if BadTile(3,1) is 1; otherwise nums[3],[1] is 1
final answer:
It is the sum of all the values in the column n.

(d).
The runtime complexity is O(n).

9.
(a).
Compute the maximum amount of money I can get for a rod of length n, where n is less or
equal to N.
(b).
Price(N)=max(Pi + Price(N-i)) for 1<=i<=k
where Price(N) represents the max profit for a rod of length N can achieve.
(c).
def MaxProfit(N)
Initialize profits[0...N] to 0
for i from 1 to N:
maxProft=0;
for j from 1 to i:
if(Pj+profits[i-j]>maxProft):
maxProft= Pj+profits[i-j]
profits[i]=maxProft
return profits[N]
(d).
base case is that when rod has a length of 0, its profit is 0. profits[0]=0.
the final result is stored in profits[N].
(e)
the complexity of the solution is O(𝑁 2 )

10.
(a).
The maximum coins I can collect by bursting the balloons from i to j (exclude the balloons at
index i and index j)
(b).
maxCoins(i, j) = Max(maxCoins(i, k)+maxCoins(k, j)+nums[i]*nums[j]*nums[k]) for i<k<j
(c).
def findMaxCoin(nums, n):
extend array nums by appending 1 to its front and end.
initialize maxCoins[0...n+1][0...n+1] to 0
for length from 1 to n:
for i from 0 to n-length+1:
j = i+length+1
for k from i+1 to j-1:
maxCoins[i][j]=max(maxCoins[i][j], maxCoins[i][k]+maxCoins[k][j]+
nums[i]*nums[k]*nums[j])
return maxCoins[0][n+1]

(d).
base case is that when array nums has no element, which means no balloon can be burst, under
this circumstance, maxCoins[i][j]=0.
final result is stored in return maxCoins[0][n+1]
(e).
The complexity is O(𝑛 3)

You might also like