alpar@906: /* -*- C++ -*- alpar@906: * src/hugo/smart_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@105: alpar@185: #ifndef HUGO_SMART_GRAPH_H alpar@185: #define HUGO_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: ladanyi@542: #include alpar@157: deba@897: #include deba@782: #include deba@782: deba@782: #include deba@782: alpar@105: namespace hugo { 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: deba@909: deba@909: deba@909: class SymSmartGraph : public SmartGraph { deba@909: typedef SmartGraph Parent; deba@909: public: deba@909: deba@909: typedef SymSmartGraph Graph; deba@909: deba@909: typedef SmartGraph::Node Node; deba@909: typedef SmartGraph::NodeIt NodeIt; deba@909: deba@909: class SymEdge; deba@909: class SymEdgeIt; deba@909: deba@909: class Edge; deba@909: class EdgeIt; deba@909: class OutEdgeIt; deba@909: class InEdgeIt; deba@909: deba@909: template deba@909: class NodeMap : public Parent::NodeMap { deba@909: public: deba@909: NodeMap(const SymSmartGraph& g) deba@909: : SymSmartGraph::Parent::NodeMap(g) {} deba@909: NodeMap(const SymSmartGraph& g, Value v) deba@909: : SymSmartGraph::Parent::NodeMap(g, v) {} deba@909: template deba@909: NodeMap(const NodeMap& copy) deba@909: : SymSmartGraph::Parent::NodeMap(copy) { } deba@909: }; deba@909: deba@909: template deba@909: class SymEdgeMap : public Parent::EdgeMap { deba@909: public: deba@909: typedef SymEdge KeyType; deba@909: deba@909: SymEdgeMap(const SymSmartGraph& g) deba@909: : SymSmartGraph::Parent::EdgeMap(g) {} deba@909: SymEdgeMap(const SymSmartGraph& g, Value v) deba@909: : SymSmartGraph::Parent::EdgeMap(g, v) {} deba@909: template deba@909: SymEdgeMap(const SymEdgeMap& copy) deba@909: : SymSmartGraph::Parent::EdgeMap(copy) { } deba@909: deba@909: }; deba@909: deba@909: // Create edge map registry. deba@909: CREATE_EDGE_MAP_REGISTRY; deba@909: // Create edge maps. deba@909: CREATE_EDGE_MAP(ArrayMap); deba@909: deba@909: class Edge { deba@909: friend class SymSmartGraph; deba@909: friend class SymSmartGraph::EdgeIt; deba@909: friend class SymSmartGraph::OutEdgeIt; deba@909: friend class SymSmartGraph::InEdgeIt; deba@909: deba@909: protected: deba@909: int id; deba@909: deba@909: Edge(int pid) { id = pid; } deba@909: deba@909: public: deba@909: /// An Edge with id \c n. deba@909: deba@909: Edge() { } deba@909: Edge (Invalid) { id = -1; } deba@909: deba@909: operator SymEdge(){ return SymEdge(id >> 1);} deba@909: deba@909: bool operator==(const Edge i) const {return id == i.id;} deba@909: bool operator!=(const Edge i) const {return id != i.id;} deba@909: bool operator<(const Edge i) const {return id < i.id;} deba@909: // ///Validity check deba@909: // operator bool() { return n!=-1; } deba@909: }; deba@909: deba@909: class SymEdge : public SmartGraph::Edge { deba@909: friend class SymSmartGraph; deba@909: friend class SymSmartGraph::Edge; deba@909: typedef SmartGraph::Edge Parent; deba@909: deba@909: protected: deba@909: SymEdge(int pid) : Parent(pid) {} deba@909: public: deba@909: deba@909: SymEdge() { } deba@909: SymEdge(const SmartGraph::Edge& i) : Parent(i) {} deba@909: SymEdge (Invalid) : Parent(INVALID) {} deba@909: deba@909: }; deba@909: deba@909: class OutEdgeIt { deba@909: Parent::OutEdgeIt out; deba@909: Parent::InEdgeIt in; deba@909: public: deba@909: OutEdgeIt() {} deba@909: OutEdgeIt(const SymSmartGraph& g, Edge e) { deba@916: if ((e.id & 1) == 0) { deba@909: out = Parent::OutEdgeIt(g, SymEdge(e)); deba@909: in = Parent::InEdgeIt(g, g.tail(e)); deba@909: } else { deba@909: out = Parent::OutEdgeIt(INVALID); deba@909: in = Parent::InEdgeIt(g, SymEdge(e)); deba@909: } deba@909: } deba@909: OutEdgeIt (Invalid i) : out(INVALID), in(INVALID) { } deba@909: deba@909: OutEdgeIt(const SymSmartGraph& g, const Node v) deba@909: : out(g, v), in(g, v) {} deba@909: OutEdgeIt &operator++() { deba@909: if (out != INVALID) { deba@909: ++out; deba@909: } else { deba@909: ++in; deba@909: } deba@909: return *this; deba@909: } deba@909: deba@909: operator Edge() const { deba@909: if (out == INVALID && in == INVALID) return INVALID; deba@909: return out != INVALID ? forward(out) : backward(in); deba@909: } deba@909: deba@909: bool operator==(const Edge i) const {return Edge(*this) == i;} deba@909: bool operator!=(const Edge i) const {return Edge(*this) != i;} deba@909: bool operator<(const Edge i) const {return Edge(*this) < i;} deba@909: }; deba@909: deba@909: class InEdgeIt { deba@909: Parent::OutEdgeIt out; deba@909: Parent::InEdgeIt in; deba@909: public: deba@909: InEdgeIt() {} deba@909: InEdgeIt(const SymSmartGraph& g, Edge e) { deba@916: if ((e.id & 1) == 0) { deba@909: out = Parent::OutEdgeIt(g, SymEdge(e)); deba@909: in = Parent::InEdgeIt(g, g.tail(e)); deba@909: } else { deba@909: out = Parent::OutEdgeIt(INVALID); deba@909: in = Parent::InEdgeIt(g, SymEdge(e)); deba@909: } deba@909: } deba@909: InEdgeIt (Invalid i) : out(INVALID), in(INVALID) { } deba@909: deba@909: InEdgeIt(const SymSmartGraph& g, const Node v) deba@909: : out(g, v), in(g, v) {} deba@909: deba@909: InEdgeIt &operator++() { deba@909: if (out != INVALID) { deba@909: ++out; deba@909: } else { deba@909: ++in; deba@909: } deba@909: return *this; deba@909: } deba@909: deba@909: operator Edge() const { deba@909: if (out == INVALID && in == INVALID) return INVALID; deba@909: return out != INVALID ? backward(out) : forward(in); deba@909: } deba@909: deba@909: bool operator==(const Edge i) const {return Edge(*this) == i;} deba@909: bool operator!=(const Edge i) const {return Edge(*this) != i;} deba@909: bool operator<(const Edge i) const {return Edge(*this) < i;} deba@909: }; deba@909: deba@909: class SymEdgeIt : public Parent::EdgeIt { deba@909: deba@909: public: deba@909: SymEdgeIt() {} deba@909: deba@909: SymEdgeIt(const SymSmartGraph& g) deba@909: : SymSmartGraph::Parent::EdgeIt(g) {} deba@909: deba@909: SymEdgeIt(const SymSmartGraph& g, SymEdge e) deba@909: : SymSmartGraph::Parent::EdgeIt(g, e) {} deba@909: deba@909: SymEdgeIt(Invalid i) deba@909: : SymSmartGraph::Parent::EdgeIt(INVALID) {} deba@909: deba@909: SymEdgeIt& operator++() { deba@909: SymSmartGraph::Parent::EdgeIt::operator++(); deba@909: return *this; deba@909: } deba@909: deba@909: operator SymEdge() const { deba@909: return SymEdge deba@909: (static_cast(*this)); deba@909: } deba@909: bool operator==(const SymEdge i) const {return SymEdge(*this) == i;} deba@909: bool operator!=(const SymEdge i) const {return SymEdge(*this) != i;} deba@909: bool operator<(const SymEdge i) const {return SymEdge(*this) < i;} deba@909: }; deba@909: deba@909: class EdgeIt { deba@909: SymEdgeIt it; deba@909: bool fw; deba@909: public: deba@909: EdgeIt(const SymSmartGraph& g) : it(g), fw(true) {} deba@909: EdgeIt (Invalid i) : it(i) { } deba@909: EdgeIt(const SymSmartGraph& g, Edge e) deba@909: : it(g, SymEdge(e)), fw(id(e) & 1 == 0) { } deba@909: EdgeIt() { } deba@909: EdgeIt& operator++() { deba@909: fw = !fw; deba@909: if (fw) ++it; deba@909: return *this; deba@909: } deba@909: operator Edge() const { deba@909: if (it == INVALID) return INVALID; deba@909: return fw ? forward(it) : backward(it); deba@909: } deba@909: bool operator==(const Edge i) const {return Edge(*this) == i;} deba@909: bool operator!=(const Edge i) const {return Edge(*this) != i;} deba@909: bool operator<(const Edge i) const {return Edge(*this) < i;} deba@909: deba@909: }; deba@909: deba@909: ///Number of nodes. deba@909: int nodeNum() const { return Parent::nodeNum(); } deba@909: ///Number of edges. deba@909: int edgeNum() const { return 2*Parent::edgeNum(); } deba@909: ///Number of symmetric edges. deba@909: int symEdgeNum() const { return Parent::edgeNum(); } deba@909: deba@909: /// Maximum node ID. deba@909: deba@909: /// Maximum node ID. deba@909: ///\sa id(Node) deba@909: int maxNodeId() const { return Parent::maxNodeId(); } deba@909: /// Maximum edge ID. deba@909: deba@909: /// Maximum edge ID. deba@909: ///\sa id(Edge) deba@909: int maxEdgeId() const { return 2*Parent::maxEdgeId(); } deba@909: /// Maximum symmetric edge ID. deba@909: deba@909: /// Maximum symmetric edge ID. deba@909: ///\sa id(SymEdge) deba@909: int maxSymEdgeId() const { return Parent::maxEdgeId(); } deba@909: deba@909: deba@909: Node tail(Edge e) const { deba@916: return (e.id & 1) == 0 ? deba@909: Parent::tail(SymEdge(e)) : Parent::head(SymEdge(e)); deba@909: } deba@909: deba@909: Node head(Edge e) const { deba@916: return (e.id & 1) == 0 ? deba@909: Parent::head(SymEdge(e)) : Parent::tail(SymEdge(e)); deba@909: } deba@909: deba@909: Node tail(SymEdge e) const { deba@909: return Parent::tail(e); deba@909: } deba@909: deba@909: Node head(SymEdge e) const { deba@909: return Parent::head(e); deba@909: } deba@909: deba@909: NodeIt& first(NodeIt& v) const { deba@909: v=NodeIt(*this); return v; } deba@909: EdgeIt& first(EdgeIt& e) const { deba@909: e=EdgeIt(*this); return e; } deba@909: SymEdgeIt& first(SymEdgeIt& e) const { deba@909: e=SymEdgeIt(*this); return e; } deba@909: OutEdgeIt& first(OutEdgeIt& e, const Node v) const { deba@909: e=OutEdgeIt(*this,v); return e; } deba@909: InEdgeIt& first(InEdgeIt& e, const Node v) const { deba@909: e=InEdgeIt(*this,v); return e; } deba@909: deba@909: /// Node ID. deba@909: deba@909: /// The ID of a valid Node is a nonnegative integer not greater than deba@909: /// \ref maxNodeId(). The range of the ID's is not surely continuous deba@909: /// and the greatest node ID can be actually less then \ref maxNodeId(). deba@909: /// deba@909: /// The ID of the \ref INVALID node is -1. deba@909: ///\return The ID of the node \c v. deba@909: static int id(Node v) { return Parent::id(v); } deba@909: /// Edge ID. deba@909: deba@909: /// The ID of a valid Edge is a nonnegative integer not greater than deba@909: /// \ref maxEdgeId(). The range of the ID's is not surely continuous deba@909: /// and the greatest edge ID can be actually less then \ref maxEdgeId(). deba@909: /// deba@909: /// The ID of the \ref INVALID edge is -1. deba@909: ///\return The ID of the edge \c e. deba@909: static int id(Edge e) { return e.id; } deba@909: deba@909: /// The ID of a valid SymEdge is a nonnegative integer not greater than deba@909: /// \ref maxSymEdgeId(). The range of the ID's is not surely continuous deba@909: /// and the greatest edge ID can be actually less then \ref maxSymEdgeId(). deba@909: /// deba@909: /// The ID of the \ref INVALID symmetric edge is -1. deba@909: ///\return The ID of the edge \c e. deba@909: static int id(SymEdge e) { return Parent::id(e); } deba@909: deba@909: /// Adds a new node to the graph. deba@909: deba@909: /// \warning It adds the new node to the front of the list. deba@909: /// (i.e. the lastly added node becomes the first.) deba@909: Node addNode() { deba@909: return Parent::addNode(); deba@909: } deba@909: deba@909: SymEdge addEdge(Node u, Node v) { deba@909: SymEdge se = Parent::addEdge(u, v); deba@909: edge_maps.add(forward(se)); deba@909: edge_maps.add(backward(se)); deba@909: return se; deba@909: } deba@909: deba@909: /// Finds an edge between two nodes. deba@909: deba@909: /// Finds an edge from node \c u to node \c v. deba@909: /// deba@909: /// If \c prev is \ref INVALID (this is the default value), then deba@909: /// It finds the first edge from \c u to \c v. Otherwise it looks for deba@909: /// the next edge from \c u to \c v after \c prev. deba@909: /// \return The found edge or INVALID if there is no such an edge. deba@909: Edge findEdge(Node u, Node v, Edge prev = INVALID) deba@909: { deba@909: if (prev == INVALID || id(prev) & 1 == 0) { deba@909: SymEdge se = Parent::findEdge(u, v, SymEdge(prev)); deba@909: if (se != INVALID) return forward(se); deba@909: } else { deba@909: SymEdge se = Parent::findEdge(v, u, SymEdge(prev)); deba@909: if (se != INVALID) return backward(se); deba@909: } deba@909: return INVALID; deba@909: } deba@909: deba@916: // /// Finds an symmetric edge between two nodes. deba@909: deba@916: // /// Finds an symmetric edge from node \c u to node \c v. deba@916: // /// deba@916: // /// If \c prev is \ref INVALID (this is the default value), then deba@916: // /// It finds the first edge from \c u to \c v. Otherwise it looks for deba@916: // /// the next edge from \c u to \c v after \c prev. deba@916: // /// \return The found edge or INVALID if there is no such an edge. deba@909: deba@909: // SymEdge findEdge(Node u, Node v, SymEdge prev = INVALID) deba@909: // { deba@909: // if (prev == INVALID || id(prev) & 1 == 0) { deba@909: // SymEdge se = Parent::findEdge(u, v, SymEdge(prev)); deba@909: // if (se != INVALID) return se; deba@909: // } else { deba@909: // SymEdge se = Parent::findEdge(v, u, SymEdge(prev)); deba@909: // if (se != INVALID) return se; deba@909: // } deba@909: // return INVALID; deba@909: // } deba@909: deba@909: public: deba@909: deba@909: void clear() { deba@909: edge_maps.clear(); deba@909: Parent::clear(); deba@909: } deba@909: deba@909: static Edge opposite(Edge e) { deba@909: return Edge(id(e) ^ 1); deba@909: } deba@909: deba@909: static Edge forward(SymEdge e) { deba@909: return Edge(id(e) << 1); deba@909: } deba@909: deba@909: static Edge backward(SymEdge e) { deba@916: return Edge((id(e) << 1) | 1); deba@909: } deba@909: deba@909: }; 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: deba@909: /* 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: deba@909: };*/ alpar@185: alpar@407: /// @} alpar@105: } //namespace hugo alpar@104: alpar@157: alpar@157: alpar@157: alpar@590: #endif //HUGO_SMART_GRAPH_H