Lightest paths tree - algorithm

I need to write algorithms finding the lightest path tree in directed and weighted graph.
(should be efficient as possible)
I'm getting a vertex S and need to build a paths tree from S to all vertex that can be approach from S so the paths in the tree are the lightest ( path weight is the path without the ends)
I thought about first calculating all the distances from S And then for each path there will be a new weight:
The weight minus The ends
And then on the graph with the new weights to run dijkstra...
Will it work? Is it efficient enough? How to calculate the distances?

Your comments suggest you are actually looking for the shortest path from a single source - to all vertices.
Have a look at how Dijkstra's algorithm works. The algorithm starts with a tree of size 1 (the source alone), and iteratively adds vertices to the tree. In the end, the tree created by dijkstra's algorithm, represent the shortest path from the source to each of the nodes in the graph.
Note that dijkstra's algorithm, needs a weight function on edges, while your weight function is on vertices. It can be solved easily by defining a new weight function w':E->R: w'(u,v) = w(u) (it works since you don't want to count the end).

It sounds like you're asking for a minimum spanning tree. The wikipedia page discusses algorithms.

Related

Dijkstra's shortest path algorithm on directed tree with negative weight edges

Will Dijkstra's shortest path algorithm return correct results on a directed tree with negative weight edges?
On a general graph with negative weights, the algorithm will fail, but since it’s a directed tree it feels like the algorithm will succeed.
From other answers, you know that there is no good reason to run Dijkstra's algorithm if you know that the graph is a tree.
If you do run it, though, it will work even if the tree has negative edge weights.
The reason that Dijkstra's algorithm doesn't work for graphs with negative weights, is that negative weights allow a 2nd, shorter, path to be found to a vertex after its distance has already been decided. In a tree there are no 2nd paths.
In a tree there is only one path between any two given nodes, so searching for the "shortest" path in a tree makes little sense: when you find a path it is the shortest, and this search does not need to take weights into account, so there is no need to use Dijkstra's algorithm. A simple depth-first search will do.
If the graph is not a tree, but a directed acyclic graph (DAG) with negative edges, then Dijkstra's algorithm cannot be used to find a shortest path. Take this counter example:
If we have to look for the shortest path from A to C, Dijkstra's algorithm will proceed to visit B and C, and as it hits the target, it will stop looking further, never considering the edge from B to C.
Other attempts to apply Dijkstra
Another (now deleted) answer proposed to make all edge weights positive by adding an absolute value to all weights, but this does not yield correct results: what is the shortest path in the original graph, is not guaranteed to be still the shortest path in the derived graph.
Counter example:
Where in the original graph the shortest path from A to C runs via B, in the adjusted graph, the shortest path is A-C.

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.

Resources