May 1, 2024
3 minute read
Prim's Algorithm is a specialized graph traversal algorithm used to find minimum spanning trees, or the subgraphs connecting all nodes in a connected, weighted graph with the least possible total edge weight, similar to Kruskal's Algorithm. Prim's Algorithm starts with an empty spanning tree. It then grows the tree by adding one edge at a time, until all vertices are included. The edge added at each step is the lowest-weight edge that connects a vertex in the tree to a vertex not yet in the tree.
How Prim's Algorithm Works
To run Prim's Algorithm, we start by selecting any vertex in the graph as the initial vertex.
The algorithm then works as follows:
- Create a set S to store the vertices that have been added to the spanning tree and another set V to hold the other vertices.
- Initially, S contains the initial vertex. V contains all the other vertices in the graph.
- While V is not empty, do the following:
- Find the lowest-weight edge (u, v) that connects a vertex in S to a vertex in V.
- Add the edge (u, v) to S. Move vertex v from V to S.
Example of Prim's Algorithm
Consider the following graph with 7 vertices and 11 edges:

If we start Prim's Algorithm at node A, we would get the following minimum spanning tree with total edge weight of 39:

Variations of Prim's Algorithm
There are a few variations of Prim's Algorithm:
m5ytr4|
Find a path to becoming a Prim's Algorithm. Learn more at:
OpenCourser.com/topic/m5ytr4/prim
Reading list
We've selected nine books
that we think will supplement your
learning. Use these to
develop background knowledge, enrich your coursework, and gain a
deeper understanding of the topics covered in
Prim's Algorithm.
Focuses specifically on graph algorithms, including a detailed chapter on minimum spanning trees that covers Prim's algorithm. It is suitable for advanced undergraduate and graduate students.
Provides a comprehensive overview of graph theory, including a chapter on minimum spanning trees that covers Prim's algorithm. It is suitable for advanced undergraduate and graduate students.
Provides a comprehensive overview of algorithms, including a chapter on graph algorithms that covers Prim's algorithm in detail. It is suitable for both beginners and advanced readers.
This German translation of the classic textbook Introduction to Algorithms covers a wide range of algorithms, including a section on graph algorithms that includes a detailed discussion of Prim's algorithm. It is suitable for both undergraduate and graduate students.
Provides a comprehensive overview of algorithms in French, including a chapter on graph algorithms that covers Prim's algorithm. It is suitable for both undergraduate and graduate students.
This practical guide to algorithm design includes a chapter on graph algorithms that covers Prim's algorithm in detail. It is suitable for both beginners and experienced programmers.
Provides a comprehensive overview of data structures, algorithms, and applications in Java, including a chapter on graph algorithms that covers Prim's algorithm. It is suitable for both undergraduate and graduate students.
This textbook covers a wide range of topics in discrete mathematics, including a chapter on graph theory that covers Prim's algorithm. It is suitable for both undergraduate and graduate students.
Provides a concise overview of essential algorithms, including a chapter on graph algorithms that covers Prim's algorithm. It is suitable for both beginners and experienced programmers.
For more information about how these books relate to this course, visit:
OpenCourser.com/topic/m5ytr4/prim