From 4776e4830e1cd90de5a3aed3f026a842d0aba3f9 Mon Sep 17 00:00:00 2001 From: Joris Mooij Date: Tue, 26 Jul 2011 10:53:42 +0200 Subject: [PATCH] Optimized ClusterGraph( const FactorGraph&, bool) constructor --- examples/Makefile | 6 +++--- src/clustergraph.cpp | 25 +++++++++++++++++-------- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/examples/Makefile b/examples/Makefile index 6d0e65d..a6ac949 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -1,18 +1,18 @@ # Include flags INC=-I../include # Library path flags -LIBS= +LIBS=-lgmpxx -lgmp # Location of libDAI library LIB=../lib # Compiler CC=g++ # Compiler flags -CCFLAGS=-Wno-deprecated -Wall -W -Wextra -fpic -O3 -static $(INC) $(LIBS) +CCFLAGS=-Wno-deprecated -Wall -W -Wextra -fpic -O3 -static $(INC) all : uai2010-aie-solver uai2010-aie-solver : uai2010-aie-solver.cpp $(LIB)/libdai.a - $(CC) $(CCFLAGS) -o$@ $< $(LIB)/libdai.a + $(CC) $(CCFLAGS) -o$@ $< $(LIB)/libdai.a $(LIBS) # CLEAN ######## diff --git a/src/clustergraph.cpp b/src/clustergraph.cpp index 6a52643..f5458dd 100644 --- a/src/clustergraph.cpp +++ b/src/clustergraph.cpp @@ -42,19 +42,28 @@ ClusterGraph::ClusterGraph( const std::vector & cls ) : _G(), _vars(), _ } -ClusterGraph::ClusterGraph( const FactorGraph& fg, bool onlyMaximal ) : _G( fg.bipGraph() ), _vars(), _clusters() { +ClusterGraph::ClusterGraph( const FactorGraph& fg, bool onlyMaximal ) : _G( fg.nrVars(), 0 ), _vars(), _clusters() { // copy variables _vars.reserve( fg.nrVars() ); for( size_t i = 0; i < fg.nrVars(); i++ ) _vars.push_back( fg.var(i) ); - // copy clusters - _clusters.reserve( fg.nrFactors() ); - for( size_t I = 0; I < fg.nrFactors(); I++ ) - _clusters.push_back( fg.factor(I).vars() ); - - if( onlyMaximal ) - eraseNonMaximal(); + if( onlyMaximal ) { + for( size_t I = 0; I < fg.nrFactors(); I++ ) + if( fg.isMaximal( I ) ) { + _clusters.push_back( fg.factor(I).vars() ); + size_t clind = _G.addNode2(); + foreach( const Neighbor &i, fg.nbF(I) ) + _G.addEdge( i, clind, true ); + } + } else { + // copy clusters + _clusters.reserve( fg.nrFactors() ); + for( size_t I = 0; I < fg.nrFactors(); I++ ) + _clusters.push_back( fg.factor(I).vars() ); + // copy bipartite graph + _G = fg.bipGraph(); + } }