Graph theory: shortest path with vector weights - algorithm

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.

Related

Efficient approximate algorithm to determine the presence of k-sized cycle in graph

I have a very large sparse graph G (about 100 million nodes, about 50 million edges) and I would like to find an efficient algorithm (hopefully O(1) or sub-linear in the number of nodes + edges) that predicts with some probability the presence of a cycle of length k in this graph. For practical use, k will very small (between 30 and 90) relative to the size of G. It is also guaranteed that k will always be even. G is also a random graph, so I don't expect any consistent clustering.
The algorithm doesn't need to enumerate the actual nodes contained in the cycle, it just needs to eliminate G if it most likely don't have any cycles of length k.
I found a close solution with the answer presented here, where the trace and rank of L (where L is the Laplacian of G) could be compared to determine whether G had any cycles at all. However, I couldn't find a relatively efficient way to compute rank for G. Another problem was that it doesn't take k into account, which might be able to make a more efficient approach.
Getting connected components is a possibility, but it is linear in the number of nodes + edges, which is not optimal for a graph of this size.
If it's an Erdos--Renyi random graph, then since having such a cycle is a monotone property of a graph, there's a zero-one law (https://www.ams.org/journals/proc/1996-124-10/S0002-9939-96-03732-X/S0002-9939-96-03732-X.pdf), which implies that you can make a reasonably good guess by setting the right threshold. (Which threshold? I don't know offhand, but probably you can extrapolate from smaller graphs.)

Find a subset of k most distant point each other

I have a set of N points (in particular this point are binary string) and for each of them I have a discrete metric (the Hamming distance) such that given two points, i and j, Dij is the distance between the i-th and the j-th point.
I want to find a subset of k elements (with k < N of course) such that the distance between this k points is the maximum as possibile.
In other words what I want is to find a sort of "border points" that cover the maximum area in the space of the points.
If k = 2 the answer is trivial because I can try to search the two most distant element in the matrix of distances and these are the two points, but how I can generalize this question when k>2?
Any suggest? It's a NP-hard problem?
Thanks for the answer
One generalisation would be "find k points such that the minimum distance between any two of these k points is as large as possible".
Unfortunately, I think this is hard, because I think if you could do this efficiently you could find cliques efficiently. Suppose somebody gives you a matrix of distances and asks you to find a k-clique. Create another matrix with entries 1 where the original matrix had infinity, and entries 1000000 where the original matrix had any finite distance. Now a set of k points in the new matrix where the minimum distance between any two points in that set is 1000000 corresponds to a set of k points in the original matrix which were all connected to each other - a clique.
This construction does not take account of the fact that the points correspond to bit-vectors and the distance between them is the Hamming distance, but I think it can be extended to cope with this. To show that a program capable of solving the original problem can be used to find cliques I need to show that, given an adjacency matrix, I can construct a bit-vector for each point so that pairs of points connected in the graph, and so with 1 in the adjacency matrix, are at distance roughly A from each other, and pairs of points not connected in the graph are at distance B from each other, where A > B. Note that A could be quite close to B. In fact, the triangle inequality will force this to be the case. Once I have shown this, k points all at distance A from each other (and so with minimum distance A, and a sum of distances of k(k-1)A/2) will correspond to a clique, so a program finding such points will find cliques.
To do this I will use bit-vectors of length kn(n-1)/2, where k will grow with n, so the length of the bit-vectors could be as much as O(n^3). I can get away with this because this is still only polynomial in n. I will divide each bit-vector into n(n-1)/2 fields each of length k, where each field is responsible for representing the connection or lack of connection between two points. I claim that there is a set of bit-vectors of length k so that all of the distances between these k-long bit-vectors are roughly the same, except that two of them are closer together than the others. I also claim that there is a set of bit-vectors of length k so that all of the distances between them are roughly the same, except that two of them are further apart than the others. By choosing between these two different sets, and by allocating the nearer or further pair to the two points owning the current bit-field of the n(n-1)/2 fields within the bit-vector I can create a set of bit-vectors with the required pattern of distances.
I think these exist because I think there is a construction that creates such patterns with high probability. Create n random bit-vectors of length k. Any two such bit-vectors have an expected Hamming distance of k/2 with a variance of k/4 so a standard deviation of sqrt(k)/2. For large k we expect the different distances to be reasonably similar. To create within this set two points that are very close together, make one a copy of the other. To create two points that are very far apart, make one the not of the other (0s in one where the other has 1s and vice versa).
Given any two points their expected distance from each other will be (n(n-1)/2 - 1)k/2 + k (if they are supposed to be far apart) and (n(n-1)/2 -1)k/2 (if they are supposed to be close together) and I claim without proof that by making k large enough the expected difference will triumph over the random variability and I will get distances that are pretty much A and pretty much B as I require.
#mcdowella, I think that probably I don't explain very well my problem.
In my problem I have binary string and for each of them I can compute the distance to the other using the Hamming distance
In this way I have a distance matrix D that has a finite value in each element D(i,j).
I can see this distance matrix like a graph: infact, each row is a vertex in the graph and in the column I have the weight of the arc that connect the vertex Vi to the vertex Vj.
This graph, for the reason that I explain, is complete and it's a clique of itself.
For this reason, if i pick at random k vertex from the original graph I obtain a subgraph that is also complete.
From all the possible subgraph with order k I want to choose the best one.
What is the best one? Is a graph such that the distance between the vertex as much large but also much uniform as possible.
Suppose that I have two vertex v1 and v2 in my subgraph and that their distance is 25, and I have three other vertex v3, v4, v5, such that
d(v1, v3) = 24, d(v1, v4) = 7, d(v2, v3) = 5, d(v2, v4) = 22, d(v1, v5) = 14, d(v1, v5) = 14
With these distance I have that v3 is too far from v1 but is very near to v2, and the opposite situation for v4 that is too far from v2 but is near to v1.
Instead I prefer to add the vertex v5 to my subgraph because it is distant to the other two in a more uniform way.
I hope that now my problem is clear.
You think that your formulation is already correct?
I have claimed that the problem of finding k points such that the minimum distance between these points, or the sum of the distances between these points, is as large as possible is NP-complete, so there is no polynomial time exact answer. This suggests that we should look for some sort of heuristic solution, so here is one, based on an idea for clustering. I will describe it for maximising the total distance. I think it can be made to work for maximising the minimum distance as well, and perhaps for other goals.
Pick k arbitrary points and note down, for each point, the sum of the distances to the other points. For each other point in the data, look at the sum of the distances to the k chosen points and see if replacing any of the chosen points with that point would increase the sum. If so, replace whichever point increases the sum most and continue. Keep trying until none of the points can be used to increase the sum. This is only a local optimum, so repeat with another set of k arbitrary/random points in the hope of finding a better one until you get fed up.
This inherits from its clustering forebear the following property, which might at least be useful for testing: if the points can be divided into k classes such that the distance between any two points in the same class is always less than the distance between any two points in different classes then, when you have found k points where no local improvement is possible, these k points should all be from different classes (because if not, swapping out one of a pair of points from the same class would increase the sum of distances between them).
This problem is known as the MaxMin Diversity Problem (MMDP). It is known to be NP-hard. However, there are algorithms for giving good approximate solutions in reasonable time, such as this one.
I'm answering this question years after it was asked because I was looking for algorithms to solve the same problem, and had trouble even finding out what to call it.

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.

Embedding Graph in Euclidean Space

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.

How to calculate minimum time of travel between two places when a matrix containing the time of travel between each node is given

For n stations a n*n matrix A is given such that A[i][j] represents time of direct journey from station i to j (i,j <= n).
The person travelling between stations always seeks least time. Given two station numbers a, b, how to proceed about calculating minimum time of travel between them?
Can this problem be solved without using graph theory, i.e. just by matrix A alone?
You do need graph theory in order to solve it - more specifically, you need Dijkstra's algorithm. Representing the graph as a matrix is neither an advantage nor a disadvantage to that algorithm.
Note, though, that Dijkstra's algorithm requires all distances to be nonnegative. If for some reason you have negative "distances" in your matrix, you must use the slower Bellman-Ford algorithm instead.
(If you're really keen on using matrix operations and don't mind that it will be terribly slow, you could use the Floyd-Warshall algorithm, which is based on quite simple matrix operations, to compute the shortest paths between all pairs of stations (massive overkill), and then pick the pair you're interested in...)
This looks strikingly similar to the traveling salesman problem which is NP hard.
Wiki link to TSP

Resources