| 
alpar@948
 | 
     1  | 
/* -*- C++ -*-
  | 
| 
ladanyi@1435
 | 
     2  | 
 * lemon/list_graph.h - Part of LEMON, a generic C++ optimization library
  | 
| 
alpar@948
 | 
     3  | 
 *
  | 
| 
alpar@1164
 | 
     4  | 
 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
  | 
| 
alpar@1359
 | 
     5  | 
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
  | 
| 
alpar@948
 | 
     6  | 
 *
  | 
| 
alpar@948
 | 
     7  | 
 * Permission to use, modify and distribute this software is granted
  | 
| 
alpar@948
 | 
     8  | 
 * provided that this copyright notice appears in all copies. For
  | 
| 
alpar@948
 | 
     9  | 
 * precise terms see the accompanying LICENSE file.
  | 
| 
alpar@948
 | 
    10  | 
 *
  | 
| 
alpar@948
 | 
    11  | 
 * This software is provided "AS IS" with no warranty of any kind,
  | 
| 
alpar@948
 | 
    12  | 
 * express or implied, and with no claim as to its suitability for any
  | 
| 
alpar@948
 | 
    13  | 
 * purpose.
  | 
| 
alpar@948
 | 
    14  | 
 *
  | 
| 
alpar@948
 | 
    15  | 
 */
  | 
| 
alpar@395
 | 
    16  | 
  | 
| 
alpar@921
 | 
    17  | 
#ifndef LEMON_LIST_GRAPH_H
  | 
| 
alpar@921
 | 
    18  | 
#define LEMON_LIST_GRAPH_H
  | 
| 
alpar@395
 | 
    19  | 
  | 
| 
alpar@948
 | 
    20  | 
///\ingroup graphs
  | 
| 
alpar@948
 | 
    21  | 
///\file
  | 
| 
deba@1334
 | 
    22  | 
///\brief ListGraph, SymListGraph classes.
  | 
| 
alpar@948
 | 
    23  | 
  | 
| 
deba@1307
 | 
    24  | 
#include <lemon/bits/erasable_graph_extender.h>
  | 
| 
deba@1307
 | 
    25  | 
#include <lemon/bits/clearable_graph_extender.h>
  | 
| 
deba@1307
 | 
    26  | 
#include <lemon/bits/extendable_graph_extender.h>
  | 
| 
deba@1307
 | 
    27  | 
#include <lemon/bits/iterable_graph_extender.h>
  | 
| 
deba@1307
 | 
    28  | 
#include <lemon/bits/alteration_notifier.h>
  | 
| 
deba@1307
 | 
    29  | 
#include <lemon/bits/default_map.h>
  | 
| 
alpar@395
 | 
    30  | 
  | 
| 
deba@1307
 | 
    31  | 
#include <lemon/bits/undir_graph_extender.h>
  | 
| 
deba@782
 | 
    32  | 
  | 
| 
alpar@1011
 | 
    33  | 
#include <list>
  | 
| 
deba@782
 | 
    34  | 
  | 
| 
alpar@921
 | 
    35  | 
namespace lemon {
 | 
| 
alpar@395
 | 
    36  | 
  | 
| 
klao@946
 | 
    37  | 
  class ListGraphBase {
 | 
| 
alpar@406
 | 
    38  | 
  | 
| 
alpar@949
 | 
    39  | 
  protected:
  | 
| 
klao@946
 | 
    40  | 
    struct NodeT {
 | 
| 
deba@1470
 | 
    41  | 
      int first_in, first_out;
  | 
| 
alpar@397
 | 
    42  | 
      int prev, next;
  | 
| 
alpar@395
 | 
    43  | 
    };
  | 
| 
klao@946
 | 
    44  | 
 
  | 
| 
klao@946
 | 
    45  | 
    struct EdgeT {
 | 
| 
alpar@986
 | 
    46  | 
      int target, source;
  | 
| 
alpar@397
 | 
    47  | 
      int prev_in, prev_out;
  | 
| 
alpar@397
 | 
    48  | 
      int next_in, next_out;
  | 
| 
alpar@395
 | 
    49  | 
    };
  | 
| 
alpar@395
 | 
    50  | 
  | 
| 
alpar@395
 | 
    51  | 
    std::vector<NodeT> nodes;
  | 
| 
klao@946
 | 
    52  | 
  | 
| 
alpar@397
 | 
    53  | 
    int first_node;
  | 
| 
klao@946
 | 
    54  | 
  | 
| 
alpar@397
 | 
    55  | 
    int first_free_node;
  | 
| 
klao@946
 | 
    56  | 
  | 
| 
alpar@395
 | 
    57  | 
    std::vector<EdgeT> edges;
  | 
| 
klao@946
 | 
    58  | 
  | 
| 
alpar@397
 | 
    59  | 
    int first_free_edge;
  | 
| 
alpar@395
 | 
    60  | 
    
  | 
| 
deba@782
 | 
    61  | 
  public:
  | 
| 
alpar@395
 | 
    62  | 
    
  | 
| 
klao@946
 | 
    63  | 
    typedef ListGraphBase Graph;
  | 
| 
alpar@397
 | 
    64  | 
    
