# 5.7: Weighted Graphs and Dijkstra's Algorithm

$$\newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} }$$

$$\newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}}$$

$$\newcommand{\id}{\mathrm{id}}$$ $$\newcommand{\Span}{\mathrm{span}}$$

( \newcommand{\kernel}{\mathrm{null}\,}\) $$\newcommand{\range}{\mathrm{range}\,}$$

$$\newcommand{\RealPart}{\mathrm{Re}}$$ $$\newcommand{\ImaginaryPart}{\mathrm{Im}}$$

$$\newcommand{\Argument}{\mathrm{Arg}}$$ $$\newcommand{\norm}[1]{\| #1 \|}$$

$$\newcommand{\inner}[2]{\langle #1, #2 \rangle}$$

$$\newcommand{\Span}{\mathrm{span}}$$

$$\newcommand{\id}{\mathrm{id}}$$

$$\newcommand{\Span}{\mathrm{span}}$$

$$\newcommand{\kernel}{\mathrm{null}\,}$$

$$\newcommand{\range}{\mathrm{range}\,}$$

$$\newcommand{\RealPart}{\mathrm{Re}}$$

$$\newcommand{\ImaginaryPart}{\mathrm{Im}}$$

$$\newcommand{\Argument}{\mathrm{Arg}}$$

$$\newcommand{\norm}[1]{\| #1 \|}$$

$$\newcommand{\inner}[2]{\langle #1, #2 \rangle}$$

$$\newcommand{\Span}{\mathrm{span}}$$ $$\newcommand{\AA}{\unicode[.8,0]{x212B}}$$

$$\newcommand{\vectorA}[1]{\vec{#1}} % arrow$$

$$\newcommand{\vectorAt}[1]{\vec{\text{#1}}} % arrow$$

$$\newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} }$$

$$\newcommand{\vectorC}[1]{\textbf{#1}}$$

$$\newcommand{\vectorD}[1]{\overrightarrow{#1}}$$

$$\newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}}$$

$$\newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}}$$

$$\newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} }$$

$$\newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}}$$

$$\newcommand{\avec}{\mathbf a}$$ $$\newcommand{\bvec}{\mathbf b}$$ $$\newcommand{\cvec}{\mathbf c}$$ $$\newcommand{\dvec}{\mathbf d}$$ $$\newcommand{\dtil}{\widetilde{\mathbf d}}$$ $$\newcommand{\evec}{\mathbf e}$$ $$\newcommand{\fvec}{\mathbf f}$$ $$\newcommand{\nvec}{\mathbf n}$$ $$\newcommand{\pvec}{\mathbf p}$$ $$\newcommand{\qvec}{\mathbf q}$$ $$\newcommand{\svec}{\mathbf s}$$ $$\newcommand{\tvec}{\mathbf t}$$ $$\newcommand{\uvec}{\mathbf u}$$ $$\newcommand{\vvec}{\mathbf v}$$ $$\newcommand{\wvec}{\mathbf w}$$ $$\newcommand{\xvec}{\mathbf x}$$ $$\newcommand{\yvec}{\mathbf y}$$ $$\newcommand{\zvec}{\mathbf z}$$ $$\newcommand{\rvec}{\mathbf r}$$ $$\newcommand{\mvec}{\mathbf m}$$ $$\newcommand{\zerovec}{\mathbf 0}$$ $$\newcommand{\onevec}{\mathbf 1}$$ $$\newcommand{\real}{\mathbb R}$$ $$\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}$$ $$\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}$$ $$\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}$$ $$\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}$$ $$\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}$$ $$\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}$$ $$\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}$$ $$\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}$$ $$\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}$$ $$\newcommand{\laspan}[1]{\text{Span}\{#1\}}$$ $$\newcommand{\bcal}{\cal B}$$ $$\newcommand{\ccal}{\cal C}$$ $$\newcommand{\scal}{\cal S}$$ $$\newcommand{\wcal}{\cal W}$$ $$\newcommand{\ecal}{\cal E}$$ $$\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}$$ $$\newcommand{\gray}[1]{\color{gray}{#1}}$$ $$\newcommand{\lgray}[1]{\color{lightgray}{#1}}$$ $$\newcommand{\rank}{\operatorname{rank}}$$ $$\newcommand{\row}{\text{Row}}$$ $$\newcommand{\col}{\text{Col}}$$ $$\renewcommand{\row}{\text{Row}}$$ $$\newcommand{\nul}{\text{Nul}}$$ $$\newcommand{\var}{\text{Var}}$$ $$\newcommand{\corr}{\text{corr}}$$ $$\newcommand{\len}[1]{\left|#1\right|}$$ $$\newcommand{\bbar}{\overline{\bvec}}$$ $$\newcommand{\bhat}{\widehat{\bvec}}$$ $$\newcommand{\bperp}{\bvec^\perp}$$ $$\newcommand{\xhat}{\widehat{\xvec}}$$ $$\newcommand{\vhat}{\widehat{\vvec}}$$ $$\newcommand{\uhat}{\widehat{\uvec}}$$ $$\newcommand{\what}{\widehat{\wvec}}$$ $$\newcommand{\Sighat}{\widehat{\Sigma}}$$ $$\newcommand{\lt}{<}$$ $$\newcommand{\gt}{>}$$ $$\newcommand{\amp}{&}$$ $$\definecolor{fillinmathshade}{gray}{0.9}$$

Investigate!

In the table, the time it takes to travel between various locations by bus in South Bend is given. Incorporate this information in a graph, and then find shortest paths from the airport to every other location.

 Saint Mary's Holy Cross Notre Dame Ranjan's House Chocolate Cafe Crooked Ewe Airport Saint Mary's 0 3 7 - - - 17 Holy Cross 0 6 - 11 - - Notre Dame 0 8 - 14 - Ranjan's House 0 - 15 22 Chocolate Cafe 0 12 15 Crooked Ewe 0 - Airport 0

Definition

A graph with a number (usually positive) assigned to each edge is called a weighted graph. (A graph without weights can be thought of as a weighted graph with all weights equal to 1.) We denote the weight between vertices $$u$$ and $$v$$ by $$w(u,v)$$.

In the previous example, the weights represented distances. What else could we represent using weights?

In many situations, we want to find a shortest path (or path of least weight) between two locations. Dijkstra's algorithm gives us a way to do this.

Dijkstra's algorithm

• Input: a weighted graph, $$G$$, with a source vertex $$s$$
• for each vertex $$v$$ in $$G$$:
• $$dist(v):=\infty$$
• $$prev(v):=undefined$$
• $$dist(s):=0$$
• $$Q:=$$ set of all vertices in $$G$$
• while $$Q$$ is not empty:
• $$u:=$$ vertex in $$Q$$ with smallest distance
• remove $$u$$ from $$Q$$
• for each neighbor $$v$$ of $$u$$
• $$alt:=dist(u)+w(u,v)$$
• if $$alt<dist(v)$$
• $$dist(v):=alt$$
• $$prev(v):=u$$
• return $$dist(), prev()$$

Remark: If you only want to know the distance from the source to a particular vertex, you can terminate the algorithm when that vertex is removed from $$Q$$.

Challenge: Find a big-O estimate for the number of operations (additions and comparisons) used by Dijkstra's algorithm.

Exercise $$\PageIndex{1}$$

Saint Mary's once had tunnels connecting various buildings on campus. The tunnels and their lengths are as follows (and definitely not accurate): Regina to McCandless (400 ft), Regina to Student Center (200), McCandless to Student Center (100), McCandless to Angela (500), Student Center to Angela (800), Student Center to Library (1000), Angela to Library (200), Angela to Madeleva (600), Library to Madeleva (300). Find a shortest path from Regina to Madeleva using Dijkstra's Algorithm.

Theorem $$\PageIndex{2}$$

Dijkstra's Algorithm finds a shortest path between two vertices in a simple undirected weighted graph.

Proof

We will prove the theorem by induction on $$|V\setminus Q|$$. The first vertex removed from $$Q$$ is $$s$$. In this case $$|V\setminus Q|=1$$ and Dijkstra's algorithm tells us that the distance from the source to $$s$$ (ie from $$s$$ to itself) is 0. Therefore, the base case is true.

The inductive hypothesis is that for any vertex not in $$Q$$, the distance assigned to that vertex by the algorithm is in fact the minimum distance from the source to that vertex.

Let $$u$$ be the most recent vertex removed from $$Q$$. By the inductive hypothesis, the algorithm has provided the minimum distance from the source for every vertex in $$V\setminus Q$$ besides $$u$$.

Suppose by contradiction that a shortest path $$P$$ from $$s$$ to $$u$$ has length less than $$dist(u)$$. In other words, $$length(P)<dist(u)$$. Let $$xy$$ be the first edge along $$P$$ that isn't in $$V\setminus Q$$ and let $$P_x$$ be the sub-path of $$P$$ from $$s$$ to $$x$$. Then

$$length(P_x)+w(x,y)\leq length(P)$$,

and by the induction hypothesis

$$dist(x)+w(x,y)\leq length(P)$$.

Since $$y$$ is adjacent to $$x$$, the algorithm recalculates $$dist(y)$$, so

$$dist(y)\leq dist(x)+w(x,y)$$.

Combining these inequalities, we see that $$dist(y)<dist(u)$$. But the algorithm removed $$u$$ from $$Q$$ and not $$y$$, so we must have that $$dist(u)\leq dist(y)$$. This is a contradiction, and the proof is complete.

5.7: Weighted Graphs and Dijkstra's Algorithm is shared under a CC BY-NC-SA license and was authored, remixed, and/or curated by LibreTexts.