Skip to main content
Library homepage
 

Text Color

Text Size

 

Margin Size

 

Font Type

Enable Dyslexic Font
Mathematics LibreTexts

10.10: Spanning Tree Algorithms

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

Definition

Given a connected graph G, a spanning tree of G is a subgraph of G which is a tree and includes all the vertices of G.

We also provided the ideas of two algorithms to find a spanning tree in a connected graph.

Start with the graph connected graph G. If there is no cycle, then the G is already a tree and we are done. If there is a cycle, let e be any edge in that cycle and consider the new graph G1=Ge (i.e., the graph you get by deleting e). This tree is still connected since e belonged to a cycle, there were at least two paths between its incident vertices. Now repeat: if G1 has no cycles, we are done, otherwise define G2 to be G1e1, where e1 is an edge in a cycle in G1. Keep going. This process must eventually stop, since there are only a finite number of edges to remove. The result will be a tree, and since we never removed any vertex, a spanning tree.

This is by no means the only algorithm for finding a spanning tree. You could have started with the empty graph and added edges that belong to G as long as adding them would not create a cycle. You have some choices as to which edges you add first: you could always add an edge adjacent to edges you have already added (after the first one, of course), or add them using some other order. Which spanning tree you end up with depends on these choices.

We now provide two algorithms that follow this latter idea of starting with an empty graph and adding edges until we have formed a tree.

Depth-first search algorithm

  • Input G, a connected graph with vertices v1,v2,,vn
  • Let T:=tree with no edges and only the vertex v1
  • visit(v1) (i.e. apply the procedure visit to v1)

VISIT

  • Input v, a vertex of a graph G
  • For each vertex w adjacent to v and not yet in T
    • add vertex w and edge {v,w} to T
    • visit(w)

Notice that we can pick our starting vertex arbitrarily and adjacent vertices in any order. Label the vertices in the graph below, pick a starting vertex, and use depth-first search to find a spanning tree of the graph below.

clipboard_ef16f6ac207b1061dd74191c23b0d6952.png

Breadth-first search algorithm

  • Input G, a connected graph with vertices v1,v2,,vn
  • Let T:=tree with no edges and only the vertex v1
  • Let L:=empty list
  • Put v1 in L, a list of unprocessed vertices
  • While L is not empty
    • remove the first vertex v from L
    • for each neighbor w of v
      • if w is not in L and not in T
        • add w to the end of the list L
        • add w and the edge {v,w} to T

Using the same graph, same labels, and same starting vertex, apply the breadth-first search algorithm to find a spanning tree of the graph above.

Find a big-O estimate of the number of steps these algorithms require.

Spanning trees in weighted graphs

In the graph below, there is an oil well located at the left-most vertex, while other vertices represent storage facilities.

clipboard_e0c64c75e16cf2f93bc1b5d04437493e4.png

  1. Suppose the edges represent the possible pipelines we could build, and the weight on an edge represents the cost of building that edge (in millions of dollars). Which pipelines should we build so that we can transport oil from the well to each storage facility, but we want to spend as little money as possible? What is the total cost of building these pipelines? Describe an algorithm that could be used to solve this problem.
  2. Again, suppose the edges represent the possible pipelines we could build. Now suppose that the weight on each edge represents the time it takes for oil to get through that section of the pipeline (in hours). Which pipelines should we build so that we can transport oil from the well to each storage facility as quickly as possible? Describe an algorithm that could be used to solve this problem.

Definition

A minimum spanning tree in a connected weighted graph is a spanning tree with minimum possible total edge weight.

A shortest path spanning tree from v in a connected weighted graph is a spanning tree such that the distance from v to any other vertex u is as small as possible.

We present below two common algorithms used to find minimum spanning trees.

Prim's algorithm

  • Input: G, a connected weighted graph with n vertices
  • Let T:=any edge with minimum weight
  • for i from 1 to n2
    • let e:=an edge of minimum weight among those incident to a vertex in T that will not form a cycle in T if added to it
    • T:=Te
  • Return T

Kruskal's algorithm

  • Input: G, a connected weighted graph with n vertices
  • Let T be an empty graph
  • for i from 1 to n1
    • let e:=an edge in G of minimum weight among those that do not form a cycle in T if added to it
    • T:=Te
  • Return T

Notice the difference between the two algorithms. In Prim's edges that are incident to a vertex already in the tree are added, while in Kruskal's the edges that are added need not be incident to a vertex already in the tree.

We now present an algorithm that creates a shortest path spanning tree from a given vertex.

Shortest path spanning tree algorithm

  • Input: G, a connected weighted graph with n vertices v1,v2,,vnand with source vertex v1
  • Apply Dijkstra's algorithm (Section 5.7) to G
  • Let T be an empty graph
  • For i from 2 to n
    • Add edge e={vi,prev(vi)} to T
  • Return T

10.10: Spanning Tree Algorithms is shared under a CC BY-NC-SA license and was authored, remixed, and/or curated by LibreTexts.

  • Was this article helpful?

Support Center

How can we help?