Assume we are given a weighted, connected, directed graph G, a vertex s and the shortest path tree for s. So we already know all the shortest distances from s to every other vertex in the graph.
Now the following happens, we add an edge to the graph.
Is there an efficient way to get the new shortest path tree?
How can I calculate the new shortest path tree by encountering the old one?
Related
Pathfinding algorithms such as A* 100% guarantees the shortest path. But can we can manually analyze if a given path in the graph is the shortest path?
Suppose that we have this weighted graph
Weighted Graph
Let's say that our starting node is NODE 1 and we want to go to NODE 2. The path that we took is node 1 -> node 2. Obviously this is the shortest path. How can we manually prove or determine whether this path is the shortest route without any computers?
One can go with Dijkstra Algorithm to find the shortest path.
Is there any way with polynomial complexity(or better than that) to get kth or k shortest path of an undirected graph?
or can Yen's k shortest path algorithm modified for undirected graphs?
Try to find the permutations of paths from source vertex to final vertex (taking all vertex atleast once) upto k permutations but this would also mean if the total permutations of paths from source vertex to final vertex (taking all vertex atleast once) are finite but that wouldnt be the case as in case of NP hard problems. if you can find this heuristic in polynomial time then you can select kth shortest path. so apply brute force dfs bfs then find the shortest path via dynamic programming and compare your permutations with kth case
I'm trying to implement the dijkstra algorithm with priority queue, but I can't understand how it works. I read many guide on the web but I can't understand this algorithm at all.
My questions are: What is the priority for each node? I think that it is the weight of the incoming edge with the minimum value, but I'm not sure. Is this true?
Second question, when I extract the root of the queue, how does it work if this node is not adjacency with no one of the visited nodes?
You should use priority queue where the vertex with the shortest distance from the starting vertex will get the highest priority. Initially, all vertices will have the shortest distance of infinity and the starting vertex will have the shortest distance 0.
Start by inserting of all vertices (with its edges) from the graph inside the PQ. Remove vertex from the PQ and explore all its edges. Compare the shortest distances with all adjacent vertices and if any distance is less than the shortest distance on the current vertex, update adjacent vertex shortest distance inside the PQ. Continue while PQ is not empty. Vertices which got no edges will finish with the shortest distance of infinity because it is not possible 'get to them' from the starting vertex. However, they will be still removed from the PQ.
Pseudocode
initialize graph
initialize pq
pq.insertAll(graph.getVertices())
while (pq is not empty) {
vertex = pq.remove()
edges = vertex.getEdges()
for all edges {
destination = edge.getDestination()
newDistance = edge.getLength() + vertex.getDistance()
if (newDistance < destination.getDistance()) {
destination.setShortestDistance(newDistance)
pq.update(destination)
}
}
}
MIT OpenCourseWare Links:
Path problems overview
Dijkstra
Basically, I need to have a shortest path in a graph that covers all of the vertices and returns to the source. Repetition of any vertex is ok so long as it is the shortest path.
My algorithm starts from the source. I run a dijkstra algorithm to find the shortest path. Then I choose the smallest weighted unreached vertex and run the dijkstra again as the chosen vertex as the source and keep doing it until all vertices are done. Then from the last vertex use dijkstra again to find the shortest path back to the original source.
I tried, but it seems to fail and I cannot find the reason.
Google the travelling salesperson problem,that finds the shortest cycle starting from the source covering all the vertices.The code for travelling salesperson is also not difficult to implement.
Since any vertex can be visited more that once you are basically looking for the shortest closed walk in a graph. The problem with your approach is that dikstra will only find the shortest path from the chosen node back to the source . This will produce a star type solution, where you have multiple paths coming out of the source vertex. Which could be longer then a walk.
This is an NP problem, as Vincent stated, but you could try implementing it using a random walk algorithm. Or look at the Traveling Salesman Walk Problem (http://dspace.mit.edu/handle/1721.1/33668)
I am given a directed graph with a weight function and a vertex s.
My goal is to find for any other vertex v, the shortest path from s to v that goes through exactly one negative edge. The time complexity of the algorithm should be O(|E| + |V|*log|V|), so guess I need to use Dijkstra's algorithm somehow.
I am guessing that I need to translate my given graph somehow to a new graph with non-negative weights that a shortest path from s to v in this graph will be equivalent to the required shortest path in the given graph.. or maybe I need to modify Dijkstra's algorithm somehow??
I tried to think about it a little, don't have any ideas right now... :(