Sei sulla pagina 1di 4

Algorithms Final

Engin Deniz Alpman

June 3, 2017

Instructor: Yuksel Gunal

1
1
The middle term in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 is 8. If we want search further, we
will go either left or right sub-array and continue this procedure. If we go left, middle element is
4, If we go right the element in the middle is 12. Since further search is not allowed we an only found:

8,4,12

2
If we increase N by 8 (for example N1 =1000 to N2 =8000 or N2 =8000 to N3 =64000),the running
time will be increased by 32. So we know that running time is scaling with increasing N, we can
write it like that T (N ) = c.N x where c is a constant and x is a scaling parameter. We can determine
x by :
T (N2 ) a.N2x
T (N1 ) = a.N1x
N2 x
32 = ( N 1
) = (8)x
log8 32 = x = 53

Now we can put x in the known case to find constant c(for example in N=8000):
5
32 = c.(8000) 3
c = 0.00001

So:
5
T (N ) = 0.00001.N 3

3
In the question it is given that there are only distinct elements in array. If we dont have that
knowledge we would need to look all the terms in the array because of possible recurring elements.
We can check local minima like this: First check if it is in the middle of the array. If this element is
local minima, return this value, otherwise one of the elements have to be less than middle element
for it is not to be local minima. After determining which one is the smaller one, we take the
sub-array whose edge is the number who is less than the mid value and continue this process until
we find the local minimum. I put the java code named FindLocal.java on sakai.

1
4
We will ,again, use binary search for this problem. We will find the element in the middle of the
array and then compare it with its neighbouring elements, then we will move on to the sub-array
containing larger elements. Splitting array into sub-array will not change the nature of bitonic
array. Every sub-array will be bitonic as well. By doing binary search, we are decreasing size of
the array by factor of 2. So this will take log2 N to complete in worst case scenario : I put the java
code named Bitonic.java on sakai

5
I am new to the latex, and I am not very familiar with how to write symbols. Because of that, I
put the photograph of the analysis on sakai but I will write their order of growth of running time
here:

a)
O(N)

b)
O(N)

c)
O(log(N).N)

6
If we implement our algorithm in a way that gives us the difference of the sums of different Con-
tiguous sequence, we can determine the closest sum efficiently. Otherwise we should try all the
combinations to determine which Contiguous sum is the optimal.

2
7
I put the analysis on sakai(7.jpg).The answer is b,f,g,

8
I put the analysis of this on sakai as photograph(8.jpg). The answer is: G B D A C E F

9
To calculate the sum of the nodes of the tree without calculating common nodes twice I find the
lowest common ancestor of the two nodes we are examining. After that I implement my tree in a
way that enables me to find parent of the node I am looking. To do that I add parent attribute
to Node class. Also implementation of the tree could be done by a method that is not hard to
implement but I will not to be able to do that because of the due date. After that I store the
leafs in the array then calculate each of the combinations using nested for loop and the help of the
method that I wrote named sum. I put photograph of the running code and the java code of this
question on sakai(Optimumsum.Java and sumtree.jpg) . Finding lowest common ancestor and
calculating 2 nodes sum takes linear time. Nested for loops contributes to running time at a tree
that has too much leafs because running time scales with the square of the number of leaf. If the
number of leaf is small running time scales with N, If leafs number is close to N, then running time
scales with N 2

Potrebbero piacerti anche