Given undirected weighted connected graph, s,t. Find path from s to t that its most weighted edge is low as possible - algorithm

Given: undirected weighted connected graph. s,t are vertices.
Question: Find an algorithm as efficient as possible that returns a path from s to t. In that path, the edge that has the highest weight, will has the least weight as possible. So if we have 5 paths from s,t and for every path we have the heaviest edge, so the minimum edge of these 5.
What I've tried:
Use some algorithm to find the shortest path between s and t.
Delete all the edges that are not part of the shortest paths we've found
Use BFS with some modification, We run BFS depending on the number of paths from s to t. Every time we find a maximum edge and store it in an array, then we find the minimum of the array.
I'm struggling to find an algorithm that can be ran in (1), Bellman ford won't work - because it has to be directed graph. Dijkstra won't work because we don't know if it has negative circles or negative edges. And Prim is for finding MST which I'm not aware of how it can help us in finding the shortest path. Any ideas?
And other from that, If you have an algorithm in mind that can solve this question, would be much appreciated.

You can solve this with Kruskal's algorithm. Add edges as usual and stop as soon as s and t are in the same cluster.
The idea is that at each stage of the algorithm we have effectively added all edges below a certain weight threshold. Therefore, if s and t are in the same cluster then there is a route between them consisting entirely of edges with weight less than the threshold.

You can solve it by converting into a MST problem, basically the path from s to t in the MST would be the path which has the least possible maximum weight

find the most negative edge in the graph
add that (weight+1) to every edge.
Now all edge are positive so you can apply Dijkstra's algorithm
you can get the shortest_path between source and destination
Now count the number of edges between source and destination (say x)
Real shortest path will be: shortest_path - x * (weight+1)

Related

Shortest-path algorithm: multiple source, closest destination

Algorithms like the Bellman-Ford algorithm and Dijkstra's algorithm exist to find the shortest path from a single starting vertex on a graph to every other vertex. Their multiple source version can be achieved by reversing all the edges and treating destination as start node.
I'd like to extend that to find the "barycentre" of the sources on the graph, ie the vertex that is "closest" to a set of sources, finding "fair" paths to a "consensual" vertex.
Are there algorithms already providing this? What are they?
Floyd–Warshall algorithm
I think you want to calculate the "Graph Eccentricity" of the sources (S1,S2,...Sn-1,Sn).
Use Floyd-Warshall algorithm to calculate All Pair of Shortest Path in graph.
Find the result node V in graph, which is the min sum of (d[v,S1]+d[v,S2]+d[v,S3]....d[v,Sn-1]+d[v,Sn])
More Information:
Graph Eccentricity
UPDATE
Maybe finding an existed node v In Graph G(V,E) which the distance to S are all equal is not realistic. You can calculate the Stand Deviation of (d[v,S1],d[v,S2],d[v,S3]....d[v,Sn-1],d[v,Sn]) between a range that grater or equal than 0 and less than a certain value you choose.

Modified Dijkstra's Algorithm

We are given a directed graph with edge weights W lying between 0 and 1. Cost of a path from source to target node is the product of the weights of edges lying on the path from source to target node. I wanted to know of an algorithm which can find the minimum cost path in polynomial time or using any other heuristic.
I thought along the lines of taking the log values of the edges weights (taking mod values) and then applying dijkstra for this graph but think there will be precision problems which can't be calculated.
Is there any other better way or can I improve upon the log approach.
In Dijkstra's algorithm, when you visit a node you know that there is no shorter road to this node. This is not true if you multiply the edges with weights between 0..1 as if you visit more vertices you will get a smaller number.
Basically this is equivalent of finding the longest path in a graph. This can be seen also by using your idea of taking logarithms, as the logarithm of a number between 0 and 1 is negative. If you take absolute values of the logarithms of the weights, the longest path corresponds to the shortest path in the multiplicative graph.
If your graph is acyclic there is a straightforward algorithm (modified from Longest path problem).
Find a Topological ordering of the DAG.
For each vertex you need to store the cost of path. Initialize this to one at the beginning.
Travel through the DAG in topological order starting from your start vertex. In each vertex check all the children and if the cost is smaller than previously, update it. Store also the vertex where you arrive at this vertex with the lowest cost.
After you reach your final vertex, you can find the "shortest" path by travelling back from the end vertex using the stored vertices.
Of course, if you graph is not acyclic you can always reach a zero end cost by repeating a loop infinitely.

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.

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.

Single shortest path of an acyclic undirected disconnected graph

Is there a graph algorithm that given a start(v) and an end(u) will find a shortest path through the given set of edges, but if u is a disconnected vertex, it will also determine the shortest path to add missing edges until u is no longer disconnected?
I have a pixel matrix where lines are made of 255's(black) and 0's(white). lines(255) can have breaks or spurs and I must get rid of both. I could have a pixel matrix forest with say 7 or so trees of black pixels. I need to find the true end points of each tree, find the single shortest path of each tree, then union all skew trees together to form 1 single line(ie, a single shortest path from the furthest 2 end points in the original matrix). all edge weights could be considered 1.
Thanks
How about running Dijkstra's algorithm and if disconnected, connect v and u? What's your criteria for "best place to add a missing edge?" Do edges have weights (like distance)?
Edit:
For one idea of "the best place," you could try the path that has minimal sum of shortest paths between all connected pairs. Floyd–Warshall algorithm can be used to find shortest paths between all pairs. So, run Floyd-Warshall for each node in v's tree and u.
Your problem isn't well defined for disconnected graphs. I can always add and edge between v and u.
If you meant that given an acyclic undirected disconnected graph, actually known as a forest, and given a subset of edges as a subgraph, can you find the shortest path between vertices, than this problem is trivial since if there is a path in the full graph, there is one path only.
If this is a general graph G, and you are talking about a forest subgraph G', than we need more info. Is this weighted? Is it only positive weights? If it is unweighted, do some variant of Dijksta. Define the diameter of a tree to be the length of the longest path between two leaves (this is well defined in a tree, since ther is only one such path). Let S be the sum of the diameters of all the trees in G', then set the weight all edges out of G' to 2S, then Dijkstra's algorithm will automatically prefer to step through G', stepping outside G' only when there is no choice, since walking through G' would always be cheaper.
Here you have following scenarios:
case:1. (all edges>0)
Negate all edges
find the longest path in graph using this:
https://www.geeksforgeeks.org/longest-path-undirected-tree/
negate the final result
case2: edges might be or might not be negative
Read Floyd–Warshall algorithm for this

Resources