Graphs

Problem shape

Intermediate
A
B
C
D
E
Core idea

Grow a tree by repeatedly adding the cheapest edge leaving the current tree.

Step 1 / 5
Problem shape
Current lesson

Prim MST Lab

Graphs Intermediate

Prim MST

Welcome to the Prim MST algorithm lab. Grow a tree by repeatedly adding the cheapest edge leaving the current tree. Use it when: Use for minimum spanning tree with adjacency lists and priority queues. The time complexity is O(E log V), and the space complexity is O(V). The pseudocode is: start from any node push outgoing edges while tree incomplete take cheapest edge to unseen node add node and its outgoing edges As you practice, track the state that changes and the condition that makes the algorithm stop.

Time

O(E log V)

Space

O(V)

Pseudocode

start from any node
push outgoing edges
while tree incomplete
  take cheapest edge to unseen node
  add node and its outgoing edges
Open deeper structure lesson
1.0x