# pgRouting Manual (3.1)

## Prim - Family of functions

- pgr_prim

- pgr_primBFS

- pgr_primDD

- pgr_primDFS

Boost Graph Inside

- Supported versions: current(3.1) 3.0

## Description

The prim algorithm was developed in 1930 by Czech mathematician Vojtěch Jarník. It is a greedy algorithm that finds a minimum spanning tree for a weighted undirected graph. This means it finds a subset of the edges that forms a tree that includes every vertex, where the total weight of all the edges in the tree is minimized. The algorithm operates by building this tree one vertex at a time, from an arbitrary starting vertex, at each step adding the cheapest possible connection from the tree to another vertex.

This algorithms find the minimum spanning forest in a possibly disconnected graph; in contrast, the most basic form of Prim’s algorithm only finds minimum spanning trees in connected graphs. However, running Prim’s algorithm separately for each connected component of the graph, then it is called minimum spanning forest.

The main characteristics are:

- It’s implementation is only on undirected graph.

- Process is done only on edges with positive costs.

- When the graph is connected

  - The resulting edges make up a tree

- When the graph is not connected,

  - Finds a minimum spanning tree for each connected component.

  - The resulting edges make up a forest.

- Prim’s running time: \(O(E*log V)\)

Note

From boost Graph: “The algorithm as implemented in Boost.Graph does not produce correct results on graphs with parallel edges.”

## Inner query

Column | Type | Default | Description
--- | --- | --- | ---
id | `ANY-INTEGER` |  | Identifier of the edge.
source | `ANY-INTEGER` |  | Identifier of the first end point vertex of the edge.
target | `ANY-INTEGER` |  | Identifier of the second end point vertex of the edge.

cost

`ANY-NUMERICAL`

Weight of the edge (source, target)

- When negative: edge (source, target) does not exist, therefore it’s not part of the graph.

reverse_cost

`ANY-NUMERICAL`

-1

Weight of the edge (target, source),

- When negative: edge (target, source) does not exist, therefore it’s not part of the graph.

Where:

ANY-INTEGER

  SMALLINT, INTEGER, BIGINT

ANY-NUMERICAL

  SMALLINT, INTEGER, BIGINT, REAL, FLOAT

## See Also

- Spanning Tree - Category

- Boost: Prim’s algorithm documentation

- Wikipedia: Prim’s algorithm

Indices and tables

- Index

- Search Page