Finding Shortest path in weighted directional multigraph with extra constraints - algorithm

Given a weighted directed multigraph, I have to find shortest path between starting vertex u to vertex v. Apart from weight, each edge also has time. The path connecting u and v cannot take more than a given maximum time. The trouble is while using Djikstra, there are chances that shortest path takes more time than the limit.
My approach is to find all valid paths between u and v and than minimize the weight. But the approach is not practical due to its high complexity.
Any ideas?

If weights are small enough
In this case, what you can do is for each node, store all possible sums of weights that you can get on path to that node. Now you can do dijsktra on this new graph and try to minimize time over nodes which are pairs (node, weight_sum).
If times are small enough
You can do the same as in previous example, but over pairs (node, time).
General problem
I'm afraid that in general all you can do is try all possible paths, try to improve it with prunning.

Related

Given a weighted undirected graph, how do I find a path which has the total weights close to a given value?

Suppose I have a weighted, undirected graph. Each edge has a positive weight. I would like to find a simple path (no vertices appear in the path twice) from a given source node (s) to a target node (t) which has the total sum of weights close to a given value (P).
Even though it sounds like a well-studied problem, I couldn't find a satisfying solution. Many graph algorithms are aiming to find the shortest path (in a sense of steps or cost), but not to find the "matched" cost path.
A naive solution would be finding all paths from s to t, compute sum of weights for each path and select the one that is close to P. However, finding all paths between two nodes in a graph is known to be #P-hard.
A possible solution could be modified the A* algorithm, so that for each node in the frontier we get the cost from the root to that node (g), and estimate the cost from that node to the goal (h). Then instead of choosing a node with the smallest g+h, we choose a node with the smallest |P - (g+h)|. However, I am not sure if this is the best solution.
Another thought is inspired from the linear programming since the objective function of this problem is sum(weights of a path from s to t) - P = 0. I know the shortest path problem can be formed as a linear programming task but not sure how to formulate this problem as a one.
Please help, thanks in advance!
This problem is NP-hard via a reduction from the Hamiltonian path problem. In that problem, you are given a graph and a pair of nodes s and t and are asked whether there's a simple path from s to t that passes through all the nodes in the graph. You can solve the Hamiltonian path problem via your problem as follows:
Assign each edge in the graph weight 1.
Find the s-t simple path whose weight is as close to n-1 as possible, where n is the number of nodes in the graph.
Returns whether this path has cost exactly n-1.
If the graph has a Hamiltonian path, then that path will have cost n-1. Otherwise, it doesn't, and the best path found will have a cost that's lower than n-1.

Get path in graph with weighted vertices and edges

If i have the following undirected graph with weighted vertices and edges:
I am trying to come up with a ruby algorithm to find a best shortest path within a defined limit (sum of edges) with the highest value (sum of vertices).
The start point will also be the ending point.
for e.g. finding a path with a maximum of 20 with the highest total value.
This problem seems like a np hard problem and it is hard to find the best solution.
Is there a modified algorithm of dijkstra? I tried using a greedy algorithm but it did not give me a optimal solution. and by using bruteforce on all poosible path will work, but it will take very long if the number of nodes increases.
Was wondering if there is any combination of algorithms that i can use to improve my solution?
Thanks.
You can find an example of Djikstra's algorithm here. What I would do is add a variable to count the number of vertices in the shortest path, and evaluate if the shortest path has too many vertices or is too long once determining what the shortest path even is.
The problem is actually NP hard, you can prove this doing a reduction of Hamiltonian path problem to this problem. Basically given an input for the Hamiltonian path problem (we can stay with the undirected graph) you can create an input for the problem you describe if follow this steps:
build a new graph
create a new graph that is the same that receive the Hamiltonian path problem but give to each vertex and edge weight 1.
create an input for your problem
Pass to your problem the graph just created in the previous step and limit equals to infinity.
Notice now that the result given by your problem is a path as longest as posible with respect at the number of vertex, since the limit restriction is an upper bound in the sum of weight of the edges. This mean that you can add as vertex as posible and still complaint the limit restriction.
How many vertex are in the path determine the total value of this (count of vertex in the path) since the weight is 1 for all of them. So the resulting path is the longest posible in the graph. With this insight we can verify if this path is a hamiltonian path, just check the length of the path. If the path is of length N where N is the number of nodes in the graph there is a hamiltonian path otherwise there isn't.
To solve the problem you can use an approach similar to Bellman-Ford with some modifications. First create a matrix A[i, j] where you storage all the highest total value paths that ends with j and has length (number of edges) i. This is the key, you can't just storage one of this path, because in the next step you need to check all of them to make a relax, here is where the implementation become non-polynomial. The relax technique in the step i iterate through all the storage path in A[i-1, u] and try to improve the value in A[i, v] saving the new paths that actually do it (you must check for >= when try to improve A[i, v] in order to get all the paths), of course this relax take into account the limit restriction. If you uses a global max and a global path and update it in each relax process, you end with the highest value path for the entire graph.

Find the lowest-cost shortest path from one node to another?

