Finding maximum weight sequence of points in positive quadrant - algorithm

Given a sequence of weighted points in the positive quadrant we have to find
the maximum weight sequence of points so that each successive point is contained in the
rectangle formed by the previous point and the origin.
I am interested in a DP algorithm for this problem.

This problem is really asking for the longest increasing subsequence. An O(N log N) algorithm for solving this is described on the wikipedia page.
Easier O(N²) algorithm
I am assuming you have integer points. If you don't, you can use coordinate compression to place your points in an N x N grid.
So you have an two-dimensional number array W where each number is the weight assigned to that coordinate. You now have a recurrence:
// T(w,h) = "Maximum weight of the point sequence in sub-grid (w,h)"
T(0,0) = W(0,0)
T(0,y) = W(0,y)+T(0,y-1)
T(x,0) = W(x,0)+T(x-1,0)
T(x,y) = W(x,y)+max(T(x-1,y),T(x,y-1))
You can either memoize the recurrence T (O(N²) space) or compute it one row at a time (O(N) space). Both algorithms will use O(N²) time.
You can try computing this recurrence using pen and paper to see how it works.

Related

algorithm to compute the largest subset of L in which every pair of segments intersects

I came across this question in preparation for the final exam, and I could not find the recursive formula although I saw similar questions.
I will thank you for any help!
the problem is:
Suppose we are given a set L of n line segments in the plane, where the endpoints
of each segment lie on the unit circle x
2 + y
2 = 1, and all 2n endpoints are
distinct. Describe and analyze an algorithm to compute the largest subset of L in
which every pair of segments intersects
The solution needs to be an algorithm in dynamic programming approach (based on recursive formula)
I am assuming the question ("the largest subset of L...") is dealing with the subset size, and not that the subset cannot be extended. If the latter is true, the problem is trivial and the simple greedy algorithm works.
Now to your question. Following Matt Timmermans' hint (can you prove it?) this can be viewed as the longest common subsequence problem, except that we don't know what the 2 input strings are = where the splitting point between the 2 sequence occurences is.
Longest common subsequence problem can be solved in O(m*n) time and linear memory. By moving the splitting point along your 2n-length array you will create 2n instances of the LCS problem each of which can be solved in O(n^2) time, which yields the total time complexity of O(n^3).
Your problem is known as the maximum clique problem (with line segments corresponding to graph nodes, and line segments intersections corresponding to graph edges) of a circle graph and has been shown in 2010 to have a solution with O(n^2*log(n)) time complexity.
Please note that the maximum clique problem (the decision version) is NP-hard (NP-complete, to be exact) in the case of an arbitrary graph.

Making a cost matrix in graph

Problem :
Form a network, that is, all the bases should be reachable from every base.
One base is reachable from other base if there is a path of tunnels connecting bases.
Bases are suppose based on a 2-D plane having integer coordinates.
Cost of building tunnels between two bases are coordinates (x1,y1) and (x2,y2) is min{ |x1-x2|, |y1-y2| }.
What is the minimum cost such that a network is formed.
1 ≤ N ≤ 100000 // Number of bases
-10^9 ≤ xi,yi ≤ 10^9
Typical Kruskal's minimum spanning tree implementation.But u cannot store (10^5)^2 edges.
So how i should make my cost matrix , how to make a graph so i can apply Kruskal algorithm?
You should not store the whole graph as you don't actually need it. In fact in this case I think Prim's algorithm is more suitable in this case. You will not need all the edges at any single time, instead on each iteration you will update a min dist array of size N. Of course complexity will still be in the order of N**2 but at least memory will not be an issue. Also you can further use the specific way distance is computed to improve the complexity(using some ordered structure to store the points).
I believe the only edges that will ever be used (due to your cost function) will be from each base to at most 4 neighbours. The neighbours to use are the closest point with greater (or equal) x value, the closest point with smaller (or equal) x value, the closest point with greater (or equal) y value, the closest point with smaller (or equal) y value.
You can compute these neighbours efficiently by sorting the points according to each axis and then linking each point with the point ahead and behind it in sorted order.
It does not matter if there is more than one point at a particular value of coordinate.
There will therefore be only O(4n) edges for you to consider with Kruskal's algorithm.

Algorithm for maximal hypervolume simplex

