$RNMRU4B
$RNMRU4B
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:])
result.extend(left[i:])
result.extend(right[j:])
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
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)
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)
]
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.