Debian Codegraph @

ot/lp/full_bipartitegraph.h

raw symbols SCIP
1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
2 *
3 * This file has been adapted by Nicolas Bonneel (2013),
4 * from full_graph.h from LEMON, a generic C++ optimization library,
5 * to implement a lightweight fully connected bipartite graph. A previous
6 * version of this file is used as part of the Displacement Interpolation
7 * project,
8 * Web: http://www.cs.ubc.ca/labs/imager/tr/2011/DisplacementInterpolation/
9 *
10 *
11 **** Original file Copyright Notice :
12 * Copyright (C) 2003-2010
13 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
14 * (Egervary Research Group on Combinatorial Optimization, EGRES).
15 *
16 * Permission to use, modify and distribute this software is granted
17 * provided that this copyright notice appears in all copies. For
18 * precise terms see the accompanying LICENSE file.
19 *
20 * This software is provided "AS IS" with no warranty of any kind,
21 * express or implied, and with no claim as to its suitability for any
22 * purpose.
23 *
24 */
25
26 #pragma once
27
28 #include "core.h"
29 #include <cstdint>
30
31 ///\ingroup graphs
32 ///\file
33 ///\brief FullBipartiteDigraph and FullBipartiteGraph classes.
34
35
36 namespace lemon {
37
38
39 class FullBipartiteDigraphBase {
40 public:
41
42 typedef FullBipartiteDigraphBase Digraph;
43
44 //class Node;
45 typedef int Node;
46 //class Arc;
47 typedef int64_t Arc;
48
49 protected:
50
51 int _node_num;
52 int64_t _arc_num;
53
54 FullBipartiteDigraphBase() {}
55
56 void construct(int n1, int n2) { _node_num = n1+n2; _arc_num = (int64_t)n1 * (int64_t)n2; _n1=n1; _n2=n2;}
57
58 public:
59
60 int _n1, _n2;
61
62
63 Node operator()(int ix) const { return Node(ix); }
64 static int index(const Node& node) { return node; }
65
66 Arc arc(const Node& s, const Node& t) const {
67 if (s<_n1 && t>=_n1)
68 return Arc((int64_t)s * (int64_t)_n2 + (int64_t)(t-_n1) );
69 else
70 return Arc(-1);
71 }
72
73 int nodeNum() const { return _node_num; }
74 int64_t arcNum() const { return _arc_num; }
75
76 int maxNodeId() const { return _node_num - 1; }
77 int64_t maxArcId() const { return _arc_num - 1; }
78
79 Node source(Arc arc) const { return arc / _n2; }
80 Node target(Arc arc) const { return (arc % _n2) + _n1; }
81
82 static int id(Node node) { return node; }
83 static int64_t id(Arc arc) { return arc; }
84
85 static Node nodeFromId(int id) { return Node(id);}
86 static Arc arcFromId(int64_t id) { return Arc(id);}
87
88
89 Arc findArc(Node s, Node t, Arc prev = -1) const {
90 return prev == -1 ? arc(s, t) : -1;
91 }
92
93 void first(Node& node) const {
94 node = _node_num - 1;
95 }
96
97 static void next(Node& node) {
98 --node;
99 }
100
101 void first(Arc& arc) const {
102 arc = _arc_num - 1;
103 }
104
105 static void next(Arc& arc) {
106 --arc;
107 }
108
109 void firstOut(Arc& arc, const Node& node) const {
110 if (node>=_n1)
111 arc = -1;
112 else
113 arc = (node + 1) * _n2 - 1;
114 }
115
116 void nextOut(Arc& arc) const {
117 if (arc % _n2 == 0) arc = 0;
118 --arc;
119 }
120
121 void firstIn(Arc& arc, const Node& node) const {
122 if (node<_n1)
123 arc = -1;
124 else
125 arc = _arc_num + node - _node_num;
126 }
127
128 void nextIn(Arc& arc) const {
129 arc -= _n2;
130 if (arc < 0) arc = -1;
131 }
132
133 };
134
135 /// \ingroup graphs
136 ///
137 /// \brief A directed full graph class.
138 ///
139 /// FullBipartiteDigraph is a simple and fast implementation of directed full
140 /// (complete) graphs. It contains an arc from each node to each node
141 /// (including a loop for each node), therefore the number of arcs
142 /// is the square of the number of nodes.
143 /// This class is completely static and it needs constant memory space.
144 /// Thus you can neither add nor delete nodes or arcs, however
145 /// the structure can be resized using resize().
146 ///
147 /// This type fully conforms to the \ref concepts::Digraph "Digraph concept".
148 /// Most of its member functions and nested classes are documented
149 /// only in the concept class.
150 ///
151 /// This class provides constant time counting for nodes and arcs.
152 ///
153 /// \note FullBipartiteDigraph and FullBipartiteGraph classes are very similar,
154 /// but there are two differences. While this class conforms only
155 /// to the \ref concepts::Digraph "Digraph" concept, FullBipartiteGraph
156 /// conforms to the \ref concepts::Graph "Graph" concept,
157 /// moreover FullBipartiteGraph does not contain a loop for each
158 /// node as this class does.
159 ///
160 /// \sa FullBipartiteGraph
161 class FullBipartiteDigraph : public FullBipartiteDigraphBase {
162 typedef FullBipartiteDigraphBase Parent;
163
164 public:
165
166 /// \brief Default constructor.
167 ///
168 /// Default constructor. The number of nodes and arcs will be zero.
169 FullBipartiteDigraph() { construct(0,0); }
170
171 /// \brief Constructor
172 ///
173 /// Constructor.
174 /// \param n The number of the nodes.
175 FullBipartiteDigraph(int n1, int n2) { construct(n1, n2); }
176
177
178 /// \brief Returns the node with the given index.
179 ///
180 /// Returns the node with the given index. Since this structure is
181 /// completely static, the nodes can be indexed with integers from
182 /// the range <tt>[0..nodeNum()-1]</tt>.
183 /// The index of a node is the same as its ID.
184 /// \sa index()
185 Node operator()(int ix) const { return Parent::operator()(ix); }
186
187 /// \brief Returns the index of the given node.
188 ///
189 /// Returns the index of the given node. Since this structure is
190 /// completely static, the nodes can be indexed with integers from
191 /// the range <tt>[0..nodeNum()-1]</tt>.
192 /// The index of a node is the same as its ID.
193 /// \sa operator()()
194 static int index(const Node& node) { return Parent::index(node); }
195
196 /// \brief Returns the arc connecting the given nodes.
197 ///
198 /// Returns the arc connecting the given nodes.
199 /*Arc arc(Node u, Node v) const {
200 return Parent::arc(u, v);
201 }*/
202
203 /// \brief Number of nodes.
204 int nodeNum() const { return Parent::nodeNum(); }
205 /// \brief Number of arcs.
206 int64_t arcNum() const { return Parent::arcNum(); }
207 };
208
209
210
211
212 } //namespace lemon