alpar@906: /* -*- C++ -*- alpar@906: * alpar@1956: * This file is a part of LEMON, a generic C++ optimization library alpar@1956: * alpar@1956: * Copyright (C) 2003-2006 alpar@1956: * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport alpar@1359: * (Egervary Research Group on Combinatorial Optimization, EGRES). alpar@906: * alpar@906: * Permission to use, modify and distribute this software is granted alpar@906: * provided that this copyright notice appears in all copies. For alpar@906: * precise terms see the accompanying LICENSE file. alpar@906: * alpar@906: * This software is provided "AS IS" with no warranty of any kind, alpar@906: * express or implied, and with no claim as to its suitability for any alpar@906: * purpose. alpar@906: * alpar@906: */ alpar@591: alpar@921: #ifndef LEMON_FULL_GRAPH_H alpar@921: #define LEMON_FULL_GRAPH_H alpar@591: deba@983: #include deba@983: deba@1791: #include deba@1566: deba@1993: #include deba@1993: #include klao@977: klao@977: alpar@591: ///\ingroup graphs alpar@591: ///\file deba@2115: ///\brief FullGraph class. alpar@591: alpar@591: alpar@921: namespace lemon { alpar@591: deba@1986: /// \brief Base of the FullGrpah. deba@1986: /// deba@1986: /// Base of the FullGrpah. klao@946: class FullGraphBase { deba@1566: int _nodeNum; deba@1566: int _edgeNum; alpar@591: public: deba@782: klao@946: typedef FullGraphBase Graph; alpar@591: alpar@591: class Node; alpar@591: class Edge; deba@782: alpar@591: public: alpar@591: klao@946: FullGraphBase() {} klao@946: klao@946: alpar@591: ///Creates a full graph with \c n nodes. deba@1566: void construct(int n) { _nodeNum = n; _edgeNum = n * n; } alpar@591: klao@977: typedef True NodeNumTag; klao@977: typedef True EdgeNumTag; klao@977: deba@1986: /// \brief Returns the node with the given index. deba@1986: /// deba@1986: /// Returns the node with the given index. Because it is a deba@1986: /// static size graph the node's of the graph can be indiced deba@1986: /// by the range from 0 to \e nodeNum()-1 and the index of deba@1986: /// the node can accessed by the \e index() member. deba@1986: Node operator()(int index) const { return Node(index); } deba@1986: deba@1986: /// \brief Returns the index of the node. deba@1986: /// deba@1986: /// Returns the index of the node. Because it is a deba@1986: /// static size graph the node's of the graph can be indiced deba@1986: /// by the range from 0 to \e nodeNum()-1 and the index of deba@1986: /// the node can accessed by the \e index() member. deba@1986: int index(const Node& node) const { return node.id; } deba@1986: alpar@813: ///Number of nodes. deba@1566: int nodeNum() const { return _nodeNum; } alpar@813: ///Number of edges. deba@1566: int edgeNum() const { return _edgeNum; } alpar@591: alpar@813: /// Maximum node ID. alpar@813: alpar@813: /// Maximum node ID. alpar@813: ///\sa id(Node) deba@1791: int maxNodeId() const { return _nodeNum-1; } alpar@813: /// Maximum edge ID. alpar@813: alpar@813: /// Maximum edge ID. alpar@813: ///\sa id(Edge) deba@1791: int maxEdgeId() const { return _edgeNum-1; } alpar@591: deba@1566: Node source(Edge e) const { return e.id % _nodeNum; } deba@1566: Node target(Edge e) const { return e.id / _nodeNum; } alpar@591: alpar@591: alpar@813: /// Node ID. alpar@813: alpar@813: /// The ID of a valid Node is a nonnegative integer not greater than alpar@813: /// \ref maxNodeId(). The range of the ID's is not surely continuous alpar@813: /// and the greatest node ID can be actually less then \ref maxNodeId(). alpar@813: /// alpar@813: /// The ID of the \ref INVALID node is -1. alpar@813: ///\return The ID of the node \c v. klao@946: klao@946: static int id(Node v) { return v.id; } alpar@813: /// Edge ID. alpar@813: alpar@813: /// The ID of a valid Edge is a nonnegative integer not greater than alpar@813: /// \ref maxEdgeId(). The range of the ID's is not surely continuous alpar@813: /// and the greatest edge ID can be actually less then \ref maxEdgeId(). alpar@813: /// alpar@813: /// The ID of the \ref INVALID edge is -1. alpar@813: ///\return The ID of the edge \c e. klao@946: static int id(Edge e) { return e.id; } alpar@591: deba@1791: static Node nodeFromId(int id) { return Node(id);} deba@1106: deba@1791: static Edge edgeFromId(int id) { return Edge(id);} deba@1106: deba@1566: typedef True FindEdgeTag; deba@1566: alpar@774: /// Finds an edge between two nodes. alpar@774: alpar@774: /// Finds an edge from node \c u to node \c v. alpar@774: /// alpar@774: /// If \c prev is \ref INVALID (this is the default value), then alpar@774: /// It finds the first edge from \c u to \c v. Otherwise it looks for alpar@774: /// the next edge from \c u to \c v after \c prev. alpar@774: /// \return The found edge or INVALID if there is no such an edge. deba@1566: Edge findEdge(Node u,Node v, Edge prev = INVALID) const { klao@946: return prev.id == -1 ? Edge(*this, u.id, v.id) : INVALID; alpar@774: } alpar@774: alpar@774: alpar@591: class Node { klao@946: friend class FullGraphBase; alpar@591: alpar@591: protected: klao@946: int id; alpar@1643: Node(int _id) : id(_id) {} alpar@591: public: alpar@591: Node() {} alpar@1643: Node (Invalid) : id(-1) {} klao@946: bool operator==(const Node node) const {return id == node.id;} klao@946: bool operator!=(const Node node) const {return id != node.id;} klao@946: bool operator<(const Node node) const {return id < node.id;} alpar@591: }; alpar@591: klao@946: klao@946: klao@946: class Edge { klao@946: friend class FullGraphBase; klao@946: klao@946: protected: deba@1566: int id; // _nodeNum * target + source; klao@946: klao@946: Edge(int _id) : id(_id) {} klao@946: alpar@986: Edge(const FullGraphBase& _graph, int source, int target) deba@1566: : id(_graph._nodeNum * target+source) {} alpar@591: public: klao@946: Edge() { } klao@946: Edge (Invalid) { id = -1; } klao@946: bool operator==(const Edge edge) const {return id == edge.id;} klao@946: bool operator!=(const Edge edge) const {return id != edge.id;} klao@946: bool operator<(const Edge edge) const {return id < edge.id;} alpar@591: }; alpar@591: klao@946: void first(Node& node) const { deba@1566: node.id = _nodeNum-1; klao@946: } alpar@591: klao@946: static void next(Node& node) { klao@946: --node.id; klao@946: } klao@946: klao@946: void first(Edge& edge) const { deba@1566: edge.id = _edgeNum-1; klao@946: } klao@946: klao@946: static void next(Edge& edge) { klao@946: --edge.id; klao@946: } klao@946: klao@946: void firstOut(Edge& edge, const Node& node) const { deba@1566: edge.id = _edgeNum + node.id - _nodeNum; klao@946: } klao@946: klao@946: void nextOut(Edge& edge) const { deba@1566: edge.id -= _nodeNum; klao@946: if (edge.id < 0) edge.id = -1; klao@946: } klao@946: klao@946: void firstIn(Edge& edge, const Node& node) const { deba@1566: edge.id = node.id * _nodeNum; klao@946: } alpar@591: klao@946: void nextIn(Edge& edge) const { klao@946: ++edge.id; deba@1566: if (edge.id % _nodeNum == 0) edge.id = -1; klao@946: } alpar@591: alpar@591: }; alpar@591: deba@1979: typedef GraphExtender ExtendedFullGraphBase; klao@946: deba@1566: /// \ingroup graphs alpar@951: /// deba@1566: /// \brief A full graph class. deba@1566: /// deba@1566: /// This is a simple and fast directed full graph implementation. deba@1566: /// It is completely static, so you can neither add nor delete either deba@1566: /// edges or nodes. deba@1566: /// Thus it conforms to deba@2111: /// the \ref concept::Graph "Graph" concept deba@2111: /// \sa concept::Graph. deba@1566: /// deba@1986: /// \sa FullGraphBase deba@1986: /// \sa FullUGraph deba@1986: /// deba@1566: /// \author Alpar Juttner deba@1669: class FullGraph : public ExtendedFullGraphBase { klao@946: public: klao@946: deba@1979: typedef ExtendedFullGraphBase Parent; deba@1979: deba@1979: /// \brief Constructor deba@1987: FullGraph() { construct(0); } deba@1987: deba@1987: /// \brief Constructor deba@1979: /// klao@946: FullGraph(int n) { construct(n); } deba@1979: deba@1979: /// \brief Resize the graph deba@1979: /// deba@1986: /// Resize the graph. The function will fully destroy and build the graph. deba@1986: /// This cause that the maps of the graph will reallocated deba@1986: /// automatically and the previous values will be lost. deba@1979: void resize(int n) { deba@1979: Parent::getNotifier(Edge()).clear(); deba@1979: Parent::getNotifier(Node()).clear(); deba@1979: construct(n); deba@1979: Parent::getNotifier(Node()).build(); deba@1979: Parent::getNotifier(Edge()).build(); deba@1979: } klao@946: }; klao@946: alpar@921: } //namespace lemon alpar@591: alpar@591: alpar@921: #endif //LEMON_FULL_GRAPH_H