The shortest path problem asks for a graph path from a source vertex to a destination vertex minimizing its total edge weight. Here is the cost of traversing edge , and costs add along the graph path. If no route exists, its shortest-path distance is . With nonnegative edge weights, deleting cycles in a graph from a walk never increases its cost, so repetitions cannot improve the optimum.
Consider the undirected graph with vertex set and the six edge weights
Each edge can be traversed in either direction at the same cost. The direct graph path costs ; costs ; costs . The cheapest graph path is , costing . The remaining graph path costs , exhausting the possibilities without repeated vertices.
Figure 1.
The shortest path problem: three highlighted edges cost less than the direct connection
. Details.
Thus fewest edges and least total cost are different objectives. Breadth-first search minimizes the number of edges, which suffices when every edge has the same positive cost. Unequal nonnegative costs call for the Dijkstra algorithm. On a map, weights might be minutes rather than kilometres: a longer-looking route can arrive sooner. This is why a weighted graph separates the network's connections from what a particular journey costs.

Articles by others on the same topic (NaN)

The shortest path problem is a fundamental problem in graph theory and computer science that involves finding the shortest path or minimum distance between two nodes (vertices) in a graph. Graphs can be directed or undirected, weighted or unweighted, and the shortest path can be defined in terms of the smallest sum of weights of the edges traversed.