OpenGM  2.3.x
Discrete Graphical Model Library
modelTrees.hxx
Go to the documentation of this file.
1 #pragma once
2 #ifndef OPENGM_MODELTREES_HXX
3 #define OPENGM_MODELTRESS_HXX
4 
5 #include <string>
6 #include <iostream>
7 #include <vector>
8 #include <set>
9 #include <map>
10 
13 //#include "opengm/graphicalmodel/graphicalmodel_hdf5.hxx"
17 
18 #include <boost/lexical_cast.hpp>
19 
20 
21 namespace opengm{
22 
23  template<class GM>
24  class modelTrees{
25 
26  public:
27 
28  typedef GM GmType;
29  typedef typename GM::ValueType ValueType;
30  typedef typename GM::IndexType IndexType;
31  typedef typename GM::LabelType LabelType;
32  typedef typename GM::OperatorType OperatorType;
33 
34  modelTrees(const GmType&);
35  IndexType numberOfTrees() const;
36  IndexType treeOfVariable(IndexType i); //Root index if Variable is in a Tree, numberOfVariables if not
37  IndexType parentOfVariable(IndexType i); //Parent index if Variable is in a Tree, numberOfVariables if not
38  IndexType treeOfRoot(IndexType i);
39  void roots(std::vector<IndexType>&);
40  void nodes(std::vector<std::vector<IndexType> >&);
41 
42  private:
43 
44  const GmType& gm_;
45  // Partition Trees_;
46  std::map<IndexType, IndexType> representives_;
47  std::vector<IndexType> parents_;
48  std::vector<bool> b_roots_;
49  IndexType numberOfRoots_;
50 
51  };
52  // end class
53 
54 
55  // Constructor
56  template<class GM>
58  :
59  gm_(gm)
60  {
61  std::vector<std::set<IndexType> > neighbors;
62  gm_.variableAdjacencyList(neighbors);
63 
64  std::vector<IndexType> degree(gm_.numberOfVariables());
65  std::vector<IndexType> leafs;
66  b_roots_.resize(gm_.numberOfVariables());
67  parents_.resize(gm_.numberOfVariables());
68  typename std::set<typename GM::IndexType>::iterator it;
69  typename std::set<typename GM::IndexType>::iterator fi;
70 
71  for(IndexType i=0;i<degree.size();++i){
72  degree[i]=neighbors[i].size();
73  parents_[i]=gm_.numberOfVariables();
74  if(degree[i]==1){
75  leafs.push_back(i);
76  }
77  }
78  while(!leafs.empty()){
79  IndexType l=leafs.back();
80  leafs.pop_back();
81  if(degree[l]>0){
82  it=neighbors[l].begin();
83  b_roots_[*it]=1;
84  b_roots_[l]=0;
85  parents_[l]=*it;
86  parents_[*it]=*it;
87  degree[*it]=degree[*it]-1;
88  fi=neighbors[*it].find(l);
89  neighbors[*it].erase(fi);
90  if(degree[*it]==1){
91  leafs.push_back(*it);
92  }
93  }
94  }
95 
96  numberOfRoots_=0;
97  for(IndexType i=0;i<gm_.numberOfVariables();++i){
98  if(b_roots_[i]==1){
99  representives_[i]=numberOfRoots_;
100  numberOfRoots_++;
101  }
102  }
103 
104  }
105 
106  template<class GM>
107  inline
108  typename GM::IndexType modelTrees<GM>::numberOfTrees() const{
109  return numberOfRoots_;
110  }
111 
112  template<class GM>
113  inline
114  typename GM::IndexType modelTrees<GM>::treeOfVariable(IndexType i){
115  if(parents_[i]==gm_.numberOfVariables()){
116  return gm_.numberOfVariables();
117  }
118  else{
119  IndexType r=i;
120  while(parents_[r]!=r){
121  r=parents_[r];
122  }
123  return r;
124  }
125  }
126 
127  template<class GM>
128  inline
129  void modelTrees<GM>::roots(std::vector<IndexType>& roots){
130  roots.resize(numberOfRoots_);
131  IndexType j=0;
132  for(IndexType i=0;i<gm_.numberOfVariables();++i){
133  if(b_roots_[i]==1){
134  roots[j]=i;
135  j++;
136  }
137  }
138 
139  }
140 
141  template<class GM>
142  inline
143  typename GM::IndexType modelTrees<GM>::parentOfVariable(IndexType i){
144  if(parents_[i]==gm_.numberOfVariables()){
145  return gm_.numberOfVariables();
146  }
147  else{
148  return parents_[i];
149  }
150  }
151 
152  template<class GM>
153  inline
154  void modelTrees<GM>::nodes(std::vector<std::vector<IndexType> >& nodes){
155 
156  nodes.resize(numberOfRoots_);
157  for(IndexType i=0;i<gm_.numberOfVariables();++i){
158  if(parents_[i]!=gm_.numberOfVariables()){
159  IndexType treeID = representives_[treeOfVariable(i)];
160  nodes[treeID].push_back(i);
161  }
162  }
163  }
164  template<class GM>
165  inline
166  typename GM::IndexType modelTrees<GM>::treeOfRoot(IndexType i){
167  if(parents_[i]==gm_.numberOfVariables()){
168  return gm_.numberOfVariables();
169  }
170  else{
171  return representives_[treeOfVariable(i)];
172  }
173  }
174 
175 }
176 
177 #endif
IndexType treeOfVariable(IndexType i)
Definition: modelTrees.hxx:114
IndexType numberOfTrees() const
Definition: modelTrees.hxx:108
The OpenGM namespace.
Definition: config.hxx:43
GM::OperatorType OperatorType
Definition: modelTrees.hxx:32
GM::IndexType IndexType
Definition: modelTrees.hxx:30
GM::ValueType ValueType
Definition: modelTrees.hxx:29
modelTrees(const GmType &)
Definition: modelTrees.hxx:57
GM::LabelType LabelType
Definition: modelTrees.hxx:31
IndexType parentOfVariable(IndexType i)
Definition: modelTrees.hxx:143
void roots(std::vector< IndexType > &)
Definition: modelTrees.hxx:129
void nodes(std::vector< std::vector< IndexType > > &)
Definition: modelTrees.hxx:154
IndexType treeOfRoot(IndexType i)
Definition: modelTrees.hxx:166