Chapters

Hide chapters

Data Structures & Algorithms in Swift

Fourth Edition · iOS 15 · Swift 5.5 · Xcode 13

44. Prim’s Algorithm
Written by Vincent Ngo

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

In previous chapters, you’ve looked at depth-first and breadth-first search algorithms. These algorithms form spanning trees.

A spanning-tree is a subgraph of an undirected graph, containing all of the graph’s vertices, connected with the fewest number of edges. A spanning tree cannot contain a cycle and cannot be disconnected.

Here’s a graph G and all its possible spanning trees:

B C A B C A B C A B C A Spanning Trees, subgraph of G Graph G

From this undirected graph that forms a triangle, you can generate three different spanning trees in which you require only two edges to connect all vertices.

This chapter will look at Prim’s algorithm, a greedy algorithm used to construct a minimum spanning tree. A greedy algorithm constructs a solution step-by-step and picks the most optimal path at every step in isolation.

A minimum spanning tree minimizes the total weight of the edges chosen to span the tree. It is helpful in a variety of situations. For example, you might want to find the cheapest way to layout a network of water pipes.

Here’s an example of a minimum spanning tree for a weighted undirected graph:

B C A B C A Graph G total = 9 total = 10 8 1 B C A 8 1 8 Minimum Spanning Tree total = 3 B C A 1 2 2 2

Notice that only the third subgraph forms a minimum spanning tree since its total cost is 3.

Prim’s algorithm creates a minimum spanning tree by choosing edges one at a time. It’s greedy because every time you pick an edge, you pick the smallest weighted edge that connects a pair of vertices.

There are six steps to finding a minimum spanning tree with Prim’s algorithm:

5 1 5 3 1 3 3 5 2. Pick any vertex. 1. Given a network. 1 1 1 1 3 1 5 3 3 1 3. Choose the shortest weighted edge from this vertex. 4. Choose the nearest vertex that’s not in the solution. 5. If the next nearest vertex has two edges with the same weight, pick any one. 6. Repeat Step 1-5 till you have visited all vertices, forming a minimum spanning tree.

Example

Imagine the graph below represents a network of airports. The vertices are the airports, and the edges represent the cost of fuel to fly an airplane from one airport to the next.

5 2 6 2 5 6 4 3 1 6 3 5 5 6 4 1

Let’s start working through the example:

5 2 6 2 5 6 4 3 1 6 3 5 5 6 4 1 5 2 5 2 6 2 5 6 4 3 1 6 3 5 5 6 4 1 6 5 2 6 4 3 1 6 3 5 5 6 4 1 1 2 3

  1. Choose any vertex in the graph. Let’s assume you chose vertex 2.
  2. This vertex has edges with weights [6, 5, 3]. A greedy algorithm chooses the smallest-weighted edge.
  3. Choose the edge that has a weight of 3 and is connected to vertex 5.

5 2 6 2 6 4 3 1 6 3 5 5 6 4 1 5 2 6 2 5 6 4 3 1 6 3 5 5 6 4 1 5 2 6 5 3 2 6 4 1 6 3 5 5 4 1 1 2 3 5

  1. The explored vertices are {2, 5}.
  2. Choose the next shortest edge from the explored vertices. The edges are [6, 5, 6, 6]. You choose the edge with weight 5, which is connected to vertex 3.
  3. Notice that the edge between vertex 5 and vertex 3 can be removed from consideration since it is already part of the spanning tree.

5 2 6 2 6 4 3 1 6 3 5 5 6 4 1 5 2 6 2 5 6 4 3 1 6 3 5 5 4 1 5 2 6 5 3 2 6 4 1 3 5 5 4 1 1 2 3 5

  1. The explored vertices are {2, 3, 5}.
  2. The next potential edges are [6, 1, 5, 4, 6]. You choose the edge with weight 1, which is connected to vertex 1.
  3. The edge between vertex 2 and vertex 1 can be removed.

5 2 6 2 6 4 3 1 3 5 5 4 1 5 2 6 2 5 6 4 3 1 3 5 5 4 1 5 2 5 3 2 6 4 1 3 5 5 4 1 1 2 3 5

  1. The explored vertices are {2, 3, 5, 1}.
  2. Choose the next shortest edge from the explored vertices. The edges are [5, 5, 4, 6]. You choose the edge with weight 4, which is connected to vertex 6.
  3. The edge between vertex 5 and vertex 6 can be removed.

