You are on page 1of 12

DMPT – EXPERIENTIAL LEARNING

ASSIGNMENT

Name - Shreyas Mishra


Prn - 19070124061
Branch/Div – IT-T3

Q)Stable Marriage problem


Code -
#include <iostream>
#include <cstring>
#include <cstdio>
#define N 4
using namespace std;

bool wPrefersM1OverM(int prefer[2*N][N], int w, int m, int m1)


{
for (int i = 0; i < N; i++)
{
if (prefer[w][i] == m1)
return true;
if (prefer[w][i] == m)
return false;
}
}

void stableMarriage(int prefer[2*N][N])


{
int wPartner[N];
bool mFree[N];
memset(wPartner, -1, sizeof(wPartner));
memset(mFree, false, sizeof(mFree));
int freeCount = N;
while (freeCount > 0)
{
int m;
for ( m = 0; m < N; m++)
if (mFree[m] == false)
break;
for (int i = 0; i < N && mFree[m] == false; i++)
{
int w = prefer[m][i];
if (wPartner[w - N] == -1)
{
wPartner[w - N] = m;
mFree[m] = true;
freeCount--;
}

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”

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 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);

printf("gcd of(%d, %d) = %d", a, b, euclid(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.

Divide 45 by 30, and get the result 1 with remainder 15, so


45=1·30+15.

Divide 30 by 15, and get the result 2 with remainder 0, so


30=2·15+0.

The greatest common divisor of 210 and 45 is 15


Q)Piegonhole principle-
Code –
#include<stdio.h>

int piegonhole(int a,int b)


{
int ans;
{
if (b < a)
ans = ((a-1)//b ) = 1;
else
ans = ((b-1)//a);
}
}
int main()
{
int a,b;
printf("enter number of piegons -");
scanf("%d",&a);
printf("Enter number of holes -");
scanf("%d",&b);

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 gcdExtended(int a, int b, int* x, int* y);

void modInverse(int a, int m)


{
int x, y;
int g = gcdExtended(a, m, &x, &y);
if (g != 1)
printf("Inverse doesn't exist");
else
{

int res = (x % m + m) % m;
printf("Modular multiplicative inverse is %d", res);
}
}

int gcdExtended(int a, int b, int* x, int* y)


{
if (a == 0)
{
*x = 0, *y = 1;
return b;
}

int x1, y1;


int gcd = gcdExtended(b % a, a, &x1, &y1);

*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

You might also like