| 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 <cstdint> |
| 29 | |
| 30 | ///\ingroup graphs |
| 31 | ///\file |
| 32 | ///\brief FullBipartiteDigraph and FullBipartiteGraph classes. |
| 33 | |
| 34 | |
| 35 | namespace lemon_omp { |
| 36 | |
| 37 | ///This \c \#define creates convenient type definitions for the following |
| 38 | ///types of \c Digraph: \c Node, \c NodeIt, \c Arc, \c ArcIt, \c InArcIt, |
| 39 | ///\c OutArcIt, \c BoolNodeMap, \c IntNodeMap, \c DoubleNodeMap, |
| 40 | ///\c BoolArcMap, \c IntArcMap, \c DoubleArcMap. |
| 41 | /// |
| 42 | ///\note If the graph type is a dependent type, ie. the graph type depend |
| 43 | ///on a template parameter, then use \c TEMPLATE_DIGRAPH_TYPEDEFS() |
| 44 | ///macro. |
| 45 | #define DIGRAPH_TYPEDEFS(Digraph) \ |
| 46 | typedef Digraph::Node Node; \ |
| 47 | typedef Digraph::Arc Arc; \ |
| 48 | |
| 49 | |
| 50 | ///Create convenience typedefs for the digraph types and iterators |
| 51 | |
| 52 | ///\see DIGRAPH_TYPEDEFS |
| 53 | /// |
| 54 | ///\note Use this macro, if the graph type is a dependent type, |
| 55 | ///ie. the graph type depend on a template parameter. |
| 56 | #define TEMPLATE_DIGRAPH_TYPEDEFS(Digraph) \ |
| 57 | typedef typename Digraph::Node Node; \ |
| 58 | typedef typename Digraph::Arc Arc; \ |
| 59 | |
| 60 | |
| 61 | class FullBipartiteDigraphBase { |
| 62 | public: |
| 63 | |
| 64 | typedef FullBipartiteDigraphBase Digraph; |
| 65 | |
| 66 | //class Node; |
| 67 | typedef int Node; |
| 68 | //class Arc; |
| 69 | typedef int64_t Arc; |
| 70 | |
| 71 | protected: |
| 72 | |
| 73 | int _node_num; |
| 74 | int64_t _arc_num; |
| 75 | |
| 76 | FullBipartiteDigraphBase() {} |
| 77 | |
| 78 | void construct(int n1, int n2) { _node_num = n1+n2; _arc_num = (int64_t)n1 * (int64_t)n2; _n1=n1; _n2=n2;} |
| 79 | |
| 80 | public: |
| 81 | |
| 82 | int _n1, _n2; |
| 83 | |
| 84 | |
| 85 | Node operator()(int ix) const { return Node(ix); } |
| 86 | static int index(const Node& node) { return node; } |
| 87 | |
| 88 | Arc arc(const Node& s, const Node& t) const { |
| 89 | if (s<_n1 && t>=_n1) |
| 90 | return Arc((int64_t)s * (int64_t)_n2 + (int64_t)(t-_n1) ); |
| 91 | else |
| 92 | return Arc(-1); |
| 93 | } |
| 94 | |
| 95 | int nodeNum() const { return _node_num; } |
| 96 | int64_t arcNum() const { return _arc_num; } |
| 97 | |
| 98 | int maxNodeId() const { return _node_num - 1; } |
| 99 | int64_t maxArcId() const { return _arc_num - 1; } |
| 100 | |
| 101 | Node source(Arc arc) const { return arc / _n2; } |
| 102 | Node target(Arc arc) const { return (arc % _n2) + _n1; } |
| 103 | |
| 104 | static int id(Node node) { return node; } |
| 105 | static int64_t id(Arc arc) { return arc; } |
| 106 | |
| 107 | static Node nodeFromId(int id) { return Node(id);} |
| 108 | static Arc arcFromId(int64_t id) { return Arc(id);} |
| 109 | |
| 110 | |
| 111 | Arc findArc(Node s, Node t, Arc prev = -1) const { |
| 112 | return prev == -1 ? arc(s, t) : -1; |
| 113 | } |
| 114 | |
| 115 | void first(Node& node) const { |
| 116 | node = _node_num - 1; |
| 117 | } |
| 118 | |
| 119 | static void next(Node& node) { |
| 120 | --node; |
| 121 | } |
| 122 | |
| 123 | void first(Arc& arc) const { |
| 124 | arc = _arc_num - 1; |
| 125 | } |
| 126 | |
| 127 | static void next(Arc& arc) { |
| 128 | --arc; |
| 129 | } |
| 130 | |
| 131 | void firstOut(Arc& arc, const Node& node) const { |
| 132 | if (node>=_n1) |
| 133 | arc = -1; |
| 134 | else |
| 135 | arc = (node + 1) * _n2 - 1; |
| 136 | } |
| 137 | |
| 138 | void nextOut(Arc& arc) const { |
| 139 | if (arc % _n2 == 0) arc = 0; |
| 140 | --arc; |
| 141 | } |
| 142 | |
| 143 | void firstIn(Arc& arc, const Node& node) const { |
| 144 | if (node<_n1) |
| 145 | arc = -1; |
| 146 | else |
| 147 | arc = _arc_num + node - _node_num; |
| 148 | } |
| 149 | |
| 150 | void nextIn(Arc& arc) const { |
| 151 | arc -= _n2; |
| 152 | if (arc < 0) arc = -1; |
| 153 | } |
| 154 | |
| 155 | }; |
| 156 | |
| 157 | /// \ingroup graphs |
| 158 | /// |
| 159 | /// \brief A directed full graph class. |
| 160 | /// |
| 161 | /// FullBipartiteDigraph is a simple and fast implmenetation of directed full |
| 162 | /// (complete) graphs. It contains an arc from each node to each node |
| 163 | /// (including a loop for each node), therefore the number of arcs |
| 164 | /// is the square of the number of nodes. |
| 165 | /// This class is completely static and it needs constant memory space. |
| 166 | /// Thus you can neither add nor delete nodes or arcs, however |
| 167 | /// the structure can be resized using resize(). |
| 168 | /// |
| 169 | /// This type fully conforms to the \ref concepts::Digraph "Digraph concept". |
| 170 | /// Most of its member functions and nested classes are documented |
| 171 | /// only in the concept class. |
| 172 | /// |
| 173 | /// This class provides constant time counting for nodes and arcs. |
| 174 | /// |
| 175 | /// \note FullBipartiteDigraph and FullBipartiteGraph classes are very similar, |
| 176 | /// but there are two differences. While this class conforms only |
| 177 | /// to the \ref concepts::Digraph "Digraph" concept, FullBipartiteGraph |
| 178 | /// conforms to the \ref concepts::Graph "Graph" concept, |
| 179 | /// moreover FullBipartiteGraph does not contain a loop for each |
| 180 | /// node as this class does. |
| 181 | /// |
| 182 | /// \sa FullBipartiteGraph |
| 183 | class FullBipartiteDigraph : public FullBipartiteDigraphBase { |
| 184 | typedef FullBipartiteDigraphBase Parent; |
| 185 | |
| 186 | public: |
| 187 | |
| 188 | /// \brief Default constructor. |
| 189 | /// |
| 190 | /// Default constructor. The number of nodes and arcs will be zero. |
| 191 | FullBipartiteDigraph() { construct(0,0); } |
| 192 | |
| 193 | /// \brief Constructor |
| 194 | /// |
| 195 | /// Constructor. |
| 196 | /// \param n The number of the nodes. |
| 197 | FullBipartiteDigraph(int n1, int n2) { construct(n1, n2); } |
| 198 | |
| 199 | |
| 200 | /// \brief Returns the node with the given index. |
| 201 | /// |
| 202 | /// Returns the node with the given index. Since this structure is |
| 203 | /// completely static, the nodes can be indexed with integers from |
| 204 | /// the range <tt>[0..nodeNum()-1]</tt>. |
| 205 | /// The index of a node is the same as its ID. |
| 206 | /// \sa index() |
| 207 | Node operator()(int ix) const { return Parent::operator()(ix); } |
| 208 | |
| 209 | /// \brief Returns the index of the given node. |
| 210 | /// |
| 211 | /// Returns the index of the given node. Since this structure is |
| 212 | /// completely static, the nodes can be indexed with integers from |
| 213 | /// the range <tt>[0..nodeNum()-1]</tt>. |
| 214 | /// The index of a node is the same as its ID. |
| 215 | /// \sa operator()() |
| 216 | static int index(const Node& node) { return Parent::index(node); } |
| 217 | |
| 218 | /// \brief Returns the arc connecting the given nodes. |
| 219 | /// |
| 220 | /// Returns the arc connecting the given nodes. |
| 221 | /*Arc arc(Node u, Node v) const { |
| 222 | return Parent::arc(u, v); |
| 223 | }*/ |
| 224 | |
| 225 | /// \brief Number of nodes. |
| 226 | int nodeNum() const { return Parent::nodeNum(); } |
| 227 | /// \brief Number of arcs. |
| 228 | int64_t arcNum() const { return Parent::arcNum(); } |
| 229 | }; |
| 230 | |
| 231 | |
| 232 | |
| 233 | |
| 234 | } //namespace lemon_omp |