5 2 2 6 4 3 1 3 5 5 4 1 5 2 2 5 6 4 3 1 3 5 5 4 1 2 5 3 2 6 4 1 3 5 4 1 1 2 3 5

  1. The explored vertices are {2, 5, 3, 1, 6}.
  2. Choose the next shortest edge from the explored vertices. The edges are [5, 5, 2]. You choose the edge with weight 2, which is connected to vertex 4.
  3. The edges [5, 5] connected to vertex 4 from vertex 1 and vertex 3 can be removed.

Note: If all edges have the same weight, you can pick any one of them.

2 5 6 4 3 1 3 5 2 4 1

This final diagram is the minimum spanning tree from our example produced by Prim’s algorithm.

Next, let’s see how to build this in code.

Implementation

Open up the starter playground for this chapter. This playground comes with an adjacency list graph and a priority queue, which you will use to implement Prim’s algorithm.

public class Prim<T: Hashable> {

  public typealias Graph = AdjacencyList<T>
  public init() {}
}

Helper methods

Before building the algorithm, you’ll create some helper methods to keep you organized and consolidate duplicate code.

Copying a graph

To create a minimum spanning tree, you must include all vertices from the original graph. Open up AdjacencyList.swift and add the following to class AdjacencyList:

public func copyVertices(from graph: AdjacencyList) {
  for vertex in graph.vertices {
    adjacencies[vertex] = []
  }
}

Finding edges

Besides copying the graph’s vertices, you also need to find and store the edges of every vertex you explore. Open up Prim.swift and add the following to class Prim:

internal func addAvailableEdges(
    for vertex: Vertex<T>,
    in graph: Graph,
    check visited: Set<Vertex<T>>,
    to priorityQueue: inout PriorityQueue<Edge<T>>) {
  for edge in graph.edges(from: vertex) { // 1
    if !visited.contains(edge.destination) { // 2
      priorityQueue.enqueue(edge) // 3
    }
  }
}

Producing a minimum spanning tree

Add the following method to class Prim:

public func produceMinimumSpanningTree(for graph: Graph)
    -> (cost: Double, mst: Graph) { // 1
  var cost = 0.0 // 2
  let mst = Graph() // 3
  var visited: Set<Vertex<T>> = [] // 4
  var priorityQueue = PriorityQueue<Edge<T>>(sort: { // 5
    $0.weight ?? 0.0 < $1.weight ?? 0.0
  })
  // to be continued
}
mst.copyVertices(from: graph) // 1

guard let start = graph.vertices.first else { // 2
  return (cost: cost, mst: mst)
}

visited.insert(start) // 3
addAvailableEdges(for: start, // 4
                   in: graph,
                check: visited,
                   to: &priorityQueue)

// to be continued
while let smallestEdge = priorityQueue.dequeue() { // 1
  let vertex = smallestEdge.destination // 2
  guard !visited.contains(vertex) else { // 3
    continue
  }

  visited.insert(vertex) // 4
  cost += smallestEdge.weight ?? 0.0 // 5

  mst.add(.undirected, // 6
          from: smallestEdge.source,
          to: smallestEdge.destination,
          weight: smallestEdge.weight)

  addAvailableEdges(for: vertex, // 7
                    in: graph,
                    check: visited,
                    to: &priorityQueue)
}

return (cost: cost, mst: mst) // 8

Testing your code

5 2 6 2 5 6 4 3 1 6 3 5 5 6 4 1

let (cost,mst) = Prim().produceMinimumSpanningTree(for: graph)
print("cost: \(cost)")
print("mst:")
print(mst)
cost: 15.0
mst:
5 ---> [ 2 ]
6 ---> [ 3, 4 ]
3 ---> [ 2, 1, 6 ]
1 ---> [ 3 ]
2 ---> [ 5, 3 ]
4 ---> [ 6 ]
2 1 8 2 8 7 8 8 3 4 2

Performance

In the algorithm above, you maintain three data structures:

Key points

  • A spanning tree is a subgraph of an undirected graph containing all the vertices with the fewest edges.
  • Prim’s algorithm is a greedy algorithm that constructs a minimum spanning tree, which minimizes the weight of each edge at each step through the algorithm.
  • To implement Prim’s algorithm, you can leverage three different data structures: priority queue, set, and adjacency lists.
Have a technical question? Want to report a bug? You can ask questions and report bugs to the book authors in our official book forum here.
© 2024 Kodeco Inc.

You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now