I want to find the shortest path between two vertices with an additional constraint : max n vertices can be visited. The graph is directed, connected, non negative weights and may contain cycles.
Example:
Shortest path 0->2 with n = 2 is 18
Shortest path 0->3 with n = 3 is 22
Shortest path 0->3 with n = 4 is 9
So far I've implemented Djikstras algorithm to get the simple shortest path, and my idea was to keep a counter of the current vertices visited, if it exceeds n it takes one or more steps back and tries with another path.. but as far as I know Djikstras can't be used for backtracking as explained here.
Another idea is somehow to store every path between every node in a table. But I'm not really sure how Djikstra can discover the path 0->2 with weight 18 since it is not really a shortest path...
Does anyone have any ideas how to tackle this problem?
Divided each vertices into n vertices, that is, for vertices u, we create n vertices expressed as (u, 1) ... (u, n), the second number shows the number of steps to this vertices. For each edge from u to v, we create an edge from (u, i) to (v, i+1) where 1<=i<=n-1 in new graph. Now if you want to calculate the shortest path between u and v with n, just do Dijkstra from (u, 1), then your answer is min(result (v, i) | 1<=i<=n)
The total number of vertices can be n*n, so the complexity is about O(n^2*log(n^2))
Let COST_TO(v,n) be the total weight of the minimum path to vertex v with n edges or less.
When n=0, the answer is easy:
for all v, COST_T(v,0) = 0 if v is the source vertex and infinity otherwise
For n>0, COST_TO(v,n) is the minimum of COST_TO(v,n-1) and all COST_TO(w,n-1)+WEIGHT(w,v), where there is an edge from w to v
so, for n = 0 to N, keep track of all the vertices with COST_(v,n) < infinity along with their costs, and calculate the costs for n from the values for n-1.
At the same time you can keep track of the minimum weight path to each v -- every time you update the cost to v with the edge rule, the new path to v is the path to w plus that edge. A reverse-singly-linked list is handy for this.
Let's assume that you want to find the shortest path from the source vertex S to a destination vertex T consisting of at most K edges. I picked K, because the n in your question is misleading - if you want to find the shortest path visiting at most n vertices, where n is the total number of vertices, you can just run Dijkstra, because any simple path has at most n vertices - I assume that this is not what you want here.
Then, if you want a simple implementation, Bellman-Ford is a good choice here:
After the i-th iteration of the outer loop, the algorithm has computed the shortest path from the source vertex S to any other vertex consisting of at most i edges, so it consists of at at most i + 1 vertices. So in order to solve your problem, run K - 1 outer loops of Bellman-Ford, and check if the distance to the destination vertex T is well defined (is different than infinity), and if it is, you have your result. Otherwise, T is not reachable from S in visiting K or less vertices.
Maybe try a bfs and check the number of vertices against the max. Save the best of the ones that fulfill the constraints.
Heres a video about it
https://youtu.be/TvHV3PB8ANs
Nist.gov has some algos as well.
Related
Recently I see this question Bellman Ford and Some Facts as follows:
We know the bellman-ford algorithms check all edges in each step, and for each edge if, d(v)>d(u)+w(u,v) was hold then d(v) being updated. w(u,v) is the weight of edge (u, v) and d(u) is the length of best finding path for vertex u. if at any step there is no update for any vertexes, the algorithm terminate.
for finding all shortest path from vertex s in graph G with n vertexes this algorithm terminate after k < n iteration.
The following fact is true.
number of edges in all shortest paths from s is at most k-1
in this Book we have 3 implementation (some optimization) of BFord. My question is that if we have
simultaneously relaxation which algorithm of these should be used, and by using it the above fact should be true? or not in general the above fact is true?
The final algorithm, BellmanFordFinal(s) is the optimised version of all the 3 mentioned algorithms.
OPTIMIZATION 1:
In the book, the legendary Prof. Jeff Erickson has explained, how the original algorithm presented by Bellman is optimised by removing the indentation of the last 3 lines in the algorithm.
Because the outermost iteration considers each edge u->v exactly once, the order in which they get processed, does not matter.
OPTIMIZATION 2:
The indices i-1 has been changed to i in the last 2 lines, which enables the algorithm to work more faster with correct computation of the values (shortest path).
OPTIMIZATION 3:
The 2 dimensional DP array is converted to 1 dimension as it was needless.
Therefore, use the final algorithm, titled, BellmanFordFinal(s).
The fact that we need to run the outermost loop for N-1 times is always true and independent of any implementation, because, the longest shortest path from a source to destination will be N-1 in case of the linear graph, where N is the number of the nodes in the graph.
Answer to your concern about k-1
number of edges in all shortest paths from s is at most k-1
The above statement is dependent on the implementation of your algorithm.
If you add a if condition to break the outermost loop when none of the edges are further relaxed, then this statement is false.
Otherwise it is true.
Have a look at the following implementation I found on https://cp-algorithms.com/graph/bellman_ford.html:
void solve()
{
vector<int> d (n, INF);
d[v] = 0;
for (;;)
{
bool any = false;
for (int j=0; j<m; ++j)
if (d[e[j].a] < INF)
if (d[e[j].b] > d[e[j].a] + e[j].cost)
{
d[e[j].b] = d[e[j].a] + e[j].cost;
any = true;
}
if (!any) break;
}
// display d, for example, on the screen
}
The variable any is used to check if their is any relaxation done in any of the iteration. If not done, break the loop.
Therefore, it might happen that the loop gets terminated before, for example, when k=2 and the number of edges in the longest shortest path is 3. Now, 3 <= 2-1, does not hold correct.
I'm wondering if A* search is suitable for the following situation:
There exist adjacency matrix for graph G (m x n)
Blocked cell is marked by 0
Non-blocked cell is marked by some value (let's call this prize!)
The goal is to find a Path from start to end but given multiple options to move vertical/horizontal, always choose cell with the largest prize.
Would this mean that the f(n) = g(n) + h(n) check would differ than the regular A* algorithms? Or perhaps given multiple f(n)'s (neighbors) with the same minimum value, choose the neighbor with the highest cell-value?
Would that skew the A* algorithms accuracy?
I was given a graph, including a start vertex, other vertices, and edges represent the costs going from one vertex to another. I need to find the set of destination vertices that I can travel to from the start vertex. The budget is a certain amount of dollars and the travel total cost should be within the budget. How can i implement the Dijkstra's algorithm to this problem? I think we usually use Dijkstra to find the shortest path between two fixed vertices before. But I am not sure how to implement Dijkstra on this budget problem. If someone can give some ideas, that really helps!
To my undersatanding, Dijkstra's algorithm solves the single-source shortest path problem. This means that the resulting data structure is a tree rooted at the starting node. Typically, an implementation would store the minimum cost to reach each node from the starting node. In total, the set of vertices that can be reached within the budget are the ones to which the cost of the shortest path does not exceed the budget. The algorithm itself does not need to be modified; in an additional step, the nodes which can be reached within budged has to be returned as the output.
If you use Dijkstra algorithm, you may end up with the below scenarios:
Assuming budget of 50 and the graph of 4 nodes (start, node 1, node 2, node 3)
Start Node -> node 1 (15) -> node 2 (10): so total cost is 25
Start Node -> node 3 (15): total cost is 15
So now what is your expected result? You should go to node 1 then node 2 and ignore node 3 (since you cannot return back to start then go to node 3, the return will cost 30 too). Or you should go to node 1, back to start, then go to node 3 (total cost of 45, the largest cost you can utilise)
What you need is not the shortest path covering all destinations which is Floyd-Warshall algorithm https://en.wikipedia.org/wiki/Floyd–Warshall_algorithm
I have some troubles implementing Dijkstra algorithm in Java.
I use this (first) pseudocode: http://en.wikipedia.org/wiki/Dijkstra%27s_algorithm
Line 15 you need to get the vertex with the lowest distance.
But how can I save the distance with the according distance.
Note: vertex is defined as an Integer.
My solutions that didn't work properly:
Map with K = vertex, V = distance, Problem: long searching to get min dist
SortedMap with K = distance, V = vertex, Problem: almost every distance is defined as Integer.MAX_VALUE
So I am looking for a fast way to save a vertex to a distance and it should be easy to get the vertex with min dist.
Try using a PriorityQueue. That way, you can simply remove the head, as it would have the minimum distance of all vertices.
See more information about PriorityQueue here:
http://docs.oracle.com/javase/7/docs/api/java/util/PriorityQueue.html
How to decide heuristic cost for the cities connected with roads problem. The graph has non negative weighted unidirectional edges and no edge connects any vertex to itself. In this graph, there is only one edge between any two vertices. My aim is to get the shortest distance between single source and single destination.
If your edges lie in a Euclidian plane, your vertices correspond to roads, and the vertex cost is the length of the road, then the Euclidian distance or L2 norm is a good choice for the heuristic cost.
Here's why. But first, some quick terminology:
Let f(x) be the path cost, the calculated shortest distance from the start node to node x.
Let h(x) be the heuristic cost, an estimate of the distance to the goal from node x.
Because A* is a directed best-first search algorithm. At each step it moves to the node which minimizes h(x) + f(x) (and calculating h(x) requires that we have a goal node in mind).
For this approach to be guaranteed to find the correct shortest patch distance between the start and end nodes, h(x) must be an admissible heuristic. This essentially means that it must not overestimate the distance to the goal node.
Therefore, if your nodes are organized on a Euclidian plane, and your costs correspond to the L2 norm distance between the nodes, then the Euclidian distance or L2 norm between the current node x and the goal node is guaranteed to be an admissible heuristic (it's the shortest possible path between the two nodes, so any actual path along a series of vertices in your graph must be longer).
As a bonus, it's informative to note that Dijkstra's Algorithm is simply a special case of A* with h(x) = 0. For any node, we assume that the path to the goal is 0, which means we simply take the smallest possible step. This is certainly an admissible heuristic because the distance between any two nodes cannot be less than 0 (if we're assuming non-negative edge costs).
If you're trying to optimize for distance travelled, euclidean distance is a good baseline heuristic.
If you're given a weighted graph, use the edge weight as if it's the length of the segment. The shortest path in a weighted graph is the path with the lowest combined edge weight.