# 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)$ with two special vertices, and we want to find the shortest path from a source vertex $s$ to a target vertex $t$. That is, we want to find the directed path $P$ starting at $s$ and ending at $t$ that minimizes the function

$w(P) := \underset{u\rightarrow v \epsilon P}{\sum} w(u\rightarrow v).$

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, $s$ is Champaign, and $t$ 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 $s$ to every other vertex in the graph. This problem is usually solved by finding a **shortest path tree** rooted at $s$ 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 $s$ to two vertices $u$ and $v$ 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