Skip to main content
Mathematics LibreTexts

6.7: Spanning Trees

  • Page ID
    34210
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

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

    \( \newcommand{\dsum}{\displaystyle\sum\limits} \)

    \( \newcommand{\dint}{\displaystyle\int\limits} \)

    \( \newcommand{\dlim}{\displaystyle\lim\limits} \)

    \( \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}\)

    A graph with 5 vertices and edges labeled with costs to connect officesA company requires reliable internet and phone connectivity between their five offices (named A, B, C, D, and E for simplicity) in New York, so they decide to lease dedicated lines from the phone company. The phone company will charge for each link made. The costs, in thousands of dollars per year, are shown in the graph.

    In this case, we don’t need to find a circuit, or even a specific path; all we need to do is make sure we can make a call from any office to any other. In other words, we need to be sure there is a path from any vertex to any other vertex.

    Spanning Tree

    A spanning tree is a connected graph using all vertices in which there are no circuits.

    In other words, there is a path from any vertex to any other vertex, but no circuits.

    Some examples of spanning trees are shown below. Notice there are no circuits in the trees, and it is fine to have vertices with degree higher than two.

    Five graphs, each using 4 vertices. Three of the graphs show various spanning trees connecting sequences of vertices, like A to B to C to D, or A to C to B to D. Two show spanning trees based at one vertex, like A to B, A to C, and A to D

    Usually we have a starting graph to work from, like in the phone example above. In this case, we form our spanning tree by finding a subgraph – a new graph formed using all the vertices but only some of the edges from the original graph. No edges will be created where they didn’t already exist.

    Of course, any random spanning tree isn’t really what we want. We want the minimum cost spanning tree (MCST).

    Minimum Cost Spanning Tree (MCST)

    The minimum cost spanning tree is the spanning tree with the smallest total edge weight.

    A nearest neighbor style approach doesn’t make as much sense here since we don’t need a circuit, so instead we will take an approach similar to sorted edges.

    Kruskal’s Algorithm

    1. Select the cheapest unused edge in the graph.
    2. Repeat step 1, adding the cheapest unused edge, unless:
      1. adding the edge would create a circuit
    3. Repeat until a spanning tree is formed

    Example 22

    Using our phone line graph

    costs graph; see adjacency table that follows

    Adjacency Table
    Adjacency table showing cost to connect offices, in thousands
      A B C D E
    A - 4 8 9 5
    B 4 - 10 14 6
    C 8 10 - 7 11
    D 9 14 7 - 13
    E 5 6 11 13 -

     

    Begin adding edges:

    • AB: $4 OK
    • AE: $5 OK
    • BE: $6 reject - closes circuit ABEA
    • DC: $7 OK
    • AC: $8 OK

    At this point we stop – every vertex is now connected, so we have formed a spanning tree with cost $4 + $5 + $7 + $8 = $24 thousand a year.

    The office connection costs graph with the added edges highlighted

    Remarkably, Kruskal’s algorithm is both optimal and efficient; we are guaranteed to always produce the optimal MCST.

    Example 23

    The power company needs to lay updated distribution lines connecting the ten Oregon cities below to the power grid. How can they minimize the amount of new line to lay?

    Distances between cities
      Ashland Astoria Bend Corvallis Crater Lake Eugene Newport Portland Salem Seaside
    Ashland - 374 200 223 108 178 252 285 240 356
    Astoria 374 - 255 166 433 199 135 95 136 17
    Bend 200 255 - 128 277 128 180 160 131 247
    Corvallis 223 166 128 - 430 47 52 84 40 155
    Crater Lake 108 433 277 430 - 453 478 344 389 423
    Eugene 178 199 128 47 453 - 91 110 64 181
    Newport 252 135 180 52 478 91 - 114 83 117
    Portland 285 95 160 84 344 110 114 - 47 78
    Salem 240 136 131 40 389 64 83 47 - 118
    Seaside 356 17 247 155 423 181 117 78 118 -
    Solution

    Using Kruskal’s algorithm, we add edges from cheapest to most expensive, rejecting any that close a circuit. We stop when the graph is connected.

    • Seaside to Astoria: 17 miles
    • Corvallis to Salem: 40 miles
    • Portland to Salem: 47 miles
    • Corvallis to Eugene: 47 miles
    • Corvallis to Newport: 52 miles
    • Salem to Eugene: reject - closes circuit Corvallis-Salem-Eugene-Corvallis
    • Portland to Seaside: 78 miles

    The graph up to this point is below.

    A graph of the cities with the added edges drawn

    Continuing,

    • Newport to Salem: reject - closes circuit
    • Corvallis to Portland: reject
    • Eugene to Newport: reject
    • Portland to Astoria: reject
    • Ashland to Crater Lake: 108 miles
    • Eugene to Portland: reject
    • Newport to Portland: reject
    • Newport to Seaside: reject
    • Salem to Seaside: reject
    • Bend to Eugene: 128 miles
    • Bend to Salem: reject
    • Astoria to Newport: reject
    • Salem to Astoria: reject
    • Corvallis to Seaside: reject
    • Portland to Bend: reject
    • Astoria to Corvallis: reject
    • Eugene to Ashland: 178 miles

    The city graph with all the added edges shown, forming a spanning tree

    This connects the graph. The total length of cable to lay would be 695 miles.

    Try it Now 7

    Find a minimum cost spanning tree on the graph below using Kruskal’s algorithm.

    A graph with 6 vertices; see adjacency table following

    Adjacency Table
    Adjacency table with weights
      A B C E F G
    A - 11 33 14 41 27
    B 11 - 25 43 23 13
    C 33 25 - 17 37 36
    E 14 43 17 - 15 45
    F 41 23 37 15 - 19
    G 27 13 36 45 19 -
     
    Answer

    The graph with the added edges highlightedAB: Add, cost 11

    BG: Add, cost 13

    AE: Add, cost 14

    AG: Skip, would create circuit ABGA

    EF: Add, cost 16

    EC: Add, cost 17

    This completes the spanning tree


    This page titled 6.7: Spanning Trees is shared under a CC BY-SA 3.0 license and was authored, remixed, and/or curated by David Lippman (The OpenTextBookStore) via source content that was edited to the style and standards of the LibreTexts platform.