  | 
| 
klao@946
 | 
    65  | 
    class Node {
 | 
| 
marci@975
 | 
    66  | 
      friend class ListGraphBase;
  | 
| 
klao@946
 | 
    67  | 
    protected:
  | 
| 
alpar@395
 | 
    68  | 
  | 
| 
klao@946
 | 
    69  | 
      int id;
  | 
| 
klao@946
 | 
    70  | 
      Node(int pid) { id = pid;}
 | 
| 
alpar@395
 | 
    71  | 
  | 
| 
klao@946
 | 
    72  | 
    public:
  | 
| 
klao@946
 | 
    73  | 
      Node() {}
 | 
| 
klao@946
 | 
    74  | 
      Node (Invalid) { id = -1; }
 | 
| 
klao@946
 | 
    75  | 
      bool operator==(const Node& node) const {return id == node.id;}
 | 
| 
klao@946
 | 
    76  | 
      bool operator!=(const Node& node) const {return id != node.id;}
 | 
| 
klao@946
 | 
    77  | 
      bool operator<(const Node& node) const {return id < node.id;}
 | 
| 
klao@946
 | 
    78  | 
    };
  | 
| 
deba@782
 | 
    79  | 
  | 
| 
klao@946
 | 
    80  | 
    class Edge {
 | 
| 
marci@975
 | 
    81  | 
      friend class ListGraphBase;
  | 
| 
klao@946
 | 
    82  | 
    protected:
  | 
| 
deba@782
 | 
    83  | 
  | 
| 
klao@946
 | 
    84  | 
      int id;
  | 
| 
klao@946
 | 
    85  | 
      Edge(int pid) { id = pid;}
 | 
| 
alpar@395
 | 
    86  | 
  | 
| 
klao@946
 | 
    87  | 
    public:
  | 
| 
klao@946
 | 
    88  | 
      Edge() {}
 | 
| 
klao@946
 | 
    89  | 
      Edge (Invalid) { id = -1; }
 | 
| 
klao@946
 | 
    90  | 
      bool operator==(const Edge& edge) const {return id == edge.id;}
 | 
| 
klao@946
 | 
    91  | 
      bool operator!=(const Edge& edge) const {return id != edge.id;}
 | 
| 
klao@946
 | 
    92  | 
      bool operator<(const Edge& edge) const {return id < edge.id;}
 | 
| 
klao@946
 | 
    93  | 
    };
  | 
| 
klao@946
 | 
    94  | 
  | 
| 
klao@946
 | 
    95  | 
  | 
| 
klao@946
 | 
    96  | 
  | 
| 
klao@946
 | 
    97  | 
    ListGraphBase()
  | 
| 
deba@782
 | 
    98  | 
      : nodes(), first_node(-1),
  | 
| 
deba@782
 | 
    99  | 
	first_free_node(-1), edges(), first_free_edge(-1) {}
 | 
| 
deba@782
 | 
   100  | 
  | 
| 
alpar@395
 | 
   101  | 
    
  | 
| 
alpar@813
 | 
   102  | 
    /// Maximum node ID.
  | 
| 
alpar@813
 | 
   103  | 
    
  | 
| 
alpar@813
 | 
   104  | 
    /// Maximum node ID.
  | 
| 
alpar@813
 | 
   105  | 
    ///\sa id(Node)
  | 
| 
deba@980
 | 
   106  | 
    int maxId(Node = INVALID) const { return nodes.size()-1; } 
 | 
| 
klao@946
 | 
   107  | 
  | 
| 
alpar@813
 | 
   108  | 
    /// Maximum edge ID.
  | 
| 
alpar@813
 | 
   109  | 
    
  | 
| 
alpar@813
 | 
   110  | 
    /// Maximum edge ID.
  | 
| 
alpar@813
 | 
   111  | 
    ///\sa id(Edge)
  | 
| 
deba@980
 | 
   112  | 
    int maxId(Edge = INVALID) const { return edges.size()-1; }
 | 
| 
alpar@395
 | 
   113  | 
  | 
| 
alpar@986
 | 
   114  | 
    Node source(Edge e) const { return edges[e.id].source; }
 | 
| 
alpar@986
 | 
   115  | 
    Node target(Edge e) const { return edges[e.id].target; }
 | 
| 
alpar@395
 | 
   116  | 
  | 
| 
alpar@395
 | 
   117  | 
  | 
| 
klao@946
 | 
   118  | 
    void first(Node& node) const { 
 | 
| 
klao@946
 | 
   119  | 
      node.id = first_node;
  | 
| 
klao@946
 | 
   120  | 
    }
  | 
| 
klao@946
 | 
   121  | 
  | 
| 
klao@946
 | 
   122  | 
    void next(Node& node) const {
 | 
| 
klao@946
 | 
   123  | 
      node.id = nodes[node.id].next;
  | 
| 
klao@946
 | 
   124  | 
    }
  | 
| 
klao@946
 | 
   125  | 
  | 
| 
klao@946
 | 
   126  | 
  | 
| 
klao@946
 | 
   127  | 
    void first(Edge& e) const { 
 | 
| 
klao@946
 | 
   128  | 
      int n;
  | 
| 
klao@946
 | 
   129  | 
      for(n = first_node; 
  | 
| 
klao@946
 | 
   130  | 
	  n!=-1 && nodes[n].first_in == -1; 
  | 
| 
klao@946
 | 
   131  | 
	  n = nodes[n].next);
  | 
| 
klao@946
 | 
   132  | 
      e.id = (n == -1) ? -1 : nodes[n].first_in;
  | 
| 
klao@946
 | 
   133  | 
    }
  | 
| 
klao@946
 | 
   134  | 
  | 
| 
klao@946
 | 
   135  | 
    void next(Edge& edge) const {
 | 
| 
klao@946
 | 
   136  | 
      if (edges[edge.id].next_in != -1) {
 | 
| 
klao@946
 | 
   137  | 
	edge.id = edges[edge.id].next_in;
  | 
| 
klao@946
 | 
   138  | 
      } else {
 | 
| 
klao@946
 | 
   139  | 
	int n;
  | 
| 
alpar@986
 | 
   140  | 
	for(n = nodes[edges[edge.id].target].next;
  | 
| 
klao@946
 | 
   141  | 
	  n!=-1 && nodes[n].first_in == -1; 
  | 
| 
klao@946
 | 
   142  | 
	  n = nodes[n].next);
  | 
| 
klao@946
 | 
   143  | 
	edge.id = (n == -1) ? -1 : nodes[n].first_in;
  | 
| 
klao@946
 | 
   144  | 
      }      
  | 
| 
klao@946
 | 
   145  | 
    }
  | 
| 
klao@946
 | 
   146  | 
  | 
| 
klao@946
 | 
   147  | 
    void firstOut(Edge &e, const Node& v) const {
 | 
| 
klao@946
 | 
   148  | 
      e.id = nodes[v.id].first_out;
  | 
| 
klao@946
 | 
   149  | 
    }
  | 
| 
klao@946
 | 
   150  | 
    void nextOut(Edge &e) const {
 | 
| 
klao@946
 | 
   151  | 
      e.id=edges[e.id].next_out;
  | 
| 
klao@946
 | 
   152  | 
    }
  | 
| 
klao@946
 | 
   153  | 
  | 
| 
klao@946
 | 
   154  | 
    void firstIn(Edge &e, const Node& v) const {
 | 
| 
klao@946
 | 
   155  | 
      e.id = nodes[v.id].first_in;
  | 
| 
klao@946
 | 
   156  | 
    }
  | 
| 
klao@946
 | 
   157  | 
    void nextIn(Edge &e) const {
 | 
| 
klao@946
 | 
   158  | 
      e.id=edges[e.id].next_in;
  | 
| 
klao@946
 | 
   159  | 
    }
  | 
| 
klao@946
 | 
   160  | 
  | 
| 
alpar@813
 | 
   161  | 
    
