Professional Documents
Culture Documents
ASSIGNMENT
else
{
int m1 = wPartner[w-N];
if (wPrefersM1OverM(prefer, w, m, m1) == false)
{
wPartner[w-N] = m;
mFree[m] = true;
mFree[m1] = false;
}
}
}
}
cout << "Woman Man" << endl;
for (int i = 0; i < N; i++)
cout << " " << i + N << "\t" << wPartner[i] << endl;
}
int main()
{
int prefer[2*N][N] = { {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);
return 0;
}
OUTPUT –
Example –
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 opposite sex who would both rather have
each other than their current partners. If there are no such people, all
the marriages are “stable”
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 opposite sex that would prefer each other over their
assigned partners.
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
Q)Euclid s Algorithm
Code –
#include <stdio.h>
int euclid(int a, int b)
{
while (a != b)
{
if (a > b)
a = a - b;
else
b = b - a;
}
return a;
}
int main()
{
int a,b;
printf("enter two numbers to find gcd");
scanf("%d%d",&a,&b);
return 0;
}
OUTPUT –
Example –
The Euclidean algorithm is a way to find the greatest common
divisor of two positive integers, a and b. Example a=210 and b=45.
Divide 210 by 45, and get the result 4 with remainder 30, so
210=4·45+30.
printf("ans = %d",a,b,piegonhole(a,b));
}
}
OUTPUT-
Example-
Imagine seven people who want to play in a tournament of teams (n =
7 items), with a limitation of only four teams (m = 4 holes) to choose
from. The pigeonhole principle tells us that they cannot all play for
different teams; there must be at least one team featuring at least two
of the seven players
[n-1]/m + 1 = [7 – 1]/4 + 1 = [6/4] + 1 = 1+1 = 2
Q)Multiplicative inverse
Code –
#include <stdio.h>
int res = (x % m + m) % m;
printf("Modular multiplicative inverse is %d", res);
}
}
*x = y1 - (b / a) * x1;
*y = x1;
return gcd;
}
int main()
{
int a, m;
printf("Enter two numbers a nd m,such that multiplicative inverse
of(a modulo m)- ");
scanf("%d%d",&a,&m);
modInverse(a, m);
return 0;
}
OUTPUT –
Example -
The Euclidean Algorithm to Find the Greatest Common Divisor Let us
begin with the two positive integers, say, 13566 and 35742. Divide
the smaller into the larger: 35742 2 13566 8610 =× + Divide the
remainder (8610) into the previous divisor (35742): 13566 1 8610
4956 =× + Continue to divide remainders into previous divisors: 8610
1 4956 3654 =× + 4956 1 3654 1302 =× + 3654 1 1302 1050 =× + 1302
1 1050 252 =× + 1050 4 252 42 =× + 252 6 42 = × The process stops
when the remainder is 0. The greatest common divisor of 13566 and
35742 is 42. gcd(13566, 35742)=42