96a383042dfeb80a894f2ed9629244eb5779e82b
1 /* This file is part of libDAI - http://www.libdai.org/
3 * libDAI is licensed under the terms of the GNU General Public License version
4 * 2, or (at your option) any later version. libDAI is distributed without any
5 * warranty. See the file COPYING for more details.
7 * Copyright (C) 2006-2009 Joris Mooij [joris dot mooij at libdai dot org]
8 * Copyright (C) 2006-2007 Radboud University Nijmegen, The Netherlands
13 * \brief Defines some utility functions for (weighted) undirected graphs
14 * \todo Improve general support for graphs and trees.
18 #ifndef __defined_libdai_weightedgraph_h
19 #define __defined_libdai_weightedgraph_h
27 #include <climits> // Work-around for bug in boost graph library
29 #include <boost/graph/adjacency_list.hpp>
30 #include <boost/graph/prim_minimum_spanning_tree.hpp>
36 /// Represents a directed edge
44 /// Default constructor
47 /// Constructs a directed edge pointing from \a m1 to \a m2
48 DEdge( size_t m1
, size_t m2
) : n1(m1
), n2(m2
) {}
50 /// Tests for equality
51 bool operator==( const DEdge
&x
) const { return ((n1
== x
.n1
) && (n2
== x
.n2
)); }
53 /// Tests for inequality
54 bool operator!=( const DEdge
&x
) const { return !(*this == x
); }
56 /// Smaller-than operator (performs lexicographical comparison)
57 bool operator<( const DEdge
&x
) const {
58 return( (n1
< x
.n1
) || ((n1
== x
.n1
) && (n2
< x
.n2
)) );
61 /// Writes a directed edge to an output stream
62 friend std::ostream
& operator << (std::ostream
& os
, const DEdge
& e
) {
63 os
<< "(" << e
.n1
<< "," << e
.n2
<< ")";
69 /// Represents an undirected edge
77 /// Default constructor
80 /// Constructs an undirected edge between \a m1 and \a m2
81 UEdge( size_t m1
, size_t m2
) : n1(m1
), n2(m2
) {}
83 /// Construct from DEdge
84 UEdge( const DEdge
&e
) : n1(e
.n1
), n2(e
.n2
) {}
86 /// Tests for inequality (disregarding the ordering of the nodes)
87 bool operator==( const UEdge
&x
) {
88 return ((n1
== x
.n1
) && (n2
== x
.n2
)) || ((n1
== x
.n2
) && (n2
== x
.n1
));
91 /// Smaller-than operator
92 bool operator<( const UEdge
&x
) const {
93 size_t s
= n1
, l
= n2
;
96 size_t xs
= x
.n1
, xl
= x
.n2
;
99 return( (s
< xs
) || ((s
== xs
) && (l
< xl
)) );
102 /// Writes an undirected edge to an output stream
103 friend std::ostream
& operator << (std::ostream
& os
, const UEdge
& e
) {
105 os
<< "{" << e
.n1
<< "," << e
.n2
<< "}";
107 os
<< "{" << e
.n2
<< "," << e
.n1
<< "}";
114 typedef std::vector
<UEdge
> UEdgeVec
;
117 typedef std::vector
<DEdge
> DEdgeVec
;
119 /// Represents an undirected weighted graph, with weights of type \a T
120 template<class T
> class WeightedGraph
: public std::map
<UEdge
, T
> {};
122 /// Represents an undirected graph
123 typedef std::set
<UEdge
> Graph
;
126 /// Constructs a rooted tree from a tree and a root
127 /** \pre T has no cycles and contains node \a Root
129 DEdgeVec
GrowRootedTree( const Graph
&T
, size_t Root
);
132 /// Uses Prim's algorithm to construct a minimal spanning tree from the (positively) weighted graph \a G.
133 /** Uses implementation in Boost Graph Library.
135 template<typename T
> DEdgeVec
MinSpanningTreePrims( const WeightedGraph
<T
> &G
) {
138 using namespace boost
;
140 typedef adjacency_list
< vecS
, vecS
, undirectedS
, property
<vertex_distance_t
, int>, property
<edge_weight_t
, double> > boostGraph
;
141 typedef pair
<size_t, size_t> E
;
145 vector
<double> weights
;
146 edges
.reserve( G
.size() );
147 weights
.reserve( G
.size() );
148 for( typename WeightedGraph
<T
>::const_iterator e
= G
.begin(); e
!= G
.end(); e
++ ) {
149 weights
.push_back( e
->second
);
150 edges
.push_back( E( e
->first
.n1
, e
->first
.n2
) );
151 nodes
.insert( e
->first
.n1
);
152 nodes
.insert( e
->first
.n2
);
155 boostGraph
g( edges
.begin(), edges
.end(), weights
.begin(), nodes
.size() );
156 vector
< graph_traits
< boostGraph
>::vertex_descriptor
> p( num_vertices(g
) );
157 prim_minimum_spanning_tree( g
, &(p
[0]) );
159 // Store tree edges in result
162 for( size_t i
= 0; i
!= p
.size(); i
++ )
164 tree
.insert( UEdge( p
[i
], i
) );
168 result
= GrowRootedTree( tree
, root
);
175 /// Use Prim's algorithm to construct a minimal spanning tree from the (positively) weighted graph \a G.
176 /** Uses implementation in Boost Graph Library.
178 template<typename T
> DEdgeVec
MaxSpanningTreePrims( const WeightedGraph
<T
> &G
) {
182 T maxweight
= G
.begin()->second
;
183 for( typename WeightedGraph
<T
>::const_iterator it
= G
.begin(); it
!= G
.end(); it
++ )
184 if( it
->second
> maxweight
)
185 maxweight
= it
->second
;
186 // make a copy of the graph
187 WeightedGraph
<T
> gr( G
);
188 // invoke MinSpanningTreePrims with negative weights
189 // (which have to be shifted to satisfy positivity criterion)
190 for( typename WeightedGraph
<T
>::iterator it
= gr
.begin(); it
!= gr
.end(); it
++ )
191 it
->second
= maxweight
- it
->second
;
192 return MinSpanningTreePrims( gr
);
197 /// Constructs a random undirected graph of \a N nodes, where each node has connectivity \a d
198 /** Algorithm 1 in [\ref StW99].
199 * Draws a random graph of size \a N and uniform degree \a d
200 * from an almost uniform probability distribution over these graphs
201 * (which becomes uniform in the limit that \a d is small and \a N goes
204 UEdgeVec
RandomDRegularGraph( size_t N
, size_t d
);
207 } // end of namespace dai