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