Algorithms (CSCI 3104, Fall 2016)In this page, we will post new challenge problems and their solutions. For each challenge problems, students are invited to send their solutions to the instructor. Note that
Fall 2016 ProblemsWeek 3: Find max occupancyWe are given the everyday airplane schedule at a busy international airport serving s of flights. For each airplane, we know the landing time and takeoff time.
Suppose we are given a large list containing pairs of landing and takeoff times for each plane, expressed in minutes after midnight. Write an efficient algorithm that will present the airport manager information about the number of planes in the airport during the various time intervals of time. In particular, the table must be sorted by increasing times, should cover the entire hour period and should have as few rows as possible.
Week 1: Find smallest missing positive number in an array.You are given an array of positive numbers. Eg., consider the array . Find the smallest positive number that is missing in the array. For the example given above, the smallest missing number is . If all elements from to are present, then the smallest missing number is . Your algorithm should run in linear time, ie , given the size of the array . You are not allowed to use extra arrays or hashtables. You may however rearrange the elements in the input array. Successfully solved: Byron Becker and Chance Roberts SolutionFirst, we can find the minimum element of the array and subtract this minimum element from everything. This way, elements in the array start from . Suppose the new array has as the smallest missing elements, we should be able to discover contiguous elements . The algorithm we propose is going to attempt to find by placing the elements in the positions respectively. code
def findSmallestMissing(a): n = len(a) m = min(a) for i in range(0,n): a[i] = a[i] -m To do so, the algorithm maintains two counters , . Initially, and . The following invariant will be maintained, Invariant
For the subarray , we will place the elements . The subarray will contain elements that for some reason or another cannot be the missing elements. code
i = 0 n = len(a) while (i < n): if (a[i] == i): # a[i] already equals i, nothing more to do i=i+1 else: if (a[i] < i): # a[i] is strictly less than i # This means a[i] is a repeated element and cannot be missing swap(a,i,n-1) # Place a[i] in the range of elements that cannot be the missing one n=n-1 # reduce n by 1 else: e=a[i] # Let us call e = a[i] if (e < n): if (a[e] == e): # if a[e] already has e in it, e cannot be missing swap(a,i,n-1) n = n -1 else: # otherwise, place a[e] = e and bring a[i] = a[e] e = a[i] swap(a,i,e) else: # if e >= n swap(a,i,n-1) # then a[i] cannot be missing n = n-1 Once this is done, we can search for the first element where that is the missing element. The overall code can be downloaded. Let us illustrate what happens when we call ’'findSmallestMissing([0,3,2,0,4,6,7,9,1,11,13,11])’’
Here the smallest missing element is . It is found by incrementally arranging the elements in the initial portion of the array and pushing elements that cannot be missing to the end of the array. Why does this work in linear time? Complexity Analysis
Each step of the loop, one of the following happens: (a) increases by , increasing the number of contiguous elements in the start of the array. (b) decreases by , increasing the number of elements that cannot be the missing elements. (c) Or else, we take the element and swap it with , provided, is not already (in which case (b) happens). This means, that the number of elements for which holds increases by . Using the three facts above, we can prove that the running time is . |