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 G=(V,E,w)G = (V, E, w) with two special vertices, and we want to find the shortest path from a source vertex ss to a target vertex tt. That is, we want to find the directed path PP starting at ss and ending at tt that minimizes the function. The expression below represents the sum of the weights of all edges:

w(P):=uvϵPw(uv)w(P) := \underset{u\rightarrow v \epsilon P}{\sum} w(u\rightarrow v)

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, ss is Champaign, and tt is Columbus. The graph is directed because driving times along the same road might be different in different directions. (For example, once 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 ss to every other vertex in the graph. This problem is usually solved by finding a shortest path tree rooted at ss 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 ss to two vertices uu and vv 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