1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/lemon/list_graph.h Wed Sep 29 15:30:04 2004 +0000
1.3 @@ -0,0 +1,1106 @@
1.4 +/* -*- C++ -*-
1.5 + * src/lemon/list_graph.h - Part of LEMON, a generic C++ optimization library
1.6 + *
1.7 + * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
1.8 + * (Egervary Combinatorial Optimization Research Group, EGRES).
1.9 + *
1.10 + * Permission to use, modify and distribute this software is granted
1.11 + * provided that this copyright notice appears in all copies. For
1.12 + * precise terms see the accompanying LICENSE file.
1.13 + *
1.14 + * This software is provided "AS IS" with no warranty of any kind,
1.15 + * express or implied, and with no claim as to its suitability for any
1.16 + * purpose.
1.17 + *
1.18 + */
1.19 +
1.20 +#ifndef LEMON_LIST_GRAPH_H
1.21 +#define LEMON_LIST_GRAPH_H
1.22 +
1.23 +///\ingroup graphs
1.24 +///\file
1.25 +///\brief ListGraph, SymListGraph, NodeSet and EdgeSet classes.
1.26 +
1.27 +#include <vector>
1.28 +#include <climits>
1.29 +
1.30 +#include <lemon/invalid.h>
1.31 +
1.32 +#include <lemon/map_registry.h>
1.33 +#include <lemon/array_map.h>
1.34 +
1.35 +#include <lemon/sym_map.h>
1.36 +
1.37 +#include <lemon/map_defines.h>
1.38 +
1.39 +
1.40 +namespace lemon {
1.41 +
1.42 +/// \addtogroup graphs
1.43 +/// @{
1.44 +
1.45 + ///A list graph class.
1.46 +
1.47 + ///This is a simple and fast erasable graph implementation.
1.48 + ///
1.49 + ///It conforms to the
1.50 + ///\ref skeleton::ErasableGraph "ErasableGraph" concept.
1.51 + ///\sa skeleton::ErasableGraph.
1.52 + class ListGraph {
1.53 +
1.54 + //Nodes are double linked.
1.55 + //The free nodes are only single linked using the "next" field.
1.56 + struct NodeT
1.57 + {
1.58 + int first_in,first_out;
1.59 + int prev, next;
1.60 + };
1.61 + //Edges are double linked.
1.62 + //The free edges are only single linked using the "next_in" field.
1.63 + struct EdgeT
1.64 + {
1.65 + int head, tail;
1.66 + int prev_in, prev_out;
1.67 + int next_in, next_out;
1.68 + };
1.69 +
1.70 + std::vector<NodeT> nodes;
1.71 + //The first node
1.72 + int first_node;
1.73 + //The first free node
1.74 + int first_free_node;
1.75 + std::vector<EdgeT> edges;
1.76 + //The first free edge
1.77 + int first_free_edge;
1.78 +
1.79 + public:
1.80 +
1.81 + typedef ListGraph Graph;
1.82 +
1.83 + class Node;
1.84 + class Edge;
1.85 +
1.86 +
1.87 + public:
1.88 +
1.89 + class NodeIt;
1.90 + class EdgeIt;
1.91 + class OutEdgeIt;
1.92 + class InEdgeIt;
1.93 +
1.94 + // Create map registries.
1.95 + CREATE_MAP_REGISTRIES;
1.96 + // Create node and edge maps.
1.97 + CREATE_MAPS(ArrayMap);
1.98 +
1.99 + public:
1.100 +
1.101 + ListGraph()
1.102 + : nodes(), first_node(-1),
1.103 + first_free_node(-1), edges(), first_free_edge(-1) {}
1.104 +
1.105 + ListGraph(const ListGraph &_g)
1.106 + : nodes(_g.nodes), first_node(_g.first_node),
1.107 + first_free_node(_g.first_free_node), edges(_g.edges),
1.108 + first_free_edge(_g.first_free_edge) {}
1.109 +
1.110 + ///Number of nodes.
1.111 + int nodeNum() const { return nodes.size(); }
1.112 + ///Number of edges.
1.113 + int edgeNum() const { return edges.size(); }
1.114 +
1.115 + ///Set the expected maximum number of edges.
1.116 +
1.117 + ///With this function, it is possible to set the expected number of edges.
1.118 + ///The use of this fasten the building of the graph and makes
1.119 + ///it possible to avoid the superfluous memory allocation.
1.120 + void reserveEdge(int n) { edges.reserve(n); };
1.121 +
1.122 + /// Maximum node ID.
1.123 +
1.124 + /// Maximum node ID.
1.125 + ///\sa id(Node)
1.126 + int maxNodeId() const { return nodes.size()-1; }
1.127 + /// Maximum edge ID.
1.128 +
1.129 + /// Maximum edge ID.
1.130 + ///\sa id(Edge)
1.131 + int maxEdgeId() const { return edges.size()-1; }
1.132 +
1.133 + Node tail(Edge e) const { return edges[e.n].tail; }
1.134 + Node head(Edge e) const { return edges[e.n].head; }
1.135 +
1.136 + NodeIt& first(NodeIt& v) const {
1.137 + v=NodeIt(*this); return v; }
1.138 + EdgeIt& first(EdgeIt& e) const {
1.139 + e=EdgeIt(*this); return e; }
1.140 + OutEdgeIt& first(OutEdgeIt& e, const Node v) const {
1.141 + e=OutEdgeIt(*this,v); return e; }
1.142 + InEdgeIt& first(InEdgeIt& e, const Node v) const {
1.143 + e=InEdgeIt(*this,v); return e; }
1.144 +
1.145 + /// Node ID.
1.146 +
1.147 + /// The ID of a valid Node is a nonnegative integer not greater than
1.148 + /// \ref maxNodeId(). The range of the ID's is not surely continuous
1.149 + /// and the greatest node ID can be actually less then \ref maxNodeId().
1.150 + ///
1.151 + /// The ID of the \ref INVALID node is -1.
1.152 + ///\return The ID of the node \c v.
1.153 + static int id(Node v) { return v.n; }
1.154 + /// Edge ID.
1.155 +
1.156 + /// The ID of a valid Edge is a nonnegative integer not greater than
1.157 + /// \ref maxEdgeId(). The range of the ID's is not surely continuous
1.158 + /// and the greatest edge ID can be actually less then \ref maxEdgeId().
1.159 + ///
1.160 + /// The ID of the \ref INVALID edge is -1.
1.161 + ///\return The ID of the edge \c e.
1.162 + static int id(Edge e) { return e.n; }
1.163 +
1.164 + /// Adds a new node to the graph.
1.165 +
1.166 + /// \warning It adds the new node to the front of the list.
1.167 + /// (i.e. the lastly added node becomes the first.)
1.168 + Node addNode() {
1.169 + int n;
1.170 +
1.171 + if(first_free_node==-1)
1.172 + {
1.173 + n = nodes.size();
1.174 + nodes.push_back(NodeT());
1.175 + }
1.176 + else {
1.177 + n = first_free_node;
1.178 + first_free_node = nodes[n].next;
1.179 + }
1.180 +
1.181 + nodes[n].next = first_node;
1.182 + if(first_node != -1) nodes[first_node].prev = n;
1.183 + first_node = n;
1.184 + nodes[n].prev = -1;
1.185 +
1.186 + nodes[n].first_in = nodes[n].first_out = -1;
1.187 +
1.188 + Node nn; nn.n=n;
1.189 +
1.190 + //Update dynamic maps
1.191 + node_maps.add(nn);
1.192 +
1.193 + return nn;
1.194 + }
1.195 +
1.196 + Edge addEdge(Node u, Node v) {
1.197 + int n;
1.198 +
1.199 + if(first_free_edge==-1)
1.200 + {
1.201 + n = edges.size();
1.202 + edges.push_back(EdgeT());
1.203 + }
1.204 + else {
1.205 + n = first_free_edge;
1.206 + first_free_edge = edges[n].next_in;
1.207 + }
1.208 +
1.209 + edges[n].tail = u.n; edges[n].head = v.n;
1.210 +
1.211 + edges[n].next_out = nodes[u.n].first_out;
1.212 + if(nodes[u.n].first_out != -1) edges[nodes[u.n].first_out].prev_out = n;
1.213 + edges[n].next_in = nodes[v.n].first_in;
1.214 + if(nodes[v.n].first_in != -1) edges[nodes[v.n].first_in].prev_in = n;
1.215 + edges[n].prev_in = edges[n].prev_out = -1;
1.216 +
1.217 + nodes[u.n].first_out = nodes[v.n].first_in = n;
1.218 +
1.219 + Edge e; e.n=n;
1.220 +
1.221 + //Update dynamic maps
1.222 + edge_maps.add(e);
1.223 +
1.224 + return e;
1.225 + }
1.226 +
1.227 + /// Finds an edge between two nodes.
1.228 +
1.229 + /// Finds an edge from node \c u to node \c v.
1.230 + ///
1.231 + /// If \c prev is \ref INVALID (this is the default value), then
1.232 + /// It finds the first edge from \c u to \c v. Otherwise it looks for
1.233 + /// the next edge from \c u to \c v after \c prev.
1.234 + /// \return The found edge or INVALID if there is no such an edge.
1.235 + Edge findEdge(Node u,Node v, Edge prev = INVALID)
1.236 + {
1.237 + int e = (prev.n==-1)? nodes[u.n].first_out : edges[prev.n].next_out;
1.238 + while(e!=-1 && edges[e].tail!=v.n) e = edges[e].next_out;
1.239 + prev.n=e;
1.240 + return prev;
1.241 + }
1.242 +
1.243 + private:
1.244 + void eraseEdge(int n) {
1.245 +
1.246 + if(edges[n].next_in!=-1)
1.247 + edges[edges[n].next_in].prev_in = edges[n].prev_in;
1.248 + if(edges[n].prev_in!=-1)
1.249 + edges[edges[n].prev_in].next_in = edges[n].next_in;
1.250 + else nodes[edges[n].head].first_in = edges[n].next_in;
1.251 +
1.252 + if(edges[n].next_out!=-1)
1.253 + edges[edges[n].next_out].prev_out = edges[n].prev_out;
1.254 + if(edges[n].prev_out!=-1)
1.255 + edges[edges[n].prev_out].next_out = edges[n].next_out;
1.256 + else nodes[edges[n].tail].first_out = edges[n].next_out;
1.257 +
1.258 + edges[n].next_in = first_free_edge;
1.259 + first_free_edge = n;
1.260 +
1.261 + //Update dynamic maps
1.262 + Edge e; e.n=n;
1.263 + edge_maps.erase(e);
1.264 +
1.265 + }
1.266 +
1.267 + public:
1.268 +
1.269 + void erase(Node nn) {
1.270 + int n=nn.n;
1.271 +
1.272 + int m;
1.273 + while((m=nodes[n].first_in)!=-1) eraseEdge(m);
1.274 + while((m=nodes[n].first_out)!=-1) eraseEdge(m);
1.275 +
1.276 + if(nodes[n].next != -1) nodes[nodes[n].next].prev = nodes[n].prev;
1.277 + if(nodes[n].prev != -1) nodes[nodes[n].prev].next = nodes[n].next;
1.278 + else first_node = nodes[n].next;
1.279 +
1.280 + nodes[n].next = first_free_node;
1.281 + first_free_node = n;
1.282 +
1.283 + //Update dynamic maps
1.284 + node_maps.erase(nn);
1.285 +
1.286 + }
1.287 +
1.288 + void erase(Edge e) { eraseEdge(e.n); }
1.289 +
1.290 + void clear() {
1.291 + edge_maps.clear();
1.292 + edges.clear();
1.293 + node_maps.clear();
1.294 + nodes.clear();
1.295 + first_node=first_free_node=first_free_edge=-1;
1.296 + }
1.297 +
1.298 + class Node {
1.299 + friend class ListGraph;
1.300 + template <typename T> friend class NodeMap;
1.301 +
1.302 + friend class Edge;
1.303 + friend class OutEdgeIt;
1.304 + friend class InEdgeIt;
1.305 + friend class SymEdge;
1.306 +
1.307 + protected:
1.308 + int n;
1.309 + friend int ListGraph::id(Node v);
1.310 + Node(int nn) {n=nn;}
1.311 + public:
1.312 + Node() {}
1.313 + Node (Invalid) { n=-1; }
1.314 + bool operator==(const Node i) const {return n==i.n;}
1.315 + bool operator!=(const Node i) const {return n!=i.n;}
1.316 + bool operator<(const Node i) const {return n<i.n;}
1.317 + // ///Validity check
1.318 + // operator bool() { return n!=-1; }
1.319 + };
1.320 +
1.321 + class NodeIt : public Node {
1.322 + const ListGraph *G;
1.323 + friend class ListGraph;
1.324 + public:
1.325 + NodeIt() : Node() { }
1.326 + NodeIt(Invalid i) : Node(i) { }
1.327 + NodeIt(const ListGraph& _G) : Node(_G.first_node), G(&_G) { }
1.328 + NodeIt(const ListGraph& _G,Node n) : Node(n), G(&_G) { }
1.329 + NodeIt &operator++() {
1.330 + n=G->nodes[n].next;
1.331 + return *this;
1.332 + }
1.333 + // ///Validity check
1.334 + // operator bool() { return Node::operator bool(); }
1.335 + };
1.336 +
1.337 + class Edge {
1.338 + friend class ListGraph;
1.339 + template <typename T> friend class EdgeMap;
1.340 +
1.341 + friend class SymListGraph;
1.342 +
1.343 + friend class Node;
1.344 + friend class NodeIt;
1.345 + protected:
1.346 + int n;
1.347 + friend int ListGraph::id(Edge e);
1.348 +
1.349 + public:
1.350 + /// An Edge with id \c n.
1.351 +
1.352 + /// \bug It should be
1.353 + /// obtained by a member function of the Graph.
1.354 + Edge(int nn) {n=nn;}
1.355 +
1.356 + Edge() { }
1.357 + Edge (Invalid) { n=-1; }
1.358 + bool operator==(const Edge i) const {return n==i.n;}
1.359 + bool operator!=(const Edge i) const {return n!=i.n;}
1.360 + bool operator<(const Edge i) const {return n<i.n;}
1.361 + // ///Validity check
1.362 + // operator bool() { return n!=-1; }
1.363 + };
1.364 +
1.365 + class EdgeIt : public Edge {
1.366 + const ListGraph *G;
1.367 + friend class ListGraph;
1.368 + public:
1.369 + EdgeIt(const ListGraph& _G) : Edge(), G(&_G) {
1.370 + int m;
1.371 + for(m=_G.first_node;
1.372 + m!=-1 && _G.nodes[m].first_in == -1; m = _G.nodes[m].next);
1.373 + n = (m==-1)?-1:_G.nodes[m].first_in;
1.374 + }
1.375 + EdgeIt (Invalid i) : Edge(i) { }
1.376 + EdgeIt(const ListGraph& _G, Edge e) : Edge(e), G(&_G) { }
1.377 + EdgeIt() : Edge() { }
1.378 + EdgeIt &operator++() {
1.379 + if(G->edges[n].next_in!=-1) n=G->edges[n].next_in;
1.380 + else {
1.381 + int nn;
1.382 + for(nn=G->nodes[G->edges[n].head].next;
1.383 + nn!=-1 && G->nodes[nn].first_in == -1;
1.384 + nn = G->nodes[nn].next) ;
1.385 + n = (nn==-1)?-1:G->nodes[nn].first_in;
1.386 + }
1.387 + return *this;
1.388 + }
1.389 + // ///Validity check
1.390 + // operator bool() { return Edge::operator bool(); }
1.391 + };
1.392 +
1.393 + class OutEdgeIt : public Edge {
1.394 + const ListGraph *G;
1.395 + friend class ListGraph;
1.396 + public:
1.397 + OutEdgeIt() : Edge() { }
1.398 + OutEdgeIt(const ListGraph& _G, Edge e) : Edge(e), G(&_G) { }
1.399 + OutEdgeIt (Invalid i) : Edge(i) { }
1.400 +
1.401 + OutEdgeIt(const ListGraph& _G,const Node v)
1.402 + : Edge(_G.nodes[v.n].first_out), G(&_G) {}
1.403 + OutEdgeIt &operator++() { n=G->edges[n].next_out; return *this; }
1.404 + // ///Validity check
1.405 + // operator bool() { return Edge::operator bool(); }
1.406 + };
1.407 +
1.408 + class InEdgeIt : public Edge {
1.409 + const ListGraph *G;
1.410 + friend class ListGraph;
1.411 + public:
1.412 + InEdgeIt() : Edge() { }
1.413 + InEdgeIt(const ListGraph& _G, Edge e) : Edge(e), G(&_G) { }
1.414 + InEdgeIt (Invalid i) : Edge(i) { }
1.415 + InEdgeIt(const ListGraph& _G,Node v)
1.416 + : Edge(_G.nodes[v.n].first_in), G(&_G) { }
1.417 + InEdgeIt &operator++() { n=G->edges[n].next_in; return *this; }
1.418 + // ///Validity check
1.419 + // operator bool() { return Edge::operator bool(); }
1.420 + };
1.421 + };
1.422 +
1.423 + ///Graph for bidirectional edges.
1.424 +
1.425 + ///The purpose of this graph structure is to handle graphs
1.426 + ///having bidirectional edges. Here the function \c addEdge(u,v) adds a pair
1.427 + ///of oppositely directed edges.
1.428 + ///There is a new edge map type called
1.429 + ///\ref lemon::SymListGraph::SymEdgeMap "SymEdgeMap"
1.430 + ///that complements this
1.431 + ///feature by
1.432 + ///storing shared values for the edge pairs. The usual
1.433 + ///\ref lemon::skeleton::StaticGraph::EdgeMap "EdgeMap"
1.434 + ///can be used
1.435 + ///as well.
1.436 + ///
1.437 + ///The oppositely directed edge can also be obtained easily
1.438 + ///using \ref lemon::SymListGraph::opposite() "opposite()" member function.
1.439 + ///
1.440 + ///Here erase(Edge) deletes a pair of edges.
1.441 + ///
1.442 + ///\todo this date structure need some reconsiderations. Maybe it
1.443 + ///should be implemented independently from ListGraph.
1.444 +
1.445 + class SymListGraph : public ListGraph
1.446 + {
1.447 + public:
1.448 +
1.449 + typedef SymListGraph Graph;
1.450 +
1.451 + // Create symmetric map registry.
1.452 + CREATE_SYM_EDGE_MAP_REGISTRY;
1.453 + // Create symmetric edge map.
1.454 + CREATE_SYM_EDGE_MAP(ArrayMap);
1.455 +
1.456 + SymListGraph() : ListGraph() { }
1.457 + SymListGraph(const ListGraph &_g) : ListGraph(_g) { }
1.458 + ///Adds a pair of oppositely directed edges to the graph.
1.459 + Edge addEdge(Node u, Node v)
1.460 + {
1.461 + Edge e = ListGraph::addEdge(u,v);
1.462 + Edge f = ListGraph::addEdge(v,u);
1.463 + sym_edge_maps.add(e);
1.464 + sym_edge_maps.add(f);
1.465 +
1.466 + return e;
1.467 + }
1.468 +
1.469 + void erase(Node n) { ListGraph::erase(n);}
1.470 + ///The oppositely directed edge.
1.471 +
1.472 + ///Returns the oppositely directed
1.473 + ///pair of the edge \c e.
1.474 + static Edge opposite(Edge e)
1.475 + {
1.476 + Edge f;
1.477 + f.n = e.n - 2*(e.n%2) + 1;
1.478 + return f;
1.479 + }
1.480 +
1.481 + ///Removes a pair of oppositely directed edges to the graph.
1.482 + void erase(Edge e) {
1.483 + Edge f = opposite(e);
1.484 + sym_edge_maps.erase(e);
1.485 + sym_edge_maps.erase(f);
1.486 + ListGraph::erase(f);
1.487 + ListGraph::erase(e);
1.488 + }
1.489 + };
1.490 +
1.491 +
1.492 + ///A graph class containing only nodes.
1.493 +
1.494 + ///This class implements a graph structure without edges.
1.495 + ///The most useful application of this class is to be the node set of an
1.496 + ///\ref EdgeSet class.
1.497 + ///
1.498 + ///It conforms to
1.499 + ///the \ref skeleton::ExtendableGraph "ExtendableGraph" concept
1.500 + ///with the exception that you cannot
1.501 + ///add (or delete) edges. The usual edge iterators are exists, but they are
1.502 + ///always \ref INVALID.
1.503 + ///\sa skeleton::ExtendableGraph
1.504 + ///\sa EdgeSet
1.505 + class NodeSet {
1.506 +
1.507 + //Nodes are double linked.
1.508 + //The free nodes are only single linked using the "next" field.
1.509 + struct NodeT
1.510 + {
1.511 + int first_in,first_out;
1.512 + int prev, next;
1.513 + // NodeT() {}
1.514 + };
1.515 +
1.516 + std::vector<NodeT> nodes;
1.517 + //The first node
1.518 + int first_node;
1.519 + //The first free node
1.520 + int first_free_node;
1.521 +
1.522 + public:
1.523 +
1.524 + typedef NodeSet Graph;
1.525 +
1.526 + class Node;
1.527 + class Edge;
1.528 +
1.529 + public:
1.530 +
1.531 + class NodeIt;
1.532 + class EdgeIt;
1.533 + class OutEdgeIt;
1.534 + class InEdgeIt;
1.535 +
1.536 + // Create node map registry.
1.537 + CREATE_NODE_MAP_REGISTRY;
1.538 + // Create node maps.
1.539 + CREATE_NODE_MAP(ArrayMap);
1.540 +
1.541 + /// Creating empty map structure for edges.
1.542 + template <typename Value>
1.543 + class EdgeMap {
1.544 + public:
1.545 + EdgeMap(const Graph&) {}
1.546 + EdgeMap(const Graph&, const Value&) {}
1.547 +
1.548 + EdgeMap(const EdgeMap&) {}
1.549 + template <typename CMap> EdgeMap(const CMap&) {}
1.550 +
1.551 + EdgeMap& operator=(const EdgeMap&) {}
1.552 + template <typename CMap> EdgeMap& operator=(const CMap&) {}
1.553 +
1.554 + class ConstIterator {
1.555 + public:
1.556 + bool operator==(const ConstIterator&) {return true;}
1.557 + bool operator!=(const ConstIterator&) {return false;}
1.558 + };
1.559 +
1.560 + typedef ConstIterator Iterator;
1.561 +
1.562 + Iterator begin() { return Iterator();}
1.563 + Iterator end() { return Iterator();}
1.564 +
1.565 + ConstIterator begin() const { return ConstIterator();}
1.566 + ConstIterator end() const { return ConstIterator();}
1.567 +
1.568 + };
1.569 +
1.570 + public:
1.571 +
1.572 + ///Default constructor
1.573 + NodeSet()
1.574 + : nodes(), first_node(-1), first_free_node(-1) {}
1.575 + ///Copy constructor
1.576 + NodeSet(const NodeSet &_g)
1.577 + : nodes(_g.nodes), first_node(_g.first_node),
1.578 + first_free_node(_g.first_free_node) {}
1.579 +
1.580 + ///Number of nodes.
1.581 + int nodeNum() const { return nodes.size(); }
1.582 + ///Number of edges.
1.583 + int edgeNum() const { return 0; }
1.584 +
1.585 + /// Maximum node ID.
1.586 +
1.587 + /// Maximum node ID.
1.588 + ///\sa id(Node)
1.589 + int maxNodeId() const { return nodes.size()-1; }
1.590 + /// Maximum edge ID.
1.591 +
1.592 + /// Maximum edge ID.
1.593 + ///\sa id(Edge)
1.594 + int maxEdgeId() const { return 0; }
1.595 +
1.596 + Node tail(Edge e) const { return INVALID; }
1.597 + Node head(Edge e) const { return INVALID; }
1.598 +
1.599 + NodeIt& first(NodeIt& v) const {
1.600 + v=NodeIt(*this); return v; }
1.601 + EdgeIt& first(EdgeIt& e) const {
1.602 + e=EdgeIt(*this); return e; }
1.603 + OutEdgeIt& first(OutEdgeIt& e, const Node v) const {
1.604 + e=OutEdgeIt(*this,v); return e; }
1.605 + InEdgeIt& first(InEdgeIt& e, const Node v) const {
1.606 + e=InEdgeIt(*this,v); return e; }
1.607 +
1.608 + /// Node ID.
1.609 +
1.610 + /// The ID of a valid Node is a nonnegative integer not greater than
1.611 + /// \ref maxNodeId(). The range of the ID's is not surely continuous
1.612 + /// and the greatest node ID can be actually less then \ref maxNodeId().
1.613 + ///
1.614 + /// The ID of the \ref INVALID node is -1.
1.615 + ///\return The ID of the node \c v.
1.616 + static int id(Node v) { return v.n; }
1.617 + /// Edge ID.
1.618 +
1.619 + /// The ID of a valid Edge is a nonnegative integer not greater than
1.620 + /// \ref maxEdgeId(). The range of the ID's is not surely continuous
1.621 + /// and the greatest edge ID can be actually less then \ref maxEdgeId().
1.622 + ///
1.623 + /// The ID of the \ref INVALID edge is -1.
1.624 + ///\return The ID of the edge \c e.
1.625 + static int id(Edge e) { return -1; }
1.626 +
1.627 + /// Adds a new node to the graph.
1.628 +
1.629 + /// \warning It adds the new node to the front of the list.
1.630 + /// (i.e. the lastly added node becomes the first.)
1.631 + Node addNode() {
1.632 + int n;
1.633 +
1.634 + if(first_free_node==-1)
1.635 + {
1.636 + n = nodes.size();
1.637 + nodes.push_back(NodeT());
1.638 + }
1.639 + else {
1.640 + n = first_free_node;
1.641 + first_free_node = nodes[n].next;
1.642 + }
1.643 +
1.644 + nodes[n].next = first_node;
1.645 + if(first_node != -1) nodes[first_node].prev = n;
1.646 + first_node = n;
1.647 + nodes[n].prev = -1;
1.648 +
1.649 + nodes[n].first_in = nodes[n].first_out = -1;
1.650 +
1.651 + Node nn; nn.n=n;
1.652 +
1.653 + //Update dynamic maps
1.654 + node_maps.add(nn);
1.655 +
1.656 + return nn;
1.657 + }
1.658 +
1.659 + void erase(Node nn) {
1.660 + int n=nn.n;
1.661 +
1.662 + if(nodes[n].next != -1) nodes[nodes[n].next].prev = nodes[n].prev;
1.663 + if(nodes[n].prev != -1) nodes[nodes[n].prev].next = nodes[n].next;
1.664 + else first_node = nodes[n].next;
1.665 +
1.666 + nodes[n].next = first_free_node;
1.667 + first_free_node = n;
1.668 +
1.669 + //Update dynamic maps
1.670 + node_maps.erase(nn);
1.671 + }
1.672 +
1.673 +
1.674 + Edge findEdge(Node u,Node v, Edge prev = INVALID)
1.675 + {
1.676 + return INVALID;
1.677 + }
1.678 +
1.679 + void clear() {
1.680 + node_maps.clear();
1.681 + nodes.clear();
1.682 + first_node = first_free_node = -1;
1.683 + }
1.684 +
1.685 + class Node {
1.686 + friend class NodeSet;
1.687 + template <typename T> friend class NodeMap;
1.688 +
1.689 + friend class Edge;
1.690 + friend class OutEdgeIt;
1.691 + friend class InEdgeIt;
1.692 +
1.693 + protected:
1.694 + int n;
1.695 + friend int NodeSet::id(Node v);
1.696 + Node(int nn) {n=nn;}
1.697 + public:
1.698 + Node() {}
1.699 + Node (Invalid i) { n=-1; }
1.700 + bool operator==(const Node i) const {return n==i.n;}
1.701 + bool operator!=(const Node i) const {return n!=i.n;}
1.702 + bool operator<(const Node i) const {return n<i.n;}
1.703 + };
1.704 +
1.705 + class NodeIt : public Node {
1.706 + const NodeSet *G;
1.707 + friend class NodeSet;
1.708 + public:
1.709 + NodeIt() : Node() { }
1.710 + NodeIt(const NodeSet& _G,Node n) : Node(n), G(&_G) { }
1.711 + NodeIt(Invalid i) : Node(i) { }
1.712 + NodeIt(const NodeSet& _G) : Node(_G.first_node), G(&_G) { }
1.713 + NodeIt &operator++() {
1.714 + n=G->nodes[n].next;
1.715 + return *this;
1.716 + }
1.717 + };
1.718 +
1.719 + class Edge {
1.720 + public:
1.721 + Edge() { }
1.722 + Edge (Invalid) { }
1.723 + bool operator==(const Edge i) const {return true;}
1.724 + bool operator!=(const Edge i) const {return false;}
1.725 + bool operator<(const Edge i) const {return false;}
1.726 + };
1.727 +
1.728 + class EdgeIt : public Edge {
1.729 + public:
1.730 + EdgeIt(const NodeSet& G) : Edge() { }
1.731 + EdgeIt(const NodeSet&, Edge) : Edge() { }
1.732 + EdgeIt (Invalid i) : Edge(i) { }
1.733 + EdgeIt() : Edge() { }
1.734 + EdgeIt operator++() { return INVALID; }
1.735 + };
1.736 +
1.737 + class OutEdgeIt : public Edge {
1.738 + friend class NodeSet;
1.739 + public:
1.740 + OutEdgeIt() : Edge() { }
1.741 + OutEdgeIt(const NodeSet&, Edge) : Edge() { }
1.742 + OutEdgeIt (Invalid i) : Edge(i) { }
1.743 + OutEdgeIt(const NodeSet& G,const Node v) : Edge() {}
1.744 + OutEdgeIt operator++() { return INVALID; }
1.745 + };
1.746 +
1.747 + class InEdgeIt : public Edge {
1.748 + friend class NodeSet;
1.749 + public:
1.750 + InEdgeIt() : Edge() { }
1.751 + InEdgeIt(const NodeSet&, Edge) : Edge() { }
1.752 + InEdgeIt (Invalid i) : Edge(i) { }
1.753 + InEdgeIt(const NodeSet& G,Node v) :Edge() {}
1.754 + InEdgeIt operator++() { return INVALID; }
1.755 + };
1.756 +
1.757 + };
1.758 +
1.759 +
1.760 +
1.761 + ///Graph structure using a node set of another graph.
1.762 +
1.763 + ///This structure can be used to establish another graph over a node set
1.764 + /// of an existing one. The node iterator will go through the nodes of the
1.765 + /// original graph, and the NodeMap's of both graphs will convert to
1.766 + /// each other.
1.767 + ///
1.768 + ///\warning Adding or deleting nodes from the graph is not safe if an
1.769 + ///\ref EdgeSet is currently attached to it!
1.770 + ///
1.771 + ///\todo Make it possible to add/delete edges from the base graph
1.772 + ///(and from \ref EdgeSet, as well)
1.773 + ///
1.774 + ///\param GG The type of the graph which shares its node set with this class.
1.775 + ///Its interface must conform to the
1.776 + ///\ref skeleton::StaticGraph "StaticGraph" concept.
1.777 + ///
1.778 + ///It conforms to the
1.779 + ///\ref skeleton::ExtendableGraph "ExtendableGraph" concept.
1.780 + ///\sa skeleton::ExtendableGraph.
1.781 + ///\sa NodeSet.
1.782 + template<typename GG>
1.783 + class EdgeSet {
1.784 +
1.785 + typedef GG NodeGraphType;
1.786 +
1.787 + NodeGraphType &G;
1.788 +
1.789 + public:
1.790 +
1.791 + class Node;
1.792 + class Edge;
1.793 + class OutEdgeIt;
1.794 + class InEdgeIt;
1.795 + class SymEdge;
1.796 +
1.797 + typedef EdgeSet Graph;
1.798 +
1.799 + int id(Node v) const;
1.800 +
1.801 + class Node : public NodeGraphType::Node {
1.802 + friend class EdgeSet;
1.803 +
1.804 + friend class Edge;
1.805 + friend class OutEdgeIt;
1.806 + friend class InEdgeIt;
1.807 + friend class SymEdge;
1.808 +
1.809 + public:
1.810 + friend int EdgeSet::id(Node v) const;
1.811 + public:
1.812 + Node() : NodeGraphType::Node() {}
1.813 + Node (Invalid i) : NodeGraphType::Node(i) {}
1.814 + Node(const typename NodeGraphType::Node &n) : NodeGraphType::Node(n) {}
1.815 + };
1.816 +
1.817 + class NodeIt : public NodeGraphType::NodeIt {
1.818 + friend class EdgeSet;
1.819 + public:
1.820 + NodeIt() : NodeGraphType::NodeIt() { }
1.821 + NodeIt(const EdgeSet& _G,Node n) : NodeGraphType::NodeIt(_G.G,n) { }
1.822 + NodeIt (Invalid i) : NodeGraphType::NodeIt(i) {}
1.823 + NodeIt(const EdgeSet& _G) : NodeGraphType::NodeIt(_G.G) { }
1.824 + NodeIt(const typename NodeGraphType::NodeIt &n)
1.825 + : NodeGraphType::NodeIt(n) {}
1.826 +
1.827 + operator Node() { return Node(*this);}
1.828 + NodeIt &operator++()
1.829 + { this->NodeGraphType::NodeIt::operator++(); return *this;}
1.830 + };
1.831 +
1.832 + private:
1.833 + //Edges are double linked.
1.834 + //The free edges are only single linked using the "next_in" field.
1.835 + struct NodeT
1.836 + {
1.837 + int first_in,first_out;
1.838 + NodeT() : first_in(-1), first_out(-1) { }
1.839 + };
1.840 +
1.841 + struct EdgeT
1.842 + {
1.843 + Node head, tail;
1.844 + int prev_in, prev_out;
1.845 + int next_in, next_out;
1.846 + };
1.847 +
1.848 +
1.849 + typename NodeGraphType::template NodeMap<NodeT> nodes;
1.850 +
1.851 + std::vector<EdgeT> edges;
1.852 + //The first free edge
1.853 + int first_free_edge;
1.854 +
1.855 + public:
1.856 +
1.857 + class Node;
1.858 + class Edge;
1.859 +
1.860 + class NodeIt;
1.861 + class EdgeIt;
1.862 + class OutEdgeIt;
1.863 + class InEdgeIt;
1.864 +
1.865 +
1.866 + // Create edge map registry.
1.867 + CREATE_EDGE_MAP_REGISTRY;
1.868 + // Create edge maps.
1.869 + CREATE_EDGE_MAP(ArrayMap);
1.870 +
1.871 + // Import node maps from the NodeGraphType.
1.872 + IMPORT_NODE_MAP(NodeGraphType, graph.G, EdgeSet, graph);
1.873 +
1.874 +
1.875 + public:
1.876 +
1.877 + ///Constructor
1.878 +
1.879 + ///Construates a new graph based on the nodeset of an existing one.
1.880 + ///\param _G the base graph.
1.881 + explicit EdgeSet(NodeGraphType &_G)
1.882 + : G(_G), nodes(_G), edges(),
1.883 + first_free_edge(-1) {}
1.884 + ///Copy constructor
1.885 +
1.886 + ///Makes a copy of an EdgeSet.
1.887 + ///It will be based on the same graph.
1.888 + explicit EdgeSet(const EdgeSet &_g)
1.889 + : G(_g.G), nodes(_g.G), edges(_g.edges),
1.890 + first_free_edge(_g.first_free_edge) {}
1.891 +
1.892 + ///Number of nodes.
1.893 + int nodeNum() const { return G.nodeNum(); }
1.894 + ///Number of edges.
1.895 + int edgeNum() const { return edges.size(); }
1.896 +
1.897 + /// Maximum node ID.
1.898 +
1.899 + /// Maximum node ID.
1.900 + ///\sa id(Node)
1.901 + int maxNodeId() const { return G.maxNodeId(); }
1.902 + /// Maximum edge ID.
1.903 +
1.904 + /// Maximum edge ID.
1.905 + ///\sa id(Edge)
1.906 + int maxEdgeId() const { return edges.size()-1; }
1.907 +
1.908 + Node tail(Edge e) const { return edges[e.n].tail; }
1.909 + Node head(Edge e) const { return edges[e.n].head; }
1.910 +
1.911 + NodeIt& first(NodeIt& v) const {
1.912 + v=NodeIt(*this); return v; }
1.913 + EdgeIt& first(EdgeIt& e) const {
1.914 + e=EdgeIt(*this); return e; }
1.915 + OutEdgeIt& first(OutEdgeIt& e, const Node v) const {
1.916 + e=OutEdgeIt(*this,v); return e; }
1.917 + InEdgeIt& first(InEdgeIt& e, const Node v) const {
1.918 + e=InEdgeIt(*this,v); return e; }
1.919 +
1.920 + /// Node ID.
1.921 +
1.922 + /// The ID of a valid Node is a nonnegative integer not greater than
1.923 + /// \ref maxNodeId(). The range of the ID's is not surely continuous
1.924 + /// and the greatest node ID can be actually less then \ref maxNodeId().
1.925 + ///
1.926 + /// The ID of the \ref INVALID node is -1.
1.927 + ///\return The ID of the node \c v.
1.928 + int id(Node v) { return G.id(v); }
1.929 + /// Edge ID.
1.930 +
1.931 + /// The ID of a valid Edge is a nonnegative integer not greater than
1.932 + /// \ref maxEdgeId(). The range of the ID's is not surely continuous
1.933 + /// and the greatest edge ID can be actually less then \ref maxEdgeId().
1.934 + ///
1.935 + /// The ID of the \ref INVALID edge is -1.
1.936 + ///\return The ID of the edge \c e.
1.937 + static int id(Edge e) { return e.n; }
1.938 +
1.939 + /// Adds a new node to the graph.
1.940 + Node addNode() { return G.addNode(); }
1.941 +
1.942 + Edge addEdge(Node u, Node v) {
1.943 + int n;
1.944 +
1.945 + if(first_free_edge==-1)
1.946 + {
1.947 + n = edges.size();
1.948 + edges.push_back(EdgeT());
1.949 + }
1.950 + else {
1.951 + n = first_free_edge;
1.952 + first_free_edge = edges[n].next_in;
1.953 + }
1.954 +
1.955 + edges[n].tail = u; edges[n].head = v;
1.956 +
1.957 + edges[n].next_out = nodes[u].first_out;
1.958 + if(nodes[u].first_out != -1) edges[nodes[u].first_out].prev_out = n;
1.959 + edges[n].next_in = nodes[v].first_in;
1.960 + if(nodes[v].first_in != -1) edges[nodes[v].first_in].prev_in = n;
1.961 + edges[n].prev_in = edges[n].prev_out = -1;
1.962 +
1.963 + nodes[u].first_out = nodes[v].first_in = n;
1.964 +
1.965 + Edge e; e.n=n;
1.966 +
1.967 + //Update dynamic maps
1.968 + edge_maps.add(e);
1.969 +
1.970 + return e;
1.971 + }
1.972 +
1.973 + /// Finds an edge between two nodes.
1.974 +
1.975 + /// Finds an edge from node \c u to node \c v.
1.976 + ///
1.977 + /// If \c prev is \ref INVALID (this is the default value), then
1.978 + /// It finds the first edge from \c u to \c v. Otherwise it looks for
1.979 + /// the next edge from \c u to \c v after \c prev.
1.980 + /// \return The found edge or INVALID if there is no such an edge.
1.981 + Edge findEdge(Node u,Node v, Edge prev = INVALID)
1.982 + {
1.983 + int e = (prev.n==-1)? nodes[u].first_out : edges[prev.n].next_out;
1.984 + while(e!=-1 && edges[e].tail!=v) e = edges[e].next_out;
1.985 + prev.n=e;
1.986 + return prev;
1.987 + }
1.988 +
1.989 + private:
1.990 + void eraseEdge(int n) {
1.991 +
1.992 + if(edges[n].next_in!=-1)
1.993 + edges[edges[n].next_in].prev_in = edges[n].prev_in;
1.994 + if(edges[n].prev_in!=-1)
1.995 + edges[edges[n].prev_in].next_in = edges[n].next_in;
1.996 + else nodes[edges[n].head].first_in = edges[n].next_in;
1.997 +
1.998 + if(edges[n].next_out!=-1)
1.999 + edges[edges[n].next_out].prev_out = edges[n].prev_out;
1.1000 + if(edges[n].prev_out!=-1)
1.1001 + edges[edges[n].prev_out].next_out = edges[n].next_out;
1.1002 + else nodes[edges[n].tail].first_out = edges[n].next_out;
1.1003 +
1.1004 + edges[n].next_in = first_free_edge;
1.1005 + first_free_edge = -1;
1.1006 +
1.1007 + //Update dynamic maps
1.1008 + Edge e; e.n = n;
1.1009 + edge_maps.erase(e);
1.1010 + }
1.1011 +
1.1012 + public:
1.1013 +
1.1014 + void erase(Edge e) { eraseEdge(e.n); }
1.1015 +
1.1016 + ///Clear all edges. (Doesn't clear the nodes!)
1.1017 + void clear() {
1.1018 + edge_maps.clear();
1.1019 + edges.clear();
1.1020 + first_free_edge=-1;
1.1021 + }
1.1022 +
1.1023 +
1.1024 + class Edge {
1.1025 + public:
1.1026 + friend class EdgeSet;
1.1027 + template <typename T> friend class EdgeMap;
1.1028 +
1.1029 + friend class Node;
1.1030 + friend class NodeIt;
1.1031 + protected:
1.1032 + int n;
1.1033 + friend int EdgeSet::id(Edge e) const;
1.1034 +
1.1035 + Edge(int nn) {n=nn;}
1.1036 + public:
1.1037 + Edge() { }
1.1038 + Edge (Invalid) { n=-1; }
1.1039 + bool operator==(const Edge i) const {return n==i.n;}
1.1040 + bool operator!=(const Edge i) const {return n!=i.n;}
1.1041 + bool operator<(const Edge i) const {return n<i.n;}
1.1042 + };
1.1043 +
1.1044 + class EdgeIt : public Edge {
1.1045 + friend class EdgeSet;
1.1046 + template <typename T> friend class EdgeMap;
1.1047 +
1.1048 + const EdgeSet *G;
1.1049 + public:
1.1050 + EdgeIt(const EdgeSet& _G) : Edge(), G(&_G) {
1.1051 + NodeIt m;
1.1052 + for(G->first(m);
1.1053 + m!=INVALID && G->nodes[m].first_in == -1; ++m);
1.1054 + ///\bug AJJAJ! This is a non sense!!!!!!!
1.1055 + this->n = m!=INVALID?-1:G->nodes[m].first_in;
1.1056 + }
1.1057 + EdgeIt(const EdgeSet& _G, Edge e) : Edge(e), G(&_G) { }
1.1058 + EdgeIt (Invalid i) : Edge(i) { }
1.1059 + EdgeIt() : Edge() { }
1.1060 + ///.
1.1061 +
1.1062 + ///\bug UNIMPLEMENTED!!!!!
1.1063 + //
1.1064 + EdgeIt &operator++() {
1.1065 + return *this;
1.1066 + }
1.1067 + };
1.1068 +
1.1069 + class OutEdgeIt : public Edge {
1.1070 + const EdgeSet *G;
1.1071 + friend class EdgeSet;
1.1072 + public:
1.1073 + OutEdgeIt() : Edge() { }
1.1074 + OutEdgeIt (Invalid i) : Edge(i) { }
1.1075 + OutEdgeIt(const EdgeSet& _G, Edge e) : Edge(e), G(&_G) { }
1.1076 +
1.1077 + OutEdgeIt(const EdgeSet& _G,const Node v) :
1.1078 + Edge(_G.nodes[v].first_out), G(&_G) { }
1.1079 + OutEdgeIt &operator++() {
1.1080 + Edge::n = G->edges[Edge::n].next_out;
1.1081 + return *this;
1.1082 + }
1.1083 + };
1.1084 +
1.1085 + class InEdgeIt : public Edge {
1.1086 + const EdgeSet *G;
1.1087 + friend class EdgeSet;
1.1088 + public:
1.1089 + InEdgeIt() : Edge() { }
1.1090 + InEdgeIt (Invalid i) : Edge(i) { }
1.1091 + InEdgeIt(const EdgeSet& _G, Edge e) : Edge(e), G(&_G) { }
1.1092 + InEdgeIt(const EdgeSet& _G,Node v)
1.1093 + : Edge(_G.nodes[v].first_in), G(&_G) { }
1.1094 + InEdgeIt &operator++() {
1.1095 + Edge::n = G->edges[Edge::n].next_in;
1.1096 + return *this;
1.1097 + }
1.1098 + };
1.1099 +
1.1100 + };
1.1101 +
1.1102 + template<typename GG>
1.1103 + inline int EdgeSet<GG>::id(Node v) const { return G.id(v); }
1.1104 +
1.1105 +/// @}
1.1106 +
1.1107 +} //namespace lemon
1.1108 +
1.1109 +#endif //LEMON_LIST_GRAPH_H