First of all, I do not know very much about skip-lists, I am just trying to prepare for my exam. What is the frequency of the elements for optimal search (log(n)) on each level when having log(n) lists in a skiplist ? I know that when working with 2 lists, the first has n elements, and the second one has sqrt(n) elements. So, when searching for an element I do at most sqrt(n) steps on the second list (that above), and sqrt(n) steps in the first list (the one that contains all the elements) because the gaps between elements are sqrt(n)-long. So that seems ok. But how many elements does each list have when working with log(n) lists ?
When an element is added to a skip list "a coin is flipped" several times to decide in how many layers the new element should be included. The element is always added to the first (lowest) layer and then added to the next layer above each time the coin lands on heads. If we call the probability of getting heads in a coin flip p (commonly p = 0.5 is used), each element is added with probability 1 to the first layer, with probability p to the second layer, with probability p^2 to the third layer and in general with probability p^(i-1) to the i-th layer. If the skip list contains n elements, the second layer contains n*p elements in expectation (not sqrt(n)), the third layer n*p^2 elements and in general the i-th layer n*p^(i-1) elements. For example, in a skip list with p = 0.5 and n = 64 elements we would expect the second layer to contain about 32 elements, the third about 16 and so on, and we would thus expect to have about log_(1/p)(n) = log_2(n) layers in total. To search an element in a skip list, we first linearly scan the topmost list (which contains only a few elements, maybe 2 or 3). This allows us to restrict our search to a smaller range in the list below. We then scan that range of the list below and repeat the process until we reach the bottommost list. With p = 0.5 you can almost think of it as a binary search. For example, with n = 64 elements we would expect to take about 2 steps of about 32 elements in the topmost list, restricting our search in the list below to a range of about 32 elements. We would then scan these 32 elements in the list below at steps of about 16 elements, and so on.
Related
Question:
Let B = {0, 1}. Bn is the set of binary strings with n bits. Define the set En to be the set of binary strings with n bits that have an even number of 1's. Note that zero is an even number, so a string with zero 1's (i.e., a string that is all 0's) has an even number of 1's.
(a)
Show a bijection between B^9 and E^10. Explain why your function is a bijection.
(b)
What is |E^10|?
I having trouble finding a solution that satisfies the set and is a bijection. How do I approach solving this problem.
Is it something to do with cases? For exampple, if B^9 has an even number of one's add a zero, and if there is an odd number of one's add a one to obtain E^10?
Thanks!
(a) Every string in E^10 begins with a prefix of length nine which is also a member of B^9. Given the prefix of length nine, the last bit is uniquely determined since it either must be 0 (if the prefix is also in E^9) or it must be 1 (if the prefix is not also in E^9). Therefore, for each element of E^10, there is exactly one element of B^9 to which it is uniquely mapped. Similarly, for any element in B^9, an element of E^10 can be uniquely formed by adding either a 0 or a 1 to the end of the element in B^9 (choosing the one that results in parity). This operation - appending either 0 or 1 to create parity - maps each element of B^9 to a unique element of E^10. Because there is a unique mapping from all E^10 to B^9, and from all B^9 to E^10, we have our bijection.
(b) Because there is a bijection between B^9 and E^10, we know |E^10| = |B^9|. But |B^9| = 2^9, since for each of the nine positions in any string in B^9 we can independently choose one of two values for the bit. Therefore, |E^10| = 2^9 also.
I have this code
A = unidrnd(2,100,30)-1;
B = reshape(A, 100, 3, 10);
B is a multidimensional array with 10 layers of 100x3 Matrices. Now I want to perform this code,
C = length(nonzeros(all(B,2)))/100;
where the function on the right hand side of the code is suppose to generate 10 values corresponding to the result of the 10 layers, but all I get is a single value. The right hand of the code checks how many rows are all 1's. It takes the number of rows that are all 1's and divides it by 100 to obtain the fraction of the number of rows that are all 1's.
How can I obtain the result of every 100 x 3 layers of the 3D matrix using the single line of code I have shown above such that I do not have to use a loop? The result C had to be array of the results as expected.
You started out well. all(B,2) is good, it gives you the 100x1x10 matrix that's 1 where the corresponding rows are all 1's and 0 otherwise.
nonzeros, however, simply lists all of the nonzero elements of the entire matrix, in your case, a string of 1's, completely disregarding the dimensions of the array. You'd get the same results with nonzeros(A(:)) as with nonzeros(A).
[Note: nnz(A) would get you the same results as length(nonzeros(A)), but that's not what we want to do anyway.]
Since your matrix is binary (the output of all is a logical array), we can count the number of non-zero elements by summing the matrix elements. And sum gives us a dimension argument just like all, so we just sum the columns that all gave us.
C = sum(all(B,2),1)/100;
This gives you a 1x1x10 array of percentages. If you wanted that to just be a normal vector, you could use squeeze.
C = squeeze(sum(all(B,2),1)/100);
I am working on 2D rectangular packing. In order to minimize the length of the infinite sheet (Width is constant) by changing the order in which parts are placed. For example, we could place 11 parts in 11! ways.
I could label those parts and save all possible permutations using perms function and run it one by one, but I need a large amount of memory even for 11 parts. I'd like to be able to do it for around 1000 parts.
Luckily, I don't need every possible sequence. I would like to index each permutation to a number. Test a random sequence and then use GA to converge the results to find the optimal sequence.
Therefore, I need a function which gives a specific permutation value when run for any number of times unlike randperm function.
For example, function(5,6) should always return say [1 4 3 2 5 6] for 6 parts. I don't need the sequences in a specific order, but the function should give the same sequence for same index. and also for some other index, the sequence should not be same as this one.
So far, I have used randperm function to generate random sequence for around 2000 iterations and finding a best sequence out of it by comparing length, but this works only for few number of parts. Also using randperm may result in repeated sequence instead of unique sequence.
Here's a picture of what I have done.
I can't save the outputs of randperm because I won't have a searchable function space. I don't want to find the length of the sheet for all sequences. I only need do it for certain sequence identified by certain index determined by genetic algorithm. If I use randperm, I won't have the sequence for all indexes (even though I only need some of them).
For example, take some function, 'y = f(x)', in the range [0,10] say. For each value of x, I get a y. Here y is my sheet length. x is the index of permutation. For any x, I find its sequence (the specific permutation) and then its corresponding sheet length. Based on the results of some random values of x, GA will generate me a new list of x to find a more optimal y.
I need a function that duplicates perms, (I guess perms are following the same order of permutations each time it is run because perms(1:4) will yield same results when run any number of times) without actually storing the values.
Is there a way to write the function? If not, then how do i solve my problem?
Edit (how i approached the problem):
In Genetic Algorithm, you need to crossover parents(permutations), But if you crossover permutations, you will get the numbers repeated. for eg:- crossing over 1 2 3 4 with 3 2 1 4 may result something like 3 2 3 4. Therefore, to avoid repetition, i thought of indexing each parent to a number and then convert the number to binary form and then crossover the binary indices to get a new binary number then convert it back to decimal and find its specific permutation. But then later on, i discovered i could just use ordered crossover of the permutations itself instead of crossing over their indices.
More details on Ordered Crossover could be found here
Below are two functions that together will generate permutations in lexographical order and return the nth permutation
For example, I can call
nth_permutation(5, [1 2 3 4])
And the output will be [1 4 2 3]
Intuitively, how long this method takes is linear in n. The size of the set doesn't matter. I benchmarked nth_permutations(n, 1:1000) averaged over 100 iterations and got the following graph
So timewise it seems okay.
function [permutation] = nth_permutation(n, set)
%%NTH_PERMUTATION Generates n permutations of set in lexographical order and
%%outputs the last one
%% set is a 1 by m matrix
set = sort(set);
permutation = set; %First permutation
for ii=2:n
permutation = next_permute(permutation);
end
end
function [p] = next_permute(p)
%Following algorithm from https://en.wikipedia.org/wiki/Permutation#Generation_in_lexicographic_order
%Find the largest index k such that p[k] < p[k+1]
larger = p(1:end-1) < p(2:end);
k = max(find(larger));
%If no such index exists, the permutation is the last permutation.
if isempty(k)
display('Last permutation reached');
return
end
%Find the largest index l greater than k such that p[k] < p[l].
larger = [false(1, k) p(k+1:end) > p(k)];
l = max(find(larger));
%Swap the value of p[k] with that of p[l].
p([k, l]) = p([l, k]);
%Reverse the sequence from p[k + 1] up to and including the final element p[n].
p(k+1:end) = p(end:-1:k+1);
end
I have a vector [x1, x2,...xn]. Is there a way to find all possible combinations of elements that contain the last element xn? For example, if I have 4 elements I want the combinations:
x1,x4
x2,x4
x3,x4
x1,x2,x3,x4
x1,x2,x4
x1,x3,x4
x2,x3,x4
In reality though I have number of elements up to a few hundreds.
Thank you for your time!
You really just need to do a choose on all of the elements except the last one.
C = cell(length(x)-1,1);
for n = 1:length(x)-1
C{n} = nchoosek(x(1:end-1),n);
end
Each element of C contains all possible vectors with n elements. All you have to do is tack onx(end) to each one to get what you're looking for. For example, if combo=C{4}(7,:) is one solved set without the last element of x, then your desired output is combo=[combo x(end)]. To do this for all solutions, just add this line of code inside the loop above:
C{n} = [C{n} x(end)*ones(size(C{n},1),1)];
WARNING: With thousands of elements you will run out of memory very quickly. Just 100 elements gives you over 6e29 possible combinations!
Suppose I have 121 elements and want to get all combinations of 4 elements taken at a time, i.e. 121c4.
Since combnk(1:121, 4) takes a lot of time, I want to go for 2% of that combination by providing:
z = 1:50:length(121c4(:, 1))
For example: 1st row, 5th row, 100th row and so on, up to 121c4, picking only those rows from a 121c4 matrix without generating the complete combination (it's consuming too much for large numbers like 625c4).
If you haven't defined an ordering on the combinations, why not just use
randi(121,p,4)
where p is the number of combinations you want in your set ? With this approach you may, or may not, want to replace duplicates.
If you have defined an ordering on the combinations, tell us what it is.