This file is indexed.

/usr/include/opengm/inference/auxiliary/planar_graph.hxx is in libopengm-dev 2.3.6-2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
#pragma once
#ifndef OPENGM_PLANAR_GRAPH_HXX
#define OPENGM_PLANAR_GRAPH_HXX

#include <iostream>
#include <vector>
#include <stack>
#include <list>
#include "opengm/opengm.hxx"

//TODO: Fix include path
#include <planarity.src-patched/graph.h>
#include <planarity.src-patched/listcoll.h>
#include <planarity.src-patched/stack.h>
#include <planarity.src-patched/appconst.h>

#include <blossom5.src-patched/PerfectMatching.h>
#include <blossom5.src-patched/PMimplementation.h>
#include <blossom5.src-patched/MinCost/MinCost.h>

namespace opengm {
   namespace external {
      namespace planargraph {

         typedef double DataType;

//////////////////////////////////////////////////////
// PlanarGraph components
//////////////////////////////////////////////////////

         struct Node
         {
            Node() : weight(0.0), adj(0) {};

            // Node weight
            // Unused (0.0) for dual edges
            // do zrobienia: make node class a template class and only use weight in reweighted_planar_max_cut
            DataType weight;
    
            // List of indices of the dual edges
            std::list<size_t> adj;
         };


         struct Edge
         {
            Edge()
               : tail(0), head(0), weight(0.0), left_face(-1), right_face(-1) {};

            // Indices of tail and head node
            size_t tail;
            size_t head;
    
            // Edge weight
            DataType weight;

            // Indices of left and right face (as seen from head to tail).
            // -1 if unassigned
            int left_face;
            int right_face;
         };


         struct Face
         {
            Face() : edges(0), dual_nodes(0) {};

            // List of edges surrounding the face. Is in sorted order (s.t. the
            // edges form an orbit) after calling planarize()
            std::list<size_t> edges;
    
            // List of the dual nodes_ (forming a clique) belonging to  this face
            std::list<size_t> dual_nodes;
         };


//////////////////////////////////////////////////////
// PlanarGraph class definition
//////////////////////////////////////////////////////

         class PlanarGraph
         {
         public:
            PlanarGraph();
            PlanarGraph(size_t n, bool debug);
            ~PlanarGraph();

            size_t num_nodes() const { return nodes_.size(); };
            size_t num_edges() const { return edges.size(); };
            size_t num_faces() const { return faces.size(); };

    
            size_t add_node();
            long int find_edge(size_t u, size_t v) const;
            size_t add_edge(size_t u, size_t v, DataType w);
            void add_edge_weight(size_t e, DataType w);
    
            void print();

            void planarize();
            void construct_dual();
    
            void get_labeling(std::vector<int> & x) const;

            void calculate_maxcut();
            std::vector<bool> get_cut() const; 
            std::vector<int> get_labeling_from_cut(const std::vector<bool>& cut) const;
            double cost_of_cut(const std::vector<int>& x) const;
            double cost_of_cut() const;

         protected:
            long int get_dest(size_t v, size_t e) const;
            long int get_following_edge(size_t v, size_t e) const;
    
            void clear_faces();    

            size_t compute_dual_num_edges() const;

            std::vector<Node> nodes_;
            std::vector<Edge> edges;
            std::vector<Face> faces;
    
            //std::unique_ptr<PerfectMatching> Dual_;
            PerfectMatching* Dual_;

            bool debug_;
    
         };


//////////////////////////////////////////////////////
// Basic functionality
//////////////////////////////////////////////////////


         //

         PlanarGraph::PlanarGraph()
            : nodes_(0), edges (0), faces(0), Dual_(NULL), debug_(false)
              //  : nodes_(0), edges (0), faces(0), Dual_(nullptr), debug_(debug)
         {
            nodes_.reserve(0);
            edges.reserve(0);
         }

         PlanarGraph::PlanarGraph(size_t n, bool debug = false)
            : nodes_(0), edges (0), faces(0), Dual_(NULL), debug_(debug)
              //  : nodes_(0), edges (0), faces(0), Dual_(nullptr), debug_(debug)
         {
            nodes_.reserve(n);
            edges.reserve(3*n);
         }

