Skip to main content

Section 34.14 Prims’s Algorithm

Another common goal in a weighted graph is to find the least expensive way to connect all the vertices together. Consider the graph below. It represents a number of buildings that we want to connect with fiber optic cable. We don’t care about how far it is from any particular building to any other particular building. We just want to make sure that all the buildings are connected together and that the total length of cable we need to use is as small as possible.
The minimum spanning tree for a graph connects all the vertices together with the least total weight.🔗
Figure 34.14.1. A set of building connected with the minimum total cost.
A collection of edges that connects all the vertices in a graph with as little cost as possible is known as a minimum spanning tree. To find a minimum spanning tree we can use Prim’s algorithm. It is almost identical to Dijkstra’s algorithm except that instead of tracking the cost to reach each vertex from the start vertex, it tracks the minimum cost to reach each vertex from any vertex in the tree that has already been built.
In Djikstra’s algorithm the cost function to access a neighbor looks like this:
costThroughVertex = costMap[minCostVertex] + graph.getEdgeWeight(minCostVertex, neighbor)
In Prim’s algorithm, we don’t worry about the cost involved in reaching the current vertex. We just care about the cost to reach the neighbor from the current vertex. So the cost function looks like this:
costThroughVertex = graph.getEdgeWeight(minCostVertex, neighbor)
Prims(graph, start):
  Map parentMap
  Map costMap
  Map knownMap
  costMap[start] = 0
  while costMap has a vertex that is not in knownMap:
    select lowest cost vertex that is not in knownMap as minCostVertex
    knownMap[minCostVertex] = true
    for each neighbor in graph.getNeighbors(minCostVertex):
      if neighbor not in knownMap:
        costThroughVertex = graph.getEdgeWeight(minCostVertex, neighbor)
        if (neighbor not in costMap) or (costThroughVertex < costMap[neighbor]):
          costMap[neighbor] = costThroughVertex
          parentMap[neighbor] = minCostVertex

  return parentMap
In Prim’s algorithm, the starting vertex is less meaningful than in Dijkstra’s algorithm. Picking any vertex within a connected set of vertices of the graph will produce a minimum spanning tree for those vertices.

Activity 34.14.1. Prim’s Algorithm.

Pick a vertex to find shortest paths from by entering a value into Start Vertex and then pressing Run Dijkstra. Then step through the animation.
This animation shows a cost table instead of a priority queue. Each time it needs to select the next vertex to visit, it will scan the cost table to find the vertex with the lowest cost that has not yet been visited. Making that table into a priority queue would make

Instructions.

When the > button is highlighted, an animation is prepared to run. Use the Step button to step through the animation one step at a time.
When no animation is in progress, you can use the data controls to start a new animation.
You can click and drag on the animation area to pan around. Use the Ctrl + mouse wheel (or touchpad scroll gesture) while over the animation area to zoom in and out.
  • Core Controls.
    • << : Skip to the beginning of the animation.
    • < : Step backwards one step in the animation.
    • > : Step forward one step in the animation.
    • >> : Skip to the end of the animation.
    • Auto Step Speed Set to anything other than off to automatically step through the animation at the selected speed.
    • Zoom Set the zoom level. You can also use Ctrl + Mouse Wheel to zoom in and out.
  • Graph Controls.
    • New Graph Randomly generate a new graph with the same settings.
    • Directed Graph Randomly generate a new directed graph.
    • Undirected Graph Randomly generate a new undirected graph.
    • Small Graph Randomly generate a graph with 8 vertices.
    • Large Graph Randomly generate a graph with 19 vertices.
    • Logical Representation Display current graph’s logical representation.
    • Adjacency List Representation Display current graph’s adjacency list representation.
    • Adjacency Matrix Representation Display current graph’s adjacency matrix representation.

Checkpoint 34.14.1.

Which are true about Prim’s algorithm?
  • It calculates the cost of a vertex B being considered from vertex A as the weight of the edge connecting them plus the cost from the starting vertex to vertex A
  • It finds a minimal spanning tree for the reachable vertices from the start vertex
  • It finds a shortest path from one vertex to all other reachable vertices
  • the cost of a vertex B being considered from vertex A as the weight of the edge connecting them
You have attempted of activities on this page.