The short answer
Dijkstra’s algorithm and Prim’s algorithm are both greedy, and both use a priority queue to pick the next vertex. They solve different problems. Dijkstra’s algorithm finds the cheapest path from one source vertex to every other vertex. Prim’s algorithm finds a minimum spanning tree: a set of edges that connects every vertex, has no cycles, and has the lowest possible total weight.
What each one produces
Dijkstra’s algorithm outputs a distance from the source to each vertex, along with a shortest-path tree that shows the route to each one. The tree starts at the source, and the route to each vertex is the cheapest one by total weight.
Prim’s algorithm outputs a spanning tree. A spanning tree on V vertices has exactly V − 1 edges and touches every vertex. It does not care about routes from a single source. The tree is built from the cheapest edges that connect new vertices to the part already chosen.
How each one picks the next vertex
This is the key difference.
- Dijkstra’s algorithm ranks each candidate vertex by its total distance from the source, which is the sum of edge weights along the path.
- Prim’s algorithm ranks each candidate by the weight of the single edge that connects it to the tree. Path totals are never added up.
Because of this, the two can build different trees from the same graph.
A small example where they differ
Take three vertices, A, B and C, with these edges:
- A to B, weight 2
- B to C, weight 2
- A to C, weight 3
Start both algorithms at A.
Dijkstra’s algorithm: B is reached directly at distance 2. C can be reached directly at distance 3, or through B at 2 + 2 = 4. The direct edge is cheaper, so C is at distance 3. The shortest-path tree uses A to B and A to C, with a total weight of 2 + 3 = 5.
Prim’s algorithm: The cheapest edge out of A is A to B, with weight 2. The tree now holds A and B. The candidate edges are A to C (weight 3) and B to C (weight 2). The cheaper one is B to C, so the tree is A to B and B to C, with a total weight of 2 + 2 = 4.
Prim’s tree is lighter, so Dijkstra’s tree is not a minimum spanning tree here. Prim’s tree is not a shortest-path tree either. Its route from A to C goes through B and costs 4, while Dijkstra’s route costs 3.
Negative edge weights
Prim’s algorithm works with negative edge weights. It only compares individual edge weights when it builds the tree, so a negative edge is simply a cheap one. Dijkstra’s algorithm requires every edge weight to be zero or positive, because it adds weights along a path and relies on that total never shrinking as the path grows. If negative weights matter for shortest paths, see dijkstra algorithm vs bellman ford.
Running time
With a binary heap, both algorithms take about O(E log V) time on a connected graph, where V is the number of vertices and E is the number of edges. Dijkstra’s algorithm is written as O((V + E) log V), which is the same thing for a connected graph. The running times are similar, so speed rarely decides between them. The problem you are solving does.
Which one to use
Use Dijkstra’s algorithm when you need the cheapest route from one starting point to each other point, such as the fastest way to drive from one town to others.
Use Prim’s algorithm when you need to connect all the points as cheaply as possible, without caring about routes between particular pairs, such as laying out a network of cables.
The other common method for minimum spanning trees is Kruskal’s algorithm, which sorts all edges first and adds the cheapest ones that do not form a cycle. For that comparison, see prim’s algorithm vs kruskal.