# How does the prims algorithm work

## Prim algorithm

The Prim algorithm is used to find the minimal spanning tree of a graph. It can be used in graphs that are connected, weighted and undirected.

This chapter brings you closer to the Prim algorithm. We will show you why the Prim algorithm is important, what it is and how it is used. You can then use our exercises to check whether you have retained this knowledge.

Synonym:Algorithm of Prim

### Why is the Prim algorithm important?

In many cases, algorithms are used to solve optimization problems in the context of operations research in a company. One of these algorithms is the Prim algorithm. It can be used to to find the cheapest way in a graph, with which all nodes of a graph can be connected with each other. This can be useful, for example, when planning a logistics network.

### What is the Prim algorithm?

The Prim algorithm was originally developed in 1930 by the mathematician Vojtěch Jarník from what was then Czechoslovakia. Rediscovered in 1957 by Robert C. Prim, one speaks today in the literature of the Prim algorithm, not the algorithm of Jarník.

It is an algorithm from the group of greedy algorithms that can determine the minimum spanning tree in undirected, weighted and connected graphs. The minimal spanning tree of a graph is understood to be the subgraph that connects all nodes with one another in the most cost-effective way.

The Kruskal algorithm can also be used to determine the minimum spanning tree of a graph. However, the two algorithms differ in how they work.

The Kruskal algorithm starts with all nodes of the graph and looks at the globally most favorable edges one after the other. The Prim algorithm, on the other hand, starts with a node and inserts the most favorable edge at the current node. The starting point can be selected as desired, since ultimately all nodes must be included in the minimal spanning tree.

Gradually, the most favorable edge of the node under consideration is always inserted, i.e. the one that has the lowest edge weight in order to connect the next node.

### Application of the Prim algorithm

We show how to use the Prim algorithm with the following example:

The following graph is given:

The minimum spanning tree is to be determined for this graph. We use the Prim algorithm for this. Since the starting node in the Prim algorithm can be freely selected, we decide in our example for node B. When looking at node B, we notice that we find that four edges are adjacent to it. The shortest edge is the edge BE with an edge weight of 8.

So we add this edge to our new graph:

Then we consider the node E. Here we find that three edges are adjacent to the node. The edge with the lowest edge weight is the edge EC with a weight of 4.

So we insert the edge EC into our new graph:

These steps are now repeated for the other nodes in the graph until all nodes are connected to one another by the most favorable edges.

So we get the minimal spanning tree of the graph: