What algorithm should I use if in a directed graph I want to find the shortest paths, having 2 starting vertices, so that the paths meet and both have the minimum distance for this to happen.
I would do an All-Pairs-Shortest-Path, find common endpoints, and then find the minimum of (distance(vertex1,endpoint)+distance(vertex2,endpoint)) for all possible endpoints
I would use Dijkstra's to get minimal distance trees for both starting vertices, then add the two distance vectors together and take the minimum. C.B.'s answer is fine, but you don't need the shortest distances between all pairs, so there's some redundant work there.
Related
I have a weighted directed graph. I'm defining a "spanning walk" here as a walk through the graph that visits each vertex at least once, with no limitations on what edges need to be traversed. I'm trying to find the shortest of such walks, if one exists.
Here's an image of an example graph, whose shortest spanning walk is c->d->b->a, with a length of 6
I tried looking into Hamiltonian paths and the Traveling Salesman Problem and such, but as I'm allowed to visit a vertex multiple times in my problem, I don't know if I can make much use of it. I'm almost completely convinced the problem is NP-hard, but wondering if there's anything to make a search not take centuries for larger graphs.
Suppose we have a set of N points on the cartesian plane (x_i and y_i). Suppose we connect those points with lines.
Is there any way like using a graph and something like a shortest path algorithm or minimum spanning tree so that we can reach any point starting from any point but minimizing the total length of the lines??
I though that maybe I could set the cost of the edges with the distance of a graph and use a shortest path algorithm but I'm not sure if this is possible.
Any ideas ?
I'm not 100% sure what you want, so I go for two algorithms.
First: you just want a robust algorithm, use dijkstras algorithm. The only challange left is to define the edge cost. Which would be 1 for neighboring nodes, I assume.
Second: you want to use heuristics to estimate the next best node and optimize time consumption. Use A*, but you need to write a heuristic which under estimates the distance. You could use the euclidean distance to do so. The edge problematic stays the same.
My problem is a generalization of a task solved by [Blossom algorithm] by Edmonds. The original task is the following: given a complete graph with weighted undirected edges, find a set of edges such that
1) every vertex of the graph is adjacent to only one edge from this set (i.e. vertices are grouped into pairs)
2) sum over weights of edges in this set is minimal.
Now, I would like to modify the first goal into
1') vertices are grouped into sets of 3 vertices (or in general, d vertices), and leave condition 2) unchanged.
My questions:
Do you know if this 'generalised' problem has a name?
Do you know about an algorithm solving it in number of steps being polynomial of number of vertices (like Blossom algorithm for an original problem)? I don't see a straightforward generalisation of Blossom algorithm, as it is based on looking for augmenting paths on a graph compressed to a bipartite graph (and uses here Hungarian algorithm). But augmenting paths do not seem to point to groups of vertices different than pairs.
Best regards,
Paweł
I'm trying to the find the k-shortest paths between a pair of vertices in a directed graph, with negative weights allowed. As far I know, Eppstein's algorithm and Yen's algorithm only work with positive weights.
I know bellman-ford can be used to find the shorest path with with negative edges but I'm also trying to find the second shortest, third shortest, etc.
I want to project an undirected graph into the 2d plane such that:
the euclidean distance preserves the stepwise distance (i.e. if the shortest path between A and B is shorter than the shortest path between C and D, then the euclidean distance between A and B is less than the euclidean distance between A and B)
the minimum difference between the euclidean distance and the stepwise distance is minimized. Ideally the set of solutions is generated or described if there is not a unique minimum.
If this is not possible, what are the most minimal sets of constraints on the graph that make it possible? I'm interested in the question in general, although at the moment I want it for a finite lattice with its minimum removed.
It's called graph embeddng. There's even a theorem that gives an upper limit to the distortion. The embedding algorithm that I like the most is SDE. It's fairly easy to implement on any language if you have a SDP library.
Here's an algorithm that's a bit simpler.
I think the first requirement is impossible, at least for the general case. Consider a fully-connected graph consisting of four nodes, with all path-lengths equal. It's not possible to choose four points in 2D Euclidean space that exhibits the same property (other than 4 coincident points).
See Diego's answer for some useful information (my knowledge of graph theory is very limited!).