aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
gum::SpanningForestPrim Class Reference

The Prim algorithm for computing min cost spanning trees or forests. More...

#include <spanningForestPrim.h>

Inheritance diagram for gum::SpanningForestPrim:
Collaboration diagram for gum::SpanningForestPrim:

Public Member Functions

Constructors / Destructors
 SpanningForestPrim (const UndiGraph *graph, const EdgeProperty< float > *costTable)
 Default constructor.
 SpanningForestPrim (const SpanningForestPrim &toCopy)
 Copy constructor.
virtual ~SpanningForestPrim ()
 Destructor.
Accessors / Modifiers
const EdgeSetedgesInSpanningForest ()
 Returns the edges in a min cost spanning forest.
const UndiGraphspanningForest ()
 Construct the spanning forest.
float costOfSpanningForest ()
 Returns the cost of the spanning forest.

Private Member Functions

void _compute_ ()
 Computes the spanning forest.
void _computeInAComponent_ (const NodeId id)
 compute a spanning tree in a given connected component of graph
void _exploreNode_ (const NodeId id)
 explore the neighborhood of a node belonging to the spanning tree
SpanningForestPrimoperator= (const SpanningForestPrim &toCopy)
 Copy operator: private to prevent using it.

Private Attributes

const UndiGraph_graph_
 the graph the spanning tree of which we wish to compute
const EdgeProperty< float > & _costTable_
 the costs of the edges
PriorityQueue< Edge, float > _edgesToExplore_
 the next edges that may be added to the spanning tree
UndiGraph _spanning_tree_
 the computed spanning tree
float _spanning_tree_cost_
 the cost of the spanning tree
bool _require_computation_
 a Boolean indicating whether we need recompute the spanning tree

Detailed Description

The Prim algorithm for computing min cost spanning trees or forests.

Binary heap implementation : O(E log(V))

Definition at line 63 of file spanningForestPrim.h.

Constructor & Destructor Documentation

◆ SpanningForestPrim() [1/2]

gum::SpanningForestPrim::SpanningForestPrim ( const UndiGraph * graph,
const EdgeProperty< float > * costTable )

Default constructor.

Note that this algorithm takes into account the fact that the graph given in input is not connected (that, is, it may contain several connected components)

Parameters
graphthe graph the spanning forest of which we look for
costTablethe cost for each edge of graph
Warning
note that, by aGrUM's rule, the graph and the costs are not copied but only referenced by the elimination sequence algorithm.
Exceptions
GraphErrorif the grand and/or the cost table are null pointers

Definition at line 55 of file spanningForestPrim.cpp.

56 :
59 if (!graph || !cost) { GUM_ERROR(GraphError, "invalid null graph or edge cost pointer") }
60
61 // for debugging purposes
62 GUM_CONSTRUCTOR(SpanningForestPrim);
63 }
SpanningForestPrim(const UndiGraph *graph, const EdgeProperty< float > *costTable)
Default constructor.
const EdgeProperty< float > & _costTable_
the costs of the edges
bool _require_computation_
a Boolean indicating whether we need recompute the spanning tree
float _spanning_tree_cost_
the cost of the spanning tree
const UndiGraph & _graph_
the graph the spanning tree of which we wish to compute
SpanningForest()
default constructor
#define GUM_ERROR(type, msg)
Definition exceptions.h:72

References gum::SpanningForest::SpanningForest(), SpanningForestPrim(), _costTable_, _graph_, _require_computation_, _spanning_tree_cost_, and GUM_ERROR.

Referenced by SpanningForestPrim(), SpanningForestPrim(), ~SpanningForestPrim(), and operator=().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ SpanningForestPrim() [2/2]

gum::SpanningForestPrim::SpanningForestPrim ( const SpanningForestPrim & toCopy)

Copy constructor.

Definition at line 66 of file spanningForestPrim.cpp.

