logo
SEE ALGORITHMS
    Bubble Sort
    Insertion Sort
    Selection Sort
    Heap Sort
    Radix Sort
    Merge Sort
    Quick Sort

Prim's Algorithm

Prim's Algorithm builds a Minimum Spanning Tree (MST) by starting with a single node and "growing" the tree outward. At every step, it finds the cheapest edge that connects a node inside the growing tree to a node outside of it. This greedy approach continues until all nodes are part of the tree, ensuring the total weight of all edges is as low as possible. It is perfect for problems like designing cost-effective road or utility networks.

Step by Step

  • Initialize an empty set of edges for the MST.
  • Start with an arbitrary vertex and mark it as visited (part of MST).
  • Find the cheapest edge connecting a vertex in the MST to a vertex outside the MST.
  • Add this edge to the MST and mark the new vertex as visited.
  • Repeat until all vertices are part of the MST.
Draw Graph

Curious to Learn More?

Hand-picked resources to deepen your understanding

Beginner Friendly
Coding Interview Bootcamp: Algorithms + Data Structures

Learn essential data structures and algorithms step-by-step with practical JavaScript examples.

Practical Guide
JavaScript Algorithms & Data Structures Masterclass

Master DSA fundamentals, problem-solving techniques, and advanced structures using JavaScript.

Deep Dive
Master the Coding Interview: Data Structures + Algorithms

Prepare for top tech interviews with advanced DSA concepts and real-world coding challenges.

As an Udemy Associate, I earn from qualifying purchases. This helps support the site at no extra cost to you.

© 2025 SEE Algorithms. Code licensed under MIT, content under CC BY-NC 4.0.