         PlanarGraph::~PlanarGraph(){
            if(Dual_ != NULL)
               delete Dual_;
         }

         // Adds a new node with weight w to the graph
         size_t PlanarGraph::add_node()
         {
            // Create new node object (in place) and set its weight
            size_t v = nodes_.size();
            nodes_.resize(v+1);

            // Return index of the new node
            return v;
         }


         // Adds new edge with weight w between nodes_ u and v
         size_t PlanarGraph::add_edge(size_t u, size_t v, DataType w)
         {
            // Check that u and v are valid node indices
            OPENGM_ASSERT(u >= 0 && u<nodes_.size());
            OPENGM_ASSERT(v >= 0 && v<nodes_.size());
	
            // Create new edge object (in place) and set properties
            size_t e = edges.size();
            edges.resize(e+1);
            edges[e].tail = u;
            edges[e].head = v;
            edges[e].weight = w;

            // Add edge index to both nodes_' adjacency lists
            nodes_[u].adj.push_back(e);
            nodes_[v].adj.push_back(e);

            // Return index of the new edge
            return e;
         }


         // Adds weight w to the existing edge e
         void PlanarGraph::add_edge_weight(size_t e, DataType w)
         {
            // Check the e is a valid edge index
            OPENGM_ASSERT(e >= 0 && e < edges.size());
	
            edges[e].weight = w;
         }


         // Returns index of the edge connecting nodes_ u and v
         // -1 if such an edge does not exist
         long int PlanarGraph::find_edge(size_t u, size_t v) const
         {
            // Search u's adjacency list for an edge connecting it to v.
            for(size_t e=0; e<nodes_[u].adj.size(); ++e)
            {
               if( edges[e].tail==v || edges[e].head==v )
               {
                  return e;
               }
            }

            // Return -1 if the loop did not find one.
            return -1;
         }


         // Returns index of the destination node of Edge e as seen from node v
         // -1 if e is not incident on v
         long int PlanarGraph::get_dest(size_t v, size_t e) const
         {
            if(v == edges[e].tail)
               return edges[e].head;
            else if (v == edges[e].head)
               return edges[e].tail;
            else
               return -1;
         }
	

         // Simple command line output of the graph for debugging
         void PlanarGraph::print()
         {
            if(debug_)
               std::cout << "PlanarGraph with " << num_nodes() << " nodes_, "
                         << num_edges() << " edges and " << num_faces() << " faces.\n";

            for(size_t u = 0; u<nodes_.size(); ++u)
            {
               // Print current node's id and weight
               if(debug_)
                  std::cout << u << "\t[" << nodes_[u].weight << "]:\t";

               // For all edges in current node's adjacency list
               for(std::list<size_t>::iterator it = nodes_[u].adj.begin();
                   it != nodes_[u].adj.end(); ++it)
               {
                  // Get the destination of the current edge
                  // Print destination id and weight of the edge
                  size_t v = get_dest(u, *it);
                  if(debug_)
                     std::cout << v << " (" << edges[*it].weight << "), ";
               }
               if(debug_)
                  std::cout << "\n";
            }
         }


//////////////////////////////////////////////////////
// Construction of planar embedding
//////////////////////////////////////////////////////

         // Returns the index of the edge that succeeds e in v's adjacency list
         // -1 if e is not incident on v
         long int PlanarGraph::get_following_edge(size_t v, size_t e) const
         {
            // Iterate to e in v's adjacency list
            std::list<size_t>::const_iterator it = nodes_[v].adj.begin();
            while((*it != e) && (it != nodes_[v].adj.end()))
               ++it;

            if(it==nodes_[v].adj.end()) // e is not in v's adj list
            {
               return -1;
            }
            else  // e is in v's adj list
            {
               ++it; // Make one more step
               if(it==nodes_[v].adj.end()) // e is the last element in v's adj list
                  return nodes_[v].adj.front();
               else
                  return *it;
            }
         }


