Introduction to Shortest Paths
Understand the fundamental concepts of shortest paths and their applications in problem solving.
Weighted directed graph (two vertices)
Suppose we are given a weighted directed graph with two special vertices, and we want to find the shortest path from a source vertex to a target vertex . That is, we want to find the directed path starting at and ending at that minimizes the function
The expression above represents the sum of the weights of all edges. For example, if we want to answer the question, “What’s the fastest way to drive from my old apartment in Champaign, Illinois, to my wife’s old apartment in Columbus, Ohio?” we might use a graph whose vertices are cities, edges are roads, weights are driving times, is Champaign, and is Columbus. The graph is directed because driving times along the same road might be different in different directions. (At one time, there was a speed trap on I-70 just east of the Indiana/Ohio border, but only for eastbound traffic.)
Shortest path trees
Almost every algorithm known for computing shortest paths from one vertex to another actually solves (large portions of) the following more general single-source shortest path or SSSP problem: find shortest paths from the source vertex to every other vertex in the graph. This problem is usually solved by finding a shortest path tree rooted at that contains all the desired shortest paths.
It’s not hard to see that if shortest paths are unique, then they form a tree because any subpath of the shortest path is itself the shortest path. If there are multiple shortest paths to some vertices, we can always choose one shortest path to each vertex so that the union of the paths is a tree. If there are shortest paths from to two vertices and that diverge, then meet, then diverge again, we can modify one of the paths without changing its length so that the two paths only diverge once.
Create a free account to access the full course.
By signing up, you agree to Educative's Terms of Service and Privacy Policy