66 :
67 SpanningForest(), _graph_(from._graph_), _costTable_(from._costTable_),
68 _edgesToExplore_(from._edgesToExplore_), _spanning_tree_(from._spanning_tree_),
69 _spanning_tree_cost_(from._spanning_tree_cost_),
70 _require_computation_(from._require_computation_) {
71 // for debugging purposes
72 GUM_CONS_CPY(SpanningForestPrim);
73 }
UndiGraph _spanning_tree_
the computed spanning tree
PriorityQueue< Edge, float > _edgesToExplore_
the next edges that may be added to the spanning tree

References gum::SpanningForest::SpanningForest(), SpanningForestPrim(), _costTable_, _edgesToExplore_, _graph_, _require_computation_, _spanning_tree_, and _spanning_tree_cost_.

Here is the call graph for this function:

◆ ~SpanningForestPrim()

gum::SpanningForestPrim::~SpanningForestPrim ( )
virtual

Destructor.

Definition at line 76 of file spanningForestPrim.cpp.

76 {
77 // for debugging purposes
78 GUM_DESTRUCTOR(SpanningForestPrim);
79 }

References SpanningForestPrim().

Here is the call graph for this function:

Member Function Documentation

◆ _compute_()

void gum::SpanningForestPrim::_compute_ ( )
private

Computes the spanning forest.

compute the spanning forest

Definition at line 103 of file spanningForestPrim.cpp.

103 {
104 // compute a spanning tree in every connected component
105 for (const auto node: _graph_.nodes()) {
106 if (!_spanning_tree_.existsNode(node)) { _computeInAComponent_(node); }
107 }
108
109 // indicate that everything was computed
110 _require_computation_ = false;
111 }
void _computeInAComponent_(const NodeId id)
compute a spanning tree in a given connected component of graph

References _computeInAComponent_(), _graph_, _require_computation_, and _spanning_tree_.

Referenced by costOfSpanningForest(), edgesInSpanningForest(), and spanningForest().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ _computeInAComponent_()

void gum::SpanningForestPrim::_computeInAComponent_ ( const NodeId id)
private

compute a spanning tree in a given connected component of graph

compute a spanning tree

Definition at line 114 of file spanningForestPrim.cpp.

114 {
115 // add the node to the spanning tree
116 _spanning_tree_.addNodeWithId(id);
117
118 // explore its neighborhood
119 _exploreNode_(id);
120
121 // get the next nodes to link to the current spanning tree nodes
122
123 while (!_edgesToExplore_.empty()) {
124 const Edge edge = _edgesToExplore_.pop();
125 const NodeId first = edge.first();
126 const NodeId second = edge.second();
127
128 // consider only the edges that have one extremal node not in the spanning
129 // tree as those that can be added to the tree
130
131 if (!_spanning_tree_.existsNode(first)) {
132 // add the edge to the spanning tree
133 _spanning_tree_.addNodeWithId(first);
134 _spanning_tree_.addEdge(first, second);
136
137 // We must explore the first node's neighborhood
138 _exploreNode_(first);
139 } else if (!_spanning_tree_.existsNode(second)) {
140 // add the edge to the spanning tree
141 _spanning_tree_.addNodeWithId(second);
142 _spanning_tree_.addEdge(first, second);
144
145 // We must explore the second node
146 _exploreNode_(second);
147 }
148 }
149 }
void _exploreNode_(const NodeId id)
explore the neighborhood of a node belonging to the spanning tree
Size NodeId
Type for node ids.

References _costTable_, _edgesToExplore_, _exploreNode_(), _spanning_tree_, _spanning_tree_cost_, gum::Edge::first(), and gum::Edge::second().

Referenced by _compute_().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ _exploreNode_()

void gum::SpanningForestPrim::_exploreNode_ ( const NodeId id)
private

explore the neighborhood of a node belonging to the spanning tree

Definition at line 152 of file spanningForestPrim.cpp.