  | 
| 
klao@946
 | 
   162  | 
    static int id(Node v) { return v.id; }
 | 
| 
klao@946
 | 
   163  | 
    static int id(Edge e) { return e.id; }
 | 
| 
alpar@395
 | 
   164  | 
  | 
| 
deba@1106
 | 
   165  | 
    static Node fromId(int id, Node) { return Node(id);}
 | 
| 
deba@1106
 | 
   166  | 
    static Edge fromId(int id, Edge) { return Edge(id);}
 | 
| 
deba@1106
 | 
   167  | 
  | 
| 
alpar@397
 | 
   168  | 
    /// Adds a new node to the graph.
  | 
| 
alpar@397
 | 
   169  | 
  | 
| 
alpar@813
 | 
   170  | 
    /// \warning It adds the new node to the front of the list.
  | 
| 
alpar@397
 | 
   171  | 
    /// (i.e. the lastly added node becomes the first.)
  | 
| 
klao@946
 | 
   172  | 
    Node addNode() {     
 | 
| 
alpar@397
 | 
   173  | 
      int n;
  | 
| 
alpar@397
 | 
   174  | 
      
  | 
| 
klao@946
 | 
   175  | 
      if(first_free_node==-1) {
 | 
| 
klao@946
 | 
   176  | 
	n = nodes.size();
  | 
| 
klao@946
 | 
   177  | 
	nodes.push_back(NodeT());
  | 
| 
klao@946
 | 
   178  | 
      } else {
 | 
| 
alpar@397
 | 
   179  | 
	n = first_free_node;
  | 
| 
alpar@397
 | 
   180  | 
	first_free_node = nodes[n].next;
  | 
| 
alpar@397
 | 
   181  | 
      }
  | 
| 
alpar@397
 | 
   182  | 
      
  | 
| 
alpar@397
 | 
   183  | 
      nodes[n].next = first_node;
  | 
| 
alpar@397
 | 
   184  | 
      if(first_node != -1) nodes[first_node].prev = n;
  | 
| 
alpar@397
 | 
   185  | 
      first_node = n;
  | 
| 
alpar@397
 | 
   186  | 
      nodes[n].prev = -1;
  | 
| 
alpar@397
 | 
   187  | 
      
  | 
| 
alpar@397
 | 
   188  | 
      nodes[n].first_in = nodes[n].first_out = -1;
  | 
| 
alpar@397
 | 
   189  | 
      
  | 
| 
klao@946
 | 
   190  | 
      return Node(n);
  | 
| 
alpar@395
 | 
   191  | 
    }
  | 
| 
alpar@395
 | 
   192  | 
    
  | 
| 
alpar@395
 | 
   193  | 
    Edge addEdge(Node u, Node v) {
 | 
| 
klao@946
 | 
   194  | 
      int n;      
  | 
| 
klao@946
 | 
   195  | 
  | 
| 
klao@946
 | 
   196  | 
      if (first_free_edge == -1) {
 | 
| 
klao@946
 | 
   197  | 
	n = edges.size();
  | 
| 
klao@946
 | 
   198  | 
	edges.push_back(EdgeT());
  | 
| 
klao@946
 | 
   199  | 
      } else {
 | 
| 
alpar@397
 | 
   200  | 
	n = first_free_edge;
  | 
| 
alpar@397
 | 
   201  | 
	first_free_edge = edges[n].next_in;
  | 
| 
alpar@397
 | 
   202  | 
      }
  | 
| 
alpar@397
 | 
   203  | 
      
  | 
| 
alpar@986
 | 
   204  | 
      edges[n].source = u.id; 
  | 
| 
alpar@986
 | 
   205  | 
      edges[n].target = v.id;
  | 
| 
alpar@395
 | 
   206  | 
  | 
| 
klao@946
 | 
   207  | 
      edges[n].next_out = nodes[u.id].first_out;
  | 
| 
klao@946
 | 
   208  | 
      if(nodes[u.id].first_out != -1) {
 | 
| 
klao@946
 | 
   209  | 
	edges[nodes[u.id].first_out].prev_out = n;
  | 
| 
klao@946
 | 
   210  | 
      }
  | 
| 
klao@946
 | 
   211  | 
      
  | 
| 
klao@946
 | 
   212  | 
      edges[n].next_in = nodes[v.id].first_in;
  | 
| 
klao@946
 | 
   213  | 
      if(nodes[v.id].first_in != -1) {
 | 
| 
klao@946
 | 
   214  | 
	edges[nodes[v.id].first_in].prev_in = n;
  | 
| 
klao@946
 | 
   215  | 
      }
  | 
| 
klao@946
 | 
   216  | 
      
  | 
| 
alpar@397
 | 
   217  | 
      edges[n].prev_in = edges[n].prev_out = -1;
  | 
| 
alpar@397
 | 
   218  | 
	
  | 
| 
klao@946
 | 
   219  | 
      nodes[u.id].first_out = nodes[v.id].first_in = n;
  | 
| 
alpar@397
 | 
   220  | 
  | 
| 
klao@946
 | 
   221  | 
      return Edge(n);
  | 
| 
alpar@395
 | 
   222  | 
    }
  | 
| 
alpar@774
 | 
   223  | 
    
