/* -*- C++ -*- * src/lemon/list_graph.h - Part of LEMON, a generic C++ optimization library * * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport * (Egervary Combinatorial Optimization Research Group, EGRES). * * Permission to use, modify and distribute this software is granted * provided that this copyright notice appears in all copies. For * precise terms see the accompanying LICENSE file. * * This software is provided "AS IS" with no warranty of any kind, * express or implied, and with no claim as to its suitability for any * purpose. * */ #ifndef LEMON_LIST_GRAPH_H #define LEMON_LIST_GRAPH_H ///\ingroup graphs ///\file ///\brief ListGraph, SymListGraph, NodeSet and EdgeSet classes. #include #include #include #include #include #include #include namespace lemon { class ListGraphBase { struct NodeT { int first_in,first_out; int prev, next; }; struct EdgeT { int head, tail; int prev_in, prev_out; int next_in, next_out; }; std::vector nodes; int first_node; int first_free_node; std::vector edges; int first_free_edge; public: typedef ListGraphBase Graph; class Node { friend class Graph; protected: int id; Node(int pid) { id = pid;} public: Node() {} Node (Invalid) { id = -1; } bool operator==(const Node& node) const {return id == node.id;} bool operator!=(const Node& node) const {return id != node.id;} bool operator<(const Node& node) const {return id < node.id;} }; class Edge { friend class Graph; protected: int id; Edge(int pid) { id = pid;} public: Edge() {} Edge (Invalid) { id = -1; } bool operator==(const Edge& edge) const {return id == edge.id;} bool operator!=(const Edge& edge) const {return id != edge.id;} bool operator<(const Edge& edge) const {return id < edge.id;} }; ListGraphBase() : nodes(), first_node(-1), first_free_node(-1), edges(), first_free_edge(-1) {} ///Using this it possible to avoid the superfluous memory allocation. ///\todo more docs... ///\todo It should be defined in ListGraph. void reserveEdge(int n) { edges.reserve(n); }; /// Maximum node ID. /// Maximum node ID. ///\sa id(Node) int maxNodeId() const { return nodes.size()-1; } /// Maximum edge ID. /// Maximum edge ID. ///\sa id(Edge) int maxEdgeId() const { return edges.size()-1; } Node tail(Edge e) const { return edges[e.id].tail; } Node head(Edge e) const { return edges[e.id].head; } void first(Node& node) const { node.id = first_node; } void next(Node& node) const { node.id = nodes[node.id].next; } void first(Edge& e) const { int n; for(n = first_node; n!=-1 && nodes[n].first_in == -1; n = nodes[n].next); e.id = (n == -1) ? -1 : nodes[n].first_in; } void next(Edge& edge) const { if (edges[edge.id].next_in != -1) { edge.id = edges[edge.id].next_in; } else { int n; for(n = nodes[edges[edge.id].head].next; n!=-1 && nodes[n].first_in == -1; n = nodes[n].next); edge.id = (n == -1) ? -1 : nodes[n].first_in; } } void firstOut(Edge &e, const Node& v) const { e.id = nodes[v.id].first_out; } void nextOut(Edge &e) const { e.id=edges[e.id].next_out; } void firstIn(Edge &e, const Node& v) const { e.id = nodes[v.id].first_in; } void nextIn(Edge &e) const { e.id=edges[e.id].next_in; } static int id(Node v) { return v.id; } static int id(Edge e) { return e.id; } /// Adds a new node to the graph. /// \warning It adds the new node to the front of the list. /// (i.e. the lastly added node becomes the first.) Node addNode() { int n; if(first_free_node==-1) { n = nodes.size(); nodes.push_back(NodeT()); } else { n = first_free_node; first_free_node = nodes[n].next; } nodes[n].next = first_node; if(first_node != -1) nodes[first_node].prev = n; first_node = n; nodes[n].prev = -1; nodes[n].first_in = nodes[n].first_out = -1; return Node(n); } Edge addEdge(Node u, Node v) { int n; if (first_free_edge == -1) { n = edges.size(); edges.push_back(EdgeT()); } else { n = first_free_edge; first_free_edge = edges[n].next_in; } edges[n].tail = u.id; edges[n].head = v.id; edges[n].next_out = nodes[u.id].first_out; if(nodes[u.id].first_out != -1) { edges[nodes[u.id].first_out].prev_out = n; } edges[n].next_in = nodes[v.id].first_in; if(nodes[v.id].first_in != -1) { edges[nodes[v.id].first_in].prev_in = n; } edges[n].prev_in = edges[n].prev_out = -1; nodes[u.id].first_out = nodes[v.id].first_in = n; return Edge(n); } void erase(const Node& node) { int n = node.id; if(nodes[n].next != -1) { nodes[nodes[n].next].prev = nodes[n].prev; } if(nodes[n].prev != -1) { nodes[nodes[n].prev].next = nodes[n].next; } else { first_node = nodes[n].next; } nodes[n].next = first_free_node; first_free_node = n; } void erase(const Edge& edge) { int n = edge.id; if(edges[n].next_in!=-1) { edges[edges[n].next_in].prev_in = edges[n].prev_in; } if(edges[n].prev_in!=-1) { edges[edges[n].prev_in].next_in = edges[n].next_in; } else { nodes[edges[n].head].first_in = edges[n].next_in; } if(edges[n].next_out!=-1) { edges[edges[n].next_out].prev_out = edges[n].prev_out; } if(edges[n].prev_out!=-1) { edges[edges[n].prev_out].next_out = edges[n].next_out; } else { nodes[edges[n].tail].first_out = edges[n].next_out; } edges[n].next_in = first_free_edge; first_free_edge = n; } void clear() { edges.clear(); nodes.clear(); first_node = first_free_node = first_free_edge = -1; } }; typedef AlterableGraphExtender AlterableListGraphBase; typedef IterableGraphExtender IterableListGraphBase; typedef IdMappableGraphExtender IdMappableListGraphBase; typedef DefaultMappableGraphExtender MappableListGraphBase; typedef ExtendableGraphExtender ExtendableListGraphBase; typedef ClearableGraphExtender ClearableListGraphBase; typedef ErasableGraphExtender ErasableListGraphBase; /// \addtogroup graphs /// @{ ///A list graph class. ///This is a simple and fast erasable graph implementation. /// ///It conforms to the ///\ref skeleton::ErasableGraph "ErasableGraph" concept. ///\sa skeleton::ErasableGraph. class ListGraph : public ErasableListGraphBase { public: /// Moves the head of \c e to \c n /// Moves the head of \c e to \c n /// void moveHead(Edge e, Node n) { if(edges[e.n].next_in != -1) edges[edges[e.n].next_in].prev_in = edges[e.n].prev_in; if(edges[e.n].prev_in != -1) edges[edges[e.n].prev_in].next_in = edges[e.n].next_in; else nodes[edges[e.n].head].first_in = edges[e.n].next_in; edges[e.n].head = n.n; edges[e.n].prev_in = -1; edges[e.n].next_in = nodes[n.n].first_in; nodes[n.n].first_in = e.n; } /// Moves the tail of \c e to \c n /// Moves the tail of \c e to \c n /// void moveTail(Edge e, Node n) { if(edges[e.n].next_out != -1) edges[edges[e.n].next_out].prev_out = edges[e.n].prev_out; if(edges[e.n].prev_out != -1) edges[edges[e.n].prev_out].next_out = edges[e.n].next_out; else nodes[edges[e.n].tail].first_out = edges[e.n].next_out; edges[e.n].tail = n.n; edges[e.n].prev_out = -1; edges[e.n].next_out = nodes[n.n].first_out; nodes[n.n].first_out = e.n; } } /// @} } //namespace lemon #endif