Sei sulla pagina 1di 8

COMP2211 Analysis Of Algorithms

ID# : 620055419 Danuel Williams


Dr. Daniel Coore
Problem 1
a. Suppose that we have an n-storey building that is tall enough to cause the egg to break at some
height , h n. Describe a (lg n) process by which we could determine the minimum height at
which the egg breaks .

To Find the minimum height at which the eg would break, we would have to use the divide and
conquer method to find that point by:
I. Dividing n floors by 2 (rounding up the answer to the nearest integer)
II. Using the result from (I.) above we would drop the egg from that floor and based on the
state of the egg(broken or unbroken) we would pursue one of two options.
IF THE EGG BREAKS
At this point we can check if minimum breaking point is at this point that we have
calculated (n/2), or if the minimum breaking point is on a lower floor (h < n/2). We could
set h as our new building height and again find the median between the new building
height and the ground floor. This procedure of issuing a new height and finding its
median would continue until an unbroken egg state is found. At this point we would find
the minimum height by incrementally testing floors in an ascending order from the last
median calculated to the last new height specified until again a broken egg state is
achieved, at that point, we know that we have found the minimum height at which the
egg breaks .
IF THE EGG DOESNT BREAK
We can check if the minimum breaking point is at the median point calculated or if it is
at a higher point on the building. We would find the median in the opposite way, by
updating our new floor to be the median that we calculated, and after we would
calculate a new median from the new ground floor, and n. We would continue this
procedure, until we got an egg state of broken. At this point our last median, would be
the uppermost portion of our range, and our last floor would be our lowermost portion
of the range. The minimum heigh would be found in decrementing fashion from the
uppermost portion of the range to the lowermost portion, until an unbroken egg state
and at that point, the floor before this point resurfaced would be the minimum height at
which the egg broke.


The above process can be seen as a log n process based on the amount of visits made to
each floor. In a floor by floor check approach we will cover all of n, which would give us a
time of n for completion as well. In our procedure laid out above we notice that we visit
but a fraction of that amount, for example, in with a building size of 100 storeys we
would visit, in the worst case, an approximate 10 floors before finding the floor which
would be the minimum height at which the egg would break. This reduction in floors
visited can be represented by a mathematical function of lg, hence proving that the time
complexity would indeed be within the bounds of lg n.

b. Suppose that h
b
is the minimum height that causes the egg to break, describe how to determine
h
b
in (lg h
b
) steps (individual steps )


Problem 2
a) Write a procedure called checkBST that takes a binary tree as an argument and returns True if
the tree is a binary search tree, and False otherwise. It might help to define an intenal auxiliary
procedure that takes a tree, a lower bound and an upper bound and returns true if all the
values in the tree lie within the bounds.

def checkBST(tree):
return (checkBSTUtil(node,min,max))

def checkBSTUtil(node, min, max)
if(node == None)
return 1
if (root(node) <min || root(node) >max)
return 0
return checkBSTUtil(left(node), min, (root(tree)-1)) &&
checkBSTUtil(right(node), (root(tree)+1), max)




b) Prove that for a fixed set of (unique) keys, any binary search tree containing, those keys can be
converted to any other BST on the same set of keys via a sequence of left- and/or right-
rotations.

Base Case: A empty BST Tree has only one tree that can be possible via any right or left rotations

Inductive Step: Since BST
0
we shall assume that BST
n
should be true as well,
Showing this graphically we shall test the plausibility of this assumption for BSTs of n=1,2 and 3






Left Rotation Left Rotation Left Rotation






Right Rotation Right Rotation Right Rotation







Since we see that BST
n
is true, we need to prove the assumption for BST
n+1

We can prove this by the logical reasoning , addition to any of the branches of the tree using
the proper Binary Tree Insertion methodologies will result in a binary tree, and also, because of
the properties of the tree, rotating the tree right or left will not violate the tree in any way,
because of the tree is ordered.



c) Write a procedure called checkHeap that take a binary tree as an argument and returns True if
the given tree is a valid binary heap. This means that your procedure must check for both the
ordering property as well as the structural property of heaps.

Assuming that the data stored in the node is as such [value, marker] and that this format is
returned by the functions left(), right(), and root(), which are also assumed to be implemented in
this ADT.

def checkHeap(b_tree):
if b_tree == None:
return False
if (root(b_tree)[0] > left(b_tree)[0] && root(b_tree)[0]>right(b_tree)[0]) &&
(root(b_tree)[1] == (right(b_tree)[1]-1)/2) && root(b_tree[1] ==
(left(b_tree)[1]-1)/2))
return True
return checkHeap(right(b_tree)) && checkHeap(left(b_tree))


Problem 3
a. Describe a simple modification to the merge sort procedure that would keep track of (and
return) the number of swaps that the bubble sort procedure would have performed had it
sorted the same set of values. Your modification should no change the asymptotic running time
of the merge sort procedure. Give a brief explanation of why your modification would produce
the correct result.

The modification would have merge sort having a bubble_count variable that would be
incremented with each merge based on length of list of the higher of the two halves( if
necessary). This works based on the fact that each time a merge is performed each comparison
between the lists to ensure that there is order in the merged list can be considered a swap, and
for the fact that the longer list that is to be merge would control the amount of comparisons
that is what guides the precedent of this solution. The asymptotic running time of the sort would
not be changed because basic operations of assignment and increments are being used to
achieve our result.


b. Recall that the sort procedure, in their most general implementations, accept as a second
argument, a comparator to determine the actual ordering produced by the sort. So, if we
created a comparator that used the first permutation as the reference permutation, and used it
to sort the second, we would obtain the first. Using our modified merge sort would yield the
kendall tau distance. The following implementation of kendall_tau illustrates this.
def kendall_tau(perm1, perm2):
Return the Kendall tau distance between perm1 and perm2. The two permutations
are assumed to have the same length, and to consist only of indexes (integers from 0 to
n-1).
comparator = make_comparator(perm1)
result= perm2[:]
return modified_merge_sort(result, comparator)



i. Describe how to define make_comparator so that the comparator it returns runs in O(1)
time. (It would be acceptable for make_comparator itself to run in O(n lg n) time, if
necessary, since the asymptotic running time of the overall algorithm would be
unaffected.)[Note: for this part, I am just asking for the description and the anaylsis; the
actual implementation is asked for in the subsequent part.]
Taking a permutation the make_comparator would allow the comparator made to search
through the listing passed to it, and comparing the location of the values to be compared
with their value in the list.
ii. In Python (as in Java), the comparator used by the sort functions is a procedure of two
arguments that returns either -1, 0 or 1, depending on whether the first argument is less
than, equal to, or greater than the second, respectively.
For example, after correctly defining make_comparator, you would observe the following:

Cmp = make_comparator([4,3,0,1,2])
>> cmp(0,1)
-1
>> cmp(4,1)
-1
>> cmp(0,4)
1
Notice that cmp returns the value indicating the relative ordering of the given arguments in
the list on which it was based. Provide an implementation of make_comparator, a higher
order procedure, that takes a permutation as an argument and returns a comparator that
respects the given permutation. It should follow your description from the previous
question.

Answer on following page


def make_comparator(perm):
def cmp(number, compareTo):
ordering = perm
position = len(ordering)/2
found = False
while (!found):
if (ordering[] == number):
if (ordering


return cmp

Potrebbero piacerti anche