Create an implementation for the Gale-Shapley algorithm, determine its time complexity. you will need to write up your implementation and discuss the data structures that you select and the time complexity of your algorithm. You can use pseudocode, but more concrete
The Gale-Shapley algorithm can be explained by taking the example of Stable marriage problem:
The Stable Marriage Problem states that given N men and N women, where each person has ranked all members of the opposite sex in order of preference, marry the men and women together such that there are no two people of the opposite sex who would both rather have each other than their current partners. If there are no such people, all the marriages are “stable”.
Consider the following example.
Let there be two men m1 and m2 and two women w1 and w2.
Let m1‘s list of preferences be {w1, w2}
Let m2‘s list of preferences be {w1, w2}
Let w1‘s list of preferences be {m1, m2}
Let w2‘s list of preferences be {m1, m2}
The matching { {m1, w2}, {w1, m2} } is not stable because m1 and w1 would prefer each other over their assigned partners. The matching {m1, w1} and {m2, w2} is stable because there are no two people of the opposite sex that would prefer each other over their assigned partners.
It is always possible to form stable marriages from lists of preferences.
Following is Gale–Shapley algorithm to find a stable matching:
The idea is to iterate through all free men while there is any free man available. Every free man goes to all women in his preference list according to the order. For every woman he goes to, he checks if the woman is free, if yes, they both become engaged. If the woman is not free, then the woman chooses either says no to him or dumps her current engagement according to her preference list. So an engagement done once can be broken if a woman gets a better option.
Time Complexity of Gale-Shapley Algorithm is O(n2).
Pseudo-Code:
Initialize all men and women to free
while there exist a free man m who still has a woman w to propose to
{
w = m's highest ranked such woman to whom he has not yet proposed
if w is free
(m, w) become engaged
else some pair (m', w) already exists
if w prefers m to m'
(m, w) become engaged
m' becomes free
else
(m', w) remain engaged
}
Describing data-structures of input and output:
Input is a 2D matrix of size (2*N)*N where N is number of women or men. Rows from 0 to N-1 represent preference lists of men and rows from N to 2*N – 1 represent preference lists of women. So men are numbered from 0 to N-1 and women are numbered from N to 2*N – 1. The output is list of married pairs.
Following is the implementation of the above algorithm in python. The bold text is the piece of code and the explanation is given as python comments:
# Number of Men or Women
N = 4
# This function returns true if woman 'w' prefers man 'm1' over man 'm'
def wPrefersM1OverM(prefer, w, m, m1):
#Check if w prefers m over her current engagment m1
for i in range(N):
#If m1 comes before m in lisr of w, then w prefers her current
engagement, don't do anything
if (prefer[w][i] == m1):
return True
#If m comes before m1 in w's list, then free her current
engagement and engage her with m
if(prefer[w][i] == m):
return False
#Prints stable matching for N boys and N girls.
#Boys are numbered as 0 to N-1.
#Girls are numbered as N to 2N-1.
def stableMarriage(prefer):
#Stores partner of women. This is our output array that stores
pairing information.
#The value of wPartner[i] indicates the
partner assigned to woman N+i.
#Note that the woman numbers between N and 2*N-1. The value -1
indicates that (N+i)'th woman is free
wPartner = [-1 for i in range(N)]
#An array to store availability of
men.
#If mFree[i] is false, then man 'i' is
free,otherwise engaged.
mFree = [False for i in
range(N)]
freeCount = N
#While there are free men
while(freeCount > 0):
#Pick the first
free man (we could pick any)
m = 0
while(m <
N):
if(mFree[m]
== False):
break
m
+= 1
#One by one go to all women according to m's preferences. Here m is
the picked free man
i = 0
while i < N and
mFree[m] == False:
w
= prefer[m][i]
#The
woman of preference is free, w and m become partners (Note that the
partnership maybe changed later).
#So
we can say they are engaged not married
if
(wPartner[w - N] == -1):
wPartner[w
- N] = m
mFree[m]
= True
freeCount
-= 1
else:
#If
w is not free, Find current engagement of w
m1
= wPartner[w - N]
#If
w prefers m over her current engagement m1, then break the
engagement between w and m1 and engage m with w.
if (wPrefersM1OverM(prefer, w, m, m1) == False):
wPartner[w
- N] = m
mFree[m]
= True
mFree[m1]
= False
i
+= 1
#End
of Else
#End of the for
loop that goes to all women in m's list
#End of the main while loop
#Printing the solution
print("Woman ", " Man")
for i in range(N):
print(i + N, "\t",
wPartner[i])
#Driver Code
prefer = [[7, 5, 6, 4], [5, 4, 6, 7],
[4, 5,
6, 7], [4, 5, 6, 7],
[0, 1,
2, 3], [0, 1, 2, 3],
[0, 1,
2, 3], [0, 1, 2, 3]]
stableMarriage(prefer)
OUTPUT:

Create an implementation for the Gale-Shapley algorithm, determine its time complexity. you will need to write...
Write a pseudocode algorithm to search for a value in a graph (you can assume that each node v has a value field i.e. v.value). Compute the time complexity of your algorithm
C++ ONLY a) Write a non-recursive implementation of Fibonacci function that uses stack (you can use STL stack). b) Calculate the complexity of your algorithm.
Develop the Implementation Model using a Pseudocode and make certain you: Have a minimum of five modules in your pseudocode (a deduction will occur if there are fewer than five modules). It is recommended that you look at the Essential Model for this problem to help divide the logic into modules. Write the Pseudocode using simple fields. Meaning if you have programming background "do not" use arrays or other aggregate data types or data structures.
You are working as the software developer and need to identify the best sorting algorithm. Please refer to the following URL: https://www.geeksforgeeks.org/sorting-algorithms/ You can pick any two-sorting algorithm. You need to determine which sorting algorithm is the best to sort the array of 10k elements. Use the following steps to help with your solution: Create C++ functions for any two-sorting algorithm. Write down the random array generation function to generate at least 10k elements. Pass the same array into both...
Analyze the time complexity of the following algorithm. You may assume that the floor function in line 2 takes Theta (1) time. Please show your work. Input: data: array of integers Input: n: size of data Output: median of data 1 Algorithm: MedianSelect 2 lim = [n/2] + 1 3 min = - infinity 4 for i = 1 to lim do 5 prev = min 6 min = infinity 7 for j = 1 to n do 8 if...
Implement an array-based Linked List in Java. Use double as the item. You need to create a driver includes several items and inserts them in order in a list. Identify the necessary methods in a List Linked implementation. Look at previous Data Structures (stack or queue) and be sure to include all necessary methods. DO NOT USE your language's Library List. You will receive zero points. Write a LinkedList class Write a driver (tester) call LinkListedDriver to show you have...
Subject: Algorithm
need this urgent please thank you.
4. Give pseudocode for an algorithm that will solve the following problem. Given an array A[1..n) that contains every number between 1 and n +1 in order, except that one of the numbers is missing. Find the miss sorted ing mber. Your algorithm should run in time (log n). (Hint: Modify Binary search). A pseudocode means an algorithm with if statements and loops, etc. Don't just write a paragraph. Also, if your...
Given an array of integer numbers, write a linear running time complexity program in Java to find the stability index in the given input array. For an array A consisting n integers elements, index i is a stability index in A if A[0] + A[1] + ... + A[i-1] = A[i+1] + A[i+2] + ... + A[n-1]; where 0 < i < n-1. Similarly, 0 is an stability index if (A[1] + A[2] + ... + A[n-1]) = 0 and...
Java question
Given an array of integer numbers, write a linear running time complexity program in Java to find the stability index in the given input array. For an array A consisting n integers elements, index i is a stability index in A itf ATO] + A[1] + +A[iI-1] Ali+1]+ Ali+2] +... + A[n-1]; where 0 <i< n-1 Similarly, 0 is an stability index if (A[1] A[2]A[n-1]) 0 and n-1 is an stability index if (A[0] A[1]+... A[n-21) 0 Example:...
Explain how you get the answer please. Thank
you
5. Find the time complexity, big-9 function, of the algorithm of finding the maximum element in a finite sequence. (Determine the number of comparisons.) (10 points) LGORITIMs Finding the Maximum Element in a Finite Sequence procedure maaistegers for 2 to " return maxf max is the larpest eleet
5. Find the time complexity, big-9 function, of the algorithm of finding the maximum element in a finite sequence. (Determine the number of...