aGrUM  0.20.3
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. More...
 
 SpanningForestPrim (const SpanningForestPrim &toCopy)
 Copy constructor. More...
 
virtual ~SpanningForestPrim ()
 Destructor. More...
 
Accessors / Modifiers
const EdgeSetedgesInSpanningForest ()
 Returns the edges in a min cost spanning forest. More...
 
const UndiGraphspanningForest ()
 Construct the spanning forest. More...
 
float costOfSpanningForest ()
 Returns the cost of the spanning forest. More...
 

Detailed Description

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

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

Definition at line 43 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 35 of file spanningForestPrim.cpp.

References gum::Set< Key, Alloc >::emplace().

36  :
39  if (!graph || !cost) { GUM_ERROR(GraphError, "invalid null graph or edge cost pointer") }
40 
41  // for debugging purposes
42  GUM_CONSTRUCTOR(SpanningForestPrim);
43  }
SpanningForest()
default constructor
bool _require_computation_
a Boolean indicating whether we need recompute the spanning tree
float _spanning_tree_cost_
the cost of the spanning tree
SpanningForestPrim(const UndiGraph *graph, const EdgeProperty< float > *costTable)
Default constructor.
const EdgeProperty< float > & _costTable_
the costs of the edges
const UndiGraph & _graph_
the graph the spanning tree of which we wish to compute
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
+ Here is the call graph for this function:

◆ SpanningForestPrim() [2/2]

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

Copy constructor.

Definition at line 46 of file spanningForestPrim.cpp.

References gum::Set< Key, Alloc >::emplace().

46  :
47  SpanningForest(), _graph_(from._graph_), _costTable_(from._costTable_),
48  _edgesToExplore_(from._edgesToExplore_), _spanning_tree_(from._spanning_tree_),
49  _spanning_tree_cost_(from._spanning_tree_cost_),
50  _require_computation_(from._require_computation_) {
51  // for debugging purposes
52  GUM_CONS_CPY(SpanningForestPrim);
53  }
SpanningForest()
default constructor
bool _require_computation_
a Boolean indicating whether we need recompute the spanning tree
UndiGraph _spanning_tree_
the computed spanning tree
float _spanning_tree_cost_
the cost of the spanning tree
SpanningForestPrim(const UndiGraph *graph, const EdgeProperty< float > *costTable)
Default constructor.
const EdgeProperty< float > & _costTable_
the costs of the edges
PriorityQueue< Edge, float > _edgesToExplore_
the next edges that may be added to the spanning tree
const UndiGraph & _graph_
the graph the spanning tree of which we wish to compute
+ Here is the call graph for this function:

◆ ~SpanningForestPrim()

gum::SpanningForestPrim::~SpanningForestPrim ( )
virtual

Destructor.

Definition at line 56 of file spanningForestPrim.cpp.

References gum::Set< Key, Alloc >::emplace().

56  {
57  // for debugging purposes
58  GUM_DESTRUCTOR(SpanningForestPrim);
59  }
SpanningForestPrim(const UndiGraph *graph, const EdgeProperty< float > *costTable)
Default constructor.
+ 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 83 of file spanningForestPrim.cpp.

References gum::Set< Key, Alloc >::emplace().

83  {
84  // compute a spanning tree in every connected component
85  for (const auto node: _graph_.nodes()) {
86  if (!_spanning_tree_.existsNode(node)) { _computeInAComponent_(node); }
87  }
88 
89  // indicate that everything was computed
90  _require_computation_ = false;
91  }
bool _require_computation_
a Boolean indicating whether we need recompute the spanning tree
UndiGraph _spanning_tree_
the computed spanning tree
const NodeGraphPart & nodes() const
return *this as a NodeGraphPart
bool existsNode(const NodeId id) const
returns true iff the NodeGraphPart contains the given nodeId
void _computeInAComponent_(const NodeId id)
compute a spanning tree in a given connected component of graph
const UndiGraph & _graph_
the graph the spanning tree of which we wish to compute
+ Here is the call 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 94 of file spanningForestPrim.cpp.

References gum::Set< Key, Alloc >::emplace().

