1 /* Copyright (C) 2006-2008 Joris Mooij [j dot mooij at science dot ru dot nl]
2 Radboud University Nijmegen, The Netherlands
4 This file is part of libDAI.
6 libDAI is free software; you can redistribute it and/or modify
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 libDAI is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with libDAI; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
22 #include <algorithm>
23 #include <cassert>
24 #include <dai/weightedgraph.h>
25 #include <dai/util.h>
28 namespace dai {
31 using namespace std;
34 /// Calculate rooted tree from a tree T and a root
35 DEdgeVec GrowRootedTree( const Graph & T, size_t Root ) {
36 DEdgeVec result;
37 if( T.size() == 0 )
38 return result;
39 else {
40 // Make a copy
41 Graph Gr = T;
43 // Nodes in the tree
44 set<size_t> treeV;
47 treeV.insert( Root );
49 // Keep adding edges until done
50 while( !(Gr.empty()) )
51 for( Graph::iterator e = Gr.begin(); e != Gr.end(); ) {
52 bool e1_in_treeV = treeV.count( e->n1 );
53 bool e2_in_treeV = treeV.count( e->n2 );
54 assert( !(e1_in_treeV && e2_in_treeV) );
55 if( e1_in_treeV ) {
56 // Add directed edge, pointing away from the root
57 result.push_back( DEdge( e->n1, e->n2 ) );
58 treeV.insert( e->n2 );
59 // Erase the edge
60 Gr.erase( e++ );
61 } else if( e2_in_treeV ) {
62 result.push_back( DEdge( e->n2, e->n1 ) );
63 treeV.insert( e->n1 );
64 // Erase the edge
65 Gr.erase( e++ );
66 } else
67 e++;
68 }
70 return result;
71 }
72 }
75 UEdgeVec RandomDRegularGraph( size_t N, size_t d ) {
76 // Algorithm 1 in "Generating random regular graphs quickly"
77 // by A. Steger and N.C. Wormald
78 //
79 // Draws a random graph with size N and uniform degree d
80 // from an almost uniform probability distribution over these graphs
81 // (which becomes uniform in the limit that d is small and N goes
82 // to infinity).
84 assert( (N * d) % 2 == 0 );
87 UEdgeVec G;
89 size_t tries = 0;
91 tries++;
93 // Start with N*d points {0,1,...,N*d-1} (N*d even) in N groups.
94 // Put U = {0,1,...,N*d-1}. (U denotes the set of unpaired points.)
95 vector<size_t> U;
96 U.reserve( N * d );
97 for( size_t i = 0; i < N * d; i++ )
98 U.push_back( i );
100 // Repeat the following until no suitable pair can be found: Choose
101 // two random points i and j in U, and if they are suitable, pair
102 // i with j and delete i and j from U.
103 G.clear();
104 bool finished = false;
105 while( !finished ) {
106 random_shuffle( U.begin(), U.end() );
107 size_t i1, i2;
108 bool suit_pair_found = false;
109 for( i1 = 0; i1 < U.size()-1 && !suit_pair_found; i1++ )
110 for( i2 = i1+1; i2 < U.size() && !suit_pair_found; i2++ )
111 if( (U[i1] / d) != (U[i2] / d) ) {
112 // they are suitable
113 suit_pair_found = true;
114 G.push_back( UEdge( U[i1] / d, U[i2] / d ) );
115 U.erase( U.begin() + i2 ); // first remove largest
116 U.erase( U.begin() + i1 ); // remove smallest
117 }
118 if( !suit_pair_found || U.empty() )
119 finished = true;
120 }
122 if( U.empty() ) {
123 // G is a graph with edge from vertex r to vertex s if and only if
124 // there is a pair containing points in the r'th and s'th groups.
125 // If G is d-regular, output, otherwise return to Step 1.
127 vector<size_t> degrees;
128 degrees.resize( N, 0 );
129 for( UEdgeVec::const_iterator e = G.begin(); e != G.end(); e++ ) {
130 degrees[e->n1]++;
131 degrees[e->n2]++;
132 }