

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Solutions to problem 1 using the gale-shapley algorithm for different proposing orders between males and females, and includes an algorithm and argument for problem 2 to find two integers a, b such that a/b = v. The document also analyzes the running time of the given algorithm and compares it with a faster algorithm after sorting the array a.
Typology: Assignments
1 / 2
This page cannot be seen from the preview
Don't miss anything!


Answer: A-W, B-V, C-X, D-Y, E-Z
Answer: V-B, W-A, X-C, Y-D, Z-E
for (X in A){ loop every value in array A, store in variable X
for (Y in A){ loop every value in array A, store in variable Y
if (Y/X =V) test Y divided by X , if the result equal V
print X,Y print out X,Y , the printed results are two integers a, b
For example:
Assume array A=[1,3,7,6],v=2;
when I loop X as 3 and loop Y as 6,
the result of Y divider by X is 2(equal to v)
so, 3,6 is the integers a,b.
assume the amount of value in array A is n,running time:
inner for-loop is performed n times
outer for-loop is performed n times
Total 𝑇
2
( 1 )if sort A as first step, and use divide-and-conquer algorithm:
Step: explain:
Sort(A) sort array A, ascending from lift to right
for ( X in A){ loop the value of array A, store in X variable
for ( Y in the median of the rest array A) use divide-and-conquer algorithm to search Y
if (Y/X<v)
the rest array A = the value of the rest array A over than median
elseif (Y/X>v)
the rest array A= the value of the rest array A less than median
elseif(Y/X==v)
print out (X,Y) the printed results are two integers a, b
( 2 )Assume the amount of value in array A is n,running time:
outer for-loop is performed n times
inner for-loop is performed :
assume total looping time is 𝑘, each times the number to search is:
𝑛
2
𝑛
4
𝑛
2
𝑘
and
𝑛
2
𝑘
= 1 ,so 𝑘 = log 𝑛
Total 𝑇(𝑛) = 𝑂(𝑛 ∗ log 𝑛) = 𝑂(𝑛 log 𝑛)
( 3 )compare with the algorithm in a):
𝑂(𝑛 log 𝑛) < 𝑂(𝑛
2
So, “sort A as our first step” is a faster algorithm and can reduce running time.