This question already has answers here:
Generating Combinations in python
(3 answers)
Closed 9 years ago.
I have several vectors a=[1 2 3 ...], b=[1 2 3 ...], c=[1 2 3 ...]. I have to find all possible combinations composed from elements taken from each of these vectors like:
[1 1 1]
[1 1 2]
[3 3 3]
etc.
The problem is that I have to exclude combinations containing same elements since order does not matter. For example, it the combination [1 2 1] is presented, the combination [2 1 1] should be excluded. How can I do that in any programing language (python is preferred)?
I am not sure I have completely understood your requirements, but you may find that itertools is helpful.
For example:
from itertools import combinations_with_replacement as cr
for a in cr([1,2,3],3):
print a
prints
(1, 1, 1)
(1, 1, 2)
(1, 1, 3)
(1, 2, 2)
(1, 2, 3)
(1, 3, 3)
(2, 2, 2)
(2, 2, 3)
(2, 3, 3)
(3, 3, 3)
This might work if you're not that worried about efficiency.
from itertools import product
def specialCombinations(*vectors):
return {tuple(sorted(i)): i for i in product(*vectors)}.values()
It takes the Cartesian product of the input vectors and filters the
ones equivalent under permutation.
Related
I have two arrays and an empty matrix, I need to perform a function such that the resulting matrix includes every combination of the two arrays.
Unfortunately I cannot run the arrays separately as they are both optional parameters for the function. I thought that the best way to do this was through nested loops but now I am unsure...
I've tried multiplying one of the matrices so that it includes the necessary duplicates, but I struggled with that as the real data is somewhat larger.
I've tried many versions of these nested loops.
a = [ 1 2 3 ]
b = [ 4 5 6 7 ]
ab = zeros(3,4)
for i = 1:length(a)
for j = 1:length(b)
ab[??] = function(x = a[??], y = b[??])
end
end
ab = [1x4 1x5 1x6 1x7, 2x4 2x5 2x6 2x7, 3x4 3x5 3x6 3x7]
Your problem can be solved by broadcasting:
julia> f(x, y) = (x,y) # trivial example
f (generic function with 1 method)
julia> f.([1 2 3]', [4 5 6 7])
3×4 Array{Tuple{Int64,Int64},2}:
(1, 4) (1, 5) (1, 6) (1, 7)
(2, 4) (2, 5) (2, 6) (2, 7)
(3, 4) (3, 5) (3, 6) (3, 7)
The prime in a' transposes a to make the shapes work out correctly.
But note that a = [ 1 2 3 ] constructs a 1×3 Array{Int64,2}, which is a matrix. For a vector (what you probably call "array"), use commas: a = [ 1, 2, 3 ] etc. If you have your data in that form, you have to transpose the other way round:
julia> f.([1,2,3], [4,5,6,7]')
3×4 Array{Tuple{Int64,Int64},2}:
(1, 4) (1, 5) (1, 6) (1, 7)
(2, 4) (2, 5) (2, 6) (2, 7)
(3, 4) (3, 5) (3, 6) (3, 7)
BTW, this is called an "outer product" (for f = *), or a generalization of it. And if f is an operator ∘, you can use dotted infix broadcasting: a' ∘. b.
Isn't that just
a'.*b
?
Oh, now I have to write some more characters to get past the minimum acceptable answer length but I don't really have anything to add, I hope the code is self-explanatory.
Also a list comprehension:
julia> a = [1,2,3];
julia> b = [4,5,6,7];
julia> ab = [(x,y) for x in a, y in b]
3×4 Array{Tuple{Int64,Int64},2}:
(1, 4) (1, 5) (1, 6) (1, 7)
(2, 4) (2, 5) (2, 6) (2, 7)
(3, 4) (3, 5) (3, 6) (3, 7)
Given this list List(1, 1, 2, 2, 3, 3, 4, 5), how can I produce all combinations of subsets of the list, with the following constraints:
Each subset can only contain unique elements.
Each combination of subsets must contain all elements in the initial list (including duplicates). Flattening a combination should equal my initial list.
Here are some of the possible combinations:
List(List(1), List(1), List(2), List(2), List(3), List(3),
List(4),List(5)) // 1*8
List(List(1,2), List(1,2), List(3,4), List(3,5)) // 2 *4
List(List(1,2,3), List(1,2,3), List(4,5)) // 3,3 and 2
List(List(1,2,3,4), List(1,2,3,5)) // 4 and 4
List(List(1,2,3,4,5), List(1,2,3)) // 5 and 3
I have tried something like this:
val myList = List(1, 1, 2, 2, 3, 3, 4, 5)
val combs = (1 to 5).map(n => myList.combinations(n).toList.filter(x => x.distinct sameElements x)).toList.flatten
val step2 = (1 to combs.length).flatMap(n => combs.combinations(n)).toList
But the computation is too expensive:
java.lang.OutOfMemoryError: Java heap space
Try this:
val listLen = l.toSet.size
(1 to listLen).flatMap(x => l.combinations(x).map(x => x.distinct))
Please reply if it runs faster, otherwise need to go with other approaches
I want to get number of permutations of {1, ..., n} for which Insertion Sort does exactly n(n-1)/2 comparisions.
For example, for {1, 2, 3, 4} we got (4, 3, 2, 1), (3, 4, 2, 1), (4, 2, 3, 1) etc. - for all of them InsertionSort does 4*3/2 = 6 comparisions.
Anybody know some exact formula for that?
I am thinking about something like (n-1) + 1 = n, where
1 stands for reverse sequence and then we can swap all of (n-1) pairs in reverse sequence.
Here is a hint. The complete list for (1, 2, 3, 4) are:
(4, 3, 2, 1)
(3, 4, 2, 1)
(4, 2, 3, 1)
(2, 4, 3, 1)
(4, 3, 1, 2)
(3, 4, 1, 2)
(4, 1, 3, 2)
(1, 4, 3, 2)
Look at it from last column to first.
Walk step by step through the insertion sorts. See where they merge. Do you see a pattern there?
Reversing it, can you figure out how I generated this list? Can you prove that the list is complete?
The why is what matters here. Just saying 2n-1 is useless.
n(n-1)/2 is the sum of all elements in the range (1, n - 1). Since your sequence has length n, you can expand that range to (0, n - 1).
The number of swaps for each insertion would be:
run # list value swaps
1 [] a 0 (no swaps possible)
2 [a] b 1
3 [b, a] c 2
...
10 [i,...,a] j 9
...
n [...] ? n - 1
So we need to move every element through the entire list in order to achieve the required count of swaps. The number of comparisons can be at most one higher than the number of swaps, which means each value that is being inserted must either be placed at the first or second index of the resulting list. Or
Put differently, assuming ascending ordering of the output:
The input list should in general be a nearly descending list, where each element in the list may be preceded by at most one element that is not larger than the element in question.
I`m having trouble with this problem:
Suppose relation R(A,B) has the following tuples:
A B
1 2
3 4
5 6
and relation S(B,C,D) has the following tuples:
B C D
2 4 6
4 6 8
4 7 9
Compute the theta-join of R and S with the condition R.A < S.C AND R.B < S.D. Which of the following tuples is in the result? Assume each tuple has schema (A, R.B, S.B, C, D).
Choose from the following answers:
(3,4,2,4,6)
(1,2,4,4,6)
(1,2,2,6,8)
(3,4,4,7,8)
So when I try it, I see that
(1, 2) matches (2, 4, 6)
(3, 4) matches (4, 6, 8)
(3, 4) matches (4, 7, 9)
so I found the following tuples (they all respect the condition):
(1, 2, 2, 4, 6)
(3, 4, 4, 6, 8)
(3, 4, 4, 7, 9)
The problem is that none of these are found in the multiple choices...
Am I doing something wrong?
Thanks for the help!
To compute a theta-join, one basically does a cartesian product of the two relations, (here, R and S), and arrives at all possible combinations. On each of these tuples, you apply the condition theta and get the ones that are true.
Here, the cartesian gives 3x3 = 9 tuples. Of them, 8 tuples satisfy the condition (R.A < S.C AND R.B < S.D). That makes the tuple (3,4,2,4,6) an element of the theta join set.
What you have done is an a theta join for (R.B = S.B AND R.A < S.C AND R.B < S.D). Hope that helps you get the difference.
Could someone explain to me what is going on here and how to solve this problem?
Suppose relation R(A,B) has the tuples:
A B
1 2
3 4
5 6
and the relation S(B,C,D) has tuples:
B C D
2 4 6
4 6 8
4 7 9
Compute the natural join of R and S. Then, identify which of the following tuples is in the
natural join R |><| S. You may assume each tuple has schema (A,B,C,D).
I don't know what a natural join truly means. Can you explain it to me?
A natural join is joining ("sticking together") elements from two relations where there is a match. In this example
(1, 2) matches (2, 4, 6) so you get (1, 2, 4, 6)
(3, 4) matches (4, 6, 8) so you get (3, 4, 6, 8)
(3, 4) matches (4, 7, 9) so you get (3, 4, 7, 9)
So the natural join is {(1, 2, 4, 6), (3, 4, 6, 8), (3, 4, 7, 9)}
I assume R(A,B) is the master, S(B,C,D) is the detail and B is the foreign key.
SQL: select * from R, S where R.B = S.B
Then the result is:
A B C D
1 2 4 6
3 4 6 8
3 4 7 9