         void PlanarGraph::clear_faces()
         {
            // Pop all elements from the graph's list of faces
            while(!faces.empty())
            {
               faces.pop_back();
            }

            // Set the face indices of all edges to -1
            for(std::vector<Edge>::iterator it = edges.begin(); it != edges.end(); ++it)
            {
               it->left_face = -1;
               it->right_face = -1;
            }
         }

         size_t PlanarGraph::compute_dual_num_edges() const
         {
            // number of dual edges if the number of original edges ( = cross edges ) + clique edges in each face
            size_t dual_num_edges = num_edges();
            for(size_t f=0; f<faces.size(); ++f) {
               const size_t face_size = faces[f].edges.size();
               dual_num_edges += (face_size * (face_size - 1))/2;
            }
            return dual_num_edges;
         }


         // Planarizes the graph, i.e.
         // - Sorts the adjacency lists of all nodes_ to form a rotation system
         // - Constructs faces and sets face/edge relations (see definitions of
         //   struct Edge and struct Face)
         void PlanarGraph::planarize()
         {
            // ToDo
            // Assert that the graph is biconnected
            // Reserve space for faces (how much?)

            //// Copy graph in planarity code graph data structure.
            graphP g = gp_New();
            gp_InitGraph(g, num_nodes());
            for(std::vector<Edge>::iterator it=edges.begin(); it!=edges.end(); ++it)
            {
               gp_AddEdge(g, it->tail, 0, it->head, 0);
            }

            // Invoke code that sorts the adjacency lists
            if (gp_Embed(g, EMBEDFLAGS_PLANAR) == OK) {
               gp_SortVertices(g);
            } else {
               throw("PlanarGraph not planar\n");
            }

            //// Repopulate edges in the embedding order
            for (size_t i = 0; i < g->N; ++i)
            {
               size_t u = i;
		
               size_t j = g->G[i].link[1];
               while (j >= g->N)
               {
                  OPENGM_ASSERT(i != g->G[j].v); // What does this OPENGM_ASSERT do?
                  OPENGM_ASSERT(g->G[j].v < g->N);
            
                  size_t v = g->G[j].v;

                  // Find the edge connecting u and v
                  std::list<size_t>::iterator it = nodes_[u].adj.begin();
                  while(edges[*it].tail != v && edges[*it].head != v && it != nodes_[u].adj.end())
                     ++it;
                  size_t e = *it;
                  OPENGM_ASSERT(it != nodes_[i].adj.end());

                  // Remove the edge from its current position, and insert at the back
                  nodes_[u].adj.erase(it);
                  nodes_[u].adj.push_back(e);

                  j = g->G[j].link[1];
               }
            }

            //// Clear faces
            clear_faces();


            //// Construct faces
            // do zrobienia: code for following the orbit starting from left and right face is mostly duplication: clean up!
            for(size_t e = 0; e < edges.size(); ++e) // Loop over all edges
            {
               // Check if the right face of e has already been dealt with.
               // If not, construct it!
               
               //enum class face_type {left,right};
               typedef int face_type;
               const face_type face_type_left  = 1;
               const face_type face_type_right = 2;

        
               if(edges[e].right_face == -1)
               {
                  // Create new face object
                  const size_t f = faces.size();
                  faces.resize(f+1);

                  // Assign e <-> f
                  faces[f].edges.push_back(e);
                  edges[e].right_face = f;

                  // Follow the orbit in FORWARD direction (i.e. starting with e's head)
                  size_t v = edges[e].head; // Next node
                  size_t ee = e;
                  //size_t ee = get_following_edge(v, e); // Next edge
                  face_type ee_face;
                  do {
                     // Get next node and edge
                     ee = get_following_edge(v, ee);
                     v = get_dest(v, ee);

                     // Set f as face of ee, left or right depends on the formal direction of ee
                     if(v==edges[ee].tail) {
                        edges[ee].left_face = f;
                        ee_face = face_type_left;
                     }
                     if(v==edges[ee].head) {
                        edges[ee].right_face = f;
                        ee_face = face_type_right;
                     }
                     faces[f].edges.push_back(ee); // a face can have the same edge in the left and right. do zeobienia: this must be reflected in the faces data structure as well.


                  } while(! (ee == e && ee_face == face_type_right) ); // If ee==e and we are on the same side again, we went the full circle
               }

               // Check if the left face of e has already been dealt with.
               // If not, construct it!
               // to do: remove duplicate code by switching left for right and vice versa
               if(edges[e].left_face == -1)
               {
                  // Create new face object
                  const size_t f = faces.size();
                  faces.resize(f+1);

                  // Assign e <-> f
                  faces[f].edges.push_back(e);
                  edges[e].left_face = f;

                  // Follow the orbit in BACKWARD direction (i.e. starting with e's tail)
                  size_t v = edges[e].tail; // Next node
                  size_t ee = e;
                  //size_t ee = get_following_edge(v, e); // Next edge
                  face_type ee_face;
                  do {
                     // Get next node and edge
                     ee = get_following_edge(v, ee);
                     v = get_dest(v, ee);

                     // Set f as face of ee, left or right depends on the formal direction of ee
                     if(v==edges[ee].tail) {
                        edges[ee].left_face = f;
                        ee_face = face_type_left;
                     }
                     if(v==edges[ee].head) {
                        edges[ee].right_face = f;
                        ee_face = face_type_right;
                     }
                     faces[f].edges.push_back(ee); // a face can have the same edge in the left and right. do zeobienia: this must be reflected in the faces data structure as well.


                  } while(! (ee == e && ee_face == face_type_left) ); // If ee==e and we are on the same side again, we went the full circle
               }
            }
    
            //// Checks and clean-up
            // Check: Do all edges have a left and a right face?
            for(std::vector<Edge>::iterator it=edges.begin(); it!=edges.end(); ++it)
            {
               OPENGM_ASSERT((*it).left_face != -1);
               OPENGM_ASSERT((*it).right_face != -1);
            }

            // Check if genus = 0, i.e graph is planar
            OPENGM_ASSERT(num_nodes()-num_edges()+num_faces() == 2);

            // Delete planarity code graph
            gp_Free(&g);
         }


//////////////////////////////////////////////////////
// Construction of dual graph
//////////////////////////////////////////////////////

