Prim's Algorithm

Builds a minimum spanning tree by repeatedly adding the cheapest edge from the growing tree to a new node.

Optimal:YesWeighted:YesDirected:NoNegative weights:Yes
Complexity
Best
O(E log V)
Average
O(E log V)
Worst
O(E log V)
Space
O(V + E)
When to use: When you need a minimum spanning tree of a connected, weighted, undirected graph.Simple conceptually (grow a tree)Requires connectedness for a full MST
Pseudocode
choose start node s; MST = {s}
while MST does not contain all nodes:
pick minimum-weight edge (u,v) with u in MST and v not in MST
add v to MST
update candidate edges crossing MST → outside
return MST
Waiting to start…