[ library(graph_algorithms) | Reference Manual | Alphabetic Index ]

minimum_spanning_forest(+Graph, +DistanceArg, -Forest, -ForestSize, -ForestWeight)

Computes a minimum spanning forest, its size and weight
a graph structure
which argument of EdgeData to use as distance: integer
a list of e/3 edge structures
the number of edges in the Forest list
sum of the forest's edge weights: number


Computes a minimum spanning forest for the given graph. A minimum spanning forest is a smallest subset of the graph's edges that still connects all the graph's connected components. Such a forest is not unique, but all minimum spanning forests will have the same cost. As opposed to a minimum spanning tree, a forest exists also if the original graph is not connnected. The forest will have the same number of connected components as the original graph.

The computed forest is returned in Forest, which is simply a list of the edges that form the forest. The ForestSize is the number of edges that constitute the forest. The ForestWeight is the total length of the forest's edges, according to DistanceArg.

DistanceArg refers to the graph's EdgeData information that was specified when the graph was constructed. If EdgeData is a simple number, then DistanceArg should be 0 and EdgeData will be taken as the length of the edge. If EdgeData is a compound data structure, DistanceArg should be a number between 1 and the arity of that structure and determines which argument of the EdgeData structure will be interpreted as the edge's length. Important: the distance information in EdgeData must be a non-negative number, and the numeric type (integer, float, etc) must be the same in all edges.

If DistanceArg is given as -1, then any EdgeData is ignored and the length of every edge is assumed to be equal to 1.

The direction of the graph's edges is ignored by this predicate.

The implementation uses Kruskal's algorithm which has a complexity of O(Nedges*log(Nedges)).

Modes and Determinism


    ?- sample_graph(G), minimum_spanning_forest(G, 0, T, S, W).
    T = [e(2, 10, 1), e(4, 8, 1), e(9, 2, 1), e(7, 3, 2), ...]
    S = 8
    W = 16

See Also

minimum_spanning_tree / 4