alpar@906: /* -*- C++ -*- alpar@906: * src/hugo/list_graph.h - Part of HUGOlib, 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@395: alpar@405: #ifndef HUGO_LIST_GRAPH_H alpar@405: #define HUGO_LIST_GRAPH_H alpar@395: klao@491: ///\ingroup graphs alpar@395: ///\file alpar@405: ///\brief ListGraph, SymListGraph, NodeSet and EdgeSet classes. alpar@395: alpar@395: #include deba@782: #include alpar@395: ladanyi@542: #include alpar@395: deba@782: #include deba@897: #include deba@782: alpar@919: #include alpar@919: deba@782: #include deba@782: deba@782: alpar@395: namespace hugo { alpar@395: alpar@406: /// \addtogroup graphs alpar@406: /// @{ alpar@406: alpar@401: ///A list graph class. alpar@395: alpar@397: ///This is a simple and fast erasable graph implementation. alpar@397: /// alpar@880: ///It conforms to the alpar@880: ///\ref skeleton::ErasableGraph "ErasableGraph" concept. alpar@880: ///\sa skeleton::ErasableGraph. alpar@397: class ListGraph { alpar@395: alpar@397: //Nodes are double linked. alpar@397: //The free nodes are only single linked using the "next" field. alpar@395: struct NodeT alpar@395: { alpar@397: int first_in,first_out; alpar@397: int prev, next; alpar@395: }; alpar@397: //Edges are double linked. alpar@397: //The free edges are only single linked using the "next_in" field. alpar@395: struct EdgeT alpar@395: { alpar@397: int head, tail; alpar@397: int prev_in, prev_out; alpar@397: int next_in, next_out; alpar@395: }; alpar@395: alpar@395: std::vector nodes; alpar@397: //The first node alpar@397: int first_node; alpar@397: //The first free node alpar@397: int first_free_node; alpar@395: std::vector edges; alpar@397: //The first free edge alpar@397: int first_free_edge; alpar@395: deba@782: public: alpar@395: deba@782: typedef ListGraph Graph; alpar@397: alpar@395: class Node; alpar@395: class Edge; alpar@395: alpar@395: alpar@395: public: alpar@395: alpar@395: class NodeIt; alpar@395: class EdgeIt; alpar@395: class OutEdgeIt; alpar@395: class InEdgeIt; deba@782: alpar@904: // Create map registries. deba@782: CREATE_MAP_REGISTRIES; alpar@905: // Create node and edge maps. deba@897: CREATE_MAPS(ArrayMap); deba@782: alpar@395: public: alpar@395: deba@782: ListGraph() deba@782: : nodes(), first_node(-1), deba@782: first_free_node(-1), edges(), first_free_edge(-1) {} deba@782: deba@782: ListGraph(const ListGraph &_g) deba@782: : nodes(_g.nodes), first_node(_g.first_node), deba@782: first_free_node(_g.first_free_node), edges(_g.edges), deba@782: first_free_edge(_g.first_free_edge) {} alpar@395: 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@395: alpar@813: ///Set the expected maximum number of edges. alpar@695: alpar@695: ///With this function, it is possible to set the expected number of edges. alpar@695: ///The use of this fasten the building of the graph and makes alpar@695: ///it possible to avoid the superfluous memory allocation. alpar@695: void reserveEdge(int n) { edges.reserve(n); }; alpar@695: 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@395: alpar@395: Node tail(Edge e) const { return edges[e.n].tail; } alpar@395: Node head(Edge e) const { return edges[e.n].head; } alpar@395: alpar@713: NodeIt& first(NodeIt& v) const { alpar@395: v=NodeIt(*this); return v; } alpar@713: EdgeIt& first(EdgeIt& e) const { alpar@395: e=EdgeIt(*this); return e; } alpar@713: OutEdgeIt& first(OutEdgeIt& e, const Node v) const { alpar@395: e=OutEdgeIt(*this,v); return e; } alpar@713: InEdgeIt& first(InEdgeIt& e, const Node v) const { alpar@395: e=InEdgeIt(*this,v); return e; } alpar@395: 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@395: alpar@397: /// Adds a new node to the graph. alpar@397: alpar@813: /// \warning It adds the new node to the front of the list. alpar@397: /// (i.e. the lastly added node becomes the first.) alpar@395: Node addNode() { alpar@397: int n; alpar@397: alpar@397: if(first_free_node==-1) alpar@397: { alpar@397: n = nodes.size(); alpar@397: nodes.push_back(NodeT()); alpar@397: } alpar@397: else { alpar@397: n = first_free_node; alpar@397: first_free_node = nodes[n].next; alpar@397: } alpar@397: alpar@397: nodes[n].next = first_node; alpar@397: if(first_node != -1) nodes[first_node].prev = n; alpar@397: first_node = n; alpar@397: nodes[n].prev = -1; alpar@397: alpar@397: nodes[n].first_in = nodes[n].first_out = -1; alpar@397: alpar@397: Node nn; nn.n=n; alpar@395: alpar@397: //Update dynamic maps deba@782: node_maps.add(nn); alpar@395: alpar@397: return nn; alpar@395: } alpar@395: alpar@395: Edge addEdge(Node u, Node v) { alpar@397: int n; alpar@397: alpar@397: if(first_free_edge==-1) alpar@397: { alpar@397: n = edges.size(); alpar@397: edges.push_back(EdgeT()); alpar@397: } alpar@397: else { alpar@397: n = first_free_edge; alpar@397: first_free_edge = edges[n].next_in; alpar@397: } alpar@397: alpar@397: edges[n].tail = u.n; edges[n].head = v.n; alpar@395: alpar@397: edges[n].next_out = nodes[u.n].first_out; alpar@397: if(nodes[u.n].first_out != -1) edges[nodes[u.n].first_out].prev_out = n; alpar@397: edges[n].next_in = nodes[v.n].first_in; alpar@397: if(nodes[v.n].first_in != -1) edges[nodes[v.n].first_in].prev_in = n; alpar@397: edges[n].prev_in = edges[n].prev_out = -1; alpar@397: alpar@397: nodes[u.n].first_out = nodes[v.n].first_in = n; alpar@397: alpar@397: Edge e; e.n=n; alpar@397: alpar@397: //Update dynamic maps deba@782: edge_maps.add(e); alpar@395: alpar@395: return e; alpar@395: } alpar@774: alpar@774: /// Finds an edge between two nodes. alpar@395: 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: alpar@397: private: alpar@397: void eraseEdge(int n) { alpar@397: alpar@397: if(edges[n].next_in!=-1) alpar@397: edges[edges[n].next_in].prev_in = edges[n].prev_in; alpar@397: if(edges[n].prev_in!=-1) alpar@397: edges[edges[n].prev_in].next_in = edges[n].next_in; alpar@397: else nodes[edges[n].head].first_in = edges[n].next_in; alpar@397: alpar@397: if(edges[n].next_out!=-1) alpar@397: edges[edges[n].next_out].prev_out = edges[n].prev_out; alpar@397: if(edges[n].prev_out!=-1) alpar@397: edges[edges[n].prev_out].next_out = edges[n].next_out; alpar@397: else nodes[edges[n].tail].first_out = edges[n].next_out; alpar@397: alpar@397: edges[n].next_in = first_free_edge; alpar@695: first_free_edge = n; alpar@397: alpar@397: //Update dynamic maps alpar@397: Edge e; e.n=n; deba@782: edge_maps.erase(e); deba@782: alpar@397: } alpar@397: alpar@397: public: alpar@397: alpar@397: void erase(Node nn) { alpar@397: int n=nn.n; alpar@397: alpar@397: int m; alpar@397: while((m=nodes[n].first_in)!=-1) eraseEdge(m); alpar@397: while((m=nodes[n].first_out)!=-1) eraseEdge(m); alpar@397: alpar@397: if(nodes[n].next != -1) nodes[nodes[n].next].prev = nodes[n].prev; alpar@397: if(nodes[n].prev != -1) nodes[nodes[n].prev].next = nodes[n].next; alpar@397: else first_node = nodes[n].next; alpar@397: alpar@397: nodes[n].next = first_free_node; alpar@397: first_free_node = n; alpar@397: alpar@397: //Update dynamic maps deba@782: node_maps.erase(nn); deba@782: alpar@397: } alpar@397: alpar@397: void erase(Edge e) { eraseEdge(e.n); } alpar@397: alpar@397: void clear() { deba@782: edge_maps.clear(); deba@782: edges.clear(); deba@782: node_maps.clear(); deba@782: nodes.clear(); alpar@397: first_node=first_free_node=first_free_edge=-1; alpar@397: } alpar@395: alpar@395: class Node { alpar@397: friend class ListGraph; alpar@395: template friend class NodeMap; alpar@400: alpar@395: friend class Edge; alpar@395: friend class OutEdgeIt; alpar@395: friend class InEdgeIt; alpar@395: friend class SymEdge; alpar@395: alpar@395: protected: alpar@395: int n; alpar@722: friend int ListGraph::id(Node v); alpar@395: Node(int nn) {n=nn;} alpar@395: public: alpar@395: Node() {} alpar@503: Node (Invalid) { n=-1; } alpar@395: bool operator==(const Node i) const {return n==i.n;} alpar@395: bool operator!=(const Node i) const {return n!=i.n;} alpar@395: bool operator<(const Node i) const {return nnodes[n].next; alpar@774: return *this; alpar@774: } alpar@774: // ///Validity check alpar@774: // operator bool() { return Node::operator bool(); } alpar@395: }; alpar@395: alpar@395: class Edge { alpar@397: friend class ListGraph; alpar@395: template friend class EdgeMap; alpar@395: alpar@905: friend class SymListGraph; alpar@395: alpar@395: friend class Node; alpar@395: friend class NodeIt; alpar@395: protected: alpar@395: int n; alpar@722: friend int ListGraph::id(Edge e); alpar@395: alpar@706: public: alpar@706: /// An Edge with id \c n. alpar@706: alpar@706: /// \bug It should be alpar@706: /// obtained by a member function of the Graph. alpar@395: Edge(int nn) {n=nn;} alpar@706: alpar@395: Edge() { } alpar@395: Edge (Invalid) { n=-1; } alpar@395: bool operator==(const Edge i) const {return n==i.n;} alpar@395: bool operator!=(const Edge i) const {return n!=i.n;} alpar@395: bool operator<(const Edge i) const {return nedges[n].next_in!=-1) n=G->edges[n].next_in; alpar@774: else { alpar@774: int nn; alpar@774: for(nn=G->nodes[G->edges[n].head].next; alpar@774: nn!=-1 && G->nodes[nn].first_in == -1; alpar@774: nn = G->nodes[nn].next) ; alpar@774: n = (nn==-1)?-1:G->nodes[nn].first_in; alpar@774: } alpar@774: return *this; alpar@774: } alpar@774: // ///Validity check alpar@774: // operator bool() { return Edge::operator bool(); } alpar@395: }; alpar@395: alpar@395: class OutEdgeIt : public Edge { alpar@774: const ListGraph *G; alpar@397: friend class ListGraph; alpar@395: public: alpar@395: OutEdgeIt() : Edge() { } alpar@774: OutEdgeIt(const ListGraph& _G, Edge e) : Edge(e), G(&_G) { } alpar@395: OutEdgeIt (Invalid i) : Edge(i) { } alpar@395: alpar@774: OutEdgeIt(const ListGraph& _G,const Node v) alpar@774: : Edge(_G.nodes[v.n].first_out), G(&_G) {} alpar@774: OutEdgeIt &operator++() { n=G->edges[n].next_out; return *this; } alpar@774: // ///Validity check alpar@774: // operator bool() { return Edge::operator bool(); } alpar@395: }; alpar@395: alpar@395: class InEdgeIt : public Edge { alpar@774: const ListGraph *G; alpar@397: friend class ListGraph; alpar@395: public: alpar@395: InEdgeIt() : Edge() { } alpar@774: InEdgeIt(const ListGraph& _G, Edge e) : Edge(e), G(&_G) { } alpar@395: InEdgeIt (Invalid i) : Edge(i) { } alpar@774: InEdgeIt(const ListGraph& _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@395: }; alpar@395: }; alpar@395: alpar@395: ///Graph for bidirectional edges. alpar@395: alpar@395: ///The purpose of this graph structure is to handle graphs alpar@395: ///having bidirectional edges. Here the function \c addEdge(u,v) adds a pair alpar@395: ///of oppositely directed edges. alpar@395: ///There is a new edge map type called alpar@911: ///\ref hugo::SymListGraph::SymEdgeMap "SymEdgeMap" alpar@395: ///that complements this alpar@395: ///feature by alpar@395: ///storing shared values for the edge pairs. The usual alpar@911: ///\ref hugo::skeleton::StaticGraph::EdgeMap "EdgeMap" alpar@395: ///can be used alpar@395: ///as well. alpar@395: /// alpar@395: ///The oppositely directed edge can also be obtained easily alpar@911: ///using \ref hugo::SymListGraph::opposite() "opposite()" member function. alpar@397: /// alpar@397: ///Here erase(Edge) deletes a pair of edges. alpar@397: /// alpar@397: ///\todo this date structure need some reconsiderations. Maybe it alpar@397: ///should be implemented independently from ListGraph. alpar@919: alpar@397: class SymListGraph : public ListGraph alpar@395: { alpar@395: public: deba@782: deba@782: typedef SymListGraph 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); alpar@395: alpar@397: SymListGraph() : ListGraph() { } alpar@397: SymListGraph(const ListGraph &_g) : ListGraph(_g) { } alpar@397: ///Adds a pair of oppositely directed edges to the graph. alpar@395: Edge addEdge(Node u, Node v) alpar@395: { alpar@397: Edge e = ListGraph::addEdge(u,v); deba@782: Edge f = ListGraph::addEdge(v,u); deba@782: sym_edge_maps.add(e); deba@782: sym_edge_maps.add(f); deba@782: alpar@395: return e; alpar@395: } alpar@395: deba@782: void erase(Node n) { ListGraph::erase(n);} alpar@395: ///The oppositely directed edge. alpar@395: alpar@395: ///Returns the oppositely directed alpar@395: ///pair of the edge \c e. alpar@713: static Edge opposite(Edge e) alpar@395: { alpar@395: Edge f; alpar@905: f.n = e.n - 2*(e.n%2) + 1; alpar@395: return f; alpar@395: } alpar@395: alpar@397: ///Removes a pair of oppositely directed edges to the graph. alpar@397: void erase(Edge e) { deba@782: Edge f = opposite(e); deba@782: sym_edge_maps.erase(e); deba@782: sym_edge_maps.erase(f); deba@782: ListGraph::erase(f); alpar@397: ListGraph::erase(e); deba@782: } alpar@919: }; deba@909: alpar@395: alpar@401: ///A graph class containing only nodes. alpar@400: alpar@401: ///This class implements a graph structure without edges. alpar@401: ///The most useful application of this class is to be the node set of an alpar@401: ///\ref EdgeSet class. alpar@400: /// alpar@880: ///It conforms to alpar@880: ///the \ref skeleton::ExtendableGraph "ExtendableGraph" concept alpar@880: ///with the exception that you cannot alpar@401: ///add (or delete) edges. The usual edge iterators are exists, but they are alpar@401: ///always \ref INVALID. alpar@880: ///\sa skeleton::ExtendableGraph alpar@880: ///\sa EdgeSet alpar@400: class NodeSet { alpar@400: alpar@400: //Nodes are double linked. alpar@400: //The free nodes are only single linked using the "next" field. alpar@400: struct NodeT alpar@400: { alpar@400: int first_in,first_out; alpar@400: int prev, next; alpar@400: // NodeT() {} alpar@400: }; alpar@400: alpar@400: std::vector nodes; alpar@400: //The first node alpar@400: int first_node; alpar@400: //The first free node alpar@400: int first_free_node; alpar@400: alpar@400: public: deba@782: deba@782: typedef NodeSet Graph; alpar@400: alpar@400: class Node; alpar@400: class Edge; alpar@400: alpar@400: public: alpar@400: alpar@400: class NodeIt; alpar@400: class EdgeIt; alpar@400: class OutEdgeIt; alpar@400: class InEdgeIt; alpar@400: alpar@904: // Create node map registry. deba@822: CREATE_NODE_MAP_REGISTRY; alpar@904: // Create node maps. deba@897: CREATE_NODE_MAP(ArrayMap); deba@822: deba@822: /// Creating empty map structure for edges. deba@822: template deba@822: class EdgeMap { deba@822: public: deba@822: EdgeMap(const Graph&) {} deba@822: EdgeMap(const Graph&, const Value&) {} deba@822: deba@822: EdgeMap(const EdgeMap&) {} deba@822: template EdgeMap(const CMap&) {} deba@822: deba@822: EdgeMap& operator=(const EdgeMap&) {} deba@822: template EdgeMap& operator=(const CMap&) {} deba@822: deba@822: class ConstIterator { deba@822: public: deba@822: bool operator==(const ConstIterator&) {return true;} deba@822: bool operator!=(const ConstIterator&) {return false;} deba@822: }; deba@822: deba@822: typedef ConstIterator Iterator; deba@822: deba@822: Iterator begin() { return Iterator();} deba@822: Iterator end() { return Iterator();} deba@822: deba@822: ConstIterator begin() const { return ConstIterator();} deba@822: ConstIterator end() const { return ConstIterator();} deba@822: deba@822: }; alpar@400: alpar@400: public: alpar@400: alpar@408: ///Default constructor deba@782: NodeSet() deba@782: : nodes(), first_node(-1), first_free_node(-1) {} alpar@408: ///Copy constructor deba@782: NodeSet(const NodeSet &_g) deba@782: : nodes(_g.nodes), first_node(_g.first_node), deba@782: first_free_node(_g.first_free_node) {} alpar@400: alpar@813: ///Number of nodes. alpar@813: int nodeNum() const { return nodes.size(); } alpar@813: ///Number of edges. alpar@813: int edgeNum() const { return 0; } alpar@400: 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 0; } alpar@400: alpar@400: Node tail(Edge e) const { return INVALID; } alpar@400: Node head(Edge e) const { return INVALID; } alpar@400: alpar@400: NodeIt& first(NodeIt& v) const { alpar@400: v=NodeIt(*this); return v; } alpar@400: EdgeIt& first(EdgeIt& e) const { alpar@400: e=EdgeIt(*this); return e; } alpar@400: OutEdgeIt& first(OutEdgeIt& e, const Node v) const { alpar@400: e=OutEdgeIt(*this,v); return e; } alpar@400: InEdgeIt& first(InEdgeIt& e, const Node v) const { alpar@400: e=InEdgeIt(*this,v); return e; } alpar@400: 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@905: 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@905: static int id(Edge e) { return -1; } alpar@400: alpar@400: /// Adds a new node to the graph. alpar@400: alpar@813: /// \warning It adds the new node to the front of the list. alpar@400: /// (i.e. the lastly added node becomes the first.) alpar@400: Node addNode() { alpar@400: int n; alpar@400: alpar@400: if(first_free_node==-1) alpar@400: { alpar@400: n = nodes.size(); alpar@400: nodes.push_back(NodeT()); alpar@400: } alpar@400: else { alpar@400: n = first_free_node; alpar@400: first_free_node = nodes[n].next; alpar@400: } alpar@400: alpar@400: nodes[n].next = first_node; alpar@400: if(first_node != -1) nodes[first_node].prev = n; alpar@400: first_node = n; alpar@400: nodes[n].prev = -1; alpar@400: alpar@400: nodes[n].first_in = nodes[n].first_out = -1; alpar@400: alpar@400: Node nn; nn.n=n; alpar@400: alpar@400: //Update dynamic maps deba@782: node_maps.add(nn); alpar@400: alpar@400: return nn; alpar@400: } alpar@400: alpar@400: void erase(Node nn) { alpar@400: int n=nn.n; alpar@400: alpar@400: if(nodes[n].next != -1) nodes[nodes[n].next].prev = nodes[n].prev; alpar@400: if(nodes[n].prev != -1) nodes[nodes[n].prev].next = nodes[n].next; alpar@400: else first_node = nodes[n].next; alpar@400: alpar@400: nodes[n].next = first_free_node; alpar@400: first_free_node = n; alpar@400: alpar@400: //Update dynamic maps deba@782: node_maps.erase(nn); alpar@400: } alpar@400: alpar@774: alpar@774: Edge findEdge(Node u,Node v, Edge prev = INVALID) alpar@774: { alpar@774: return INVALID; alpar@774: } alpar@774: alpar@400: void clear() { deba@782: node_maps.clear(); alpar@400: nodes.clear(); alpar@400: first_node = first_free_node = -1; alpar@400: } alpar@400: alpar@400: class Node { alpar@400: friend class NodeSet; alpar@400: template friend class NodeMap; alpar@400: alpar@400: friend class Edge; alpar@400: friend class OutEdgeIt; alpar@400: friend class InEdgeIt; alpar@400: alpar@400: protected: alpar@400: int n; alpar@905: friend int NodeSet::id(Node v); alpar@400: Node(int nn) {n=nn;} alpar@400: public: alpar@400: Node() {} alpar@400: Node (Invalid i) { n=-1; } alpar@400: bool operator==(const Node i) const {return n==i.n;} alpar@400: bool operator!=(const Node i) const {return n!=i.n;} alpar@400: bool operator<(const Node i) const {return nnodes[n].next; alpar@774: return *this; alpar@774: } alpar@400: }; alpar@400: alpar@400: class Edge { alpar@400: public: alpar@400: Edge() { } alpar@400: Edge (Invalid) { } alpar@400: bool operator==(const Edge i) const {return true;} alpar@400: bool operator!=(const Edge i) const {return false;} alpar@400: bool operator<(const Edge i) const {return false;} alpar@400: }; alpar@400: alpar@400: class EdgeIt : public Edge { alpar@400: public: alpar@400: EdgeIt(const NodeSet& G) : Edge() { } alpar@774: EdgeIt(const NodeSet&, Edge) : Edge() { } alpar@400: EdgeIt (Invalid i) : Edge(i) { } alpar@400: EdgeIt() : Edge() { } alpar@774: EdgeIt operator++() { return INVALID; } alpar@400: }; alpar@400: alpar@400: class OutEdgeIt : public Edge { alpar@400: friend class NodeSet; alpar@400: public: alpar@400: OutEdgeIt() : Edge() { } alpar@774: OutEdgeIt(const NodeSet&, Edge) : Edge() { } alpar@400: OutEdgeIt (Invalid i) : Edge(i) { } alpar@400: OutEdgeIt(const NodeSet& G,const Node v) : Edge() {} alpar@774: OutEdgeIt operator++() { return INVALID; } alpar@400: }; alpar@400: alpar@400: class InEdgeIt : public Edge { alpar@400: friend class NodeSet; alpar@400: public: alpar@400: InEdgeIt() : Edge() { } alpar@774: InEdgeIt(const NodeSet&, Edge) : Edge() { } alpar@400: InEdgeIt (Invalid i) : Edge(i) { } alpar@400: InEdgeIt(const NodeSet& G,Node v) :Edge() {} alpar@774: InEdgeIt operator++() { return INVALID; } alpar@400: }; alpar@400: alpar@400: }; alpar@400: alpar@400: alpar@400: alpar@401: ///Graph structure using a node set of another graph. alpar@401: alpar@401: ///This structure can be used to establish another graph over a node set alpar@401: /// of an existing one. The node iterator will go through the nodes of the alpar@401: /// original graph, and the NodeMap's of both graphs will convert to alpar@401: /// each other. alpar@401: /// alpar@404: ///\warning Adding or deleting nodes from the graph is not safe if an alpar@404: ///\ref EdgeSet is currently attached to it! alpar@404: /// alpar@404: ///\todo Make it possible to add/delete edges from the base graph alpar@404: ///(and from \ref EdgeSet, as well) alpar@404: /// alpar@401: ///\param GG The type of the graph which shares its node set with this class. alpar@880: ///Its interface must conform to the alpar@880: ///\ref skeleton::StaticGraph "StaticGraph" concept. alpar@400: /// alpar@880: ///It conforms to the alpar@880: ///\ref skeleton::ExtendableGraph "ExtendableGraph" concept. alpar@880: ///\sa skeleton::ExtendableGraph. alpar@880: ///\sa NodeSet. alpar@400: template alpar@400: class EdgeSet { alpar@400: alpar@400: typedef GG NodeGraphType; alpar@400: alpar@400: NodeGraphType &G; alpar@400: alpar@515: public: deba@782: alpar@400: class Node; alpar@705: class Edge; alpar@705: class OutEdgeIt; alpar@705: class InEdgeIt; alpar@705: class SymEdge; deba@782: deba@782: typedef EdgeSet Graph; deba@782: alpar@531: int id(Node v) const; alpar@531: alpar@531: class Node : public NodeGraphType::Node { alpar@531: friend class EdgeSet; alpar@531: alpar@531: friend class Edge; alpar@531: friend class OutEdgeIt; alpar@531: friend class InEdgeIt; alpar@531: friend class SymEdge; alpar@531: alpar@531: public: alpar@531: friend int EdgeSet::id(Node v) const; alpar@531: public: alpar@531: Node() : NodeGraphType::Node() {} alpar@531: Node (Invalid i) : NodeGraphType::Node(i) {} alpar@531: Node(const typename NodeGraphType::Node &n) : NodeGraphType::Node(n) {} alpar@531: }; alpar@531: alpar@531: class NodeIt : public NodeGraphType::NodeIt { alpar@531: friend class EdgeSet; alpar@531: public: alpar@531: NodeIt() : NodeGraphType::NodeIt() { } alpar@774: NodeIt(const EdgeSet& _G,Node n) : NodeGraphType::NodeIt(_G.G,n) { } alpar@531: NodeIt (Invalid i) : NodeGraphType::NodeIt(i) {} alpar@531: NodeIt(const EdgeSet& _G) : NodeGraphType::NodeIt(_G.G) { } alpar@531: NodeIt(const typename NodeGraphType::NodeIt &n) alpar@531: : NodeGraphType::NodeIt(n) {} alpar@579: alpar@531: operator Node() { return Node(*this);} alpar@774: NodeIt &operator++() alpar@774: { this->NodeGraphType::NodeIt::operator++(); return *this;} alpar@531: }; alpar@515: alpar@515: private: alpar@400: //Edges are double linked. alpar@400: //The free edges are only single linked using the "next_in" field. alpar@400: struct NodeT alpar@400: { alpar@400: int first_in,first_out; alpar@400: NodeT() : first_in(-1), first_out(-1) { } alpar@400: }; alpar@400: alpar@400: struct EdgeT alpar@400: { alpar@400: Node head, tail; alpar@400: int prev_in, prev_out; alpar@400: int next_in, next_out; alpar@400: }; alpar@400: alpar@400: alpar@515: typename NodeGraphType::template NodeMap nodes; alpar@400: alpar@400: std::vector edges; alpar@400: //The first free edge alpar@400: int first_free_edge; alpar@400: alpar@400: public: alpar@400: alpar@400: class Node; alpar@400: class Edge; alpar@400: alpar@400: class NodeIt; alpar@400: class EdgeIt; alpar@400: class OutEdgeIt; alpar@400: class InEdgeIt; deba@782: deba@782: alpar@904: // Create edge map registry. deba@782: CREATE_EDGE_MAP_REGISTRY; alpar@904: // Create edge maps. deba@897: CREATE_EDGE_MAP(ArrayMap); deba@822: alpar@904: // Import node maps from the NodeGraphType. deba@822: IMPORT_NODE_MAP(NodeGraphType, graph.G, EdgeSet, graph); alpar@400: alpar@400: alpar@400: public: alpar@400: alpar@408: ///Constructor alpar@408: alpar@408: ///Construates a new graph based on the nodeset of an existing one. alpar@408: ///\param _G the base graph. alpar@880: explicit EdgeSet(NodeGraphType &_G) deba@782: : G(_G), nodes(_G), edges(), deba@782: first_free_edge(-1) {} alpar@408: ///Copy constructor alpar@408: alpar@408: ///Makes a copy of an EdgeSet. alpar@408: ///It will be based on the same graph. alpar@880: explicit EdgeSet(const EdgeSet &_g) deba@782: : G(_g.G), nodes(_g.G), edges(_g.edges), deba@782: first_free_edge(_g.first_free_edge) {} alpar@400: alpar@813: ///Number of nodes. alpar@813: int nodeNum() const { return G.nodeNum(); } alpar@813: ///Number of edges. alpar@813: int edgeNum() const { return edges.size(); } alpar@400: alpar@813: /// Maximum node ID. alpar@813: alpar@813: /// Maximum node ID. alpar@813: ///\sa id(Node) alpar@813: int maxNodeId() const { return G.maxNodeId(); } 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@400: alpar@400: Node tail(Edge e) const { return edges[e.n].tail; } alpar@400: Node head(Edge e) const { return edges[e.n].head; } alpar@400: alpar@400: NodeIt& first(NodeIt& v) const { alpar@400: v=NodeIt(*this); return v; } alpar@400: EdgeIt& first(EdgeIt& e) const { alpar@400: e=EdgeIt(*this); return e; } alpar@400: OutEdgeIt& first(OutEdgeIt& e, const Node v) const { alpar@400: e=OutEdgeIt(*this,v); return e; } alpar@400: InEdgeIt& first(InEdgeIt& e, const Node v) const { alpar@400: e=InEdgeIt(*this,v); return e; } alpar@400: 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@813: int id(Node v) { return G.id(v); } 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@905: static int id(Edge e) { return e.n; } alpar@400: alpar@400: /// Adds a new node to the graph. alpar@579: Node addNode() { return G.addNode(); } alpar@400: alpar@400: Edge addEdge(Node u, Node v) { alpar@400: int n; alpar@400: alpar@400: if(first_free_edge==-1) alpar@400: { alpar@400: n = edges.size(); alpar@400: edges.push_back(EdgeT()); alpar@400: } alpar@400: else { alpar@400: n = first_free_edge; alpar@400: first_free_edge = edges[n].next_in; alpar@400: } alpar@400: alpar@401: edges[n].tail = u; edges[n].head = v; alpar@400: alpar@401: edges[n].next_out = nodes[u].first_out; alpar@401: if(nodes[u].first_out != -1) edges[nodes[u].first_out].prev_out = n; alpar@401: edges[n].next_in = nodes[v].first_in; alpar@401: if(nodes[v].first_in != -1) edges[nodes[v].first_in].prev_in = n; alpar@400: edges[n].prev_in = edges[n].prev_out = -1; alpar@400: alpar@401: nodes[u].first_out = nodes[v].first_in = n; alpar@400: alpar@400: Edge e; e.n=n; alpar@400: alpar@400: //Update dynamic maps deba@782: edge_maps.add(e); alpar@400: alpar@400: return e; alpar@400: } alpar@400: 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].first_out : edges[prev.n].next_out; alpar@774: while(e!=-1 && edges[e].tail!=v) e = edges[e].next_out; alpar@774: prev.n=e; alpar@774: return prev; alpar@774: } alpar@774: alpar@400: private: alpar@400: void eraseEdge(int n) { alpar@400: alpar@400: if(edges[n].next_in!=-1) alpar@400: edges[edges[n].next_in].prev_in = edges[n].prev_in; alpar@400: if(edges[n].prev_in!=-1) alpar@400: edges[edges[n].prev_in].next_in = edges[n].next_in; alpar@400: else nodes[edges[n].head].first_in = edges[n].next_in; alpar@400: alpar@400: if(edges[n].next_out!=-1) alpar@400: edges[edges[n].next_out].prev_out = edges[n].prev_out; alpar@400: if(edges[n].prev_out!=-1) alpar@400: edges[edges[n].prev_out].next_out = edges[n].next_out; alpar@400: else nodes[edges[n].tail].first_out = edges[n].next_out; alpar@400: alpar@400: edges[n].next_in = first_free_edge; alpar@400: first_free_edge = -1; alpar@400: alpar@400: //Update dynamic maps deba@782: Edge e; e.n = n; deba@782: edge_maps.erase(e); alpar@400: } alpar@400: alpar@400: public: alpar@400: alpar@400: void erase(Edge e) { eraseEdge(e.n); } alpar@400: alpar@579: ///Clear all edges. (Doesn't clear the nodes!) alpar@579: void clear() { deba@782: edge_maps.clear(); alpar@579: edges.clear(); alpar@579: first_free_edge=-1; alpar@579: } alpar@579: alpar@579: alpar@400: class Edge { alpar@579: public: alpar@400: friend class EdgeSet; alpar@400: template friend class EdgeMap; alpar@400: alpar@400: friend class Node; alpar@400: friend class NodeIt; alpar@905: protected: alpar@579: int n; alpar@400: friend int EdgeSet::id(Edge e) const; alpar@400: alpar@400: Edge(int nn) {n=nn;} alpar@400: public: alpar@400: Edge() { } alpar@400: Edge (Invalid) { n=-1; } alpar@400: bool operator==(const Edge i) const {return n==i.n;} alpar@400: bool operator!=(const Edge i) const {return n!=i.n;} alpar@400: bool operator<(const Edge i) const {return n friend class EdgeMap; alpar@579: alpar@774: const EdgeSet *G; alpar@400: public: alpar@774: EdgeIt(const EdgeSet& _G) : Edge(), G(&_G) { alpar@503: NodeIt m; alpar@774: for(G->first(m); alpar@774: m!=INVALID && G->nodes[m].first_in == -1; ++m); alpar@774: ///\bug AJJAJ! This is a non sense!!!!!!! alpar@774: this->n = m!=INVALID?-1:G->nodes[m].first_in; alpar@400: } alpar@774: EdgeIt(const EdgeSet& _G, Edge e) : Edge(e), G(&_G) { } alpar@400: EdgeIt (Invalid i) : Edge(i) { } alpar@400: EdgeIt() : Edge() { } alpar@774: ///. alpar@774: alpar@774: ///\bug UNIMPLEMENTED!!!!! alpar@774: // alpar@774: EdgeIt &operator++() { alpar@774: return *this; alpar@774: } alpar@400: }; alpar@400: alpar@400: class OutEdgeIt : public Edge { alpar@774: const EdgeSet *G; alpar@400: friend class EdgeSet; alpar@400: public: alpar@400: OutEdgeIt() : Edge() { } alpar@400: OutEdgeIt (Invalid i) : Edge(i) { } alpar@774: OutEdgeIt(const EdgeSet& _G, Edge e) : Edge(e), G(&_G) { } alpar@400: alpar@774: OutEdgeIt(const EdgeSet& _G,const Node v) : alpar@774: Edge(_G.nodes[v].first_out), G(&_G) { } deba@844: OutEdgeIt &operator++() { deba@844: Edge::n = G->edges[Edge::n].next_out; deba@844: return *this; deba@844: } alpar@400: }; alpar@400: alpar@400: class InEdgeIt : public Edge { alpar@774: const EdgeSet *G; alpar@400: friend class EdgeSet; alpar@400: public: alpar@400: InEdgeIt() : Edge() { } alpar@400: InEdgeIt (Invalid i) : Edge(i) { } alpar@774: InEdgeIt(const EdgeSet& _G, Edge e) : Edge(e), G(&_G) { } alpar@774: InEdgeIt(const EdgeSet& _G,Node v) alpar@774: : Edge(_G.nodes[v].first_in), G(&_G) { } deba@844: InEdgeIt &operator++() { deba@844: Edge::n = G->edges[Edge::n].next_in; deba@844: return *this; deba@844: } alpar@400: }; deba@782: alpar@400: }; alpar@406: alpar@579: template alpar@579: inline int EdgeSet::id(Node v) const { return G.id(v); } alpar@531: alpar@406: /// @} alpar@406: alpar@395: } //namespace hugo alpar@395: alpar@405: #endif //HUGO_LIST_GRAPH_H