Writing a Recurrence Equation - recursion

I am confused on where the 2nd term [T(n) = 2T(n/2) + THETA(n)] is derived from when writing the recurrence equation for Merger Sort.
From the Coursera class, it was stated that the 2nd term is due to what occurs outside of the recursive calls. So my guess is because it is due to the 2 For Loops, each would go up to n/2, so the total would be counting to n:
function mergesort(m)
var list left, right
if length(m) ≤ 1
return m
else
middle = length(m) / 2
for each x in m up to middle
add x to left
for each x in m after middle
add x to right
left = mergesort(left)
right = mergesort(right)
result = merge(left, right)
return result
Any help would be appreciated.
Thanks

Yes, that's right. There's linear work done to iterate across the elements of the input list, distributing each element into either the left or right subarray. That accounts for the Θ(n) term in the recurrence.
Hope this helps!

Related

Solve T(n) = T(n-n/k)+n using summation

I am currently trying to solve the above recurrence relation but am having trouble trying to decipher the pattern an rewrite it as a sum. Could anyone help me out?
k >= 0. T(n<=2) = 1.
This recurrence relation was obtained from an algorithm I wrote to obtain a single sorted array from an array that is k sorted (meaning that every k'th element is in sorted order). This algorithm runs at most k times. Each time k is reduced by one and every k'th element is added to another array. Finally each array is merged using the merge from merge sort (n time). This algorithm is called recursively until k = 0, meaning we have found each sorted sub array.
I have a feeling that this is O(k*n), but I am not sure.
It might help to note that
n - n/k = ((k - 1) / k)n,
so your recurrence relation represents n decaying geometrically by a factor of (k-1)/k at each step. To see how much work is done, let a = (k-1)/k. Then the work done is upper-bounded by
n + an + a2n + a3n + ...
= n / (1 - a)
= n / (1 / k)
= nk.
So your total work is O(nk).
As a note, I haven’t checked whether the recurrence relation you have matches your code - I’m just showing the math here. :-)

Recursion Confusion - Summation Symbol

I have an assignment with this symbol on it: [Image of unfamiliar symbol
Basically the question asks "Write a recursive Java method which, given a positive integer n, computes and returns the sum of the integers from 1 to n as follows".
I do not need any help on the recursion itself, I really just need to understand what that symbol means (Link Included), so I can answer the question properly.
My Question: What meaning does the symbol possess? What is my instructor expecting as a valid response?
NOTE: I do NOT want anyone to attempt to answer the actual assignment question. I ONLY want know understand what the symbol being used means and what should be returned in my recursion method.
IT is the sigma symbol which means take the sum from i = 1 to n.
so your output comes as 1 + 2 + 3 + ..... + n
This explanation is to left hand side of the equation. others are the same.
It's a summation symbol
The sum of each i starting from i = 1 to i == n equals the sum of each i starting from i = 1 to i == n/2 plus the sum of of each i starting from i = n/2 + 1 to i == n

How would I convert numbers to their decimal representation?

I am not sure how to approach it but could someone help me convert the following numbers to their decimal representation:
and
The general method goes something like this:
Work from right to left, you'll want to count the positions (starting with zero) and sum up the terms according to a the following formula:
Say you're working in base x. Then, if you're at the ith position, and that digit is d, then that position will contribute a term of d times x^i to the final sum.
As a concrete example, take your first number - here, x=7 (the base). Starting from the right, the first digit is d=6 at the i=0 position. So we start with 6*(7^0) = 6(1) = 6.
Moving to the left, i=1 and d=5. So we get 5(7^1) = 5(7) = 35 for this term.
Then, moving to the last digit, i=2 and d=4. So we get 4*(7^2)=4(49)=196 for the last term.
Now, you can just add all of these up to get 35 + 6 + 196 = 237 as your final number (in base 10, that is).
The exact same algorithm works for any base, so you should be able to apply it to the binary number in the exact same way.
(Just let x=2 and work right to left, noting that i ranges from 0 to 7 here.)

Recurrence relation in Josephus p‌r‌o‌b‌l‌e‌m

The josephus problem can be solved by the below recursion:
josephus(n, k) = (josephus(n - 1, k) + k-1) % n + 1
josephus(1, k) = 1
How this recurrence relation has been derived?
josephus(n, k) = (josephus(n - 1, k) + k-1) % n + 1 ...... (1)
To put it in simple words -
starting with the "+1" in the formula. It implies that 1 iteration of the recurrence has already been done. Now, we would be left with n-1 persons/elements. We need to process n-1 elements recursively at intervals of k. But, now, since the last element to be removed was at kth location, we would continue from thereof. Thus, k-1 added. Further, this addition might upset the indexing of the array. Thus %n done to keep the array index within the bounds.
Hope it is lucid and elaborate enough :) .
This paragraph is sufficient from wikipedia..
When the index starts from one, then the person at s shifts from the
first person is in position ((s-1)\bmod n)+1, where n is the total
number of persons. Let f(n,k) denote the position of the survivor.
After the k-th person is killed, we're left with a circle of n-1, and
we start the next count with the person whose number in the original
problem was (k\bmod n)+1. The position of the survivor in the
remaining circle would be f(n-1,k) if we start counting at 1;
shifting this to account for the fact that we're starting at (k\bmod
n)+1 yields the recurrence
f(n,k)=((f(n-1,k)+k-1) \bmod n)+1,\text{ with }f(1,k)=1\,,