         // Constructs the expanded dual of the graph. The primal graph needs to
         // be planarized before.
         void PlanarGraph::construct_dual()
         {    
            // Allocate dual graph in PerfectMatching data structure
            // Todo: Reasonable number of max dual edges
            Dual_ = new PerfectMatching(2*num_edges(), compute_dual_num_edges());
            // Dual_ = std::unique_ptr<PerfectMatching>(new PerfectMatching(2*num_edges(), compute_dual_num_edges()));
            PerfectMatching::Options Dual_options;
            Dual_options.verbose = false;
            Dual_->options = Dual_options;
    
            // insert all cross edges corresponding to the original edges
            // note: cross edges directly correspond to the original edges
            size_t counter = 0;
            for(size_t e = 0; e < edges.size(); ++e)
            {		
               // For the current edge of G, add dual nodes_ for its two faces
               const size_t u = counter;
               const size_t v = counter + 1;
               counter += 2;

               // Add the dual cross edge of e, connecting u and v
               // Weight is the negative of e's weight
               Dual_->AddEdge(u, v, edges[e].weight);
            }

            // insert clique edges connecting all dual nodes inside a face
            counter = 0;
            for(size_t e = 0; e < edges.size(); ++e)
            {		
               size_t u = counter;
               size_t v = counter + 1;
               counter += 2;

               // "Integrate" u into the left face of e
               size_t f = edges[e].left_face;
               for(std::list<size_t>::iterator it = faces[f].dual_nodes.begin(); it != faces[f].dual_nodes.end(); ++it)
               {
                  Dual_->AddEdge(u, *it, 0.0);
               }
               faces[f].dual_nodes.push_back(u);

               // "Integrate" v into the right face
               f = edges[e].right_face;
               for(std::list<size_t>::iterator it = faces[f].dual_nodes.begin(); it != faces[f].dual_nodes.end(); ++it)
               {
                  Dual_->AddEdge(v, *it, 0.0);
               }
               faces[f].dual_nodes.push_back(v);
            }
         }