Given a set of points in D-dimensional space. What is the optimal algorithm to find maximal possible D-simplex, all the vertexes of which is in the set? Algebraically it means that we have to find a subset of D + 1 points such, that determinant of D * D matrix, constructed from rows as deltas of coordinates each of first D points and last D + 1-st point, have greatest possible value (absolute value) on the set.
I sure, that all D + 1 required points are vertexes of convex hull of given set of points, but I need the algorithm, which not used any convex hull algorithm, because simplex required for they, in turn, required for such algorithms as starting polytope.
If it is not possible to obtain the simplex in less than exponential time, then what is the algorithm, which gives adjustable ratio run-time/precision of approximation for approximate solving of the problem?
I can't think of an exact solution, but you could probably get a reasonable approximation with an iterative approach. Note than I'm assuming that N is larger than D+1 here; if not then I have misunderstood the problem.
First, use a greedy algorithm to construct an initial simplex; choose the first two vertices to be the two most distant points, the next one to maximise your size measure in two dimensions, the next to maximise it in three, and so on. This has polynomial complexity in N and D.
One you have the initial simplex you can switch to iterative improvement. For example, for a given vertex in the simplex you can iterate through the points not in it measuring the change in the size measure that would result if you swapped them. At the end you swap it with the one, if any, that gave the greatest increase. Doing this once for each vertex in the simplex is again polynomial in N and D.
To trade-off betwen run-time cost and how large the resulting simplex is, simply choose how many times you're willing to do this.
Now this is a relatively crude local optimisation algorithm so cannot guarantee that it will find the maximal simplex. However, such approaches have been found to result in reasonably good approximations to the solution of problems like the travelling salesman problem, in the sense that whilst they're not optimal, they result in a distance that isn't too much greater than that of the actual solution in most cases.
Quickhull does not require to find a maximal simplex, this is overkill (too hard a problem, and will not guarantee that the next steps will be quicker).
I suggest you to select D+1 independent directions and take the farthest point in every direction. This will give you a good starting simplex in time O(N.D²). (The D² is because there are D+1 directions and evaluation of the distance in a direction takes D operations.)
Beware anyway that it can be degenerate (several vertexes being identical).
My own approximation of the solution is to take one point, compute furhtest from it and reject first point (totally N=1 point selected), then select else D - 1 points in such manner, that non-oriented N - 1-dimensional hypervolume (formula for S) of each N-points selection is maximal. Finally I find N = D + 1'st point it the way, that oriented D dimensional hypervolume (formula for V) of defined simplex is maximal by absolute value. Total complexity on my mind is something about O(D * N * D^3) (1...D + 1 vertices of simplex, N...N - D - 1 remaining points and D^3 is upper estimate of D * M, M in {1,2,...,D} matrix multiplication complexity). The approach allows us to find the right amount of linearly independent points, or else to find a dimension of the subspace and non-normalized and non-orthogonal basis of the subspace. For large amount of points and large dimensionalities the complexity of proposed algorithm does not predominate over the complexity of, say, quickhull algorithm.
The implementation's repository.

Calculate the maximum distance between vectors in an array

Assume we have an array that holds n vectors. We want to calculate the maximum euclidean distance between those vectors.
The easiest (naive?) approach would be to iterate the array and for each vector calculate its distance with the all subsequent vectors and then find the maximum.
This algorithm, however, would grow (n-1)! with respect to the size of the array.
Is there any other more efficient approach to this problem?
Thanks.
Your computation of the naive algorithm's complexity is wonky, it should be O(n(n-1)/2), which reduces to O(n^2). Computing the distance between two vectors is O(k) where k is the number of elements in the vector; this still gives a complexity well below O(n!).
Complexity is O(N^2 * K) for brute force algorithm (K is number of elem in vector). But we can do better by knowing that in euclidean space for points A,B and C:
|AB| + |AC| >= |BC|
Algorithm should be something like this:
If max distance found so far is MAX and for a |AB| there is a point C, such that distance |AC| and |CB| already computed and MAX > |AC|+|CB|, then we can skip calculation for |AB|.
It is difficult to tell complexity of this algorithm, but my gut feeling tells me it is not far from O(N*log(N)*K)
This question has been here before, see How to find two most distant points?
And the answer is: is can be done in less than O(n^2) in Euclidean space. See also http://mukeshiiitm.wordpress.com/2008/05/27/find-the-farthest-pair-of-points/
So suppose you have a pair of points A and B. Consider the hypersphere that have A and B at the north and south pole respectively. Could any point C contained in the hypersphere be farther from A than B?
Further suppose we partition the pointset into sqrt(N) hyperboxes with sqrt(N) points each. For any pair of hyperboxes, we can calculate in k time the maximum distance possible between any two points of the infinite set of points contained within them - by simply calculating the distance between their furthest corners. If we already have a candidate better than this we can discard all pairs of points from those hyperboxes.

Algorithm to find special point k in O(n log n) time

Give an n log n time lower bound for an algorithm to check if a set of points has a special point k.
k is defined as:
for a set A of points, if for every point m in A, there is a point q in A such that k is in the middle of the line segment mq such a k does not have to belong to A.
For example, this set has a special point k = (0.5, 0.5) for a set of four points (1,0), (0,1), (1,1), (0,0).
I was totally poker faced when they asked me this, nothing came to my mind. I guess it needs some strong geometrical background.
O(nlogn) solution (I'm still not clear why you're looking for a lower bound solution. You might as well just do an exhaustive check, and then just run an nlogn loop to make sure of the lower bound. Not very difficult. I think you must mean upper bound):
Find the only valid candidate point by averaging all the points. I.e. summing their co-ordinates and dividing by the number of points. If such a k exists, this is it. If no such k exists, we'll find that the point found is invalid in the final step.
Create a new array (set) of points, where we shift our axes so they centre on the point k. I.e. if k = (xk,yk), a point (x,y) will become (x-xk, y-yk). Sort the points according to the ratio x/y and the norm sqrt(x2+y2). As the next step shows, it doesn't matter how this sort is done, i.e. which is the main criterion and which the secondary.
We could search for each point's complement, or better, simply traverse the array and verify that every two adjacent points are indeed complements. I.e. if this is a solution then every two complementary points in this new array are of the form (x,y) and (-x,-y) since we re-centered our axes, which means they have the same ratio ("gradient") and norm, and after the sort, must be adjacent.
If k is not valid, then the there is a point who we will arrive at in this traversal, and find that it's neighbour is not of the right/complementary form ==> there is no such k.
Time =
O(n) for finding the candidate k +
O(n) for building the new array, since each new point can be calculated in O(1) +
O(nlogn) for the sort +
O(n) for the verifying traversal
= O(nlogn)
I'd say you just compute the center of mass (having removed duplicates first) and check if it is your k. Probably the only thing that cause it to be O(n log n) would be searching for a point at specified location.

Resources