Significant improvement of documentation
[libdai.git] / include / dai / clustergraph.h
1 /* Copyright (C) 2006-2008 Joris Mooij [joris dot mooij at tuebingen dot mpg dot de]
2 Radboud University Nijmegen, The Netherlands /
3 Max Planck Institute for Biological Cybernetics, Germany
4
5 This file is part of libDAI.
6
7 libDAI is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 libDAI is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with libDAI; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22
23 /// \file
24 /// \brief Defines class ClusterGraph
25
26
27 #ifndef __defined_libdai_clustergraph_h
28 #define __defined_libdai_clustergraph_h
29
30
31 #include <set>
32 #include <vector>
33 #include <dai/varset.h>
34 #include <dai/bipgraph.h>
35
36
37 namespace dai {
38
39
40 /// A ClusterGraph is a hypergraph with VarSets as nodes.
41 /** It is implemented as bipartite graph with variable (Var) nodes
42 * and cluster (VarSet) nodes.
43 */
44 class ClusterGraph {
45 public:
46 /// Stores the neighborhood structure
47 BipartiteGraph G;
48
49 /// Stores the variables corresponding to the nodes
50 std::vector<Var> vars;
51
52 /// Stores the clusters corresponding to the hyperedges
53 std::vector<VarSet> clusters;
54
55 /// Shorthand for BipartiteGraph::Neighbor
56 typedef BipartiteGraph::Neighbor Neighbor;
57
58 /// Shorthand for BipartiteGraph::Edge
59 typedef BipartiteGraph::Edge Edge;
60
61 public:
62 /// Default constructor
63 ClusterGraph() : G(), vars(), clusters() {}
64
65 /// Construct from vector<VarSet>
66 ClusterGraph( const std::vector<VarSet> & cls );
67
68 /// Copy constructor
69 ClusterGraph( const ClusterGraph &x ) : G(x.G), vars(x.vars), clusters(x.clusters) {}
70
71 /// Assignment operator
72 ClusterGraph& operator=( const ClusterGraph &x ) {
73 if( this != &x ) {
74 G = x.G;
75 vars = x.vars;
76 clusters = x.clusters;
77 }
78 return *this;
79 }
80
81 /// Returns true if cluster I is not contained in a larger cluster
82 bool isMaximal( size_t I ) const {
83 #ifdef DAI_DEBUG
84 assert( I < G.nr2() );
85 #endif
86 const VarSet & clI = clusters[I];
87 bool maximal = true;
88 // The following may not be optimal, since it may repeatedly test the same cluster *J
89 foreach( const Neighbor &i, G.nb2(I) ) {
90 foreach( const Neighbor &J, G.nb1(i) )
91 if( (J != I) && (clI << clusters[J]) ) {
92 maximal = false;
93 break;
94 }
95 if( !maximal )
96 break;
97 }
98 return maximal;
99 }
100
101 /// Erases all VarSets that are not maximal
102 ClusterGraph& eraseNonMaximal() {
103 for( size_t I = 0; I < G.nr2(); ) {
104 if( !isMaximal(I) ) {
105 clusters.erase( clusters.begin() + I );
106 G.erase2(I);
107 } else
108 I++;
109 }
110 return *this;
111 }
112
113 /// Returns number of clusters
114 size_t size() const {
115 return G.nr2();
116 }
117
118 /// Returns index of variable n
119 size_t findVar( const Var &n ) const {
120 return find( vars.begin(), vars.end(), n ) - vars.begin();
121 }
122
123 /// Returns true if vars with indices i1 and i2 are adjacent, i.e., both contained in the same cluster
124 bool adj( size_t i1, size_t i2 ) {
125 bool result = false;
126 foreach( const Neighbor &I, G.nb1(i1) )
127 if( find( G.nb2(I).begin(), G.nb2(I).end(), i2 ) != G.nb2(I).end() ) {
128 result = true;
129 break;
130 }
131 return result;
132 }
133
134 /// Returns union of clusters that contain the variable with index i
135 VarSet Delta( size_t i ) const {
136 VarSet result;
137 foreach( const Neighbor &I, G.nb1(i) )
138 result |= clusters[I];
139 return result;
140 }
141
142 /// Inserts a cluster (if it does not already exist)
143 void insert( const VarSet &cl ) {
144 if( find( clusters.begin(), clusters.end(), cl ) == clusters.end() ) {
145 clusters.push_back( cl );
146 // add variables (if necessary) and calculate neighborhood of new cluster
147 std::vector<size_t> nbs;
148 for( VarSet::const_iterator n = cl.begin(); n != cl.end(); n++ ) {
149 size_t iter = find( vars.begin(), vars.end(), *n ) - vars.begin();
150 nbs.push_back( iter );
151 if( iter == vars.size() ) {
152 G.add1();
153 vars.push_back( *n );
154 }
155 }
156 G.add2( nbs.begin(), nbs.end(), nbs.size() );
157 }
158 }
159
160 /// Returns union of clusters that contain variable with index i, minus this variable
161 VarSet delta( size_t i ) const {
162 return Delta( i ) / vars[i];
163 }
164
165 /// Erases all clusters that contain n where n is the variable with index i
166 ClusterGraph& eraseSubsuming( size_t i ) {
167 while( G.nb1(i).size() ) {
168 clusters.erase( clusters.begin() + G.nb1(i)[0] );
169 G.erase2( G.nb1(i)[0] );
170 }
171 return *this;
172 }
173
174 /// Returns a const reference to the clusters
175 const std::vector<VarSet> & toVector() const { return clusters; }
176
177 /// Calculates cost of eliminating the variable with index i.
178 /** The cost is measured as "number of added edges in the adjacency graph",
179 * where the adjacency graph has the variables as its nodes and
180 * connects nodes i1 and i2 iff i1 and i2 occur in some common cluster.
181 */
182 size_t eliminationCost( size_t i ) {
183 std::vector<size_t> id_n = G.delta1( i );
184
185 size_t cost = 0;
186
187 // for each unordered pair {i1,i2} adjacent to n
188 for( size_t _i1 = 0; _i1 < id_n.size(); _i1++ )
189 for( size_t _i2 = _i1 + 1; _i2 < id_n.size(); _i2++ ) {
190 // if i1 and i2 are not adjacent, eliminating n would make them adjacent
191 if( !adj(id_n[_i1], id_n[_i2]) )
192 cost++;
193 }
194
195 return cost;
196 }
197
198 /// Performs Variable Elimination without Probs, i.e. only keeping track of
199 /* the interactions that are created along the way.
200 * \param ElimSeq A set of outer clusters and an elimination sequence
201 * \return A set of elimination "cliques"
202 */
203 ClusterGraph VarElim( const std::vector<Var> &ElimSeq ) const;
204
205 /// Performs Variable Eliminiation using the MinFill heuristic
206 ClusterGraph VarElim_MinFill() const;
207
208 /// Writes a ClusterGraph to an output stream
209 friend std::ostream & operator << ( std::ostream & os, const ClusterGraph & cl ) {
210 os << cl.toVector();
211 return os;
212 }
213 };
214
215
216 } // end of namespace dai
217
218
219 #endif