aGrUM  0.16.0
spanningForestPrim.cpp
Go to the documentation of this file.
1 
29 #include <agrum/agrum.h>
30 
32 
33 namespace gum {
34 
37  const EdgeProperty< float >* cost) :
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  }
48 
49  // copy constructor
56  // for debugging purposes
57  GUM_CONS_CPY(SpanningForestPrim);
58  }
59 
60  // destructor
62  // for debugging purposes
63  GUM_DESTRUCTOR(SpanningForestPrim);
64  }
65 
69 
70  return __spanning_tree_cost;
71  }
72 
76 
77  return __spanning_tree.edges();
78  }
79 
83 
84  return __spanning_tree;
85  }
86 
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  }
97 
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  }
135 
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  }
147 
148 } /* namespace gum */
NodeId second() const
returns the node ID of the other extremal node ID
virtual void addNodeWithId(const NodeId id)
try to insert a node with the given id
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.
virtual void addEdge(const NodeId first, const NodeId second)
insert a new edge into the undirected graph
Definition: undiGraph_inl.h:35
const UndiGraph & spanningForest()
Construct the spanning forest.
The Prim algorithm for computing min cost spanning trees or forests.
void __computeInAComponent(const NodeId id)
compute a spanning tree in a given connected component of __graph
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Definition: agrum.h:25
const EdgeSet & edgesInSpanningForest()
Returns the edges in a min cost spanning forest.
The class for generic Hash Tables.
Definition: hashTable.h:679
const NodeSet & neighbours(const NodeId id) const
returns the set of edges adjacent to a given node
float costOfSpanningForest()
Returns the cost of the spanning forest.
const EdgeSet & edges() const
returns the set of edges stored within the EdgeGraphPart
NodeId first() const
returns one extremal node ID (whichever one it is is unspecified)
const NodeGraphPart & nodes() const
return *this as a NodeGraphPart
virtual ~SpanningForestPrim()
Destructor.
bool existsNode(const NodeId id) const
returns true iff the NodeGraphPart contains the given nodeId
The base class for all undirected edges.
void __exploreNode(const NodeId id)
explore the neighborhood of a node belonging to the spanning tree
Base class for undirected graphs.
Definition: undiGraph.h:109
float __spanning_tree_cost
the cost of the spanning tree
void __compute()
Computes the spanning forest.
PriorityQueue< Edge, float > __edgesToExplore
the next edges that may be added to the spanning tree
Base class for computing min cost spanning trees or forests.
Size NodeId
Type for node ids.
Definition: graphElements.h:98
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
bool __require_computation
a Boolean indicating whether we need recompute the spanning tree