Euclidean TSP is known to be NP-complete.
In my special metric, the distance between A and B is defined as:
from A to B = max(x coordinate of A , y coordinate of B);
from B to A = max(x coordinate of B , y coordinate of A).
Is this still NP-complete?
Yes. The calculation of the cost function is not what makes the TSP NP-complete.
The difference between your formulation and the "standard" TSP is that the cost
differs depending on the direction you are traveling. That is cost(i,j) != cost(j,i).
Costs are usually represented as a matrix for easy look up and the symmetry lets you halve the size of the cost matrix. Your formulation requires the matrix to be completely filled in. The generation of the cost matrix is still only O(n^2).
For an exact answer you will still need to brute-force your answer (with the number of possibilities == the number of permutations of "cities" O(n!)) or use a fancy algorithm like a SAT solver.
Related
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.
I have a strongly connected digraph with edges weighted with vectors, where each vector has only non-negative entries. I want to find a cycle such that the angle between the sum of the weights and a diagonal vector ([1, 1, 1, ... 1]) is minimized. Are there any algorithms out there for this sort of thing?
I'm fairly confident a Bellman-Ford type algorithm would give me a fairly good solution, but I'm not convinced that it will be the -best-...
Since arcs can be used multiple times, this problem can be formulated as a quadratic program. If the instance is not large, then it might be worthwhile to try one of the solvers that Wikipedia links to.
Let's take feasible solutions to be circulations x, i.e., positive linear combinations of simple cycles. Let A be the matrix representing the linear map from arcs to vectors. There's one trick, proved with a little algebra: instead of minimizing the angle of Ax relative to the all-ones vector, we minimize the length of Ax subject to the constraint that the dot product of Ax and the all-ones vector be 1.
Now we can write down the quadratic program.
minimize y1^2 + ... + yn^2 (positive semidefinite objective)
subject to
Ax - y = 0
x is a circulation
The last constraint breaks down as the linear constraints x >= 0 and, for each vertex, that the sum of x values on arcs entering the vertex equals the sum of x values on arcs leaving.
as far as I understand the Bellman-Ford Algo it seems, that Dijkstra Shortest Path Algo does the same. But it's way faster couse of its greedy behaviour.
Edit: Dijkstra only works with non-negative entries. But that would fit your Problem
Consider two cycles that share a common vertex. It's totally possible that there are positive integers p and q so that p times the vector for the first cycle added to q times the vector for the second cycle exactly equals a multiple of (1,1,...,1). Thus, unless you restrict to simple cycles, I don't think there is a fast algorithm for you that's provably optimal. Even if you restrict to simple cycles though, you could have part of a cycle equal to a vector x and the rest of the cycle equal to a vector c(1,1,...,1) - x, and you'd probably have no way to know this unless you enumerated all cycles and just check them. Thus I think brute force enumerating cycles may be the only feasible way to approach your problem if you want an optimal solution.
I have a total undirected graph where nodes represent points on points on a plane, and edges are approximate euclidean distances between the points. I would like to "embed" this graph in a two dimensional space. That is, I want to convert each vertex to an (x,y) position tuple so that for any two two vertices v and w, the edge (v,w) has weight close to dist(v,w).
For example, if I had the graph with nodes A, B, C, and D and edges with weights (A,B): 20; (A,C): 22; (A,D): 26; (B, C): 30; (B, D): 20, (C, D): 19, then you could assign the points A: (0,0); B: (10, 0); C: (0, 10); D: (10, 10). Clearly this is imperfect, but it is a reasonable approximation.
I don't care about getting the best possible solution, I just want a reasonable one in a reasonable amount of time.
(In case you want the motivation for this problem. I have a physical system where I have noisy measurements of distances from all pairs of points. Distance measurements are noisy, but tend to be within a factor of two of the true value. I have made all of these measurements, and now have a graph with several thousand nodes, and several million edges, and want to place the points on a plane.)
You may be able to adapt the force-based graph drawing algorithm for your needs.
This algorithm attempts to find a good layout for an undirected graph G(V,E) by treating each vertex in V as a Cartesian point and each edge in E as a linear spring. Additionally, a pair-wise repulsive force (i.e. Coulomb's law) is calculated between vertices globally - this prevents the clustering of vertices in Cartesian space that are non-adjacent in G(V,E).
In your case you could set the equilibrium length of the springs equal to your edge weights - this should give a layout with pair-wise Euclidean vertex distances close to your edge weights.
The algorithm updates an initial distribution (possibly random) in a pseudo-time stepping fashion based on the sum of forces at each vertex. The algorithm terminates when an approximate steady-state is reached. A simplified pseudo-code:
while(not converged)
for i = vertices in V
F(i) = sum of spring + repulsive forces on ith vertex
endfor
Update vertex positions based on force vector F
if (vertex positions not changing much)
converged = true
endif
endwhile
There are a number of optimisations that can be applied to reduce the complexity of the algorithm. For instance, a spatial index (such as a quadtree) can be used to allow for efficient calculation of an approximate repulsive force between "near-by" vertices, rather than the slow global calculation. It's also possible to use multi-level graph agglomeration techniques to improve convergence and optimality.
Finally, note that there are several good libraries for graph drawing that implement optimised versions of this algorithm - you might want to check out Graphviz for instance.
For starters, I think I'd go for a heuristic search approach.
You actually want to find a set of point p1,p2,...,p_n that minimizes the function:
f(X) = Sum (|dist(p_i,p_j) - weight(n_i,n_j)|) [for each i,j ]
The problem can be heuristically solved by some algorithms including Hill Climbing and Genetic Algorithms.
I personally like Hill Climbing, and the approach is as follows:
best <- [(0,0),(0,0),...,(0,0)]
while there is still time:
S <- random initialized vector of points
flag <- true
while (flag):
flag <- false
candidates <- next(S) (*)
S <- X in candidates such that f(X) <= f(Y) for each X in candidates (**)
if f(S) was improved:
flag <- true
if f(S) <= f(best):
best <- S
return best
(*) next() generates a list of candidates. It can utilize information about gradient of function (and basically decay into something similar to gradient descent) for example, or sample a few random 'directions' and put them as candidates (all in the multi-dimensional vector, where each point is a dimension).
(**) In here, you basically chose the "best" candidate, and store it in S, so you will continue with it in next iteration.
Note, the algorithm is any-time, so it is expected to get better the more time you have to give it. This behavior is achieved by the random initialization of starting point - which is likely to change the ending result, and by the random selection of points for candidates.
This is a part of a self formulated question, and hence I have not been able to "Google" it and my own attempts have been futile till now.
You are given a graph G(V,E) each Node of V has a profit wi, each Edge of E has a cost of ci. We are now given a budget C, what is required to be found is a single path such that the sum of costs is less than C where sum of wi is maximum.Path has the normal definition here that is a path will not contain repeating vertices (simple path).
It is obvious that Hamiltonian path is a special case of this(Setting cost = |N-1| and the cost of each edge=1), and hence this is an NP Hard problem, so I am looking for approximation solutions, and heuristics.
Mathematically
Given Graph G(V,E)
ci >=0 for each edge e
wi >=0 for each vertex v
find a simple path P such that
Sum ci over all edges e in P <= C
Maximise Sum wi for all v in P
This is known as the Selective Travelling Salesman Problem, or Travelling Salesman with profits. Google Scholar should be able to give you some references. Metaheuristics such as genetic programming or tabu search are often used. If you want to solve the problem optimally, linear programming techniques would probably work (unfortunately, you don't state the size of the instances you're dealing with). If the length of the path is small (say 15 vertices), also color-coding might work.
One simple heuristic that cones to mind is a variation of stochastic hill climbing and greedy algorithm.
Define value function that is increasing in the weight and decreasing with the cost. For example:
value(u,v) = w(v) / [c(u,v) + epsilon]
(+ epsilon for the case of c(u,v) = 0)
Now, the idea is:
From a vertex u, proceed to vertex v with probability:
P(v|u) = value(u,v) / sum(u,x) [ for all feasible moves (u,x) ]
Repeat until you cannot continue.
This solution will give you one solution - quickly, but it is probably not near optimal. However - it is stochastic - you can always re-run it again and again, while you have time.
This will give you an anytime algorithm for this problem, meaning - the more time you have - the better your solution is.
Some optimizations:
You can try to learn macros to accelerate each search, which will result in more searches for each amount of time, and probably - better solutions.
Usually, the first search is not stochastic, and is purely greedy, following the max{value(u,v)}
This is an exercise from CLRS 24.4-12,(not homework, I just try to solve the all the exercise in CLRS)
Give an efficient algorithm to solve a system Ax ≤ b of difference constraints when all of the elements of b are real-valued and a specified subset of some, but not necessarily all, of the unknowns xi must be integers.
If all the xi are integers, we can let b = floor(b) and using Bellman-Ford algorithm finding the shortest path to solve the problem in a constraint graph, but how about some of xi are integers and some not? It is similar to integer programming problem, but integer programming is NP-hard, This question has less constraints, is there an more efficient algorithm?
First find the element with minimum absolute value in vector b , multiply it by C to make all of the values in vector of b integer (e.g. if the minimum absolute value is 3.102, we can multiply vector b by 1000), then solve it by bellman-ford algorithm , and finally divide it by C !
You have similar things in linear optimization - the only difference is that you have no minimum or maximum requirement. Maybe you can adapt some of the methods. Starting from a real-valued solution, there is the Gomory algorithm adding additional constraints to force the integer xi to become integer, and there are Branch-and-Bound algorithms, trying to exclude big parts of the search space as soon as possible.
Well. Dude, this is an exercise of Introduction to Algorithm. My own way is to adapt Bellman-Ford algorithm, which is to basically construct a directed weighted graph based on the inequalities and then when relaxing the edges, only allow integers to be the key value of the node. I think it's gonna work.