1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/lemon/smart_bpugraph.h Fri Jun 30 12:14:36 2006 +0000
1.3 @@ -0,0 +1,273 @@
1.4 +/* -*- C++ -*-
1.5 + *
1.6 + * This file is a part of LEMON, a generic C++ optimization library
1.7 + *
1.8 + * Copyright (C) 2003-2006
1.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
1.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
1.11 + *
1.12 + * Permission to use, modify and distribute this software is granted
1.13 + * provided that this copyright notice appears in all copies. For
1.14 + * precise terms see the accompanying LICENSE file.
1.15 + *
1.16 + * This software is provided "AS IS" with no warranty of any kind,
1.17 + * express or implied, and with no claim as to its suitability for any
1.18 + * purpose.
1.19 + *
1.20 + */
1.21 +
1.22 +#ifndef LEMON_SMART_BPUGRAPH_H
1.23 +#define LEMON_SMART_BPUGRAPH_H
1.24 +
1.25 +///\ingroup graphs
1.26 +///\file
1.27 +///\brief SmartBpUGraph class.
1.28 +
1.29 +#include <vector>
1.30 +
1.31 +#include <lemon/bits/invalid.h>
1.32 +
1.33 +#include <lemon/bits/bpugraph_extender.h>
1.34 +
1.35 +#include <lemon/bits/utility.h>
1.36 +#include <lemon/error.h>
1.37 +
1.38 +#include <lemon/bits/graph_extender.h>
1.39 +
1.40 +namespace lemon {
1.41 +
1.42 + class SmartBpUGraphBase {
1.43 + public:
1.44 +
1.45 + class NodeSetError : public LogicError {
1.46 + virtual const char* exceptionName() const {
1.47 + return "lemon::SmartBpUGraph::NodeSetError";
1.48 + }
1.49 + };
1.50 +
1.51 + protected:
1.52 +
1.53 + struct NodeT {
1.54 + int first;
1.55 + NodeT() {}
1.56 + NodeT(int _first) : first(_first) {}
1.57 + };
1.58 +
1.59 + struct UEdgeT {
1.60 + int aNode, next_out;
1.61 + int bNode, next_in;
1.62 + };
1.63 +
1.64 + std::vector<NodeT> aNodes;
1.65 + std::vector<NodeT> bNodes;
1.66 +
1.67 + std::vector<UEdgeT> edges;
1.68 +
1.69 + public:
1.70 +
1.71 + class Node {
1.72 + friend class SmartBpUGraphBase;
1.73 + protected:
1.74 + int id;
1.75 +
1.76 + Node(int _id) : id(_id) {}
1.77 + public:
1.78 + Node() {}
1.79 + Node(Invalid) { id = -1; }
1.80 + bool operator==(const Node i) const {return id==i.id;}
1.81 + bool operator!=(const Node i) const {return id!=i.id;}
1.82 + bool operator<(const Node i) const {return id<i.id;}
1.83 + };
1.84 +
1.85 + class UEdge {
1.86 + friend class SmartBpUGraphBase;
1.87 + protected:
1.88 + int id;
1.89 +
1.90 + UEdge(int _id) { id = _id;}
1.91 + public:
1.92 + UEdge() {}
1.93 + UEdge (Invalid) { id = -1; }
1.94 + bool operator==(const UEdge i) const {return id==i.id;}
1.95 + bool operator!=(const UEdge i) const {return id!=i.id;}
1.96 + bool operator<(const UEdge i) const {return id<i.id;}
1.97 + };
1.98 +
1.99 + void firstANode(Node& node) const {
1.100 + node.id = 2 * aNodes.size() - 2;
1.101 + if (node.id < 0) node.id = -1;
1.102 + }
1.103 + void nextANode(Node& node) const {
1.104 + node.id -= 2;
1.105 + if (node.id < 0) node.id = -1;
1.106 + }
1.107 +
1.108 + void firstBNode(Node& node) const {
1.109 + node.id = 2 * bNodes.size() - 1;
1.110 + }
1.111 + void nextBNode(Node& node) const {
1.112 + node.id -= 2;
1.113 + }
1.114 +
1.115 + void first(Node& node) const {
1.116 + if (aNodes.size() > 0) {
1.117 + node.id = 2 * aNodes.size() - 2;
1.118 + } else {
1.119 + node.id = 2 * bNodes.size() - 1;
1.120 + }
1.121 + }
1.122 + void next(Node& node) const {
1.123 + node.id -= 2;
1.124 + if (node.id == -2) {
1.125 + node.id = 2 * bNodes.size() - 1;
1.126 + }
1.127 + }
1.128 +
1.129 + void first(UEdge& edge) const {
1.130 + edge.id = edges.size() - 1;
1.131 + }
1.132 + void next(UEdge& edge) const {
1.133 + --edge.id;
1.134 + }
1.135 +
1.136 + void firstFromANode(UEdge& edge, const Node& node) const {
1.137 + LEMON_ASSERT((node.id & 1) == 0, NodeSetError());
1.138 + edge.id = aNodes[node.id >> 1].first;
1.139 + }
1.140 + void nextFromANode(UEdge& edge) const {
1.141 + edge.id = edges[edge.id].next_out;
1.142 + }
1.143 +
1.144 + void firstFromBNode(UEdge& edge, const Node& node) const {
1.145 + LEMON_ASSERT((node.id & 1) == 1, NodeSetError());
1.146 + edge.id = bNodes[node.id >> 1].first;
1.147 + }
1.148 + void nextFromBNode(UEdge& edge) const {
1.149 + edge.id = edges[edge.id].next_in;
1.150 + }
1.151 +
1.152 + static int id(const Node& node) {
1.153 + return node.id;
1.154 + }
1.155 + static Node nodeFromId(int id) {
1.156 + return Node(id);
1.157 + }
1.158 + int maxNodeId() const {
1.159 + return aNodes.size() > bNodes.size() ?
1.160 + aNodes.size() * 2 - 2 : bNodes.size() * 2 - 1;
1.161 + }
1.162 +
1.163 + static int id(const UEdge& edge) {
1.164 + return edge.id;
1.165 + }
1.166 + static UEdge uEdgeFromId(int id) {
1.167 + return UEdge(id);
1.168 + }
1.169 + int maxUEdgeId() const {
1.170 + return edges.size();
1.171 + }
1.172 +
1.173 + static int aNodeId(const Node& node) {
1.174 + return node.id >> 1;
1.175 + }
1.176 + static Node fromANodeId(int id) {
1.177 + return Node(id << 1);
1.178 + }
1.179 + int maxANodeId() const {
1.180 + return aNodes.size();
1.181 + }
1.182 +
1.183 + static int bNodeId(const Node& node) {
1.184 + return node.id >> 1;
1.185 + }
1.186 + static Node fromBNodeId(int id) {
1.187 + return Node((id << 1) + 1);
1.188 + }
1.189 + int maxBNodeId() const {
1.190 + return bNodes.size();
1.191 + }
1.192 +
1.193 + Node aNode(const UEdge& edge) const {
1.194 + return Node(edges[edge.id].aNode);
1.195 + }
1.196 + Node bNode(const UEdge& edge) const {
1.197 + return Node(edges[edge.id].bNode);
1.198 + }
1.199 +
1.200 + static bool aNode(const Node& node) {
1.201 + return (node.id & 1) == 0;
1.202 + }
1.203 +
1.204 + static bool bNode(const Node& node) {
1.205 + return (node.id & 1) == 1;
1.206 + }
1.207 +
1.208 + Node addANode() {
1.209 + NodeT nodeT;
1.210 + nodeT.first = -1;
1.211 + aNodes.push_back(nodeT);
1.212 + return Node(aNodes.size() * 2 - 2);
1.213 + }
1.214 +
1.215 + Node addBNode() {
1.216 + NodeT nodeT;
1.217 + nodeT.first = -1;
1.218 + bNodes.push_back(nodeT);
1.219 + return Node(bNodes.size() * 2 - 1);
1.220 + }
1.221 +
1.222 + UEdge addEdge(const Node& source, const Node& target) {
1.223 + LEMON_ASSERT(((source.id ^ target.id) & 1) == 1, NodeSetError());
1.224 + UEdgeT edgeT;
1.225 + if ((source.id & 1) == 0) {
1.226 + edgeT.aNode = source.id;
1.227 + edgeT.bNode = target.id;
1.228 + } else {
1.229 + edgeT.aNode = target.id;
1.230 + edgeT.bNode = source.id;
1.231 + }
1.232 + edgeT.next_out = aNodes[edgeT.aNode >> 1].first;
1.233 + aNodes[edgeT.aNode >> 1].first = edges.size();
1.234 + edgeT.next_in = bNodes[edgeT.bNode >> 1].first;
1.235 + bNodes[edgeT.bNode >> 1].first = edges.size();
1.236 + edges.push_back(edgeT);
1.237 + return UEdge(edges.size() - 1);
1.238 + }
1.239 +
1.240 + void clear() {
1.241 + aNodes.clear();
1.242 + bNodes.clear();
1.243 + edges.clear();
1.244 + }
1.245 +
1.246 + typedef True NodeNumTag;
1.247 + int nodeNum() const { return aNodes.size() + bNodes.size(); }
1.248 + int aNodeNum() const { return aNodes.size(); }
1.249 + int bNodeNum() const { return bNodes.size(); }
1.250 +
1.251 + typedef True EdgeNumTag;
1.252 + int uEdgeNum() const { return edges.size(); }
1.253 +
1.254 + };
1.255 +
1.256 +
1.257 + typedef BpUGraphExtender<SmartBpUGraphBase> ExtendedSmartBpUGraphBase;
1.258 +
1.259 + /// \ingroup graphs
1.260 + ///
1.261 + /// \brief A smart bipartite undirected graph class.
1.262 + ///
1.263 + /// This is a simple and fast bipartite undirected graph implementation.
1.264 + /// It is also quite memory efficient, but at the price
1.265 + /// that <b> it does not support node and edge deletions</b>.
1.266 + /// Except from this it conforms to
1.267 + /// the \ref concept::BpUGraph "BpUGraph" concept.
1.268 + /// \sa concept::BpUGraph.
1.269 + ///
1.270 + class SmartBpUGraph : public ExtendedSmartBpUGraphBase {};
1.271 +
1.272 +
1.273 +} //namespace lemon
1.274 +
1.275 +
1.276 +#endif //LEMON_SMART_GRAPH_H