  | 
| 
klao@946
 | 
   224  | 
    void erase(const Node& node) {
 | 
| 
klao@946
 | 
   225  | 
      int n = node.id;
  | 
| 
klao@946
 | 
   226  | 
      
  | 
| 
klao@946
 | 
   227  | 
      if(nodes[n].next != -1) {
 | 
| 
klao@946
 | 
   228  | 
	nodes[nodes[n].next].prev = nodes[n].prev;
  | 
| 
klao@946
 | 
   229  | 
      }
  | 
| 
klao@946
 | 
   230  | 
      
  | 
| 
klao@946
 | 
   231  | 
      if(nodes[n].prev != -1) {
 | 
| 
klao@946
 | 
   232  | 
	nodes[nodes[n].prev].next = nodes[n].next;
  | 
| 
klao@946
 | 
   233  | 
      } else {
 | 
| 
klao@946
 | 
   234  | 
	first_node = nodes[n].next;
  | 
| 
klao@946
 | 
   235  | 
      }
  | 
| 
klao@946
 | 
   236  | 
      
  | 
| 
klao@946
 | 
   237  | 
      nodes[n].next = first_free_node;
  | 
| 
klao@946
 | 
   238  | 
      first_free_node = n;
  | 
| 
alpar@395
 | 
   239  | 
  | 
| 
alpar@774
 | 
   240  | 
    }
  | 
| 
alpar@774
 | 
   241  | 
    
