0% found this document useful (0 votes)
4 views6 pages

$RNMRU4B

DAA dasadadasda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views6 pages

$RNMRU4B

DAA dasadadasda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Name:Ayushman Tiwari

Reg. Num:231070008
Batch A
DAA Lab Experiment 4
Aim
1. To find inversion count of course choice of students.
2. To multiply two integers using brute force and
divide-and-conquer methods

Program
1. Inversion Count

Python
from collections import Counter
import pandas as pd

def count_inversions(arr):
"""Counts the number of inversions in an array."""
if len(arr) <= 1:
return arr, 0

mid = len(arr) // 2
left, left_inversions = count_inversions(arr[:mid])
right, right_inversions = count_inversions(arr[mid:])

merged, split_inversions = merge_and_count_split_inversions(left, right)


return merged, split_inversions + left_inversions + right_inversions

def merge_and_count_split_inversions(left, right):


"""Merges two arrays and counts the number of split inversions."""
result = []
i = j = split_inversions = 0
n = len(left)
while i < len(left) and j < len(right):
if left[i] <= right[j]:
result.append(left[i])
i += 1
else:
result.append(right[j])
j += 1
split_inversions += len(left) - i

result.extend(left[i:])
result.extend(right[j:])

return result, split_inversions

def inversions_course_codes(choices_students):
"""Counts the number of inversions in a list of choices, and classifies
them according to the count of inversions."""
inversions = []
for choices in choices_students:
_, t = count_inversions(choices)
inversions.append(t)
count = dict(sorted(Counter(inversions).items()))
# Creates a hashmap/dictionary with key as the inversion count and value as
the number of students that have that count.
# Sorts the dictionary based on the inversion count.
return count

df = pd.read_csv('course_choice.csv')
student_ids = df['Student'].tolist()
choices_students = df.drop(columns=['Student']).values.tolist()

for k, v in inversions_course_codes(choices_students).items():
print(f"{v:2d} students have {k:2d} inversion count.")
2. Integer Multiplication

Python
def normal_multiplication(x, y):
"""
Performs multiplication of two integers by the normal method.
"""
if type(x) == float or type(y) == float:
print("Not an integer")
return -1
sign = -1 if (x < 0) ^ (y < 0) else 1
x, y = abs(x), abs(y)

x = str(x)[::-1]
y = str(y)[::-1]
res = 0
for power1, digit1 in enumerate(x):
for power2, digit2 in enumerate(y):
res += int(digit1) * int(digit2) * 10 ** (power1 + power2)

return sign*res

def karatsuba_multiplication(x, y):


"""
Performs multiplication of two integers using the divide and conquer
karatsuba algorithm.
"""
if type(x) == float or type(y) == float:
print("Not an integer")
return -1
sign = -1 if (x < 0) ^ (y < 0) else 1
x, y = abs(x), abs(y)

if x < 10 or y < 10:


return x * y

m = min(
len(str(x)),
len(str(y))
)
m2 = m//2
high1, low1 = divmod(x, 10**m2) # Equivalent to splitting in the
middle
high2, low2 = divmod(y, 10**m2)

z0 = karatsuba_multiplication(low1, low2)
z2 = karatsuba_multiplication(high1, high2)
z1 = karatsuba_multiplication(low1 + high1 , low2 + high2) - z2 - z0
# The function outputs low1*low2 + high1*low2 + high1*high2 + high2*low1
# from which we must subtract low1low2(z0) and high1high2(z2)

return sign * (z2*(10 ** (2*m2)) + z1*(10 ** m2) + z0)

def tests():
numbers = [
(12342342352354534553346356, 30456034568347603463563565),
(53849450494776394611921152, -50633509739863107177770622),
(25069.51, 77777.321),
(0, 1155328940683083679213231),
(97672243867560276084372410, 0),
(-2551817852586546680292533, 73441952913786780372193468.454),
(-74494213327602150702262607, -68674501952269147188782896),
(222.01, 0),
(-444.05, 1000.001),
(-36363636, 1515.15)
]

for x,y in numbers:


print(f"Testcase: {x}, {y}")
print(normal_multiplication(x, y))
print(karatsuba_multiplication(x, y))

tests()
Output
1. Inversion count

2. Integer multiplication
Conclusion
1. We have found the inversion count of course choice of students
using divide and conquer method, which reduces the time
complexity to O(nlogn) from O(n^2) of the brute force method. We
classified the students according to their inversion count.
2. We have multiplied two integers using the divide and conquer
method, which reduces the time complexity to O(nlogn) from
O(n^2) of the brute force method. We reduced the number of
recursive calls to the function, which increased the efficiency. This
algorithm can be used to multiply numbers of large size efficiently.

You might also like