You are on page 1of 17

LAB ASSESSMENT-1

COURSE NAME: DESIGN AND ANALYSIS OF ALGORITHM

COURSE CODE: BCSE204P


NAME: M.V.SHIVANI
REG NO: 22BCE2200
SLOT: L19-L20
TIMING: 8.00AM-9.40PM
LAB FACULTY NAME: Dr. VETRISELVI.T

QUESTION 1:
1. Karatsuba’s multiplication Algorithm
a) Multiply two polynomials

PROBLEM ANALYSIS:
ALGORITHM AND TIME COMPLEXITY:
CODE:

#include <stdio.h>
#include <math.h>
void karatsubaPolynomials(int A[], int B[], int result[], int degree) {
if (degree <= 1) {
// Base case: Use regular multiplication
for (int i = 0; i <= degree; ++i) {
for (int j = 0; j <= degree; ++j) {
result[i + j] += A[i] * B[j];
}
}
} else {
int mid = degree / 2;
int a0[mid + 1], a1[mid + 1], b0[mid + 1], b1[mid + 1];
for (int i = 0; i <= mid; ++i) {
a0[i] = A[i];
a1[i] = A[mid + i + 1];
b0[i] = B[i];
b1[i] = B[mid + i + 1];
}
int z0[2 * MAX_DEGREE + 1] = {0}, z1[2 * MAX_DEGREE + 1] = {0}, z2[2 *
MAX_DEGREE + 1] = {0};

// Recursively compute the three multiplications


karatsubaPolynomials(a0, b0, z0, mid);
karatsubaPolynomials(a1, b1, z1, mid);

int tempA[mid + 1], tempB[mid + 1];


for (int i = 0; i <= mid; ++i) {
tempA[i] = a0[i] + a1[i];
tempB[i] = b0[i] + b1[i];
}

karatsubaPolynomials(tempA, tempB, z2, mid);


for (int i = 0; i <= 2 * mid; ++i) {
result[i] += z0[i];
result[i + mid + 1] += z2[i] - z0[i] - z1[i];
result[i + 2 * (mid + 1)] += z1[i];
}
}
}
void printPolynomial(int polynomial[], int degree) {
printf("Polynomial: ");
printf("%d x^%d ", polynomial[0], degree);
for (int i = 1; i <= degree; ++i) {
if (polynomial[i] != 0) {
if (polynomial[i] > 0) {
printf("+ ");
}
printf("%d x^%d ", polynomial[i], degree - i);
}
}
printf("\n");
}

int main() {
int A[MAX_DEGREE + 1] = {4, 3, 2, 1};
int B[MAX_DEGREE + 1] = {1, 3, 2, 5};
int result[MAX_DEGREE * 2 + 1] = {0};
printPolynomial(A, MAX_DEGREE);
printPolynomial(B, MAX_DEGREE);
karatsubaPolynomials(A, B, result, MAX_DEGREE);
printf("Result: ");
printPolynomial(result, MAX_DEGREE * 2);
return 0;
}
OUTPUT:

1. b) Multiply the given binary number (10010101*1010101)

PROBLEM ANALYSIS:
ALGORITHM AND TIME COMPLEXITY:
CODE:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>

int getlength(long num) {


int len = 0;
while (num > 0) {
num >>= 1;
len++;
}
return len;
}

void printBinary(long num) {


int numBits = getlength(num);
for (int i = numBits - 1; i >= 0; i--) {
printf("%ld", (num >> i) & 1);
}
printf("\n");
}
long karatsuba(long x, long y) {
long half, a, b, c, d, ac, bd;
if (x < 2 || y < 2) {
return x * y;
} else {
int m = fmax(getlength(x), getlength(y));
half = m / 2;
a = x >> half;
b = x & ((1 << half) - 1);
c = y >> half;
d = y & ((1 << half) - 1);
ac = karatsuba(a, c);
bd = karatsuba(b, d);
long ad_plus_bc = karatsuba(a + b, c + d) - ac - bd;
return (ac << (2 * half)) + (ad_plus_bc << half) + bd;
}
}

int main() {
long x = 0b10010101; // Binary representation of 10010101
long y = 0b1010101; // Binary representation of 1010101
long result = karatsuba(x, y);

printf("The product is: %ld\n", result);


printf("Binary representation of the product: ");
printBinary(result);

return 0;
}

OUTPUT:

2.Solve the given activity selection problem


A.No 1 2 3 4 5
Start 4 9 5 15 13
Time
Finish 5 10 8 17 14
Time
PROGRAM ANALYSIS:
ALGORITHM AND TIME COMPLEXITY:
CODE:
#include <stdio.h>

int partition(int s[], int f[], int low, int high) {

int pivot = f[high];

int i = low - 1;

for (int j = low; j < high; j++) {

if (f[j] <= pivot) {

i++;

int temp = s[i];

s[i] = s[j];

s[j] = temp;

// Swap f[i] and f[j]

temp = f[i];

f[i] = f[j];

f[j] = temp;

int temp = s[i + 1];

s[i + 1] = s[high];

s[high] = temp;

// Swap f[i+1] and f[high]

temp = f[i + 1];

f[i + 1] = f[high];

f[high] = temp;
return i + 1;

void quickSort(int s[], int f[], int low, int high) {

if (low < high) {

int pi = partition(s, f, low, high);

quickSort(s, f, low, pi - 1);

quickSort(s, f, pi + 1, high);

void printMaxActivities(int s[], int f[], int n) {

printf("Following activities are selected:\n");

printf("%d ", 1); // First activity always selected

int i = 1;

for (int j = 2; j <= n; j++) {

if (s[j] >= f[i]) {

printf("%d ", j);

i = j;

int main() {

int s[] = {4, 9, 5, 15, 13};

int f[] = {5, 10, 8, 17, 14};

int n = sizeof(s) / sizeof(s[0]);


quickSort(s, f, 1, n);

printMaxActivities(s, f, n);

return 0;

OUTPUT:

You might also like