         double PlanarGraph::cost_of_cut() const
         {
            double cost = 0.0;
            for(size_t e = 0; e < num_edges(); ++e)
            {
               if(Dual_->GetSolution(e) == 0)
                  cost += edges[e].weight;
            }

            return cost;
         }
	
         double PlanarGraph::cost_of_cut(const std::vector<int>& x) const
         {
            // do zrobienia: check if x is a cut
            double cost = 0.0;
            for(size_t e = 0; e < num_edges(); ++e)
            {
               if(x[e] == 1)
                  cost += edges[e].weight;
            }

            return cost;
         }

         void PlanarGraph::calculate_maxcut()
         {
            //OPENGM_ASSERT(Dual_ != nullptr); 
            OPENGM_ASSERT(Dual_ != NULL);
            Dual_->Solve();
         }

         std::vector<bool> PlanarGraph::get_cut() const
         {
            std::vector<bool> cut(num_edges(),false);
            for(size_t e = 0; e < num_edges(); ++e)
            {
               if(Dual_->GetSolution(e) == 0) {
                  cut[e] = true;
               } else if(Dual_->GetSolution(e) == 1) {
                  cut[e] = false;
               } else {
                  throw std::logic_error("Perfect matching solver did not succeed");
               }
            }
            return cut;	
         }

         // Reads the labeling defined by a given cut into the vector x. Does
         // not output the labels for the unary nodes_. A cut has to be given
         std::vector<int> PlanarGraph::get_labeling_from_cut(const std::vector<bool>& cut) const
         {
            OPENGM_ASSERT(cut.size() == edges.size());
            // Make labeling size num_nodes(), i.e. including unary nodes_. Set all
            // labels to -1 (meaning unassigned)
            std::vector<int> labeling(num_nodes());
            for(size_t v = 0; v < num_nodes(); ++v)
               labeling[v] = -1;

            std::stack<size_t> s;
            size_t visited_nodes=0;
            for(size_t startnode=0; startnode< num_nodes(); ++startnode){
               if(visited_nodes==num_nodes())
                  break;
               if( labeling[startnode]!= -1)
                  continue;
               labeling[startnode] = 0;
               s.push(startnode);
               
               while(!s.empty()) // As long as stack is not empty
               {
                  // Take top element from stack
                  size_t u = s.top();
                  s.pop();
                  ++visited_nodes;
               
                  // Go through all incident edges
                  for(std::list<size_t>::const_iterator it = nodes_[u].adj.begin(); it != nodes_[u].adj.end(); ++it)
                  {
                     size_t e = *it; // Edge and...
                     size_t v = get_dest(u, e); // its destination (i.e. the neighbor)
                  
                     // If the neighbor has not yet been seen, put it on the
                     // stack and assign the respective label
                     if(labeling[v] == -1)
                     {
                        s.push(v);
                     
                        if(cut[e])
                           labeling[v] = (labeling[u] + 1) % 2; // mapping 0->1 and 1->0
                        else
                           labeling[v] = labeling[u];
                     }
                  
                     // Check for inconsistent cut when encountering a node again
                     if(labeling[v] != -1) // node already seen
                     {
                        if(cut[e]) {
                           OPENGM_ASSERT(labeling[v] + labeling[u] == 1);
                        } else {
                           OPENGM_ASSERT(labeling[v] == labeling[u]);
                        }
                     }
                  }
               }
            }
            
            for(size_t v=0; v<labeling.size(); ++v) {
               OPENGM_ASSERT(labeling[v] == 0 || labeling[v] == 1);
            }

            return labeling;
         }


         void PlanarGraph::get_labeling(std::vector<int> & x) const
         {
            std::vector<bool> cut = get_cut();
            x = get_labeling_from_cut(cut);
         }

      } //namespace planargraph
   } // namespace external
} // namespace opengm

#endif // OPENGM_PLANAR_GRAPH_HXX