/* -*- C++ -*- * lemon/full_graph.h - Part of LEMON, a generic C++ optimization library * * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport * (Egervary Research Group on Combinatorial Optimization, EGRES). * * Permission to use, modify and distribute this software is granted * provided that this copyright notice appears in all copies. For * precise terms see the accompanying LICENSE file. * * This software is provided "AS IS" with no warranty of any kind, * express or implied, and with no claim as to its suitability for any * purpose. * */ #ifndef LEMON_FULL_GRAPH_H #define LEMON_FULL_GRAPH_H #include #include #include #include #include #include #include ///\ingroup graphs ///\file ///\brief FullGraph and UndirFullGraph classes. namespace lemon { class FullGraphBase { int _nodeNum; int _edgeNum; public: typedef FullGraphBase Graph; class Node; class Edge; public: FullGraphBase() {} ///Creates a full graph with \c n nodes. void construct(int n) { _nodeNum = n; _edgeNum = n * n; } /// // FullGraphBase(const FullGraphBase &_g) // : _nodeNum(_g.nodeNum()), _edgeNum(_nodeNum*_nodeNum) { } typedef True NodeNumTag; typedef True EdgeNumTag; ///Number of nodes. int nodeNum() const { return _nodeNum; } ///Number of edges. int edgeNum() const { return _edgeNum; } /// Maximum node ID. /// Maximum node ID. ///\sa id(Node) int maxNodeId() const { return _nodeNum-1; } /// Maximum edge ID. /// Maximum edge ID. ///\sa id(Edge) int maxEdgeId() const { return _edgeNum-1; } Node source(Edge e) const { return e.id % _nodeNum; } Node target(Edge e) const { return e.id / _nodeNum; } /// Node ID. /// The ID of a valid Node is a nonnegative integer not greater than /// \ref maxNodeId(). The range of the ID's is not surely continuous /// and the greatest node ID can be actually less then \ref maxNodeId(). /// /// The ID of the \ref INVALID node is -1. ///\return The ID of the node \c v. static int id(Node v) { return v.id; } /// Edge ID. /// The ID of a valid Edge is a nonnegative integer not greater than /// \ref maxEdgeId(). The range of the ID's is not surely continuous /// and the greatest edge ID can be actually less then \ref maxEdgeId(). /// /// The ID of the \ref INVALID edge is -1. ///\return The ID of the edge \c e. static int id(Edge e) { return e.id; } static Node nodeFromId(int id) { return Node(id);} static Edge edgeFromId(int id) { return Edge(id);} typedef True FindEdgeTag; /// Finds an edge between two nodes. /// Finds an edge from node \c u to node \c v. /// /// If \c prev is \ref INVALID (this is the default value), then /// It finds the first edge from \c u to \c v. Otherwise it looks for /// the next edge from \c u to \c v after \c prev. /// \return The found edge or INVALID if there is no such an edge. Edge findEdge(Node u,Node v, Edge prev = INVALID) const { return prev.id == -1 ? Edge(*this, u.id, v.id) : INVALID; } class Node { friend class FullGraphBase; protected: int id; Node(int _id) : id(_id) {} public: Node() {} Node (Invalid) : id(-1) {} bool operator==(const Node node) const {return id == node.id;} bool operator!=(const Node node) const {return id != node.id;} bool operator<(const Node node) const {return id < node.id;} }; class Edge { friend class FullGraphBase; protected: int id; // _nodeNum * target + source; Edge(int _id) : id(_id) {} Edge(const FullGraphBase& _graph, int source, int target) : id(_graph._nodeNum * target+source) {} public: Edge() { } Edge (Invalid) { id = -1; } bool operator==(const Edge edge) const {return id == edge.id;} bool operator!=(const Edge edge) const {return id != edge.id;} bool operator<(const Edge edge) const {return id < edge.id;} }; void first(Node& node) const { node.id = _nodeNum-1; } static void next(Node& node) { --node.id; } void first(Edge& edge) const { edge.id = _edgeNum-1; } static void next(Edge& edge) { --edge.id; } void firstOut(Edge& edge, const Node& node) const { edge.id = _edgeNum + node.id - _nodeNum; } void nextOut(Edge& edge) const { edge.id -= _nodeNum; if (edge.id < 0) edge.id = -1; } void firstIn(Edge& edge, const Node& node) const { edge.id = node.id * _nodeNum; } void nextIn(Edge& edge) const { ++edge.id; if (edge.id % _nodeNum == 0) edge.id = -1; } }; typedef StaticMappableGraphExtender< IterableGraphExtender< AlterableGraphExtender< GraphExtender > > > ExtendedFullGraphBase; /// \ingroup graphs /// /// \brief A full graph class. /// /// This is a simple and fast directed full graph implementation. /// It is completely static, so you can neither add nor delete either /// edges or nodes. /// Thus it conforms to /// the \ref concept::StaticGraph "StaticGraph" concept /// \sa concept::StaticGraph. /// /// \author Alpar Juttner class FullGraph : public ExtendedFullGraphBase { public: FullGraph(int n) { construct(n); } }; class UndirFullGraphBase { int _nodeNum; int _edgeNum; public: typedef UndirFullGraphBase Graph; class Node; class Edge; public: UndirFullGraphBase() {} ///Creates a full graph with \c n nodes. void construct(int n) { _nodeNum = n; _edgeNum = n * (n - 1) / 2; } /// // FullGraphBase(const FullGraphBase &_g) // : _nodeNum(_g.nodeNum()), _edgeNum(_nodeNum*_nodeNum) { } typedef True NodeNumTag; typedef True EdgeNumTag; ///Number of nodes. int nodeNum() const { return _nodeNum; } ///Number of edges. int edgeNum() const { return _edgeNum; } /// Maximum node ID. /// Maximum node ID. ///\sa id(Node) int maxNodeId() const { return _nodeNum-1; } /// Maximum edge ID. /// Maximum edge ID. ///\sa id(Edge) int maxEdgeId() const { return _edgeNum-1; } Node source(Edge e) const { /// \todo we may do it faster return Node(((int)sqrt((double)(1 + 8 * e.id)) + 1) / 2); } Node target(Edge e) const { int source = ((int)sqrt((double)(1 + 8 * e.id)) + 1) / 2;; return Node(e.id - (source) * (source - 1) / 2); } /// Node ID. /// The ID of a valid Node is a nonnegative integer not greater than /// \ref maxNodeId(). The range of the ID's is not surely continuous /// and the greatest node ID can be actually less then \ref maxNodeId(). /// /// The ID of the \ref INVALID node is -1. ///\return The ID of the node \c v. static int id(Node v) { return v.id; } /// Edge ID. /// The ID of a valid Edge is a nonnegative integer not greater than /// \ref maxEdgeId(). The range of the ID's is not surely continuous /// and the greatest edge ID can be actually less then \ref maxEdgeId(). /// /// The ID of the \ref INVALID edge is -1. ///\return The ID of the edge \c e. static int id(Edge e) { return e.id; } /// Finds an edge between two nodes. /// Finds an edge from node \c u to node \c v. /// /// If \c prev is \ref INVALID (this is the default value), then /// It finds the first edge from \c u to \c v. Otherwise it looks for /// the next edge from \c u to \c v after \c prev. /// \return The found edge or INVALID if there is no such an edge. Edge findEdge(Node u, Node v, Edge prev = INVALID) const { if (prev.id != -1 || u.id <= v.id) return -1; return Edge(u.id * (u.id - 1) / 2 + v.id); } typedef True FindEdgeTag; class Node { friend class UndirFullGraphBase; protected: int id; Node(int _id) { id = _id;} public: Node() {} Node (Invalid) { id = -1; } bool operator==(const Node node) const {return id == node.id;} bool operator!=(const Node node) const {return id != node.id;} bool operator<(const Node node) const {return id < node.id;} }; class Edge { friend class UndirFullGraphBase; protected: int id; // _nodeNum * target + source; Edge(int _id) : id(_id) {} public: Edge() { } Edge (Invalid) { id = -1; } bool operator==(const Edge edge) const {return id == edge.id;} bool operator!=(const Edge edge) const {return id != edge.id;} bool operator<(const Edge edge) const {return id < edge.id;} }; void first(Node& node) const { node.id = _nodeNum - 1; } static void next(Node& node) { --node.id; } void first(Edge& edge) const { edge.id = _edgeNum - 1; } static void next(Edge& edge) { --edge.id; } void firstOut(Edge& edge, const Node& node) const { int src = node.id; int trg = 0; edge.id = (trg < src ? src * (src - 1) / 2 + trg : -1); } /// \todo with specialized iterators we can make faster iterating void nextOut(Edge& edge) const { int src = source(edge).id; int trg = target(edge).id; ++trg; edge.id = (trg < src ? src * (src - 1) / 2 + trg : -1); } void firstIn(Edge& edge, const Node& node) const { int src = node.id + 1; int trg = node.id; edge.id = (src < _nodeNum ? src * (src - 1) / 2 + trg : -1); } void nextIn(Edge& edge) const { int src = source(edge).id; int trg = target(edge).id; ++src; edge.id = (src < _nodeNum ? src * (src - 1) / 2 + trg : -1); } }; typedef StaticMappableUndirGraphExtender< IterableUndirGraphExtender< AlterableUndirGraphExtender< UndirGraphExtender > > > ExtendedUndirFullGraphBase; /// \ingroup graphs /// /// \brief An undirected full graph class. /// /// This is a simple and fast undirected full graph implementation. /// It is completely static, so you can neither add nor delete either /// edges or nodes. /// /// The main difference beetween the \e FullGraph and \e UndirFullGraph class /// is that this class conforms to the undirected graph concept and /// it does not contain the loop edges. /// /// \sa FullGraph /// /// \author Balazs Dezso class UndirFullGraph : public ExtendedUndirFullGraphBase { public: UndirFullGraph(int n) { construct(n); } }; } //namespace lemon #endif //LEMON_FULL_GRAPH_H