You are on page 1of 2

University of Engineering and Technology

Department of Electrical Engineering


EE 234: Data Structures and Algorithms
Spring 2022

Problem Set 2 Points: 20 Date: February 7, 2022 Due: February 14, 2022

Lab
Question 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 points
You are supposed to write code for two classes: class Node and class LinkedList. LinkedList
is a linked list with Node as its element. The Node has data (an int) and a pointer to the next
item in the linked list. You LinkedList contains methods to calculate the length, get an item,
set the value of an item, and print out the entire linked list. For this purpose you will need to
use magic or double leading and trailing underscore methods. You have been provided a working
code for a sorting algorithm called mySort. Test your code of the previous question by generating
an unsorted linked list and running mySort on it.
Question 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 points
The worst case running time of mySort occurs when the linked list is reverse sorted i.e. the largest
element is at the head of the linked list e.g. 4, 3, 2, 1. By running your algorithm on various
inputs figure out the asymptotic worst case running time of your algorithm. You will need to
figure out the constants of your asymptotic running time. Plot your results on a graph and show
that your curve-fitting works “reasonably well”. Hint: You may need to import and may be even
install some or all of the following libraries using pip such as time, scipy, numpy and matplotlib.
You do not need to worry too much about beautiful curve fitting but if you want you can have
fun with it.
Question 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 points
Now run the mySort algorithm on a reverse sorted Python list (not a linked list but an array
like [4, 3, 2, 1]). Repeat the previous question and find out the asymptotic running time of your
algorithm. Plot your results on a graph and show that your curve-fitting works “reasonably well”.
Plot your results for the linked list sorting and the Python list sorting on the same plot.

Theory
Question 4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 points
Theoretically find out the asymptotic running time of mySort on a linked list. How does it
compare with the curve-fitting that you did in Question 2.
Question 5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 points
The Fibonacci numbers F0 , F1 · · · are defined by F0 = 0, F1 = 1 and
Fn = Fn−1 + Fn−2
(a) (1 point) Prove that Fn ≥ 20.5n for n ≥ 6.
(b) (1 point) Find a constant k < 1 such that Fn ≤ 2kn for all n ≥ 0.
Question 6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 points
Let c = a/bd . Prove that

k=logb n O(nd ), if c < 1
X
d
 a k 

d
O(n ) d = O(n log n), if c = 1
b 
k=0 O(nlogb a ), if c > 1

Question 7 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 points
Consider an algorithm UNBALANCEDSORT identical to MERGESORT except that, instead of
dividing an array of size n into two arrays of size n/2 to recursively divide into two arrays with
sizes roughly n/3 and 2n/3. For simplicity, assume that n is always divisible by divisors (i.e. you
may ignore floors and ceilings)
(a) (1 point) Write down a recurrence relation for UNBALANCEDSORT. Assume that merging
takes Θ(n) time. Solve the recurrence (assuming that T (1) = O(1)) to show that T (n) =
Θ(n lg n).
(b) (1 point) Suppose that UNBALANCEDSORT is instead divided into two arrays of size n/4
and 3n/4. How does the asymptotic runtime of the algorithm change? What about dividing
into n/a and (a − 1)n/a for arbitrary constant 1 < a?
(c) (1 point) Now suppose that UNBALANCEDSORT divided into two arrays of size a and
n−a for some positive constant integer a. How does the asymptotic runtime of the algorithm
change? Assume that merging still takes Θ(n) time, and T (a) = O(a). It may help to draw
a new recursion tree.

Page 2

You might also like