15 #ifndef __defined_libdai_weightedgraph_h
16 #define __defined_libdai_weightedgraph_h
26 #include <boost/graph/adjacency_list.hpp>
27 #include <boost/graph/prim_minimum_spanning_tree.hpp>
28 #include <boost/graph/kruskal_min_spanning_tree.hpp>
48 DEdge() : first(0), second(0) {}
51 DEdge(
size_t m1,
size_t m2 ) : first(m1), second(m2) {}
86 UEdge() : first(0), second(0) {}
89 UEdge(
size_t m1,
size_t m2 ) : first(m1), second(m2) {}
92 UEdge(
const DEdge &e ) : first(e.first), second(e.second) {}
101 size_t s = std::min( first, second );
102 size_t l = std::max( first, second );
105 return( (s < xs) || ((s == xs) && (l < xl)) );
119 std::stringstream ss;
133 template <
class InputIterator>
134 GraphEL( InputIterator begin, InputIterator end ) {
135 insert( begin, end );
140 for(
size_t n1 = 0; n1 < G.
nrNodes(); n1++ )
143 insert(
UEdge( n1, n2 ) );
178 using namespace boost;
180 typedef adjacency_list< vecS, vecS, undirectedS, no_property, property<edge_weight_t, double> > boostGraph;
184 vector<double> weights;
185 edges.reserve( G.size() );
186 weights.reserve( G.size() );
188 weights.push_back( e->second );
189 edges.push_back( e->first );
190 nodes.insert( e->first.first );
191 nodes.insert( e->first.second );
194 size_t N = nodes.size();
195 for( set<size_t>::const_iterator it = nodes.begin(); it != nodes.end(); it++ )
197 DAI_THROWE(RUNTIME_ERROR,
"Vertices must be in range [0..N) where N is the number of vertices.");
199 boostGraph g( edges.begin(), edges.end(), weights.begin(), nodes.size() );
200 size_t root = *(nodes.begin());
204 vector< graph_traits< boostGraph >::vertex_descriptor > p(N);
205 prim_minimum_spanning_tree( g, &(p[0]) );
208 for(
size_t i = 0; i != p.size(); i++ ) {
210 tree.insert(
UEdge( p[i], i ) );
214 vector< graph_traits< boostGraph >::edge_descriptor > t;
216 kruskal_minimum_spanning_tree( g, std::back_inserter(t) );
219 for(
size_t i = 0; i != t.size(); i++ ) {
220 size_t v1 = source( t[i], g );
221 size_t v2 = target( t[i], g );
223 tree.insert(
UEdge( v1, v2 ) );
244 T maxweight = G.begin()->second;
246 if( it->second > maxweight )
247 maxweight = it->second;
253 it->second = maxweight - it->second;
RootedTree MaxSpanningTree(const WeightedGraph< T > &G, bool usePrim)
Constructs a minimum spanning tree from the (non-negatively) weighted graph G.
Definition: weightedgraph.h:240
GraphEL(InputIterator begin, InputIterator end)
Construct from range of objects that can be cast to UEdge.
Definition: weightedgraph.h:134
size_t first
First node index.
Definition: weightedgraph.h:80
size_t second
Second node index (target of edge)
Definition: weightedgraph.h:45
#define DAI_THROWE(cod, msg)
Macro that simplifies throwing an exception with a user-defined error message.
Definition: exceptions.h:57
bool operator==(const UEdge &x)
Tests for inequality (disregarding the ordering of the nodes)
Definition: weightedgraph.h:95
size_t second
Second node index.
Definition: weightedgraph.h:83
size_t nrNodes() const
Returns number of nodes.
Definition: graph.h:224
size_t first
First node index (source of edge)
Definition: weightedgraph.h:42
Represents an undirected graph, implemented as a std::set of undirected edges.
Definition: weightedgraph.h:127
bool operator<(const DEdge &x) const
Smaller-than operator (performs lexicographical comparison)
Definition: weightedgraph.h:57
DEdge()
Default constructor.
Definition: weightedgraph.h:48
UEdge()
Default constructor.
Definition: weightedgraph.h:86
RootedTree()
Default constructor.
Definition: weightedgraph.h:160
#define bforeach
An alias to the BOOST_FOREACH macro from the boost::bforeach library.
Definition: util.h:48
UEdge(const DEdge &e)
Construct from DEdge.
Definition: weightedgraph.h:92
Defines the Exception class and macros for throwing exceptions and doing assertions.
GraphEL(const GraphAL &G)
Construct from GraphAL.
Definition: weightedgraph.h:139
Represents an undirected weighted graph, with weights of type T, implemented as a std::map mapping un...
Definition: weightedgraph.h:149
Defines general utility functions and adds an abstraction layer for platform-dependent functionality...
bool operator<(const UEdge &x) const
Smaller-than operator.
Definition: weightedgraph.h:100
Represents a directed edge.
Definition: weightedgraph.h:39
DEdge(size_t m1, size_t m2)
Constructs a directed edge pointing from m1 to m2.
Definition: weightedgraph.h:51
Represents an undirected edge.
Definition: weightedgraph.h:77
Defines the GraphAL class, which represents an undirected graph as an adjacency list.
Represents a rooted tree, implemented as a vector of directed edges.
Definition: weightedgraph.h:157
GraphEL()
Default constructor.
Definition: weightedgraph.h:130
friend std::ostream & operator<<(std::ostream &os, const DEdge &e)
Writes a directed edge to an output stream.
Definition: weightedgraph.h:62
Describes the neighbor relationship of two nodes in a graph.
Definition: graph.h:73
std::string toString() const
Formats a directed edge as a string.
Definition: weightedgraph.h:68
Namespace for libDAI.
Definition: alldai.cpp:16
const Neighbor & nb(size_t n1, size_t _n2) const
Returns constant reference to the _n2 'th neighbor of node n1.
Definition: graph.h:144
std::string toString() const
Formats an undirected edge as a string.
Definition: weightedgraph.h:118
RootedTree MinSpanningTree(const WeightedGraph< T > &G, bool usePrim)
Constructs a minimum spanning tree from the (non-negatively) weighted graph G.
Definition: weightedgraph.h:175
Represents the neighborhood structure of nodes in an undirected graph.
Definition: graph.h:114
UEdge(size_t m1, size_t m2)
Constructs an undirected edge between m1 and m2.
Definition: weightedgraph.h:89
bool operator==(const DEdge &x) const
Tests for equality.
Definition: weightedgraph.h:54
friend std::ostream & operator<<(std::ostream &os, const UEdge &e)
Writes an undirected edge to an output stream.
Definition: weightedgraph.h:109