Effect on shortest paths after edges have been deleted - algorithm

An input of directed graph has been provided and I have found shortest paths to a particular node 'T' using both - asynchronous and synchronous Bellman-Ford algorithm.
I was trying to find out the effect on the shortest paths after some edges are deleted.
In my approach, I tried to mark the distances at start nodes of the deleted edges as infinity and was trying to apply asynchronous Bellman-Ford, but I get stuck at the point because other nodes will not update their value as they already have the shortest path minimum value.
Can anyone help me to figure out a way to find the new shortest paths without having to run the full algorithm again on the new graph?

You can not. And a simple explanation can be found in Bellman-Ford algorithm itself:
If V is the set of nodes. A minimal path from starting node to any other node will pass maximum |V| nodes ( |V|-1 edges). This is the reason why you relax the edges for |V|-1 time, so that the 'information' from all nodes will propagate to the source.
Is you already have applied Bellman-Ford algorithm on a graph, you can start relaxing all the deleted node's neighbors and propagate the changes to their neighbors until a path that wasn't using the deleted node (until no updates are being made). Aware of negative cycle.

Related

How does Breadth First Search find shortest path from source vertex to destination vertex?

I was told that BFS can give you the shortest path from the source vertex to the destination vertex, which makes sense since you traverse the adjacent nodes. However, I do not see how that is guaranteed to happen always. Nowhere in the BFS pseudocode logic do I see to pick the correct adjacent node to guarantee to have the shortest path. BFS could pick any random adjacent node and end up with the longer path from the source vertex to the destination vertex. Then how does BFS give the shortest path from the source vertex to the destination vertex?
In the case of unweighted graphs, it is guaranteed that eventually, we will get the shortest path using BFS.
But in the case of a weighted graph, we can still run a variant of BFS, i.e., using a priority queue for neighbors nodes (based on weights of edges). This variant is called Dijkstra’s algorithm.
You are correct. Using BFS alone does not guarantee to find the shortest path.
It will only fetch you the shortest path when all the edges have an equal edge weight in the case of a weighted graph. For unweighted graphs, you can assume equal edge weights.
Other algorithms (for example, Dijkstra’s algorithm) adapt BFS to find the shortest paths.
Thanks to your question, which pushed me to come up with an interesting case, where BFS can actually fetch you the shortest path in case of weighted graphs:
When we have a linear weighted undirected graph (with no cycles), i.e., a graph with 2 adjacent nodes for middle nodes and 1 adjacent node for nodes are the extreme ends (like a double linked list), BFS could actually fetch us the shortest path.
PS: BFS does not pick up any adjacent node randomly. The adjacent node's selection depends on the adjacent nodes' order in the particular node's adjacency list. So, it's dependent on your code implementation and not on some random factor.

Is there a graph algorithm to find shortest path between nodes, incorporating nodes to avoid?

Let's say I have a graph with nodes that represent locations. Starting at some node N, I want to reach another node M via the shortest path possible. The catch is that there some nodes I want to avoid, staying some distance from them (say at least D nodes away).
Is there a graph algorithm that can solve the shortest path problem with the node avoidance requirement? Would a weighted graph (with infinite length edges emanating from the to-be-avoided nodes) be a solution here?
Temporarily eliminate the nodes you must avoid and those near them or change the weights of the appropriate edges to infinity. Then use any standard path-finding algorithm.

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.

Dijktra algorithm vs breath first search for shortest path in graph

I need some clarifications and inputts regarding Dijktra's algorithm vs breath first search in directed graphs, if these are correct.
Dijktra's algorithm finds the shortest path from Node A to Node F in a weighted graph regardless of if there is a cycle or not (as long as there are no negative weights)
but for that, All paths from A to all other Nodes in the graph are calculated and we grab the path fromAtoFby reversing the sequences of nodes inprev`.
BFS: finds the shortest path from Node A to Node F in a non-weighted graph, but if fails if a cycle detected.
however, BFS just calculates the path from Node A to Node F and not necessarily all path from Node A.
if Node F is reached early, it just returns the path.
Dijkstra doesn't search all nodes of the graph. When it has found a way from A to F and is sure there is no shorter one (because the outer border of the already visited nodes is farther away), it stops. This is possible without negative weights.
So to answer your question "if these are correct": They are not.
Those algorithms are quite different in terms of their execution.
BFS is a kind of brute-force algorithm that follows a "static execution pattern"
Dijkstra on the other hand is more dynamic and continues its search always at the point with the lowest cost.
The purpose of dijkstra is to find the shortest path. So if you are looking for the shortest path use dijkstra.

TSP-Variant, possible algorithm?

One of the classical Travelling Salesman Problem (TSP) definitions is:
Given a weighted complete undirected graph where triangle inequality holds return an Hamiltonian path of minimal total weight.
In my case I do not want an Hamiltonian path, I need a path between two well known vertexes. So the formulation would be:
Given a weighted complete undirected graph where triangle inequality holds and two special vertexes called source and destination return a minimal weighted path that visits all nodes exactly once and starts from the source and ends to the destination.
I recall that an Hamiltonian path is a path in an undirected graph that visits each vertex exactly once.
For the original problem a good approximation (at worse 3/2 of the best solution) is the Christodes' algorithm, it is possible to modify for my case? Or you know another way?
Add an edge (= road) from your destination node to your source node with cost 0 and you've got a TSP (for which the triangle inequality doesn't hold though).
The book "In pursuit of the Traveling Salesman" briefly mentions this technique.
Why don't you use dijkstra's algorithm with extra book keeping on each node for the path information. i.e., the list of vertices passed in the shortest path to that particular vertex from the source.
And stop when you reach your end vertex. Then your path will be
Path to the starting vertex of the current edge + current edge.
where current edge is the last edge which lead you to your destination.
You can alter a TSP algorithm to make sure you use the edge between start and finish. Then simply remove the edge from the TSP result and you get your path.
In other words, if you add the edge from start to finish to your path, you get a TSP solution, but not necessary the optimal one.
In greedy algorithm, you have a sorted list of all edges L and an empty list I. You keep adding the shortest edge that does not form a cycle until you pass all vertexes. Simply remove the edge from start to finish from L and add it to I before you add the rest of the edges.
In nearest neighbor, you start from one vertex, then add the shortest edge that starts at that vertex and leads to any vertex that has not been visited. Mark your finish as visited, then start from your start vertex and add the edge between the last vertex in the resulted path and the finish and you got your path from start to finish.
There are other TSP algorithms that can be altered to provide this path.

Resources