Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

CS 430 PROJECT- (V02) Introduction To Algorithms

Pushpendra Singh Chauhan

Department of Computer Science

Illinois Institute of Technology

August 10, 2021

Abstract
This is a report for the final project. It will contain the description of
the problem I am trying to solve, the algorithms I employed to solve the
problem, implementation details like program design issues and analysis of
the performance. Also in last section, I have provided link to check the
functionality of web-application.

1. Problem Statement:

 Median Finding: Given two sorted arrays of size m and n respectively,


return the median of the two sorted arrays.
 Searching: Given a positive integer (less than 10,000), convert it to a
roman numeral.

2. Proposed Solution:
 Median Finding: I have used Randomized-Select algorithm for finding
median. First we will combine both the input sorted arrays and then
pass it to the ‘randomized_select()’ function. The code for the
algorithm is as follows:
import random
def partition(A,p,r):
x = A[r-1]
i = p-1
for j in range(p,r):
if(A[j-1] <= x):
i += 1
temp = A[i-1]
A[i-1] = A[j-1]
A[j-1] = temp
temp1 = A[i-1+1]
A[i-1+1] = A[r-1]
A[r-1] = temp1
return i+1

def randomized_partition(A,p,r):
rand = random.randint(p,r)
#print(rand)
temp1 = A[r-1]
A[r-1] = A[rand-1]
A[rand-1] = temp1
return partition(A,p,r)
def randomized_select(A,p,r,i):
if(p == r):
return A[p-1]
q = randomized_partition(A,p,r)
k = q-p+1
if(i == k):
return A[q-1]
elif(i<k):
return randomized_select(A,p,q-1,i)
else:
return randomized_select(A,q+1,r,i-k)
Merging of two arrays in a single array will take O(m+n) time. The expected
running time complexity of RANDOMIZED-SELECT is θ(m+n). The worst-case
running time complexity of RANDOMIZED-SELECT is θ((m+n) 2), even to find
the minimum, because we could be extremely unlucky and always partition
around the largest remaining element, and partitioning takes θ(m+n) time.

So, the overall expected running time complexity of the algorithm to find
median of the two sorted input arrays will be : θ(m+n) + O(m+n).

 Searching: The algorithm to convert a positive integer (less than


10,000) to a roman numeral is as follows:

def number_to_roman(num):
dict_roman = {1:'I', 4:'IV', 5:'V', 9:'IX', 10:'X', 40:'XL', 50:'L',
90:'XC',100:'C', 400:'CD', 500:'D', 900:'CM', 1000:'M'}
string_roman = ''
# List of keys of dictionary
key_list = list(dict_roman.keys())
# Reverse the key list
key_list.reverse()
index = 0
while(num > 0):
quotient = num//key_list[index]
num %= key_list[index]
for i in range(quotient):
string_roman += dict_roman[key_list[index]]
index +=1
# Return the Roman Numeral
return string_roman

As there are 13 key and value pairs in the following dictionary of numbers
and their symbols in roman:
dict_roman = {1:'I', 4:'IV', 5:'V', 9:'IX', 10:'X', 40:'XL', 50:'L', 90:'XC',100:'C',
400:'CD', 500:'D', 900:'CM', 1000:'M'}

While loop will run 13 times as it will use each key of above dictionary to
compute quotient and remainder which will be later used in the for loop
which is inside the while loop. The for loop will run 21 times in worst case
when the input number will be 9888.

So, for total worst-case running time complexity, while loop and for loop
combined will be executed for 27 times.

Input number = 9888 (Symbol used for floor division ‘//’)

For 1000: for loop will run 9 times (9888//1000)

For 900 : for loop will run 0 times (888//900)

For 500 : for loop will run 1 times (888//500)

For 400 : for loop will run 0 times (388//400)

For 100 : for loop will run 3 times (388//100)

For 90 : for loop will run 0 times (88//90)

For 50 : for loop will run 1 times (88//50)

For 40 : for loop will run 0 times (38//40)

For 10 : for loop will run 3 times (38//10)

For 9 : for loop will run 0 times (8//9)

For 5 : for loop will run 1 times (8//5)

For 4 : for loop will run 0 times (3//4)

For 1 : for loop will run 3 times (3//1)


3. Link To Run The Project:
 Please click on this link to see the functionality of the web application:
http://pushpendradash.pythonanywhere.com/

You might also like