modelTrees.hxx
Go to the documentation of this file.00001 #pragma once
00002 #ifndef OPENGM_MODELTREES_HXX
00003 #define OPENGM_MODELTRESS_HXX
00004
00005 #include <string>
00006 #include <iostream>
00007 #include <vector>
00008 #include <set>
00009 #include <map>
00010
00011 #include "opengm/functions/scaled_view.hxx"
00012 #include "opengm/graphicalmodel/graphicalmodel.hxx"
00013
00014 #include "opengm/operations/adder.hxx"
00015 #include "opengm/utilities/random.hxx"
00016 #include "opengm/datastructures/partition.hxx"
00017
00018 #include <boost/lexical_cast.hpp>
00019
00020
00021 namespace opengm{
00022
00023 template<class GM>
00024 class modelTrees{
00025
00026 public:
00027
00028 typedef GM GmType;
00029 typedef typename GM::ValueType ValueType;
00030 typedef typename GM::IndexType IndexType;
00031 typedef typename GM::LabelType LabelType;
00032 typedef typename GM::OperatorType OperatorType;
00033
00034 modelTrees(const GmType&);
00035 IndexType numberOfTrees() const;
00036 IndexType treeOfVariable(IndexType i);
00037 IndexType parentOfVariable(IndexType i);
00038 IndexType treeOfRoot(IndexType i);
00039 void roots(std::vector<IndexType>&);
00040 void nodes(std::vector<std::vector<IndexType> >&);
00041
00042 private:
00043
00044 const GmType& gm_;
00045
00046 std::map<IndexType, IndexType> representives_;
00047 std::vector<IndexType> parents_;
00048 std::vector<bool> b_roots_;
00049 IndexType numberOfRoots_;
00050
00051 };
00052
00053
00054
00055
00056 template<class GM>
00057 modelTrees<GM>::modelTrees(const GmType& gm)
00058 :
00059 gm_(gm)
00060 {
00061 std::vector<std::set<IndexType> > neighbors;
00062 gm_.variableAdjacencyList(neighbors);
00063
00064 std::vector<IndexType> degree(gm_.numberOfVariables());
00065 std::vector<IndexType> leafs;
00066 b_roots_.resize(gm_.numberOfVariables());
00067 parents_.resize(gm_.numberOfVariables());
00068 typename std::set<typename GM::IndexType>::iterator it;
00069 typename std::set<typename GM::IndexType>::iterator fi;
00070
00071 for(IndexType i=0;i<degree.size();++i){
00072 degree[i]=neighbors[i].size();
00073 parents_[i]=gm_.numberOfVariables();
00074 if(degree[i]==1){
00075 leafs.push_back(i);
00076 }
00077 }
00078 while(!leafs.empty()){
00079 IndexType l=leafs.back();
00080 leafs.pop_back();
00081 if(degree[l]>0){
00082 it=neighbors[l].begin();
00083 b_roots_[*it]=1;
00084 b_roots_[l]=0;
00085 parents_[l]=*it;
00086 parents_[*it]=*it;
00087 degree[*it]=degree[*it]-1;
00088 fi=neighbors[*it].find(l);
00089 neighbors[*it].erase(fi);
00090 if(degree[*it]==1){
00091 leafs.push_back(*it);
00092 }
00093 }
00094 }
00095
00096 numberOfRoots_=0;
00097 for(IndexType i=0;i<gm_.numberOfVariables();++i){
00098 if(b_roots_[i]==1){
00099 representives_[i]=numberOfRoots_;
00100 numberOfRoots_++;
00101 }
00102 }
00103
00104 }
00105
00106 template<class GM>
00107 inline
00108 typename GM::IndexType modelTrees<GM>::numberOfTrees() const{
00109 return numberOfRoots_;
00110 }
00111
00112 template<class GM>
00113 inline
00114 typename GM::IndexType modelTrees<GM>::treeOfVariable(IndexType i){
00115 if(parents_[i]==gm_.numberOfVariables()){
00116 return gm_.numberOfVariables();
00117 }
00118 else{
00119 IndexType r=i;
00120 while(parents_[r]!=r){
00121 r=parents_[r];
00122 }
00123 return r;
00124 }
00125 }
00126
00127 template<class GM>
00128 inline
00129 void modelTrees<GM>::roots(std::vector<IndexType>& roots){
00130 roots.resize(numberOfRoots_);
00131 IndexType j=0;
00132 for(IndexType i=0;i<gm_.numberOfVariables();++i){
00133 if(b_roots_[i]==1){
00134 roots[j]=i;
00135 j++;
00136 }
00137 }
00138
00139 }
00140
00141 template<class GM>
00142 inline
00143 typename GM::IndexType modelTrees<GM>::parentOfVariable(IndexType i){
00144 if(parents_[i]==gm_.numberOfVariables()){
00145 return gm_.numberOfVariables();
00146 }
00147 else{
00148 return parents_[i];
00149 }
00150 }
00151
00152 template<class GM>
00153 inline
00154 void modelTrees<GM>::nodes(std::vector<std::vector<IndexType> >& nodes){
00155
00156 nodes.resize(numberOfRoots_);
00157 for(IndexType i=0;i<gm_.numberOfVariables();++i){
00158 if(parents_[i]!=gm_.numberOfVariables()){
00159 IndexType treeID = representives_[treeOfVariable(i)];
00160 nodes[treeID].push_back(i);
00161 }
00162 }
00163 }
00164 template<class GM>
00165 inline
00166 typename GM::IndexType modelTrees<GM>::treeOfRoot(IndexType i){
00167 if(parents_[i]==gm_.numberOfVariables()){
00168 return gm_.numberOfVariables();
00169 }
00170 else{
00171 return representives_[treeOfVariable(i)];
00172 }
00173 }
00174
00175 }
00176
00177 #endif