*/
-/// \file
-/// \brief Defines some utility functions for weighted graphs
-/// \todo Improve documentation
+/** \file
+ * \brief Defines some utility functions for weighted graphs
+ * \todo Improve documentation
+ * \todo Improve general support for graphs and trees.
+ */
#ifndef __defined_libdai_weightedgraph_h
#include <set>
#include <cassert>
#include <limits>
+#include <climits> // Work-around for bug in boost graph library
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/prim_minimum_spanning_tree.hpp>
public:
size_t n1; ///< First node index
size_t n2; ///< Second node index
-
+
/// Default constructor
DEdge() {}
public:
size_t n1; ///< First node index
size_t n2; ///< Second node index
-
+
/// Default constructor
UEdge() {}
// order, such that for all (i1, j1), (i2, j2) in result,
// if j1 == i2 then (i1, j1) comes before (i2, j2) in result.
// We do this by reordering the contents of result, effectively
- // growing the tree starting at the root. At each step,
+ // growing the tree starting at the root. At each step,
// result[0..N-1] are the edges already added to the tree,
// whereas the other elements of result still have to be added.
// The elements of nodes are the vertices that still have to
/** Uses implementation in Boost Graph Library.
*/
template<typename T> DEdgeVec MaxSpanningTreePrims( const WeightedGraph<T> & Graph ) {
- T maxweight = Graph.begin()->second;
- for( typename WeightedGraph<T>::const_iterator it = Graph.begin(); it != Graph.end(); it++ )
- if( it->second > maxweight )
- maxweight = it->second;
- // make a copy of the graph
- WeightedGraph<T> gr( Graph );
- // invoke MinSpanningTreePrims with negative weights
- // (which have to be shifted to satisfy positivity criterion)
- for( typename WeightedGraph<T>::iterator it = gr.begin(); it != gr.end(); it++ )
- it->second = maxweight - it->second;
- return MinSpanningTreePrims( gr );
+ if( Graph.size() == 0 )
+ return DEdgeVec();
+ else {
+ T maxweight = Graph.begin()->second;
+ for( typename WeightedGraph<T>::const_iterator it = Graph.begin(); it != Graph.end(); it++ )
+ if( it->second > maxweight )
+ maxweight = it->second;
+ // make a copy of the graph
+ WeightedGraph<T> gr( Graph );
+ // invoke MinSpanningTreePrims with negative weights
+ // (which have to be shifted to satisfy positivity criterion)
+ for( typename WeightedGraph<T>::iterator it = gr.begin(); it != gr.end(); it++ )
+ it->second = maxweight - it->second;
+ return MinSpanningTreePrims( gr );
+ }
}