

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Algorithms Assignment 1 (Median list of numbers, partition lst(x), FindElement(lst,k) function
Typology: Exercises
1 / 3
This page cannot be seen from the preview
Don't miss anything!


Submit your assignment via Canvas. Submission must be in pdf format, and cannot be handwritten.
As a first step go to the last page and read the section: Advice on how to do the homework. The median of a list of 2n + 1 distinct numbers is the element, m, of the list such that exactly n numbers are less than m and exactly n numbers are greater than m. For example, in the list [3, 1 , 7 , 2 , 6], the median is 3.
a) Write an algorithm^1 that computes the median of a list of numbers. You may assume the list has an odd number of distinct elements. Please note that correctness and understandability are more important than efficiency. (4 marks)
b) Give an upper bound for the running time of your algorithm using big-O notation. (3 marks)
We will now develop a more efficient algorithm for computing the median. The Partition(lst,x) function takes a list of numbers lst, and an element x of that list, and returns a reordering of lst such that everything that occurs before x is less than x and everything that occurs after x is greater than x. For example Partition([3, 1 , 7 , 2 , 6], 3) could return [2, 1 , 3 , 6 , 7]. Note that the final output does not have to be completely ordered, though such an output would be a valid output of the Partition function.
c) Write an algorithm that implements the Partition(lst,x) function. Your algorithm should run in O(n) time for a list of length n, and you should justify this. You may assume lst consists of distinct elements and x is an element of the list. Hint: Start with a doubly-linked list with one element containing x. (9 marks)
d) Write an algorithm so that instead of returning a reordering of lst it returns the (0-indexed) place of x in sort(lst) (e.g. returns 0 if x is the smallest element, or (n − 1) if x is the largest element). Your algorithm should not sort the list. Hint: you should be able to make a small modification to the algorithm in the previous part. Call this function Position(lst,x). (4 marks)
e) If lst has 2n + 1 elements and x is the median of lst, what will Position(lst,x) return? (2 marks)
The last question shows that finding the median is an instance of a more general problem: finding the element of a list that is in position k. The Position and Partition functions give us some nice tools for dividing this problem into smaller problems: Take an element x from the list. If Position(lst,x) = k then we have found the element. If Position(lst,x) is greater than k then the required element lies in the sublist of Partition(lst,x) before x. Otherwise, Position(lst,x) is less than k and the required element lies in the sublist of Partition(lst,x) after x. Suppose lst has n elements, Position(lst,x) = p, the output of Partition(lst,x) is lst1, x, lst2, and we are trying to find the element y with Position(lst,y)=k.
f) If p > k we know that y is in lst1. What will Position(lst1,y) return? (2 marks)
g) If p < k we know that y is in lst2. What will Position(lst2,y) return? (2 marks) (^1) see the note on the last page about writing algorithms
The FindElement(lst,k) function does the opposite of Position: given a list lst and a position k, FindElement(lst,k) returns the element in the k-th position of sort(lst). For example, FindElement(lst,0) would return the smallest element of lst because that is the element that is in position 0 when lst is sorted.
h) Write a recursive algorithm for FindElement(lst,k). Again, you should avoid sorting the list. (8 marks)
i) Find a recurrence relation for the running time of your algorithm, T (n), and give an upper bound for the running time using big-O notation. (8 marks)
j) Your recurrence relation for T (n) likely involves T (n − 1) because of the sizes of lst1 and lst2 in the worst case. Suppose we could guarantee that lst1 and lst2 were both at most 107 the size of lst with an additional (recursive) call that costs T ( n 5 ). Find a recurrence relation for the running time of this algorithm and give an upper bound for the running time using big-O notation. Hint: See lecture notes. (4 marks)
Hopefully your answer to the last question is an improvement on your answer to b). We now look at how we go about finding this “approximate median” choice for x so that lst1 and lst2 are both relatively large (i.e. at least 30% of lst). The idea is to break lst into n 5 groups of 5, compute the median for each group of 5 (this can be done in constant time), and then compute the “median of medians” using a recursive call on the list of medians.
k) Suppose lst has 10k +5 distinct elements, and consider any partition of lst into 2k +1 groups of 5 elements. Let m 1 < m 2 < · · · < m 2 k+1 be the (sorted) medians of those groups, so that mk+1 is the median of those medians. Show that lst has at least 3k + 2 elements that are less than mk+1 and at least 3k + 2 elements that are greater than mk+1. Hint: Start by finding k elements that are less than mk+1 and showing that each of those gives you two more elements that are less than mk+1. (4 marks)
To further illustrate the behaviour of the functions defined above, here are some example outputs.