alpar@906: /* -*- C++ -*- alpar@921: * src/lemon/smart_graph.h - Part of LEMON, a generic C++ optimization library alpar@906: * alpar@906: * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport alpar@906: * (Egervary Combinatorial Optimization Research Group, 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@105: alpar@921: #ifndef LEMON_SMART_GRAPH_H alpar@921: #define LEMON_SMART_GRAPH_H alpar@104: klao@491: ///\ingroup graphs alpar@242: ///\file alpar@242: ///\brief SmartGraph and SymSmartGraph classes. alpar@242: alpar@104: #include deba@782: #include alpar@104: alpar@921: #include alpar@157: alpar@921: #include alpar@921: #include alpar@919: alpar@921: #include deba@782: alpar@921: #include deba@782: alpar@921: namespace lemon { alpar@104: alpar@407: /// \addtogroup graphs alpar@407: /// @{ deba@782: // class SymSmartGraph; alpar@185: alpar@186: ///A smart graph class. alpar@186: alpar@186: ///This is a simple and fast graph implementation. alpar@186: ///It is also quite memory efficient, but at the price alpar@186: ///that it does not support node and edge deletion. alpar@880: ///It conforms to alpar@880: ///the \ref skeleton::ExtendableGraph "ExtendableGraph" concept. alpar@880: ///\sa skeleton::ExtendableGraph. alpar@402: /// alpar@402: ///\todo Some member functions could be \c static. alpar@753: /// alpar@753: ///\todo A possibly useful functionality: a function saveState() would alpar@753: ///give back a data sturcture X and then the function restoreState(X) alpar@753: ///would remove the nodes and edges added after the call of saveState(). alpar@753: ///Of course it should be used as a stack. (Maybe X is not necessary.) alpar@753: /// alpar@456: ///\author Alpar Juttner alpar@104: class SmartGraph { alpar@104: alpar@104: struct NodeT alpar@104: { alpar@104: int first_in,first_out; alpar@157: NodeT() : first_in(-1), first_out(-1) {} alpar@104: }; alpar@104: struct EdgeT alpar@104: { alpar@104: int head, tail, next_in, next_out; alpar@104: //FIXME: is this necessary? alpar@157: EdgeT() : next_in(-1), next_out(-1) {} alpar@104: }; alpar@104: alpar@104: std::vector nodes; alpar@129: alpar@104: std::vector edges; alpar@104: alpar@185: alpar@104: public: deba@782: deba@782: typedef SmartGraph Graph; alpar@104: alpar@164: class Node; alpar@164: class Edge; alpar@108: alpar@164: class NodeIt; alpar@164: class EdgeIt; alpar@104: class OutEdgeIt; alpar@104: class InEdgeIt; alpar@104: alpar@904: // Create map registries. deba@782: CREATE_MAP_REGISTRIES; alpar@904: // Create node and edge maps. deba@897: CREATE_MAPS(ArrayMap); alpar@104: alpar@104: public: alpar@104: alpar@104: SmartGraph() : nodes(), edges() { } alpar@136: SmartGraph(const SmartGraph &_g) : nodes(_g.nodes), edges(_g.edges) { } alpar@104: alpar@813: ///Number of nodes. alpar@813: int nodeNum() const { return nodes.size(); } alpar@813: ///Number of edges. alpar@813: int edgeNum() const { return edges.size(); } alpar@104: alpar@813: /// Maximum node ID. alpar@813: alpar@813: /// Maximum node ID. alpar@813: ///\sa id(Node) alpar@813: int maxNodeId() const { return nodes.size()-1; } alpar@813: /// Maximum edge ID. alpar@813: alpar@813: /// Maximum edge ID. alpar@813: ///\sa id(Edge) alpar@813: int maxEdgeId() const { return edges.size()-1; } alpar@108: alpar@164: Node tail(Edge e) const { return edges[e.n].tail; } alpar@164: Node head(Edge e) const { return edges[e.n].head; } alpar@104: alpar@164: NodeIt& first(NodeIt& v) const { alpar@164: v=NodeIt(*this); return v; } alpar@164: EdgeIt& first(EdgeIt& e) const { alpar@164: e=EdgeIt(*this); return e; } alpar@164: OutEdgeIt& first(OutEdgeIt& e, const Node v) const { alpar@104: e=OutEdgeIt(*this,v); return e; } alpar@164: InEdgeIt& first(InEdgeIt& e, const Node v) const { alpar@104: e=InEdgeIt(*this,v); return e; } alpar@104: 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. alpar@713: static int id(Node v) { return v.n; } 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. alpar@713: static int id(Edge e) { return e.n; } alpar@104: alpar@164: Node addNode() { alpar@164: Node n; n.n=nodes.size(); alpar@104: nodes.push_back(NodeT()); //FIXME: Hmmm... alpar@108: deba@782: deba@782: node_maps.add(n); alpar@104: return n; alpar@104: } alpar@108: alpar@164: Edge addEdge(Node u, Node v) { alpar@164: Edge e; e.n=edges.size(); edges.push_back(EdgeT()); //FIXME: Hmmm... alpar@104: edges[e.n].tail=u.n; edges[e.n].head=v.n; alpar@104: edges[e.n].next_out=nodes[u.n].first_out; alpar@104: edges[e.n].next_in=nodes[v.n].first_in; alpar@104: nodes[u.n].first_out=nodes[v.n].first_in=e.n; alpar@108: deba@782: edge_maps.add(e); alpar@108: alpar@104: return e; alpar@104: } alpar@104: 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. alpar@774: Edge findEdge(Node u,Node v, Edge prev = INVALID) alpar@774: { alpar@774: int e = (prev.n==-1)? nodes[u.n].first_out : edges[prev.n].next_out; alpar@774: while(e!=-1 && edges[e].tail!=v.n) e = edges[e].next_out; alpar@774: prev.n=e; alpar@774: return prev; alpar@774: } alpar@774: deba@782: void clear() { deba@782: edge_maps.clear(); deba@782: edges.clear(); deba@782: node_maps.clear(); deba@782: nodes.clear(); deba@782: } alpar@104: alpar@164: class Node { alpar@104: friend class SmartGraph; alpar@104: template friend class NodeMap; alpar@104: alpar@164: friend class Edge; alpar@104: friend class OutEdgeIt; alpar@104: friend class InEdgeIt; alpar@164: friend class SymEdge; alpar@104: alpar@104: protected: alpar@104: int n; alpar@722: friend int SmartGraph::id(Node v); alpar@164: Node(int nn) {n=nn;} alpar@104: public: alpar@164: Node() {} alpar@503: Node (Invalid) { n=-1; } alpar@164: bool operator==(const Node i) const {return n==i.n;} alpar@164: bool operator!=(const Node i) const {return n!=i.n;} alpar@164: bool operator<(const Node i) const {return nnodes.size()+1)-1; alpar@774: return *this; alpar@774: } alpar@774: // ///Validity check alpar@774: // operator bool() { return Node::operator bool(); } alpar@104: }; alpar@104: alpar@164: class Edge { alpar@104: friend class SmartGraph; alpar@104: template friend class EdgeMap; alpar@185: alpar@905: friend class SymSmartGraph; alpar@104: alpar@164: friend class Node; alpar@104: friend class NodeIt; alpar@104: protected: alpar@104: int n; alpar@722: friend int SmartGraph::id(Edge e); alpar@905: Edge(int nn) {n=nn;} alpar@706: public: alpar@706: /// An Edge with id \c n. alpar@706: alpar@164: Edge() { } marci@174: Edge (Invalid) { n=-1; } alpar@164: bool operator==(const Edge i) const {return n==i.n;} alpar@164: bool operator!=(const Edge i) const {return n!=i.n;} alpar@164: bool operator<(const Edge i) const {return nedges[n].next_out; return *this; } alpar@774: // ///Validity check alpar@774: // operator bool() { return Edge::operator bool(); } alpar@104: }; alpar@104: alpar@164: class InEdgeIt : public Edge { alpar@774: const SmartGraph *G; alpar@104: friend class SmartGraph; alpar@104: public: alpar@164: InEdgeIt() : Edge() { } alpar@774: InEdgeIt(const SmartGraph& _G, Edge e) : Edge(e), G(&_G) { } alpar@164: InEdgeIt (Invalid i) : Edge(i) { } alpar@774: InEdgeIt(const SmartGraph& _G,Node v) alpar@774: : Edge(_G.nodes[v.n].first_in), G(&_G) { } alpar@774: InEdgeIt &operator++() { n=G->edges[n].next_in; return *this; } alpar@774: // ///Validity check alpar@774: // operator bool() { return Edge::operator bool(); } alpar@104: }; alpar@105: alpar@104: }; alpar@185: alpar@185: ///Graph for bidirectional edges. alpar@185: alpar@185: ///The purpose of this graph structure is to handle graphs alpar@185: ///having bidirectional edges. Here the function \c addEdge(u,v) adds a pair alpar@186: ///of oppositely directed edges. alpar@186: ///There is a new edge map type called alpar@186: ///\ref SymSmartGraph::SymEdgeMap "SymEdgeMap" alpar@186: ///that complements this alpar@186: ///feature by alpar@186: ///storing shared values for the edge pairs. The usual alpar@880: ///\ref Graph::EdgeMap "EdgeMap" alpar@186: ///can be used alpar@185: ///as well. alpar@185: /// alpar@186: ///The oppositely directed edge can also be obtained easily alpar@186: ///using \ref opposite. alpar@186: ///\warning It shares the similarity with \ref SmartGraph that alpar@186: ///it is not possible to delete edges or nodes from the graph. alpar@880: //\sa SmartGraph. alpar@185: alpar@919: class SymSmartGraph : public SmartGraph alpar@185: { alpar@185: public: deba@782: typedef SymSmartGraph Graph; deba@782: alpar@904: // Create symmetric map registry. deba@782: CREATE_SYM_EDGE_MAP_REGISTRY; alpar@904: // Create symmetric edge map. deba@897: CREATE_SYM_EDGE_MAP(ArrayMap); deba@822: alpar@186: alpar@185: SymSmartGraph() : SmartGraph() { } alpar@185: SymSmartGraph(const SmartGraph &_g) : SmartGraph(_g) { } alpar@398: ///Adds a pair of oppositely directed edges to the graph. alpar@185: Edge addEdge(Node u, Node v) alpar@185: { alpar@185: Edge e = SmartGraph::addEdge(u,v); deba@798: Edge f = SmartGraph::addEdge(v,u); deba@798: sym_edge_maps.add(e); deba@798: sym_edge_maps.add(f); alpar@185: return e; alpar@185: } alpar@185: alpar@186: ///The oppositely directed edge. alpar@186: alpar@186: ///Returns the oppositely directed alpar@186: ///pair of the edge \c e. alpar@713: static Edge opposite(Edge e) alpar@185: { alpar@185: Edge f; alpar@905: f.n = e.n - 2*(e.n%2) + 1; alpar@185: return f; alpar@185: } alpar@185: alpar@185: alpar@919: }; alpar@185: alpar@407: /// @} alpar@921: } //namespace lemon alpar@104: alpar@157: alpar@157: alpar@157: alpar@921: #endif //LEMON_SMART_GRAPH_H