Read · 1 min

Dijkstra's algorithm can be used in weighted graphs to compute the shortest distance from a vertex to all other vertices in the graph.

Suppose our vertices are v1,…,vnv_1,\dots,v_n. Suppose we want to calculate the shortest distances from v1v_1 to all others. We will keep track of a set SS of solved vertices for those which we know the shortest distance. We will call this shortest distance d(v)d(v) for any v∈Sv\in S once we calculate it.

Initially let SS just contain v1v_1, and d(v1)=0d(v_1)=0. Now we iteratively add elements to SS, until we can no longer, by the following step:

For each vertex u∈Su\in S, find its neighbor v∉Sv\not\in S with the minimum weight ww. Among all u∈Su\in S, we add the one that minimizes d(u)+wd(u)+w to SS, and its distance is d(u)+wd(u)+w.

This is a complicated algorithm, so be sure to visit this interactive demonstration.

Type a concept, like “slope” or “photosynthesis”.

↑ ↓ to choose · Enter to openSearch explanations and problems