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
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)
First of all I have seen this answer and yes it explains X-Y heuristic but the example board was too simple for me to understand the general heuristic.
X-Y heuristic function for solving N-puzzle
So could someone please explain the X-Y heuristic using this example?
8 1 2
7 3 6
0 5 4
The algorithm consists of 2 separate parts - for rows and columns.
1) Rows. Divide the input matrix by rows - elements from each row go to separate set.
(1, 2, 8) - (3, 6, 7) - (0, 4, 5)
The only available move is swaping 0 with an element from adjacent set.
You finish, when each element is in the proper set.
swap 0 and 7 -> (1, 2, 8) - (0, 3, 6) - (4, 5, 7)
swap 0 and 8 -> (0, 1, 2) - (3, 6, 8) - (4, 5, 7)
swap 0 and 3 -> (1, 2, 3) - (0, 6, 8) - (4, 5, 7)
swap 0 and 4 -> (1, 2, 3) - (4, 6, 8) - (0, 5, 7)
swap 0 and 8 -> (1, 2, 3) - (0, 4, 6) - (5, 7, 8)
swap 0 and 5 -> (1, 2, 3) - (4, 5, 6) - (0, 7, 8)
Number of required steps = 6.
2) Similarly for columns. You start with:
(0, 7, 8) - (1, 3, 5) - (2, 4 ,6)
And then
(1, 7, 8) - (0, 3, 5) - (2, 4, 6)
(0, 1, 7) - (3, 5, 8) - (2, 4, 6)
(1, 3, 7) - (0, 5, 8) - (2, 4, 6)
(1, 3, 7) - (2, 5, 8) - (0, 4, 6)
(1, 3, 7) - (0, 2, 5) - (4, 6, 8)
(0, 1, 3) - (2, 5, 7) - (4, 6, 8)
(1, 2, 3) - (0, 5, 7) - (4, 6, 8)
(1, 2, 3) - (4, 5, 7) - (0, 6, 8)
(1, 2, 3) - (0, 4, 5) - (6, 7, 8)
(1, 2, 3) - (4, 5, 6) - (0, 7, 8)
Number of required steps = 10
3) Total number of steps: 6 + 10 = 16
Say I have the following ranges, in some list:
{ (1, 4), (6, 8), (2, 5), (1, 3) }
(1, 4) represents days 1, 2, 3, 4. (6, 8) represents days 6, 7, 8, and so on.
The goal is to find the total number of days that are listed in the collection of ranges -- for instance, in the above example, the answer would be 8, because days 1, 2, 3, 4, 6, 7, 8, and 5 are contained within the ranges.
This problem can be solved trivially by iterating through the days in each range and putting them in a HashSet, then returning the size of the HashSet. But is there any way to do it in O(n) time with respect to the number of range pairs? How about in O(n) time and with constant space? Thanks.
Sort the ranges in ascending order by their lower limits. You can probably do this in linear time since you're dealing with integers.
The rest is easy. Loop through the ranges once keeping track of numDays (initialized to zero) and largestDay (initialized to -INF). On reaching each interval (a, b):
if b > largestDay then
numDays <- numDays + b-max(a - 1, largestDay)
largestDay <- max(largestDay, b)
else nothing.
So, after sorting we have (1,4), (1,3), (2,5), (6,8)
(1,4): numDays <- 0 + (4 - max(1 - 1, -INF)) = 4, largestDay <- max(-INF, 4) = 4
(1,3): b < largestDay, so no change.
(2,5): numDays <- 4 + (5 - max(2 - 1, 4)) = 5, largestDay <- 5
(6,8): numDays <- 5 + (8 - max(6-1, 5)) = 8, largestDay <- 8
The complexity of the following algorithm is O(n log n) where n is the number of ranges.
Sort the ranges (a, b) lexicographically by increasing a then by decreasing b.
Before: { (1, 4), (6, 8), (2, 5), (1, 3) }
After: { (1, 4), (1, 3), (2, 5), (6, 8) }
Collapse the sorted sequence of ranges into a potentially-shorter sequence of ranges, repeatedly merging consecutive (a, b) and (c, d) into (a, max(b, d)) if b >= c.
Before: { (1, 4), (1, 3), (2, 5), (6, 8) }
{ (1, 4), (2, 5), (6, 8) }
After: { (1, 5), (6, 8) }
Map the sequence of ranges to their sizes.
Before: { (1, 5), (6, 8) }
After: { 5, 3 }
Sum the sizes to arrive at the total number of days.
8
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.
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.