Docsity

Gale-Shapley Algorithm: Stable Matching and Running Time Analysis, Assignments of Algorithms and Programming

Toggle sidebar
bg1
Problem 1
a) Compute the stable matching determined by Gale-Shapley algorithm for these
preferences when the Males propose to the Females.
Answer: A-W, B-V, C-X, D-Y, E-Z
b) Compute the stable matching determined by Gale-Shapley algorithm for these
preferences when the Females propose to the Males.
Answer: V-B, W-A, X-C, Y-D, Z-E
Problem 2
a) Describe an algorithm that solves the problem and give a brief argument as to why
your algorithm is correct.
Algorithm:
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
}
}
Argument:
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.
b) Analyse the running time of your algorithm.
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)
pf2

Partial preview of the text

Download Gale-Shapley Algorithm: Stable Matching and Running Time Analysis and more Assignments Algorithms and Programming in PDF only on Docsity!

Problem 1

a) Compute the stable matching determined by Gale-Shapley algorithm for these

preferences when the Males propose to the Females.

Answer: A-W, B-V, C-X, D-Y, E-Z

b) Compute the stable matching determined by Gale-Shapley algorithm for these

preferences when the Females propose to the Males.

Answer: V-B, W-A, X-C, Y-D, Z-E

Problem 2

a) Describe an algorithm that solves the problem and give a brief argument as to why

your algorithm is correct.

Algorithm:

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

Argument:

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.

b) Analyse the running time of your algorithm.

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

c) Suppose we sort A as our first step in the algorithm, does this allow for a faster

algorithm? If so, give this algorithm and analyse its running time. If not, explain why

๏ผˆ 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.