alpar@105: // -*- mode:C++ -*- 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@798: #include deba@782: #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@242: ///It conforms to the graph interface documented under alpar@186: ///the description of \ref GraphSkeleton. alpar@186: ///\sa \ref GraphSkeleton. 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: deba@782: CREATE_MAP_REGISTRIES; deba@798: CREATE_MAPS(DefaultMapFactory); 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@185: //template friend class SymSmartGraph::SymEdgeMap; alpar@185: //friend Edge SymSmartGraph::opposite(Edge) const; 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@157: 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@164: Edge(int nn) {n=nn;} 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@186: ///\ref GraphSkeleton::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@186: //\sa \ref SmartGraph. alpar@185: alpar@185: class SymSmartGraph : public SmartGraph alpar@185: { alpar@185: public: deba@782: typedef SymSmartGraph Graph; deba@782: deba@782: KEEP_NODE_MAP(SmartGraph); deba@782: KEEP_EDGE_MAP(SmartGraph); deba@782: deba@782: CREATE_SYM_EDGE_MAP_REGISTRY; deba@798: CREATE_SYM_EDGE_MAP_FACTORY(DefaultMapFactory); deba@782: IMPORT_SYM_EDGE_MAP(SymEdgeMapFactory); 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@185: f.idref() = e.idref() - 2*(e.idref()%2) + 1; alpar@185: return f; alpar@185: } alpar@185: alpar@185: alpar@185: }; alpar@185: alpar@407: /// @} alpar@105: } //namespace hugo alpar@104: alpar@157: alpar@157: alpar@157: alpar@590: #endif //HUGO_SMART_GRAPH_H