Presentation is loading. Please wait.

Presentation is loading. Please wait.

Shortest Paths.

Similar presentations


Presentation on theme: "Shortest Paths."— Presentation transcript:

1 Shortest Paths

2 Basic Categories Single source vs. all-pairs Weighted vs. unweighted
Single Source Shortest Path: SSSP All-pairs Shortest Path: APSP Weighted vs. unweighted Can edges be negative? Can there be negative cycles? Often, modeling the graph is the biggest issue SSSP on Unweighted Graph Just use BFS

3 SSSP on Weighted Graph: Dijkstra’s Algorithm
Basic idea: Priority Queue showing shortest vertex reachable so far (and possibly what vertex it is reachable from) Pull off the next shortest, add to found list For all edges from that vertex, relax the edge See if the edge creates a new shortest distance to the next vertex; if so, reduce the weight of that vertex Traditional: queue is a heap with a decrease_key option This is not supported in the STL Implementing with STL priority queue Reducing priority is not supported Instead, if the new edge is less than min so far, just enqueue a new vertex Creates duplicates, but can use STL implementation Must check each vertex when pulled off to see if it’s already been added (simple to do)

4 Negative Weights Negative weight non-cycle
Can be solved using the variant of Dijkstra’s that keeps enqueuing data Each time a node is pulled off, check whether it’s at a shorter distance than previously found, if so, keep adding in Negative weight cycle: need to detect Bellman-Ford algorithm Also solves the SSSP problem, but less efficient unless there’s a negative cycle Relax every edge, V-1 times Start with dist[A] = 0 Relax all edges, in any order If at the end, any edge can still be relaxed, there’s a negative cycle So, check by trying each edge one last time

5 All Pairs Shortest Path: Floyd Warshall
A dynamic programming approach to solving Dist(A,B,k) = Distance from A to B going through only nodes 1..k Dist(A,B,k) = 0 if A=B Dist(A,B,-1) = weight(A,B) for edge from A to B (and infinity if no such edge) Dist(A,B,k) = min of: Dist(A,B,k-1) Dist(A,k,k-1) + Dist(k,B,k-1) Super-easy to implement (4 lines of code if adjacency matrix is used!) Use on a graph with ~400 or fewer vertices – even if just SSSP!

6 Floyd-Warshall extensions
Can modify Floyd-Warshall to compute other things (see book): Finding shortest path itself add a “parent” matrix that’s updated along the way Transitive closure (which vertices reachable from which others) Initialize matrix with 1 and 0 based on edges, use bitwise-or in Floyd-Warshall SCC Use transitive closure, then check whether i->j and j->i both true Diameter of graph Find the maximum distance between any pair Minimax/Maximin Check max( dist(A,k,k-1), dist(k,B,k-1) ) instead of sum Cheapest cycle Initialize dist(A, A) to very high value in initial adjacency matrix If, after algorithm is run, dist is less than that val, there is a cycle. Smallest value is the minimum cycle length (or IDs a negative cycle).

7 Graph Modeling (one example)
Sometimes, need to understand how to model a graph Example: Cities (vertices) each with a price for fuel. Roads with lengths connecting cities. Fuel tank capacity. Best route from A to B SSSP on just city-road graph is not sufficient Create an extended graph (state space graph): Node is (City, Fuel Tank level) – 1 for each unit of fuel tank capacity Edges: 0 length from (X, Fuel_x) to (Y, Fuel_x – length(X,Y) ) Fuel cost length from (X, Fuel_x) to (X, Fuel_x+1) SSSP on this new graph will solve the problem Route is (A,0) to (B,0)


Download ppt "Shortest Paths."

Similar presentations


Ads by Google