94  {
95  // add the node to the spanning tree
97 
98  // explore its neighborhood
99  _exploreNode_(id);
100 
101  // get the next nodes to link to the current spanning tree nodes
102 
103  while (!_edgesToExplore_.empty()) {
104  const Edge edge = _edgesToExplore_.pop();
105  const NodeId first = edge.first();
106  const NodeId second = edge.second();
107 
108  // consider only the edges that have one extremal node not in the spanning
109  // tree as those that can be added to the tree
110 
111  if (!_spanning_tree_.existsNode(first)) {
112  // add the edge to the spanning tree
114  _spanning_tree_.addEdge(first, second);
116 
117  // We must explore the first node's neighborhood
118  _exploreNode_(first);
119  } else if (!_spanning_tree_.existsNode(second)) {
120  // add the edge to the spanning tree
122  _spanning_tree_.addEdge(first, second);
124 
125  // We must explore the second node
126  _exploreNode_(second);
127  }
128  }
129  }
UndiGraph _spanning_tree_
the computed spanning tree
virtual void addNodeWithId(const NodeId id)
try to insert a node with the given id
float _spanning_tree_cost_
the cost of the spanning tree
const EdgeProperty< float > & _costTable_
the costs of the edges
PriorityQueue< Edge, float > _edgesToExplore_
the next edges that may be added to the spanning tree
bool existsNode(const NodeId id) const
returns true iff the NodeGraphPart contains the given nodeId
void _exploreNode_(const NodeId id)
explore the neighborhood of a node belonging to the spanning tree
void addEdge(NodeId first, NodeId second) override
insert a new edge into the undirected graph
Definition: undiGraph_inl.h:34
Size NodeId
Type for node ids.
Definition: graphElements.h:97
+ Here is the call 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 132 of file spanningForestPrim.cpp.

References gum::Set< Key, Alloc >::emplace().

132  {
133  // add its neighbors _edgesToExplore_ to indicate that they are
134  // potential next nodes to explore
135  for (const auto node: _graph_.neighbours(id)) {
136  if (!_spanning_tree_.existsNode(node)) {
137  Edge edge(node, id);
138  _edgesToExplore_.insert(edge, _costTable_[edge]);
139  }
140  }
141  }
UndiGraph _spanning_tree_
the computed spanning tree
const EdgeProperty< float > & _costTable_
the costs of the edges
const NodeSet & neighbours(const NodeId id) const
returns the set of node neighbours to a given node
PriorityQueue< Edge, float > _edgesToExplore_
the next edges that may be added to the spanning tree
bool existsNode(const NodeId id) const
returns true iff the NodeGraphPart contains the given nodeId
const UndiGraph & _graph_
the graph the spanning tree of which we wish to compute
+ Here is the call 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 62 of file spanningForestPrim.cpp.

References gum::Set< Key, Alloc >::emplace().

62  {
64 
65  return _spanning_tree_cost_;
66  }
bool _require_computation_
a Boolean indicating whether we need recompute the spanning tree
float _spanning_tree_cost_
the cost of the spanning tree
void _compute_()
Computes the spanning forest.
+ 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 69 of file spanningForestPrim.cpp.

References gum::Set< Key, Alloc >::emplace().

69  {
71 
72  return _spanning_tree_.edges();
73  }
bool _require_computation_
a Boolean indicating whether we need recompute the spanning tree
UndiGraph _spanning_tree_
the computed spanning tree
void _compute_()
Computes the spanning forest.
const EdgeSet & edges() const
returns the set of edges stored within the EdgeGraphPart
+ Here is the call graph for this function:

◆ operator=()

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

Copy operator: private to prevent using it.

◆ spanningForest()

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

Construct the spanning forest.

Returns
the spanning forest

Implements gum::SpanningForest.

Definition at line 76 of file spanningForestPrim.cpp.

References gum::Set< Key, Alloc >::emplace().

76  {
78 
79  return _spanning_tree_;
80  }
bool _require_computation_
a Boolean indicating whether we need recompute the spanning tree
UndiGraph _spanning_tree_
the computed spanning tree
void _compute_()
Computes the spanning forest.
+ 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 95 of file spanningForestPrim.h.

◆ _edgesToExplore_

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

the next edges that may be added to the spanning tree

Definition at line 98 of file spanningForestPrim.h.

◆ _graph_

const UndiGraph& gum::SpanningForestPrim::_graph_
private

the graph the spanning tree of which we wish to compute

Definition at line 92 of file spanningForestPrim.h.

◆ _require_computation_

bool gum::SpanningForestPrim::_require_computation_
private

a Boolean indicating whether we need recompute the spanning tree

Definition at line 107 of file spanningForestPrim.h.

◆ _spanning_tree_

UndiGraph gum::SpanningForestPrim::_spanning_tree_
private

the computed spanning tree

Definition at line 101 of file spanningForestPrim.h.

◆ _spanning_tree_cost_

float gum::SpanningForestPrim::_spanning_tree_cost_
private

the cost of the spanning tree

Definition at line 104 of file spanningForestPrim.h.


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