I have pure theoretical question on KD Tree hierarchy.
Let's say that we have two dimensional tree with 'left rule'.
One of tree nodes has two children which should be sorted by X value.
In the same time, both children have the same X value.
So, what should I do in this case?
To my opinion, there are two option, rather I am sorting them by second (Y) value and distributed according to the 'left rule' — to the left goes one with smallest Y value, and to the right with bigger.
And the second option could be finding distances between these children points and it parent and distributing it according to distance value: closest goes to the left, the other to the right.
Maybe it is a very unsuccessful illustration, but still there could be an issue of having two nodes as 'median' candidates with the same evaluation parameter, doesn't matter X or Y.
Related
I have stared at this assignment for far too long now and I simply don't understand what I am supposed to do exactly. We are given a 3x3 chess board and have to produce some propositonal clauses to this problem. This is what information we have been given:
Write a Python program that generates the input for a SAT
solver to solve the 3-Towers problem:
a) Write a function pair2int(r,c)
which maps (1,1), (1,2), ..., (3,3) to 1 to 9
using the formula 3*(r-1)+c.
b) Write nested for-loops that go through
all positions on the board from (1,1) to (3,3)
and produces clauses that represent attacks.
c) Write a for-loop that produces clauses that
specify that all 3 rows contain a tower
We are expected to write clauses in Conjunctive normal form as far as I understand. Could also be done directly in DIMACS
So I have done the a part, but I simply don't understand how I am supposed to express attacks or even what an attack constitutes, exactly.
This is the part of the program that I have done (a):
def pair2int(r):
return [3*(p[0]-1)+p[1] for p in r]
print(pair2int([(1,1),(1,2),(1,3),(2,1),(2,2),(2,3),(3,1),(3,2),(3,3)]))
which simply returns a list of positions 1-9 on a 3x3 board:
[1,2,3,4,5,6,7,8,9]
I don't understand what I am supposed to do with this.
Can somebody push me in the right direction?
The three towers (rooks) have to be put on the chessboard without attacking eachother. Towers can either move horizontally (within a row) or vertically (within a column). Therefore, towers attack eachother if they are put on the same row or on the same column.
A problem solution consists of a set of coordinates (1..3;1..3) or cell numbers (1..9) of the three towers.
A possible binary encoding for tower coordinates consists of four binary variables per tower:
row (encoded with two bits)
column (encoded with two bits)
As alternative, you could encode the cell number 1..9 per tower. This also would require four bits per tower or 12 bits in total.
The constraints to be expressed as clauses:
All towers must have valid coordinates within the chessboard
No pair of towers shares the same row (i.e. one tower per row)
No pair of towers shares the same column (i.e. one tower per column)
3 towers is a simplification of the n queens problem.
Example solution for 8 rooks (Wikipedia):
I'm looking for help with a workflow that, given an arbitrary number of points in a (x, y) plane, can find all line segments that connect 2 (and only 2) points. My first thought is to use scipy.spatial.distance.pdist() to get all unique pairwise distances, then for each distance between two points, test whether any of the other points are collinear. I can imagine that approach becoming inefficient with large numbers of points, so I want to know if anyone knows of a more efficient way to solve this problem.
The question is unwieldy, so I'll provide an example. I have a list of 3 (x, y) coordinate points coords = [(1,1), (2,1), (3,1)] that happen to form a straight line, and want to find the lengths of all line segments that connect 2 (and only 2) points. For this example, I only have two line segments of length 1 that meet my criteria (from (1,1) to (2,1), and (2,1) to (3,1)). I can get all the pairwise distances between points using scipy.spatial.distance.pdist(), but that function's result includes the distance between (1,1) and (3,1), which I don't want.
My preferred language is Python, but I'm open to answers in other coding languages.
In N (~ 500) dimensions, I wish to find out the largest sphere or rectangle such that the sphere/rectangle does not contain already existing points. The entire set of points is bounded in an axis-aligned rectangular box (lower and upper bounds on the values).
Is there any known polynomial time method/code that I can use to solve my problem?
The two well known algorithms: i) the largest empty rectangle within a rectangle (http://www.cs.princeton.edu/~chazelle/pubs/ComputLargestEmptyRectangle.pdf) and, ii) finding largest empty circle within location constraints (http://www.cs.dartmouth.edu/reports/TR86-130.pdf) do not work.
Although the complexity of above algorithms is N log N or N^2 log N, where N is the number of already existing points, the complexity is also a linear function of the number of vertices of the convex hull or the bounding polygon. A rectangle in 500 dimensions will have 2^500 corners which makes the above techniques infeasible.
Ideally, I am looking for a method (it does not have to be exact) that can determine the largest cirle/rectangle in polynomial time in N (number of points) and D (the dimension).
Thank you.
One possible heuristic solution is to restrict the large rectangle so that it's axis-aligned. In this case, a rectangle can be bounded by at most 2 * d points, where each point represents a bounding min or max to one or more dimensions. It can be then be determined if a point is inside that rectangle in only O(d).
A heuristic method to make use of this is to pick 2 points, and use those to form an initial bounding box, then randomly add points. If the point is inside the box, shrink or split the box. If the point is outside of the box, try to make the box bigger. A single pass should take O(d * N) if the box is shrunk instead of split.
The quality perhaps can be improved a bit by ensuring that no point is within the bounding box formed by the two points. It might be ideal to find all pairs of points such that no point is within the bounding box, as when converted to a graph, they should help with expanding the solution in a less random way. Dynamic programming likely leads to an algorithm that is better than O(d*N^3) perhaps O(d*N^2) or better.
I just wonder how can I determine if a given 3D point lies on a line given by 2 points, and ALSO it lies between those 2 points?
Points a,b,c are collinear (all lie on a single line) if b-a,c-a are parallel, which is true iff the cross product (b-a) x (c-a) is zero. (That is, all three of its components are zero. You should probably actually permit them to be nonzero but very very small; exactly what that should mean will depend on your application.)
Given that points a,b,c are collinear, b lies between a and c iff the scalar product (b-a).(c-b) is positive. (Non-negative, if it's OK for b to be coincident with a or c.)
Idealy I want to supply a sequence of points and have a line drawn at a right angle at every point (starting at the second point).
The direction of each line would alternate, so if I happened to draw a curve cosisting of 6 points, a line of a given lenth would be drawn for each point starting with the second point, i.e 5 additional lines on alternating sides of the curve, a bit like a caterpillar with alternating legs.
(I understand that the lines won't be entirely at right angles to the curve but rather at right angle to the line formed by any two points on the curve).
It's a question of vector mathematics. You can calculate the directing vector between two points A and B by subtracting A from B. In 2D and only in 2D the vector right angled to this vector can be obtained by reversing x and y component and taking one component negative. If you negate the new x component you'll make a left turn, by negating y you'll make a right turn. You can then reduce the directing vector to unit size (= of length 1) by dividing each component by the length of the vector (sqrt(xx + yy)). Finally you can stretch the unit vector again by your desired length and have one of the size you want. If you add this vector to either A or B you'll get a point to which you want to draw your line.
Here's a little math help:
These are points A and B expressed as vector.
The directing vector is calculated by a simple subtraction.
The normal vector is given by flipping the directing vector, that is to reverse the components and make one component negative. nl = normal, flipped to the left, nr = normal, flipped to the right
The unit vector of the normal vector is given by dividing each component by the length of the vector.
Calculates the length of a vector
If you want to draw a line from B to the left (when coming from A) you calculate the point P to draw the line to as
So you want to alternate that one time you draw to the left and one time to the right when iterating over the points.
If you have points lying outside your canvas, then you length is probably too large. You can of course calculate the point at which the vector to P would cross the boundary by calculating the intersection point of the vector BP and the border.