aGrUM  0.20.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. 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  :
38  graph__(*graph), costTable__(*cost), spanning_tree_cost__(0),
39  require_computation__(true) {
40  if (!graph || !cost) {
41  GUM_ERROR(GraphError, "invalid null graph or edge cost pointer");
42  }
43 
44  // for debugging purposes
45  GUM_CONSTRUCTOR(SpanningForestPrim);
46  }
SpanningForest()
default constructor
SpanningForestPrim(const UndiGraph *graph, const EdgeProperty< float > *costTable)
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
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:54
+ Here is the call graph for this function:

◆ SpanningForestPrim() [2/2]

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

Copy constructor.

Definition at line 49 of file spanningForestPrim.cpp.

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

49  :
50  SpanningForest(), graph__(from.graph__), costTable__(from.costTable__),
51  edgesToExplore__(from.edgesToExplore__),
52  spanning_tree__(from.spanning_tree__),
53  spanning_tree_cost__(from.spanning_tree_cost__),
54  require_computation__(from.require_computation__) {
55  // for debugging purposes
56  GUM_CONS_CPY(SpanningForestPrim);
57  }
SpanningForest()
default constructor
PriorityQueue< Edge, float > edgesToExplore__
the next edges that may be added to the spanning tree
UndiGraph spanning_tree__
the computed spanning tree
SpanningForestPrim(const UndiGraph *graph, const EdgeProperty< float > *costTable)
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
const EdgeProperty< float > & costTable__
the costs of the edges
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 60 of file spanningForestPrim.cpp.

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

60  {
61  // for debugging purposes
62  GUM_DESTRUCTOR(SpanningForestPrim);
63  }
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 87 of file spanningForestPrim.cpp.

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

87  {
88  // compute a spanning tree in every connected component
89  for (const auto node: graph__.nodes()) {
90  if (!spanning_tree__.existsNode(node)) { computeInAComponent__(node); }
91  }
92 
93  // indicate that everything was computed
94  require_computation__ = false;
95  }
void computeInAComponent__(const NodeId id)
compute a spanning tree in a given connected component of graph__
UndiGraph spanning_tree__
the computed spanning tree
bool require_computation__
a Boolean indicating whether we need recompute the 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
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 98 of file spanningForestPrim.cpp.

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

98  {
99  // add the node to the spanning tree
101 
102  // explore its neighborhood
103  exploreNode__(id);
104 
105  // get the next nodes to link to the current spanning tree nodes
106 
107  while (!edgesToExplore__.empty()) {
108  const Edge edge = edgesToExplore__.pop();
109  const NodeId first = edge.first();
110  const NodeId second = edge.second();
111 
112  // consider only the edges that have one extremal node not in the spanning
113  // tree as those that can be added to the tree
114 
115  if (!spanning_tree__.existsNode(first)) {
116  // add the edge to the spanning tree
118  spanning_tree__.addEdge(first, second);
120 
121  // We must explore the first node's neighborhood
122  exploreNode__(first);
123  } else if (!spanning_tree__.existsNode(second)) {
124  // add the edge to the spanning tree
126  spanning_tree__.addEdge(first, second);
128 
129  // We must explore the second node
130  exploreNode__(second);
131  }
132  }
133  }
virtual void addNodeWithId(const NodeId id)
try to insert a node with the given id
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
void exploreNode__(const NodeId id)
explore the neighborhood of a node belonging to the spanning tree
bool existsNode(const NodeId id) const
returns true iff the NodeGraphPart contains the given nodeId
const EdgeProperty< float > & costTable__
the costs of the edges
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:

◆ 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 66 of file spanningForestPrim.cpp.

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

66  {
68 
69  return spanning_tree_cost__;
70  }
void compute__()
Computes the spanning forest.
bool require_computation__
a Boolean indicating whether we need recompute the spanning tree
float spanning_tree_cost__
the cost of the spanning tree
+ 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 73 of file spanningForestPrim.cpp.

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

73  {
75 
76  return spanning_tree__.edges();
77  }
void compute__()
Computes the spanning forest.
UndiGraph spanning_tree__
the computed spanning tree
bool require_computation__
a Boolean indicating whether we need recompute the spanning tree
const EdgeSet & edges() const
returns the set of edges stored within the EdgeGraphPart
+ 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 136 of file spanningForestPrim.cpp.

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

136  {
137  // add its neighbors edgesToExplore__ to indicate that they are
138  // potential next nodes to explore
139  for (const auto node: graph__.neighbours(id)) {
140  if (!spanning_tree__.existsNode(node)) {
141  Edge edge(node, id);
142  edgesToExplore__.insert(edge, costTable__[edge]);
143  }
144  }
145  }
PriorityQueue< Edge, float > edgesToExplore__
the next edges that may be added to the spanning tree
UndiGraph spanning_tree__
the computed spanning tree
const NodeSet & neighbours(const NodeId id) const
returns the set of edges adjacent to a given node
bool existsNode(const NodeId id) const
returns true iff the NodeGraphPart contains the given nodeId
const EdgeProperty< float > & costTable__
the costs of the edges
const UndiGraph & graph__
the graph the spanning tree of which we wish to compute
+ 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 80 of file spanningForestPrim.cpp.

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

80  {
82 
83  return spanning_tree__;
84  }
void compute__()
Computes the spanning forest.
UndiGraph spanning_tree__
the computed spanning tree
bool require_computation__
a Boolean indicating whether we need recompute the 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 96 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 99 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 93 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 108 of file spanningForestPrim.h.

◆ spanning_tree__

UndiGraph gum::SpanningForestPrim::spanning_tree__
private

the computed spanning tree

Definition at line 102 of file spanningForestPrim.h.

◆ spanning_tree_cost__

float gum::SpanningForestPrim::spanning_tree_cost__
private

the cost of the spanning tree

Definition at line 105 of file spanningForestPrim.h.


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