now that when we have a graph with negative edge costs, Dijkstra’s algorithm is not guaranteed to work. (a) Does Dijkstra’s algorithm ever work when some of the edge costs are negative? Explain why or why not. (b) Find an algorithm that will always find a shortest path between two nodes, under the assumption that at most one edge in the input has a negative weight. Your algorithm should run in time O(m log n), where m is the number of edges and n is the number of nodes. That is, the runnning time should be at most a constant factor slower than Dijkstra’s algorithm. To be clear, your algorithm takes as input (i) a directed graph, G, given in adjacency list form. (ii) a weight function f, which, given two adjacent nodes, v,w, returns the weight of the edge between them. For non-adjacent nodes v,w, you may assume f(v,w) returns +1. (iii) a pair of nodes, s, t. If the input contains a negative cycle, you should find one and output it. Otherwise, if the graph contains at least one directed path from s to t, you should output a shortest path. Otherwise, output “No path.”
We know that when we have a graph with negative edge costs, Dijkstra’s
(a) Does Dijkstra’s algorithm ever work when some of the edge costs are negative? Explain why or why not.
(b) Find an algorithm that will always find a shortest path between two nodes, under the assumption that at most one edge in the input has a negative weight. Your algorithm should run in time O(m log n), where m is the number of edges and n is the number of nodes. That is, the runnning time should be at most a constant factor slower than Dijkstra’s algorithm. To be clear, your algorithm takes as input
(i) a directed graph, G, given in adjacency list form. (ii) a weight function f, which, given two adjacent nodes, v,w, returns the weight of the edge between them. For non-adjacent nodes v,w, you may assume f(v,w) returns +1. (iii) a pair of nodes, s, t. If the input contains a negative cycle, you should find one and output it. Otherwise, if the graph contains at least one directed path from s to t, you should output a shortest path. Otherwise, output “No path.”
(c) Can you generalize your idea to graphs with two negative edges? Three? What happens to the running time as the number of edges increases?
Step by step
Solved in 2 steps with 1 images