Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 13

DMPT – EXPERIENTIAL LEARNING

ASSIGNMENT

Name - SIDDHANT BHALLA


Prn - 19070124062
Branch – IT-T3

Q)Stable Marriage problem


N=4

def wPrefersM1OverM(prefer, w, m, m1):

for i in range(N):

if (prefer[w][i] == m1):

return True

if (prefer[w][i] == m):

return False

def stableMarriage(prefer):

wPartner = [-1 for i in range(N)]

mFree = [False for i in range(N)]

freeCount = N

while (freeCount > 0):

m=0

while (m < N):

if (mFree[m] == False):

break

m += 1

i=0
while i < N and mFree[m] == False:

w = prefer[m][i]

if (wPartner[w - N] == -1):

wPartner[w - N] = m

mFree[m] = True

freeCount -= 1

else:

m1 = wPartner[w - N]

if (wPrefersM1OverM(prefer, w, m, m1) == False):

wPartner[w - N] = m

mFree[m] = True

mFree[m1] = False

i += 1

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)
Q)Euclid s Algorithm
#include<iostream>

• #include<conio.h>

• #include<stdlib.h>

• 

• using namespace std;

• int gcd(int u, int v)

•{

• return (v != 0) ? gcd(v, u % v) : u;

•}

• 

• int main(void)

•{

• int num1, num2, result;

• cout << "Enter two numbers to find GCD using
Euclidean algorithm: ";

• cin >> num1 >> num2;

• result = gcd(num1, num2);

• if (gcd)

• cout << "\nThe GCD of " << num1 << " and " <<
num2 << " is: " << result

• << endl;

• else

• cout << "\nInvalid input!!!\n";

• return 0;

•}

Q)Piegonhole principle
#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));
}
}
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;
}

You might also like