152 {
153 // add its neighbors _edgesToExplore_ to indicate that they are
154 // tensor next nodes to explore
155 for (const auto node: _graph_.neighbours(id)) {
156 if (!_spanning_tree_.existsNode(node)) {
157 Edge edge(node, id);
158 _edgesToExplore_.insert(edge, _costTable_[edge]);
159 }
160 }
161 }

References _costTable_, _edgesToExplore_, _graph_, and _spanning_tree_.

Referenced by _computeInAComponent_().

Here is the caller graph for this function:

◆ costOfSpanningForest()

float gum::SpanningForestPrim::costOfSpanningForest ( )
virtual

Returns the cost of the spanning forest.

Returns
cost of the spanning forest

Implements gum::SpanningForest.

Definition at line 82 of file spanningForestPrim.cpp.

82 {
84
86 }
void _compute_()
Computes the spanning forest.

References _compute_(), _require_computation_, and _spanning_tree_cost_.

Here is the call graph for this function:

◆ edgesInSpanningForest()

const EdgeSet & gum::SpanningForestPrim::edgesInSpanningForest ( )
virtual

Returns the edges in a min cost spanning forest.

Returns
edges in the spanning forest

Implements gum::SpanningForest.

Definition at line 89 of file spanningForestPrim.cpp.

89 {
91
92 return _spanning_tree_.edges();
93 }

References _compute_(), _require_computation_, and _spanning_tree_.

Here is the call graph for this function:

◆ operator=()

SpanningForestPrim & gum::SpanningForestPrim::operator= ( const SpanningForestPrim & toCopy)
private

Copy operator: private to prevent using it.

References SpanningForestPrim().

Here is the call graph for this function:

◆ spanningForest()

const UndiGraph & gum::SpanningForestPrim::spanningForest ( )
virtual

Construct the spanning forest.

Returns
the spanning forest

Implements gum::SpanningForest.

Definition at line 96 of file spanningForestPrim.cpp.

96 {
98
99 return _spanning_tree_;
100 }

References _compute_(), _require_computation_, and _spanning_tree_.

Here is the call graph for this function:

Member Data Documentation

◆ _costTable_

const EdgeProperty< float >& gum::SpanningForestPrim::_costTable_
private

the costs of the edges

Definition at line 115 of file spanningForestPrim.h.

Referenced by SpanningForestPrim(), SpanningForestPrim(), _computeInAComponent_(), and _exploreNode_().

◆ _edgesToExplore_

PriorityQueue< Edge, float > gum::SpanningForestPrim::_edgesToExplore_
private

the next edges that may be added to the spanning tree

Definition at line 118 of file spanningForestPrim.h.

Referenced by SpanningForestPrim(), _computeInAComponent_(), and _exploreNode_().

◆ _graph_

const UndiGraph& gum::SpanningForestPrim::_graph_
private

the graph the spanning tree of which we wish to compute

Definition at line 112 of file spanningForestPrim.h.

Referenced by SpanningForestPrim(), SpanningForestPrim(), _compute_(), and _exploreNode_().

◆ _require_computation_

bool gum::SpanningForestPrim::_require_computation_
private

a Boolean indicating whether we need recompute the spanning tree

Definition at line 127 of file spanningForestPrim.h.

Referenced by SpanningForestPrim(), SpanningForestPrim(), _compute_(), costOfSpanningForest(), edgesInSpanningForest(), and spanningForest().

◆ _spanning_tree_

UndiGraph gum::SpanningForestPrim::_spanning_tree_
private

the computed spanning tree

Definition at line 121 of file spanningForestPrim.h.

Referenced by SpanningForestPrim(), _compute_(), _computeInAComponent_(), _exploreNode_(), edgesInSpanningForest(), and spanningForest().

◆ _spanning_tree_cost_

float gum::SpanningForestPrim::_spanning_tree_cost_
private

the cost of the spanning tree

Definition at line 124 of file spanningForestPrim.h.

Referenced by SpanningForestPrim(), SpanningForestPrim(), _computeInAComponent_(), and costOfSpanningForest().


The documentation for this class was generated from the following files: