# Which Data Structure Is Used by Prim’s Algorithm to Find Minimum Spanning Tree?

//

Larry Thompson

Which Data Structure Is Used by Prim’s Algorithm to Find Minimum Spanning Tree?

When it comes to finding the minimum spanning tree (MST) of a graph, Prim’s algorithm is a popular choice. This algorithm, developed by mathematician Robert C. Prim in 1957, efficiently finds the MST by iteratively adding edges to a growing tree. One crucial aspect of Prim’s algorithm is the data structure used to keep track of the vertices and their associated weights.

## The Priority Queue

Prim’s algorithm relies on a priority queue to efficiently select the next edge to be added to the MST. A priority queue is a data structure that stores elements with associated priorities and provides efficient access to the element with the highest (or lowest) priority.

In this case, the priority queue used by Prim’s algorithm for finding the MST is often implemented as a min-heap. A min-heap is a binary tree where every parent node has a value smaller than or equal to its children. This structure allows for efficient retrieval of the smallest element in constant time.

## The Key Values

In addition to the priority queue, Prim’s algorithm also maintains an array called key values. The key value for each vertex represents the weight of the cheapest edge connecting it to the current MST. Initially, all key values are set to infinity except for one randomly chosen vertex, which has its key value set to 0.

The key values are updated during each iteration when an edge is added to the MST. If an edge connects a vertex with a lower key value than its current value, then this new edge becomes part of the MST and updates that vertex’s key value in both the priority queue and array.

## The Visited Array

Prim’s algorithm also utilizes a boolean array called the visited array. This array keeps track of which vertices have been added to the MST.

Initially, all vertices are marked as unvisited. As vertices are added to the MST, they are marked as visited to prevent revisiting them in subsequent iterations.

## The Final Minimum Spanning Tree

By combining the priority queue, key values, and visited array, Prim’s algorithm efficiently finds the minimum spanning tree of a graph. The process iteratively selects the edge with the lowest weight from the priority queue, adds it to the MST, updates key values and visited status for adjacent vertices, and continues until all vertices have been included in the MST.

In conclusion, Prim’s algorithm for finding a minimum spanning tree relies on several data structures to efficiently select edges and keep track of vertex weights. The priority queue (implemented as a min-heap), key values, and visited array work together to ensure that only the minimum-weight edges are added to form the final MST.

Privacy Policy