We may earn an affiliate commission when you visit our partners.

Prim's Algorithm

Save
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:
    1. Find the lowest-weight edge (u, v) that connects a vertex in S to a vertex in V.
    2. 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:

Share

Help others find this page about Prim's Algorithm: by sharing it with your friends and followers:

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 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.
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.
Table of Contents
Our mission

OpenCourser helps millions of learners each year. People visit us to learn workspace skills, ace their exams, and nurture their curiosity.

Our extensive catalog contains over 50,000 courses and twice as many books. Browse by search, by topic, or even by career interests. We'll match you to the right resources quickly.

Find this site helpful? Tell a friend about us.

Affiliate disclosure

We're supported by our community of learners. When you purchase or subscribe to courses and programs or purchase books, we may earn a commission from our partners.

Your purchases help us maintain our catalog and keep our servers humming without ads.

Thank you for supporting OpenCourser.

© 2016 - 2025 OpenCourser