Create grid out of number of elements

Ok here's what I'm trying to accomplish. Say I have 100 items. I want to create a "grid"(each Item consisting of an x, y point). I want the grid to be as close to a square as possible.
Is there any kind of math to determine the grid width, and grid height i'd need by just a single number?(By grid width and height I mean the number of x items, and the number of Y items)
Now that I think about it would it be efficient to take the square root of the number, say varI=sqrt(45), remove the decimal place from varI...X=varI...then Y would be varI+1?
The square root is precisely what you need.
N
x=floor(sqrt(N))
y=raise(N/x)
This is the minimum rectangle that has more than N places and is closest to a square.
Now... if you want to find a rectangle that has exactly N places and is closest to a square...that's a different problem.
You need to find a factor of N, x, that's closest
You have to run through the factors of N and find the closest to sqrt(N). Then the rectangle is x by N/x, both integers.
There are several issues to consider here. If you want your grid to be as square as possible, for many Ns it will have empty cells in it. A simple example is N=10. You can create a 3x4 grid for it, but it will have two empty cells. A 2x5 grid, on the other hand, will have no empty cells. Some Ns (prime numbers) will always have empty cells in the grid.
But if you just want the square and don't care about empty fields then generally yes, you should take the square root. Say your number is N. Then, take R = int(sqrt(N)). Next, do an integer division N/R, take the quotient and add 1 to it. This is C. The grid is RxC. Note that when N is a square (like 100), this is a special case so don't add 1 to the quotient.
Example:
N = 40
R = int(sqrt(N)) = 6
C = int(40 / 6) + 1 = 7
grid is 6x7
I was looking to solve this problem too for a grid in html/css that had fixed dimensions and where N items would fit. I ended up creating my own script for that in javascript.
If you're interested in the method and maths I used, you can read http://machinesaredigging.com/2013/05/21/jgridder-how-to-fit-elements-in-a-sized-grid/, it's all documented there. I used recursion and it works really well, you can use the same method for your own language. Hope this helps.
I explored Eli's answer and found something I'd like to point out. For the sake of generality, one must add 1 to C only if R x C (C = int(N/R)) is not exactly N. So, the exception includes both numbers with square root and numbers which are exactly the product of two integers.
For instance:
N = 12
R = 3
C = 4 (int(N/R))
Hope it helps.

Resources