I have a weighted graph G and a pair of nodes s and t. I want to find, of all the paths from s to t with the fewest number of edges, the one that has the lowest total cost. I'm not sure how to do this. Here are my thoughts:
I am thinking of finding the shortest path and if there are more than one path then i should compare the number of steps of these paths.
I think I can find the number of steps by setting the weights of all edges to 1 and calculate the distance.
A reasonable first guess for a place to start here is Dijkstra's algorithm, which can solve each individual piece of this problem (minimize number of edges, or minimize total length). The challenge is getting it to do both at the same time.
Normally, when talking about shortest paths, we think of paths as having a single cost. However, you could imagine assigning paths two different costs: one cost based purely on the number of edges, and one cost based purely on the weights of those edges. You could then represent the cost of a path as a pair (length, weight), where length is the number of edges in the path and weight is the total weight of all of those edges.
Imagine running Dijkstra's algorithm on a graph with the following modifications. First, instead of tracking a candidate distance to each node in the graph, you track a pair of candidate distances to each node: a candidate length and a candidate weight. Second, whenever you need to fetch the lowest-code node, pick the node that has the shortest length (not weight). If there's a tie between multiple nodes with the same length, break the tie by choosing the one with the lowest weight. (If you've heard about lexicographical orderings, you could consider this as taking the node whose (length, weight) is lexicographically first). Finally, whenever you update a distance by extending a path by one edge, update both the candidate length and the candidate weight to that node. You can show that this process will compute the best path to each node, where "best" means "of all the paths with the minimum number of edges, the one with the lowest cost."
You could alternatively implement the above technique by modifying all the costs of the edges in the graph. Suppose that the maximum-cost edge in the graph has cost U. Then do the following: Add U+1 to all the costs in the graph, then run Dijkstra's algorithm on the result. The net effect of this is that the shortest path in this new graph will be the one that minimizes the number of edges used. Why? Well, every edge adds U+1 to the cost of the path, and U+1 is greater than the cost of any edge in the graph, so if one path is cheaper than another, it either uses at least one fewer edge, or it uses the same number of edges but has cheaper weights. In fact, you can prove that this approach is essentially identical to the one above using pairs of weights - it's a good exercise!
Overall, both of these approaches will run in the same time as a normal Dijkstra's algorithm (O(m + n log n) with a Fibonacci heap, O(m log n) with another type of heap), which is pretty cool!
One node to another would be a shortest-path-algorithm (e.g. Dijkstra).
It depends on your input whether you use a heuristic function to determine the total distance to the goal-node.
If you consider heuristics, you might want to choose A*-search instead. Here you just have to accumulate the weights to each node and add the heuristic value according to it.
If you want to get all paths from any node to any other node, you might consider Kruskal’s or Prim’s algorithm.
Both to basically the same, incl. pruning.

Find the shortest path between a given source and a set of destinations

You are given a weighted connected graph (20 nodes) with all edges having positive weight. We have a robot that starts at point A and it must pass at points B, D and E for example. The idea is to find the shortest path that connects all these 4 points. The robot also has a limited battery, but it can be recharged in some points.
After researching on the internet I have two algorithms in mind: Dijkstra's and TSP. Dijkstra's will find the shortest path between a node and every other node and TSP will find the shortest path that connects all points. Is there any variant of the TSP that only finds the shortest path between a set of nodes? After all, in the TSP all nodes are labeled "must-pass". I'm still not taking in account the battery constraint.
Thanks in advance!
You can reduce your graph to a TSP and then invoke a TSP algorithm on it:
Use Floyd-Warshall algorithm to find the distance u,v for ALL pairs of vertices u and v.
Create a new graph, containing only the "desired" vertices, and set the weight between two such vertices u and v as the distance found by Floyd-Warshall.
Run TSP Solver on the modified graph to get the path in the modified graph, and switch each edge in the modified graph with a shortest path from the original graph.
The above is optimal, because assume there is a shorter path.
D0=u->...D1->...->D2->...->Dk->...->t=D{k+1}
Di->...->D{i+1} has at least the weight of FloydWarshall(Di,D{i+1}) (correctness of Floyd-Warshall), and thus the edges (D0,D1),(D1,D2),...,(Dk,D{k+1) exist in the modified graph with a weight smaller/equal the weight in the given path.
Thus, from correctness of your TSP-Solver, by using D0->D1->...->Dk->D{k+1}, you get a path that is at least as good as the candidate optimal path.
You might also want to look into the generalized traveling salesman problem (GTSP): The nodes are partitioned into subsets, and the problem is to find the minimum-length route that visits exactly one node in each subset. The model is allowed to choose whichever node it wants from each subset. If there are nodes that must be visited, you can put them in a subset all by themselves.

Maximum cost and path algorithms for weighted directed graphs

What algorithms are there, for weighted directed graphs, to find the maximum cost and path for going from a vertex A to a vertex K ?
I was thinking of modfied Dijkstra, but while watching and learning this algorithm, I found out it can't be used with negative weights and can't be used to find the maximum cost.
I suggest the following: choose any algorithm for minimum cost(distance) and also works with negative edges(thus Dijkstra can not be used for this). Then run this algorithm using the negation of the cost for each edge. You can use Bellman–Ford algorithm for instance.
You could use a modified version of the A* (A-star) algorithm. I say modified but it wouldn't actually be modified. You just need an appropriate heuristic. It is a path finding algorithm, you would just need to set your heuristic to chooses the costliest path.
A* works by starting at some vertex V, and adding all of that vertex's adjacent neighbors to an open list. Then it moves, in your case, to the node with the highest cost. The previously visited node is moved to a closed list. Then all the adjacent nodes to the current node are added to the open list. And so on and so forth.
It will find your K, and if your heuristic is to always choose the costliest path, you will have the maximum cost route.
Here is A* being applied to Infinite Mario.

Resources