aGrUM  0.16.0
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 44 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 36 of file spanningForestPrim.cpp.

References GUM_ERROR.

37  :
39  __graph(*graph), __costTable(*cost), __spanning_tree_cost(0),
40  __require_computation(true) {
41  if (!graph || !cost) {
42  GUM_ERROR(GraphError, "invalid null graph or edge cost pointer");
43  }
44 
45  // for debugging purposes
46  GUM_CONSTRUCTOR(SpanningForestPrim);
47  }
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:55
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 50 of file spanningForestPrim.cpp.

50  :
51  SpanningForest(), __graph(from.__graph), __costTable(from.__costTable),
52  __edgesToExplore(from.__edgesToExplore),
53  __spanning_tree(from.__spanning_tree),
54  __spanning_tree_cost(from.__spanning_tree_cost),
55  __require_computation(from.__require_computation) {
56  // for debugging purposes
57  GUM_CONS_CPY(SpanningForestPrim);
58  }
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 61 of file spanningForestPrim.cpp.

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

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

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

88  {
89  // compute a spanning tree in every connected component
90  for (const auto node : __graph.nodes()) {
91  if (!__spanning_tree.existsNode(node)) { __computeInAComponent(node); }
92  }
93 
94  // indicate that everything was computed
95  __require_computation = false;
96  }
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 99 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().

99  {
100  // add the node to the spanning tree
102 
103  // explore its neighborhood
104  __exploreNode(id);
105 
106  // get the next nodes to link to the current spanning tree nodes
107 
108  while (!__edgesToExplore.empty()) {
109  const Edge edge = __edgesToExplore.pop();
110  const NodeId first = edge.first();
111  const NodeId second = edge.second();
112 
113  // consider only the edges that have one extremal node not in the spanning
114  // tree as those that can be added to the tree
115 
116  if (!__spanning_tree.existsNode(first)) {
117  // add the edge to the spanning tree
119  __spanning_tree.addEdge(first, second);
121 
122  // We must explore the first node's neighborhood
123  __exploreNode(first);
124  } else if (!__spanning_tree.existsNode(second)) {
125  // add the edge to the spanning tree
127  __spanning_tree.addEdge(first, second);
129 
130  // We must explore the second node
131  __exploreNode(second);
132  }
133  }
134  }
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:35
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:98
+ 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 137 of file spanningForestPrim.cpp.

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

Referenced by __computeInAComponent().

137  {
138  // add its neighbors __edgesToExplore to indicate that they are
139  // potential next nodes to explore
140  for (const auto node : __graph.neighbours(id)) {
141  if (!__spanning_tree.existsNode(node)) {
142  Edge edge(node, id);
143  __edgesToExplore.insert(edge, __costTable[edge]);
144  }
145  }
146  }
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 67 of file spanningForestPrim.cpp.

References __compute(), __require_computation, and __spanning_tree_cost.

67  {
69 
70  return __spanning_tree_cost;
71  }
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 74 of file spanningForestPrim.cpp.

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

74  {
76 
77  return __spanning_tree.edges();
78  }
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 81 of file spanningForestPrim.cpp.

References __compute(), __require_computation, and __spanning_tree.

81  {
83 
84  return __spanning_tree;
85  }
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 97 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 100 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 94 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 109 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 103 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 106 of file spanningForestPrim.h.

Referenced by __computeInAComponent(), and costOfSpanningForest().


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