Quick question about Polyphase sorting. Do you write the files sequentially? Like so:
F1: 10 13 7 8 9 4 3 17 18 2
F2: -
F3: -
F4: -
This:
F1: -
F2: 10 13 7 8
F3: 9 4 3 17
F4: 18 2
or do you alternate? vs. this?
F1: -
F2: 10 8 3 2
F3: 13 9 17
F4: 7 4 18
or does it not matter?
For any particular set of inputs, your choice of how you split up the initial input will definitely influence the number of passes you need to make in some cases, but in the general sense, it should not matter - some will be slightly better with one method over the other, some will be slightly worse. In the end the average case over all possible inputs will be the same.
However, what the second method has going for it is that you do not have to pre-scan the input to count elements in order to figure out how many elements to put in each bucket.
Related
From the Wikipedia page for block sort I figured out that block sort works by dividing the initial array into small subarrays of length 16 for example, sorting all those subarrays in O(n) time, then merging all these blocks in a way I can't understand.
For example, considering an array of length 16, dividing it in 4 block, each of length 4, and sorting those blocks, we get:
10 1 8 3 4 19 20 13 14 17 8 9 12 18 7 20
10 1 8 3 ----- 4 19 20 13 ----- 14 17 8 9 ----- 12 18 7 20
1 3 8 10 ----- 4 13 19 20 ----- 8 9 14 17 ----- 7 12 18 20
Can anyone please explain me how does merge step works?
Usually merge sort goes even further and splits the array in blocks of 2. To merge, it creates a pointer to the begging of both blocks and compares their values. It picks the smaller and increments the corresponding pointer.
1 4 5 ...
^
2 3 4 ...
^
Pick 1, because its smaller, and update pointer
1 4 5 ...
^
2 3 4 ...
^
Pick 2
1 4 5 ...
^
2 3 4 ...
^
Pick 3 and so on....
These values are put on an array which is gonna be compared with another array created with the same technique. And it goes on and on merging until all the members are sorted. I'm not considering the whole lot of optimizations that you could do in a real merge algorithm.
The first thing of block sort merging is to extract buffers. That is the only thing I know a lot about, and it starts like this. Find the square root of the array's length, and find that many unique values in the beginning and end. Using either rotations or reversals, you can put them all in the beginning and end. Then, I don't know how to merge the other stuff.
I am trying to do http://www.spoj.com/problems/FIBTWIST/ problem by linear recursion. However, since the constraints are large I have to use matrix exponentiation.
I have read http://zobayer.blogspot.in/2010/11/matrix-exponentiation.html
so according to it equations formed are
ft(n)=ft(n-1)+ft(n-2)+g(n) ft(0)=0, ft(0)=1
g(n) =g(n-1)+1 g(1)=0
But now I am confused how to form matrices A and B of the form A*M=B. It is given as Type 7 in mentioned blogspot link but I am having difficulty in understanding it.
Define a third sequence, fut, Fibonacci-untwist, as
fut(n)=ft(n)+(n+2).
Then
fut(n)=ft(n)+n+1=ft(n-1)+ft(n-2)+(n-1)+(n+2)=fut(n-2)+fut(n-1)
So fut is just another solution of the Fibonacci recursion, and thus
fut(n)=f(n-1)*fut(0)+f(n)*fut(1)=2*f(n-1)+4*f(n)=2*f(n)+2*f(n+1)=2*f(n+2)
and finally
ft(n)=2*f(n+2)-(n+2)
Test:
f(n): 0 1 1 2 3 5 8 13 21 34
2*f(n+2): 2 4 6 10 16 26 42 68
n+2: 2 3 4 5 6 7 8 9
ft(n): 0 1 2 5 10 19 34 59
and really, the last row is the difference of the second and third row.
We are studying algorithms and we haven't yet started making any programs with it, so we have a simple exercises. For example:
I have to bubble sort:
4 2 7 16 6
Here is a proccess (we have to look from the right to left in this case):
4 2 7 16 6 (it makes 3 swaps and 4 comparisons in this)
2 4 6 7 16 (it makes 0 swaps and 3 comparison in this)
2 4 6 7 16 (the result)
This how I understand it:
4 2 7 16 6 (6 is swapped with 16)
4 2 7 6 16 (6 is swapped with 7)
4 2 6 7 16 (6 is not swapped with 2)
4 2 6 7 16 (2 is swapped with 4)
2 4 6 7 16 (result after 1st iteration)
now we have numbers which have been arranged properly, we have made 4 comparisons at the beginning and 3 swaps and we know that 2 is the smallest number so we will look at 4 6 7 16 now. However they are arranged properly but still we have to make 3 additional
comparisons.
My question is: do we have to make another 2 comparisons and then the last 1 comparison (which would result in 10 comparisons overall) to finish the bubble sort? Or it stops after 7?
I'm not sure when the Bubble-sort stops.
The Build-Heap algorithm given in CLRS
BUILD-MAX-HEAP(A)
1 heap-size[A] ← length[A]
2 for i ← ⌊length[A]/2⌋ downto 1
3 do MAX-HEAPIFY(A, i)
It produces only One of several possible cases.Are there other algorithms which would yield a different case than that of the above algorithm.
For input array
A={4,1,3,2,16,9,10,14,8,7}
Build-Heap produces A={16,14,10,8,7,9,3,2,4,1} which satisfies heap property.
May be this is the most efficient algorithm to build a heap out of an array but there are several other permutations of the array which also have the heap property.
When i generated all permutations of the array and performed a test for heap property.I got 3360 permutations of the array which had the heap property.
Count1 16 9 14 4 8 10 3 2 1 7
Count2 16 9 14 4 8 10 3 1 2 7
Count3 16 9 14 4 8 10 2 1 3 7
Count4 16 9 14 4 8 10 2 3 1 7
Count5 16 9 14 4 8 10 7 2 1 3
Count6 16 9 14 4 8 10 7 2 3 1
Count7 16 9 14 4 8 10 7 1 3 2
Count8 16 9 14 4 8 10 7 1 2 3
Count9 16 9 14 4 8 10 7 3 1 2
Count10 16 9 14 4 8 10 7 3 2 1
...........................................................
Count3358 16 8 14 7 4 9 10 2 1 3
Count3359 16 8 14 7 4 9 10 3 2 1
Count3360 16 8 14 7 4 9 10 3 1 2
So is there a different build-heap algorithm which would give an output which differs from that of the above algorithm or which gives some of the 3360 possible outcomes?
Once we have used the build-heap to get an array which satisfies the heap property.How can we generate maximum number of other cases using this array.We can swap the leaf nodes of the heap to generate some of the cases.Is there any other way to get more possible cases without checking all permutations for heap property test?
Given the range of values in the array and all values being distinct.Can we say anything about the total number of possible cases that will satisfy the heap property?
Any heap building algorithm will be sensitive to the order in which items are inserted. Even the Build-Heap algorithm will generate a different heap if you give it the same elements, but in a different order.
Remember that when you're building a heap, the partially-built part must maintain the heap property after each insertion. So that's going to limit the different permutations that can be generated by any particular algorithm.
Given a heap, it's fairly easy to generate at least some of the permitted permutations.
A node doesn't care about the relative size of its two child nodes. Therefore, you can swap the children of any node, then do a sift-up on the smaller of the two to ensure that the heap property is maintained for that subtree (i.e., if it's smaller than one of its sub-nodes, swap it with that sub-node, and continue doing the same down that path until it gets to a spot where it's larger than either sub-node, or it's moved close enough to the end of the array that it's a leaf node.
I'm trying to solve this problem and I'm new to backtracking algorithms,
The problem is about making a pyramid like this so that a number sitting on two numbers is the sum of them. Every number in the pyramid has to be different and less than 100. Like this:
88
39 49
15 24 25
4 11 13 12
1 3 8 5 7
Any pointers on how to do this using backtracking?
Not necessarily backtracking but the property you are asking for is interestingly very similar to the Pascal Triangle property.
The Pascal Triangle (http://en.wikipedia.org/wiki/Pascal's_triangle), which is used for efficient computation of binomial coefficient among other things, is a pyramid where a number is equal to the sum of the two numbers above it with the top being 1.
As you can see you are asking the opposite property where a number is the sum of the numbers below it.
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
For instance in the Pascal Triangle above, if you wanted the top of your pyramid to be 56, your pyramid will be a reconstruction bottom up of the Pascal Triangle starting from 56 and that will give something like:
56
21 35
6 15 20
1 5 10 10
Again that's not a backtracking solution and this might not give you a good enough solution for every single N though I thought this was an interesting approximation that was worth noting.