  | 
| 
klao@946
 | 
   242  | 
    void erase(const Edge& edge) {
 | 
| 
klao@946
 | 
   243  | 
      int n = edge.id;
  | 
| 
alpar@397
 | 
   244  | 
      
  | 
| 
klao@946
 | 
   245  | 
      if(edges[n].next_in!=-1) {
 | 
| 
alpar@397
 | 
   246  | 
	edges[edges[n].next_in].prev_in = edges[n].prev_in;
  | 
| 
klao@946
 | 
   247  | 
      }
  | 
| 
klao@946
 | 
   248  | 
  | 
| 
klao@946
 | 
   249  | 
      if(edges[n].prev_in!=-1) {
 | 
| 
alpar@397
 | 
   250  | 
	edges[edges[n].prev_in].next_in = edges[n].next_in;
  | 
| 
klao@946
 | 
   251  | 
      } else {
 | 
| 
alpar@986
 | 
   252  | 
	nodes[edges[n].target].first_in = edges[n].next_in;
  | 
| 
klao@946
 | 
   253  | 
      }
  | 
| 
klao@946
 | 
   254  | 
  | 
| 
alpar@397
 | 
   255  | 
      
  | 
| 
klao@946
 | 
   256  | 
      if(edges[n].next_out!=-1) {
 | 
| 
alpar@397
 | 
   257  | 
	edges[edges[n].next_out].prev_out = edges[n].prev_out;
  | 
| 
klao@946
 | 
   258  | 
      } 
  | 
| 
klao@946
 | 
   259  | 
  | 
| 
klao@946
 | 
   260  | 
      if(edges[n].prev_out!=-1) {
 | 
| 
alpar@397
 | 
   261  | 
	edges[edges[n].prev_out].next_out = edges[n].next_out;
  | 
| 
klao@946
 | 
   262  | 
      } else {
 | 
| 
alpar@986
 | 
   263  | 
	nodes[edges[n].source].first_out = edges[n].next_out;
  | 
| 
klao@946
 | 
   264  | 
      }
  | 
| 
alpar@397
 | 
   265  | 
      
  | 
| 
alpar@397
 | 
   266  | 
      edges[n].next_in = first_free_edge;
  | 
| 
alpar@695
 | 
   267  | 
      first_free_edge = n;      
  | 
| 
alpar@397
 | 
   268  | 
  | 
| 
alpar@397
 | 
   269  | 
    }
  | 
| 
alpar@397
 | 
   270  | 
  | 
| 
alpar@397
 | 
   271  | 
    void clear() {
 | 
| 
deba@782
 | 
   272  | 
      edges.clear();
  | 
| 
deba@782
 | 
   273  | 
      nodes.clear();
  | 
| 
klao@946
 | 
   274  | 
      first_node = first_free_node = first_free_edge = -1;
  | 
| 
deba@937
 | 
   275  | 
    }
  | 
| 
deba@937
 | 
   276  | 
  | 
| 
alpar@949
 | 
   277  | 
  protected:
  | 
| 
alpar@1546
 | 
   278  | 
    void _changeTarget(Edge e, Node n) 
  | 
| 
alpar@949
 | 
   279  | 
    {
 | 
| 
alpar@949
 | 
   280  | 
      if(edges[e.id].next_in != -1)
  | 
| 
alpar@949
 | 
   281  | 
	edges[edges[e.id].next_in].prev_in = edges[e.id].prev_in;
  | 
| 
alpar@949
 | 
   282  | 
      if(edges[e.id].prev_in != -1)
  | 
| 
alpar@949
 | 
   283  | 
	edges[edges[e.id].prev_in].next_in = edges[e.id].next_in;
  | 
| 
alpar@986
 | 
   284  | 
      else nodes[edges[e.id].target].first_in = edges[e.id].next_in;
  | 
| 
alpar@986
 | 
   285  | 
      edges[e.id].target = n.id;
  | 
| 
alpar@949
 | 
   286  | 
      edges[e.id].prev_in = -1;
  | 
| 
alpar@949
 | 
   287  | 
      edges[e.id].next_in = nodes[n.id].first_in;
  | 
| 
alpar@949
 | 
   288  | 
      nodes[n.id].first_in = e.id;
  | 
| 
alpar@949
 | 
   289  | 
    }
  | 
| 
alpar@1546
 | 
   290  | 
    void _changeSource(Edge e, Node n) 
  | 
| 
alpar@949
 | 
   291  | 
    {
 | 
| 
alpar@949
 | 
   292  | 
      if(edges[e.id].next_out != -1)
  | 
| 
alpar@949
 | 
   293  | 
	edges[edges[e.id].next_out].prev_out = edges[e.id].prev_out;
  | 
| 
alpar@949
 | 
   294  | 
      if(edges[e.id].prev_out != -1)
  | 
| 
alpar@949
 | 
   295  | 
	edges[edges[e.id].prev_out].next_out = edges[e.id].next_out;
  | 
| 
alpar@986
 | 
   296  | 
      else nodes[edges[e.id].source].first_out = edges[e.id].next_out;
  | 
| 
alpar@986
 | 
   297  | 
      edges[e.id].source = n.id;
  | 
| 
alpar@949
 | 
   298  | 
      edges[e.id].prev_out = -1;
  | 
| 
alpar@949
 | 
   299  | 
      edges[e.id].next_out = nodes[n.id].first_out;
  | 
| 
alpar@949
 | 
   300  | 
      nodes[n.id].first_out = e.id;
  | 
| 
alpar@949
 | 
   301  | 
    }
  | 
| 
alpar@949
 | 
   302  | 
  | 
| 
alpar@919
 | 
   303  | 
  };
  | 
| 
deba@909
 | 
   304  | 
  | 
| 
klao@946
 | 
   305  | 
  typedef AlterableGraphExtender<ListGraphBase> AlterableListGraphBase;
  | 
| 
klao@946
 | 
   306  | 
  typedef IterableGraphExtender<AlterableListGraphBase> IterableListGraphBase;
  | 
| 
deba@1669
 | 
   307  | 
  typedef MappableGraphExtender<IterableListGraphBase> MappableListGraphBase;
  | 
| 
klao@946
 | 
   308  | 
  typedef ExtendableGraphExtender<MappableListGraphBase> ExtendableListGraphBase;
  | 
| 
klao@946
 | 
   309  | 
  typedef ClearableGraphExtender<ExtendableListGraphBase> ClearableListGraphBase;
  | 
| 
deba@1669
 | 
   310  | 
  typedef ErasableGraphExtender<
  | 
| 
deba@1669
 | 
   311  | 
    ClearableGraphExtender<
  | 
| 
deba@1669
 | 
   312  | 
    ExtendableGraphExtender<
  | 
| 
deba@1669
 | 
   313  | 
    MappableGraphExtender<
  | 
| 
deba@1669
 | 
   314  | 
    IterableGraphExtender<
  | 
| 
deba@1669
 | 
   315  | 
    AlterableGraphExtender<ListGraphBase> > > > > > ExtendedListGraphBase;
  | 
| 
alpar@400
 | 
   316  | 
  | 
| 
alpar@948
 | 
   317  | 
/// \addtogroup graphs
  | 
| 
alpar@948
 | 
   318  | 
/// @{
 | 
| 
alpar@400
 | 
   319  | 
  | 
| 
alpar@948
 | 
   320  | 
  ///A list graph class.
  | 
| 
alpar@400
 | 
   321  | 
  | 
| 
alpar@948
 | 
   322  | 
  ///This is a simple and fast erasable graph implementation.
  | 
| 
alpar@948
 | 
   323  | 
  ///
  | 
| 
alpar@1010
 | 
   324  | 
  ///It addition that it conforms to the
  | 
| 
alpar@1010
 | 
   325  | 
  ///\ref concept::ErasableGraph "ErasableGraph" concept,
  | 
| 
alpar@1010
 | 
   326  | 
  ///it also provides several additional useful extra functionalities.
  | 
| 
klao@959
 | 
   327  | 
  ///\sa concept::ErasableGraph.
  | 
| 
deba@782
 | 
   328  | 
  | 
| 
deba@1669
 | 
   329  | 
  class ListGraph : public ExtendedListGraphBase 
  | 
| 
alpar@948
 | 
   330  | 
  {
 | 
| 
alpar@948
 | 
   331  | 
  public:
  | 
| 
alpar@1546
 | 
   332  | 
    /// Changes the target of \c e to \c n
  | 
| 
alpar@948
 | 
   333  | 
  | 
| 
alpar@1546
 | 
   334  | 
    /// Changes the target of \c e to \c n
  | 
| 
alpar@948
 | 
   335  | 
    ///
  | 
| 
alpar@1010
 | 
   336  | 
    ///\note The <tt>Edge</tt>'s and <tt>OutEdge</tt>'s
  | 
| 
alpar@1546
 | 
   337  | 
    ///referencing the changed edge remain
  | 
| 
alpar@1010
 | 
   338  | 
    ///valid. However <tt>InEdge</tt>'s are invalidated.
  | 
| 
alpar@1546
 | 
   339  | 
    void changeTarget(Edge e, Node n) { _changeTarget(e,n); }
 | 
| 
alpar@1546
 | 
   340  | 
    /// Changes the source of \c e to \c n
  | 
| 
alpar@948
 | 
   341  | 
  | 
| 
alpar@1546
 | 
   342  | 
    /// Changes the source of \c e to \c n
  | 
| 
alpar@948
 | 
   343  | 
    ///
  | 
| 
alpar@1010
 | 
   344  | 
    ///\note The <tt>Edge</tt>'s and <tt>InEdge</tt>'s
  | 
| 
alpar@1546
 | 
   345  | 
    ///referencing the changed edge remain
  | 
| 
alpar@1010
 | 
   346  | 
    ///valid. However <tt>OutEdge</tt>'s are invalidated.
  | 
| 
alpar@1546
 | 
   347  | 
    void changeSource(Edge e, Node n) { _changeSource(e,n); }
 | 
| 
alpar@949
 | 
   348  | 
  | 
| 
alpar@1010
 | 
   349  | 
    /// Invert the direction of an edge.
  | 
| 
alpar@1010
 | 
   350  | 
  | 
| 
alpar@1010
 | 
   351  | 
    ///\note The <tt>Edge</tt>'s
  | 
| 
alpar@1546
 | 
   352  | 
    ///referencing the changed edge remain
  | 
| 
alpar@1010
 | 
   353  | 
    ///valid. However <tt>OutEdge</tt>'s  and <tt>InEdge</tt>'s are invalidated.
  | 
| 
alpar@1010
 | 
   354  | 
    void reverseEdge(Edge e) {
 | 
| 
alpar@1010
 | 
   355  | 
      Node t=target(e);
  | 
| 
alpar@1546
 | 
   356  | 
      _changeTarget(e,source(e));
  | 
| 
alpar@1546
 | 
   357  | 
      _changeSource(e,t);
  | 
| 
alpar@1010
 | 
   358  | 
    }
  | 
| 
alpar@1010
 | 
   359  | 
  | 
| 
alpar@1010
 | 
   360  | 
    ///Using this it possible to avoid the superfluous memory allocation.
  | 
| 
alpar@1010
 | 
   361  | 
  | 
| 
alpar@949
 | 
   362  | 
    ///Using this it possible to avoid the superfluous memory allocation.
  | 
| 
alpar@949
 | 
   363  | 
    ///\todo more docs...
  | 
| 
alpar@949
 | 
   364  | 
    void reserveEdge(int n) { edges.reserve(n); };
 | 
| 
alpar@1010
 | 
   365  | 
  | 
| 
alpar@1010
 | 
   366  | 
    ///Contract two nodes.
  | 
| 
alpar@1010
 | 
   367  | 
  | 
| 
alpar@1010
 | 
   368  | 
    ///This function contracts two nodes.
  | 
| 
alpar@1010
 | 
   369  | 
    ///
  | 
| 
alpar@1010
 | 
   370  | 
    ///Node \p b will be removed but instead of deleting
  | 
| 
alpar@1010
 | 
   371  | 
    ///its neighboring edges, they will be joined to \p a.
  | 
| 
alpar@1010
 | 
   372  | 
    ///The last parameter \p r controls whether to remove loops. \c true
  | 
| 
alpar@1010
 | 
   373  | 
    ///means that loops will be removed.
  | 
| 
alpar@1010
 | 
   374  | 
    ///
  | 
| 
alpar@1010
 | 
   375  | 
    ///\note The <tt>Edge</tt>s
  | 
| 
alpar@1281
 | 
   376  | 
    ///referencing a moved edge remain
  | 
| 
alpar@1010
 | 
   377  | 
    ///valid. However <tt>InEdge</tt>'s and <tt>OutEdge</tt>'s
  | 
| 
alpar@1010
 | 
   378  | 
    ///may be invalidated.
  | 
| 
alpar@1010
 | 
   379  | 
    void contract(Node a,Node b,bool r=true) 
  | 
| 
alpar@1010
 | 
   380  | 
    {
 | 
| 
alpar@1010
 | 
   381  | 
      for(OutEdgeIt e(*this,b);e!=INVALID;) {
 | 
| 
alpar@1010
 | 
   382  | 
	OutEdgeIt f=e;
  | 
| 
alpar@1010
 | 
   383  | 
	++f;
  | 
| 
alpar@1010
 | 
   384  | 
	if(r && target(e)==a) erase(e);
  | 
| 
alpar@1546
 | 
   385  | 
	else changeSource(e,a);
  | 
| 
alpar@1010
 | 
   386  | 
	e=f;
  | 
| 
alpar@1010
 | 
   387  | 
      }
  | 
| 
alpar@1010
 | 
   388  | 
      for(InEdgeIt e(*this,b);e!=INVALID;) {
 | 
| 
alpar@1010
 | 
   389  | 
	InEdgeIt f=e;
  | 
| 
alpar@1010
 | 
   390  | 
	++f;
  | 
| 
alpar@1010
 | 
   391  | 
	if(r && source(e)==a) erase(e);
  | 
| 
alpar@1546
 | 
   392  | 
	else changeTarget(e,a);
  | 
| 
alpar@1010
 | 
   393  | 
	e=f;
  | 
| 
alpar@1010
 | 
   394  | 
      }
  | 
| 
alpar@1010
 | 
   395  | 
      erase(b);
  | 
| 
alpar@1010
 | 
   396  | 
    }
  | 
| 
alpar@1011
 | 
   397  | 
  | 
| 
alpar@1281
 | 
   398  | 
    ///Split a node.
  | 
| 
alpar@1011
 | 
   399  | 
  | 
| 
alpar@1284
 | 
   400  | 
    ///This function splits a node. First a new node is added to the graph,
  | 
| 
alpar@1284
 | 
   401  | 
    ///then the source of each outgoing edge of \c n is moved to this new node.
  | 
| 
alpar@1281
 | 
   402  | 
    ///If \c connect is \c true (this is the default value), then a new edge
  | 
| 
alpar@1281
 | 
   403  | 
    ///from \c n to the newly created node is also added.
  | 
| 
alpar@1281
 | 
   404  | 
    ///\return The newly created node.
  | 
| 
alpar@1281
 | 
   405  | 
    ///
  | 
| 
alpar@1281
 | 
   406  | 
    ///\note The <tt>Edge</tt>s
  | 
| 
alpar@1281
 | 
   407  | 
    ///referencing a moved edge remain
  | 
| 
alpar@1281
 | 
   408  | 
    ///valid. However <tt>InEdge</tt>'s and <tt>OutEdge</tt>'s
  | 
| 
alpar@1281
 | 
   409  | 
    ///may be invalidated.
  | 
| 
alpar@1284
 | 
   410  | 
    ///\warning This functionality cannot be used together with the SnapShot
  | 
| 
alpar@1284
 | 
   411  | 
    ///feature.
  | 
| 
alpar@1281
 | 
   412  | 
    ///\todo It could be implemented in a bit faster way.
  | 
| 
alpar@1281
 | 
   413  | 
    Node split(Node n, bool connect = true) 
  | 
| 
alpar@1281
 | 
   414  | 
    {
 | 
| 
alpar@1281
 | 
   415  | 
      Node b = addNode();
  | 
| 
alpar@1281
 | 
   416  | 
      for(OutEdgeIt e(*this,n);e!=INVALID;) {
 | 
| 
alpar@1281
 | 
   417  | 
 	OutEdgeIt f=e;
  | 
| 
alpar@1281
 | 
   418  | 
	++f;
  | 
| 
alpar@1546
 | 
   419  | 
	changeSource(e,b);
  | 
| 
alpar@1281
 | 
   420  | 
	e=f;
  | 
| 
alpar@1281
 | 
   421  | 
      }
  | 
| 
alpar@1281
 | 
   422  | 
      if(connect) addEdge(n,b);
  | 
| 
alpar@1281
 | 
   423  | 
      return b;
  | 
| 
alpar@1281
 | 
   424  | 
    }
  | 
| 
alpar@1281
 | 
   425  | 
      
  | 
| 
alpar@1011
 | 
   426  | 
    ///Class to make a snapshot of the graph and to restrore to it later.
  | 
| 
alpar@1011
 | 
   427  | 
  | 
| 
alpar@1011
 | 
   428  | 
    ///Class to make a snapshot of the graph and to restrore to it later.
  | 
| 
alpar@1011
 | 
   429  | 
    ///
  | 
| 
alpar@1011
 | 
   430  | 
    ///The newly added nodes and edges can be removed using the
  | 
| 
alpar@1011
 | 
   431  | 
    ///restore() function.
  | 
| 
alpar@1011
 | 
   432  | 
    ///
  | 
| 
alpar@1011
 | 
   433  | 
    ///\warning Edge and node deletions cannot be restored.
  | 
| 
alpar@1011
 | 
   434  | 
    ///\warning SnapShots cannot be nested.
  | 
| 
alpar@1035
 | 
   435  | 
    ///\todo \c SnapShot or \c Snapshot?
  | 
| 
deba@1039
 | 
   436  | 
    class SnapShot : protected AlterationNotifier<Node>::ObserverBase,
  | 
| 
deba@1039
 | 
   437  | 
		     protected AlterationNotifier<Edge>::ObserverBase
  | 
| 
alpar@1011
 | 
   438  | 
    {
 | 
| 
alpar@1011
 | 
   439  | 
      protected:
  | 
| 
alpar@1011
 | 
   440  | 
      
  | 
| 
alpar@1011
 | 
   441  | 
      ListGraph *g;
  | 
| 
alpar@1011
 | 
   442  | 
      std::list<Node> added_nodes;
  | 
| 
alpar@1011
 | 
   443  | 
      std::list<Edge> added_edges;
  | 
| 
alpar@1011
 | 
   444  | 
      
  | 
| 
alpar@1011
 | 
   445  | 
      bool active;
  | 
| 
alpar@1011
 | 
   446  | 
      virtual void add(const Node& n) {
 | 
| 
alpar@1011
 | 
   447  | 
	added_nodes.push_back(n);
  | 
| 
alpar@1011
 | 
   448  | 
      };
  | 
| 
alpar@1011
 | 
   449  | 
      ///\bug Exception...
  | 
| 
alpar@1011
 | 
   450  | 
      ///
  | 
| 
alpar@1011
 | 
   451  | 
      virtual void erase(const Node&) 
  | 
| 
alpar@1011
 | 
   452  | 
      {
 | 
| 
alpar@1011
 | 
   453  | 
	exit(1);
  | 
| 
alpar@1011
 | 
   454  | 
      }
  | 
| 
alpar@1011
 | 
   455  | 
      virtual void add(const Edge& n) {
 | 
| 
alpar@1011
 | 
   456  | 
	added_edges.push_back(n);
  | 
| 
alpar@1011
 | 
   457  | 
      };
  | 
| 
alpar@1011
 | 
   458  | 
      ///\bug Exception...
  | 
| 
alpar@1011
 | 
   459  | 
      ///
  | 
| 
alpar@1011
 | 
   460  | 
      virtual void erase(const Edge&) 
  | 
| 
alpar@1011
 | 
   461  | 
      {
 | 
| 
alpar@1011
 | 
   462  | 
	exit(1);
  | 
| 
alpar@1011
 | 
   463  | 
      }
  | 
| 
alpar@1011
 | 
   464  | 
  | 
| 
alpar@1457
 | 
   465  | 
      ///\bug What is this used for?
  | 
| 
alpar@1457
 | 
   466  | 
      ///
  | 
| 
alpar@1457
 | 
   467  | 
      virtual void build() {}
 | 
| 
alpar@1457
 | 
   468  | 
      ///\bug What is this used for?
  | 
| 
alpar@1457
 | 
   469  | 
      ///
  | 
| 
alpar@1457
 | 
   470  | 
      virtual void clear() {}
 | 
| 
alpar@1457
 | 
   471  | 
  | 
| 
alpar@1011
 | 
   472  | 
      void regist(ListGraph &_g) {
 | 
| 
alpar@1011
 | 
   473  | 
	g=&_g;
  | 
| 
deba@1039
 | 
   474  | 
	AlterationNotifier<Node>::ObserverBase::
  | 
| 
deba@1040
 | 
   475  | 
	  attach(g->getNotifier(Node()));
  | 
| 
deba@1039
 | 
   476  | 
	AlterationNotifier<Edge>::ObserverBase::
  | 
| 
deba@1040
 | 
   477  | 
	  attach(g->getNotifier(Edge()));
  | 
| 
alpar@1011
 | 
   478  | 
      }
  | 
| 
alpar@1011
 | 
   479  | 
            
  | 
| 
alpar@1011
 | 
   480  | 
      void deregist() {
 | 
| 
deba@1039
 | 
   481  | 
	AlterationNotifier<Node>::ObserverBase::
  | 
| 
alpar@1011
 | 
   482  | 
	  detach();
  | 
| 
deba@1039
 | 
   483  | 
	AlterationNotifier<Edge>::ObserverBase::
  | 
| 
alpar@1011
 | 
   484  | 
	  detach();
  | 
| 
alpar@1011
 | 
   485  | 
	g=0;
  | 
| 
alpar@1011
 | 
   486  | 
      }
  | 
| 
alpar@1011
 | 
   487  | 
            
  | 
| 
alpar@1011
 | 
   488  | 
    public:
  | 
| 
alpar@1011
 | 
   489  | 
      ///Default constructur.
  | 
| 
alpar@1011
 | 
   490  | 
      
  | 
| 
alpar@1011
 | 
   491  | 
      ///Default constructur.
  | 
| 
alpar@1011
 | 
   492  | 
      ///To actually make a snapshot you must call save().
  | 
| 
alpar@1011
 | 
   493  | 
      ///
  | 
| 
alpar@1011
 | 
   494  | 
      SnapShot() : g(0) {}
 | 
| 
alpar@1011
 | 
   495  | 
      ///Constructor that immediately makes a snapshot.
  | 
| 
alpar@1011
 | 
   496  | 
      
  | 
| 
alpar@1011
 | 
   497  | 
      ///This constructor immediately makes a snapshot of the graph.
  | 
| 
alpar@1011
 | 
   498  | 
      ///\param _g The graph we make a snapshot of.
  | 
| 
alpar@1011
 | 
   499  | 
      SnapShot(ListGraph &_g) {
 | 
| 
alpar@1011
 | 
   500  | 
	regist(_g);
  | 
| 
alpar@1011
 | 
   501  | 
      }
  | 
| 
alpar@1011
 | 
   502  | 
      ///\bug Is it necessary?
  | 
| 
alpar@1011
 | 
   503  | 
      ///
  | 
| 
alpar@1011
 | 
   504  | 
      ~SnapShot() 
  | 
| 
alpar@1011
 | 
   505  | 
      {
 | 
| 
alpar@1011
 | 
   506  | 
	if(g) deregist();
  | 
| 
alpar@1011
 | 
   507  | 
      }
  | 
| 
alpar@1011
 | 
   508  | 
      
  | 
| 
alpar@1011
 | 
   509  | 
      ///Make a snapshot.
  | 
| 
alpar@1011
 | 
   510  | 
  | 
| 
alpar@1011
 | 
   511  | 
      ///Make a snapshot of the graph.
  | 
| 
alpar@1011
 | 
   512  | 
      ///
  | 
| 
alpar@1011
 | 
   513  | 
      ///This function can be called more than once. In case of a repeated
  | 
| 
alpar@1011
 | 
   514  | 
      ///call, the previous snapshot gets lost.
  | 
| 
alpar@1011
 | 
   515  | 
      ///\param _g The graph we make the snapshot of.
  | 
| 
alpar@1011
 | 
   516  | 
      void save(ListGraph &_g) 
  | 
| 
alpar@1011
 | 
   517  | 
      {
 | 
| 
alpar@1011
 | 
   518  | 
	if(g!=&_g) {
 | 
| 
alpar@1011
 | 
   519  | 
	  if(g) deregist();
  | 
| 
alpar@1011
 | 
   520  | 
	  regist(_g);
  | 
| 
alpar@1011
 | 
   521  | 
	}
  | 
| 
alpar@1011
 | 
   522  | 
	added_nodes.clear();
  | 
| 
alpar@1011
 | 
   523  | 
	added_edges.clear();
  | 
| 
alpar@1011
 | 
   524  | 
      }
  | 
| 
alpar@1011
 | 
   525  | 
      
  | 
| 
alpar@1011
 | 
   526  | 
    ///Undo the changes until the last snapshot.
  | 
| 
alpar@1011
 | 
   527  | 
  | 
| 
alpar@1011
 | 
   528  | 
    ///Undo the changes until last snapshot created by save().
  | 
| 
alpar@1011
 | 
   529  | 
    ///
  | 
| 
alpar@1011
 | 
   530  | 
    ///\todo This function might be called undo().
  | 
| 
alpar@1011
 | 
   531  | 
      void restore() {
 | 
| 
alpar@1457
 | 
   532  | 
	ListGraph &old_g=*g;
  | 
| 
alpar@1011
 | 
   533  | 
	deregist();
  | 
| 
alpar@1011
 | 
   534  | 
	while(!added_edges.empty()) {
 | 
| 
alpar@1457
 | 
   535  | 
	  old_g.erase(added_edges.front());
  | 
| 
alpar@1011
 | 
   536  | 
	  added_edges.pop_front();
  | 
| 
alpar@1011
 | 
   537  | 
	}
  | 
| 
alpar@1011
 | 
   538  | 
 	while(!added_nodes.empty()) {
 | 
| 
alpar@1457
 | 
   539  | 
	  old_g.erase(added_nodes.front());
  | 
| 
alpar@1011
 | 
   540  | 
	  added_nodes.pop_front();
  | 
| 
alpar@1011
 | 
   541  | 
	}
  | 
| 
alpar@1011
 | 
   542  | 
      }
  | 
| 
alpar@1011
 | 
   543  | 
    };
  | 
| 
alpar@1011
 | 
   544  | 
    
  | 
| 
alpar@949
 | 
   545  | 
  };
  | 
| 
klao@1034
 | 
   546  | 
  | 
| 
alpar@1555
 | 
   547  | 
  ///@}
  | 
| 
klao@1034
 | 
   548  | 
  | 
| 
klao@1034
 | 
   549  | 
  /**************** Undirected List Graph ****************/
  | 
| 
klao@1034
 | 
   550  | 
  | 
| 
klao@1034
 | 
   551  | 
  typedef ErasableUndirGraphExtender<
  | 
| 
klao@1034
 | 
   552  | 
    ClearableUndirGraphExtender<
  | 
| 
klao@1034
 | 
   553  | 
    ExtendableUndirGraphExtender<
  | 
| 
klao@1034
 | 
   554  | 
    MappableUndirGraphExtender<
  | 
| 
klao@1034
 | 
   555  | 
    IterableUndirGraphExtender<
  | 
| 
klao@1034
 | 
   556  | 
    AlterableUndirGraphExtender<
  | 
| 
deba@1669
 | 
   557  | 
    UndirGraphExtender<ListGraphBase> > > > > > > ExtendedUndirListGraphBase;
  | 
| 
klao@1034
 | 
   558  | 
  | 
| 
alpar@1555
 | 
   559  | 
/// \addtogroup graphs
  | 
| 
alpar@1555
 | 
   560  | 
/// @{
 | 
| 
alpar@1555
 | 
   561  | 
  | 
| 
alpar@1035
 | 
   562  | 
  ///An undirected list graph class.
  | 
| 
alpar@1035
 | 
   563  | 
  | 
| 
alpar@1035
 | 
   564  | 
  ///This is a simple and fast erasable undirected graph implementation.
  | 
| 
alpar@1035
 | 
   565  | 
  ///
  | 
| 
alpar@1035
 | 
   566  | 
  ///It conforms to the
  | 
| 
alpar@1035
 | 
   567  | 
  ///\ref concept::UndirGraph "UndirGraph" concept.
  | 
| 
alpar@1035
 | 
   568  | 
  ///
  | 
| 
alpar@1035
 | 
   569  | 
  ///\sa concept::UndirGraph.
  | 
| 
alpar@1035
 | 
   570  | 
  ///
  | 
| 
alpar@1546
 | 
   571  | 
  ///\todo SnapShot, reverseEdge(), changeTarget(), changeSource(), contract()
  | 
| 
alpar@1161
 | 
   572  | 
  ///haven't been implemented yet.
  | 
| 
alpar@1035
 | 
   573  | 
  ///
  | 
| 
deba@1669
 | 
   574  | 
  class UndirListGraph : public ExtendedUndirListGraphBase {
 | 
| 
klao@1034
 | 
   575  | 
  };
  | 
| 
klao@1034
 | 
   576  | 
  | 
| 
alpar@949
 | 
   577  | 
  
  | 
| 
alpar@948
 | 
   578  | 
  /// @}  
  | 
| 
alpar@948
 | 
   579  | 
} //namespace lemon
  | 
| 
klao@946
 | 
   580  | 
  
  | 
| 
alpar@400
 | 
   581  | 
  | 
| 
klao@946
 | 
   582  | 
#endif
  |