aGrUM  0.14.2
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 42 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 33 of file spanningForestPrim.cpp.

References GUM_ERROR.

34  :
36  __graph(*graph), __costTable(*cost), __spanning_tree_cost(0),
37  __require_computation(true) {
38  if (!graph || !cost) {
39  GUM_ERROR(GraphError, "invalid null graph or edge cost pointer");
40  }
41 
42  // for debugging purposes
43  GUM_CONSTRUCTOR(SpanningForestPrim);
44  }
SpanningForest()
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
SpanningForestPrim(const UndiGraph *graph, const EdgeProperty< float > *costTable)
Default constructor.
float __spanning_tree_cost
the cost of the spanning tree
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52
bool __require_computation
a Boolean indicating whether we need recompute the spanning tree

◆ SpanningForestPrim() [2/2]

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

Copy constructor.

Definition at line 47 of file spanningForestPrim.cpp.

47  :
48  SpanningForest(), __graph(from.__graph), __costTable(from.__costTable),
49  __edgesToExplore(from.__edgesToExplore),
50  __spanning_tree(from.__spanning_tree),
51  __spanning_tree_cost(from.__spanning_tree_cost),
52  __require_computation(from.__require_computation) {
53  // for debugging purposes
54  GUM_CONS_CPY(SpanningForestPrim);
55  }
SpanningForest()
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
UndiGraph __spanning_tree
the computed spanning tree
SpanningForestPrim(const UndiGraph *graph, const EdgeProperty< float > *costTable)
Default constructor.
float __spanning_tree_cost
the cost of the spanning tree
PriorityQueue< Edge, float > __edgesToExplore
the next edges that may be added to the spanning tree
bool __require_computation
a Boolean indicating whether we need recompute the spanning tree

◆ ~SpanningForestPrim()

gum::SpanningForestPrim::~SpanningForestPrim ( )
virtual

Destructor.

Definition at line 58 of file spanningForestPrim.cpp.

58  {
59  // for debugging purposes
60  GUM_DESTRUCTOR(SpanningForestPrim);
61  }
SpanningForestPrim(const UndiGraph *graph, const EdgeProperty< float > *costTable)
Default constructor.

Member Function Documentation

◆ __compute()

void gum::SpanningForestPrim::__compute ( )
private

Computes the spanning forest.

compute the spanning forest

Definition at line 85 of file spanningForestPrim.cpp.

References __computeInAComponent(), __graph, __require_computation, __spanning_tree, gum::NodeGraphPart::existsNode(), and gum::NodeGraphPart::nodes().

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

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

References __costTable, __edgesToExplore, __exploreNode(), __spanning_tree, __spanning_tree_cost, gum::UndiGraph::addEdge(), gum::NodeGraphPart::addNodeWithId(), gum::NodeGraphPart::existsNode(), gum::Edge::first(), and gum::Edge::second().

Referenced by __compute().

96  {
97  // add the node to the spanning tree
99 
100  // explore its neighborhood
101  __exploreNode(id);
102 
103  // get the next nodes to link to the current spanning tree nodes
104 
105  while (!__edgesToExplore.empty()) {
106  const Edge edge = __edgesToExplore.pop();
107  const NodeId first = edge.first();
108  const NodeId second = edge.second();
109 
110  // consider only the edges that have one extremal node not in the spanning
111  // tree as those that can be added to the tree
112 
113  if (!__spanning_tree.existsNode(first)) {
114  // add the edge to the spanning tree
116  __spanning_tree.addEdge(first, second);
118 
119  // We must explore the first node's neighborhood
120  __exploreNode(first);
121  } else if (!__spanning_tree.existsNode(second)) {
122  // add the edge to the spanning tree
124  __spanning_tree.addEdge(first, second);
126 
127  // We must explore the second node
128  __exploreNode(second);
129  }
130  }
131  }
virtual void addNodeWithId(const NodeId id)
try to insert a node with the given id
const EdgeProperty< float > & __costTable
the costs of the edges
UndiGraph __spanning_tree
the computed spanning tree
virtual void addEdge(const NodeId first, const NodeId second)
insert a new edge into the undirected graph
Definition: undiGraph_inl.h:32
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
float __spanning_tree_cost
the cost of the spanning tree
PriorityQueue< Edge, float > __edgesToExplore
the next edges that may be added to the spanning tree
Size NodeId
Type for node ids.
Definition: graphElements.h:97
+ 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 134 of file spanningForestPrim.cpp.

References __costTable, __edgesToExplore, __graph, __spanning_tree, gum::NodeGraphPart::existsNode(), and gum::EdgeGraphPart::neighbours().

Referenced by __computeInAComponent().

134  {
135  // add its neighbors __edgesToExplore to indicate that they are
136  // potential next nodes to explore
137  for (const auto node : __graph.neighbours(id)) {
138  if (!__spanning_tree.existsNode(node)) {
139  Edge edge(node, id);
140  __edgesToExplore.insert(edge, __costTable[edge]);
141  }
142  }
143  }
const EdgeProperty< float > & __costTable
the costs of the edges
const UndiGraph & __graph
the graph the spanning tree of which we wish to compute
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
PriorityQueue< Edge, float > __edgesToExplore
the next edges that may be added to the spanning tree
+ Here is the call graph for this function:
+ 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 64 of file spanningForestPrim.cpp.

References __compute(), __require_computation, and __spanning_tree_cost.

64  {
66 
67  return __spanning_tree_cost;
68  }
float __spanning_tree_cost
the cost of the spanning tree
void __compute()
Computes the spanning forest.
bool __require_computation
a Boolean indicating whether we need recompute 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 71 of file spanningForestPrim.cpp.

References __compute(), __require_computation, __spanning_tree, and gum::EdgeGraphPart::edges().

71  {
73 
74  return __spanning_tree.edges();
75  }
UndiGraph __spanning_tree
the computed spanning tree
const EdgeSet & edges() const
returns the set of edges stored within the EdgeGraphPart
void __compute()
Computes the spanning forest.
bool __require_computation
a Boolean indicating whether we need recompute the 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.

◆ spanningForest()

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

Construct the spanning forest.

Returns
the spanning forest

Implements gum::SpanningForest.

Definition at line 78 of file spanningForestPrim.cpp.

References __compute(), __require_computation, and __spanning_tree.

78  {
80 
81  return __spanning_tree;
82  }
UndiGraph __spanning_tree
the computed spanning tree
void __compute()
Computes the spanning forest.
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 95 of file spanningForestPrim.h.

Referenced by __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 98 of file spanningForestPrim.h.

Referenced by __computeInAComponent(), and __exploreNode().

◆ __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.

Referenced by __compute(), and __exploreNode().

◆ __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.

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

◆ __spanning_tree

UndiGraph gum::SpanningForestPrim::__spanning_tree
private

the computed spanning tree

Definition at line 101 of file spanningForestPrim.h.

Referenced by __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 104 of file spanningForestPrim.h.

Referenced by __computeInAComponent(), and costOfSpanningForest().


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