Log in Sign up
Back to Discover
🔢

Prim's algorithm

math Maturity 7-9

We can connect dots with lines. We want to use the shortest lines. We pick the smallest line first. Then we find the next small line. This helps us save space. It is like making a path.

PrimAlgDemo.gif
PrimAlgDemo.gif
Can you find the shortest way?

46 words

Imagine you have many dots. You want to connect them with lines. You want to use the shortest lines possible. This helps you save space and work.

First, you pick one dot to start. Then, you find the cheapest line to a new dot.

PrimAlgDemo.gif
PrimAlgDemo.gif
You keep adding the smallest lines one by one.

A man named Vojtěch Jarník found this idea in 1930. Later, Robert Prim and others found it too. This way of solving puzzles is called an algorithm. It helps us build paths and even mazes.

91 words

Imagine you have many dots on a page. You want to connect them all with lines. Each line has a cost, like a price. You want to connect every dot using the lowest total cost. This goal is to find a minimum spanning tree. A tree is a set of lines that connects all dots without making any loops.

To do this, you use a greedy algorithm. A greedy algorithm is a set of steps that always picks the best choice right now.

PrimAlgDemo.gif
PrimAlgDemo.gif
First, pick any dot to start. Then, find the cheapest line that connects a dot in your group to a new dot. Keep doing this one step at a time.
MAZE 30x20 Prim.ogv
MAZE 30x20 Prim.ogv
This method can even help make a maze.

A man named Vojtěch Jarník found this way in 1930. Later, Robert Prim and Edsger Dijkstra found it again. Because of them, it has many names. It can also find a minimum spanning forest. This happens if the dots are in separate groups that do not touch.

174 words

Imagine you have a collection of dots on a map. Each dot is a location, like a house or a city. You want to connect every single dot using lines, but lines are expensive to build. You want to find the cheapest way to link them all together. This goal is to find a minimum spanning tree. A tree is a special shape that connects all points without any loops. By finding the minimum spanning tree, you use the lowest total cost possible.

To solve this, you use a greedy algorithm. A greedy algorithm is a way of working that always picks the best option available right now.

PrimAlgDemo.gif
PrimAlgDemo.gif
You start by picking any dot to be your first point. From that dot, you look for the cheapest line that connects to a new, unvisited dot. Once you add that line, you now have a small group of connected dots. You keep looking for the cheapest line that connects your group to a dot outside the group. You repeat these steps until every dot is part of your tree.
MAZE 30x20 Prim.ogv
MAZE 30x20 Prim.ogv

This clever way of thinking has a long history. A mathematician named Vojtěch Jarník first developed this method in 1930. Later, computer scientists Robert C. Prim and Edsger W. Dijkstra found it again. Because of these different people, the method has many names. It is sometimes called the Jarník algorithm or the Prim–Dijkstra algorithm. Some people even call it the DJP algorithm.

There are other ways to solve this same puzzle. You might hear about Kruskal's algorithm or Borůvka's algorithm. These other methods can find a minimum spanning forest. A forest is what you get if the dots are in separate groups that do not touch. Prim's algorithm is very good at handling dense graphs. A dense graph is one where there are many possible lines between the dots. In these cases, the algorithm can be made to run in linear time.

Distributed adjacency matrix for parallel prim.png
Distributed adjacency matrix for parallel prim.png

This math is useful for many real-world tasks. It can be used to create complex mazes. It also helps computers decide how to build networks. For example, it could help plan how to connect houses to water pipes or electricity. The speed of the algorithm depends on how you organize the data. Using a special tool called a priority queue can make the search much faster. This helps computers solve huge problems very quickly.

409 words

Prim's algorithm is a greedy algorithm used in computer science. It is designed to find a minimum spanning tree for a weighted undirected graph. A graph consists of vertices, which are points, and edges, which are the lines connecting them. In a weighted graph, every edge has a specific value or cost assigned to it. A minimum spanning tree is a subset of these edges that connects every single vertex without forming any loops. The goal is to ensure the total weight of all chosen edges is as small as possible.

The algorithm works by building the tree one vertex at a time. It begins at an arbitrary starting vertex chosen from the set. At each step, the algorithm looks for the cheapest possible connection between a vertex already in the tree and a vertex that is not yet included. This is a greedy approach because it always selects the immediate best option. The algorithm maintains a list of unexplored vertices and tracks the minimum cost to reach each one. As each new vertex is added, the algorithm updates the potential connection costs for its neighbors.

PrimAlgDemo.gif
PrimAlgDemo.gif

There are different ways to implement the algorithm depending on the data structures used. One common method uses an adjacency matrix or an adjacency list to represent the graph. To find the next minimum weight edge, a programmer might use a simple array. However, more advanced implementations use a priority queue to manage the vertices. A priority queue is a data structure that helps quickly find the vertex with the minimum cost. The choice of these structures directly impacts the time complexity, or how fast the algorithm runs.

Distributed adjacency matrix for parallel prim.png
Distributed adjacency matrix for parallel prim.png

The history of this mathematical discovery spans several decades and different researchers. The Czech mathematician Vojtěch Jarník originally developed the method in 1930. Later, the algorithm was rediscovered and republished by computer scientists Robert C. Prim in 1957 and Edsger W. Dijkstra in 1959. Because of these contributors, the method is known by several names. It is often called the Jarník algorithm, the Prim–Jarník algorithm, the Prim–Dijkstra algorithm, or the DJP algorithm.

Efficiency is a major focus when studying the complexity of Prim's algorithm. A simple implementation using an adjacency matrix and linear searching requires O(|V|²) running time. By using a binary heap to store vertices, the time can be improved to O(|E| log |V|). If a more sophisticated Fibonacci heap is used, the complexity becomes O(|E| + |V| log |V|). This is particularly useful for dense graphs, where the number of edges is high. For very dense graphs, the algorithm can even be made to run in linear time using a d-ary heap.

MAZE 30x20 Prim.ogv
MAZE 30x20 Prim.ogv

Prim's algorithm is not the only way to solve this problem. Other well-known methods include Kruskal's algorithm and Borůvka's algorithm. While the basic version of Prim's algorithm finds a tree in a connected graph, it can be modified. By running the algorithm separately for each connected component, it can find a minimum spanning forest. A forest is a collection of trees that may exist if the graph is disconnected. These different algorithms are equally fast for sparse graphs, but they vary in performance as the graph becomes denser.

This algorithm has many practical applications in the real world. One interesting use is in the generation of complex mazes using a randomly weighted grid graph. It also relates to broader topics in computer science, such as parallel computing. While the main loop of the algorithm is sequential, the inner loop can be parallelized. This means multiple processors can work together to inspect edges and update costs. Such techniques allow the algorithm to run on distributed machines and shared memory systems.

Distributed adjacency matrix for parallel prim.png
Distributed adjacency matrix for parallel prim.png

628 words
🖼️ Images & Media (5)
File:PrimAlgDemo.gif
PrimAlgDemo.gif
File:Prim's algorithm.svg
Prim's algorithm.svg
MAZE 30x20 Prim.ogv
File:Prim's algorithm proof.svg
Prim's algorithm proof.svg
File:Distributed adjacency matrix for parallel prim.png
Distributed adjacency matrix for parallel prim.png
Up Next
🔢
Kruskal's algorithm
Math
More to explore

What is Nepedia?

A free, ad-free encyclopedia for children. Every article is written at five reading levels, so the same page works for a five-year-old and a fifteen-year-old — use the level switcher above to see this one change. No account needed to read.