OpenGM  2.3.x
Discrete Graphical Model Library
planar_maxcut.hxx
Go to the documentation of this file.
1 #pragma once
2 #ifndef OPENGM_PLANAR_MAXCUT_HXX
3 #define OPENGM_PLANAR_MAXCUT_HXX
4 #include "planar_graph.hxx"
5 
6 namespace opengm {
7 
9 namespace external {
10 
12 
14 class PlanarMaxCut {
15 public:
16  typedef size_t node_type;
17  typedef double value_type;
18 
19  PlanarMaxCut();
20  ~PlanarMaxCut();
21  PlanarMaxCut(size_t numberOfNodes, size_t numberOfEdges);
22  void addEdge(node_type,node_type,value_type);
23  void calculateCut();
24  template <class VEC> void getCut(VEC&);
25  template <class VEC> void getLabeling(VEC&);
26 
27 private:
28  planargraph::PlanarGraph graph_;
29 
30  size_t numberOfNodes_;
31  size_t numberOfEdges_; // usused so far
32 };
33 
34 
35  inline PlanarMaxCut::PlanarMaxCut() {
36  numberOfNodes_ = 0;
37  numberOfEdges_ = 0;
38  }
39 
40  inline PlanarMaxCut::PlanarMaxCut(size_t numberOfNodes, size_t numberOfEdges) {
41  numberOfNodes_ = numberOfNodes;
42  numberOfEdges_ = numberOfEdges;
43 
44  for(size_t variableId=0; variableId < numberOfNodes; ++variableId)
45  {
46  graph_.add_node();
47  }
48  }
49 
50  inline PlanarMaxCut::~PlanarMaxCut()
51  {
52  }
53 
54  inline void PlanarMaxCut::addEdge(node_type n1, node_type n2, value_type cost) {
55  assert(n1 < numberOfNodes_);
56  assert(n2 < numberOfNodes_);
57  long int e = graph_.find_edge(n1,n2);
58  if(e == -1){
59  graph_.add_edge(n1, n2, cost);
60  } else {
61  graph_.add_edge_weight(e, cost);
62  }
63  }
64 
65  void PlanarMaxCut::calculateCut() {
66 
67  graph_.planarize(); // Planarize graph
68  graph_.construct_dual(); // Construct dual graph
69  graph_.calculate_maxcut(); // Perform perfect matching / max cut
70 
71  }
72 
73  template <class VEC>
74  void PlanarMaxCut::getCut(VEC& cut) {
75 
76  // todo: add temptated interface in planargraph
77  std::vector<bool> temp = graph_.get_cut();
78  if(cut.size()<temp.size())
79  cut.resize(temp.size());
80  for(size_t i=0; i<temp.size(); ++i)
81  cut[i]=temp[i];
82  return;
83  }
84 
85  template <class VEC>
86  void PlanarMaxCut::getLabeling(VEC& segmentation) {
87 
88  // todo: add temptated interface in planargraph
89  std::vector<int> temp;
90  graph_.get_labeling(temp);
91  if(segmentation.size()<temp.size())
92  segmentation.resize(temp.size());
93  for(size_t i=0; i<temp.size(); ++i)
94  segmentation[i]=temp[i];
95  return;
96  }
97 
99 
100 } // namespace external
101 } // namespace opengm
102 
103 #endif // #ifndef OPENGM_PLANAR_MAXCUT_HXX
The OpenGM namespace.
Definition: config.hxx:43