COIN-OR::LEMON - Graph Library

source: lemon-0.x/src/work/peter/hierarchygraph.h @ 880:9d0bfd35b97c

Last change on this file since 880:9d0bfd35b97c was 880:9d0bfd35b97c, checked in by Alpar Juttner, 20 years ago
  • Name changing: XYZGraphSkeleton -> XYZGraph
  • Fix some bad \ref's in the doc.
File size: 15.0 KB
RevLine 
[677]1// -*- c++ -*-
2#ifndef HUGO_NET_GRAPH_H
3#define HUGO_NET_GRAPH_H
4
5///\file
6///\brief Declaration of HierarchyGraph.
7
8#include <hugo/invalid.h>
9#include <hugo/maps.h>
10
11/// The namespace of HugoLib
[691]12namespace hugo
13{
[677]14
15  // @defgroup empty_graph The HierarchyGraph class
16  // @{
17
18  /// A graph class in that a simple edge can represent a path.
[690]19
[677]20  /// This class provides common features of a graph structure
21  /// that represents a network. You can handle with it layers. This
22  /// means that a node in one layer can be a complete network in a nother
23  /// layer.
24
[691]25  template < class Gact, class Gsub > class HierarchyGraph
[677]26  {
27
28  public:
29
30    /// The actual layer
31    Gact actuallayer;
32
33
[690]34    /// Map of the subnetworks in the sublayer
35    /// The appropriate edge nodes are also stored here
[677]36
[690]37    class SubNetwork
38    {
39
40      struct actedgesubnodestruct
41      {
[691]42        typename Gact::Edge actedge;
43        typename Gsub::Node subnode;
[690]44      };
45
46      int edgenumber;
47      bool connectable;
[691]48      Gact *actuallayer;
[690]49      typename Gact::Node * actuallayernode;
[691]50      Gsub *subnetwork;
51      actedgesubnodestruct *assignments;
[690]52
53    public:
54
[691]55      int addAssignment (typename Gact::Edge actedge,
56                         typename Gsub::Node subnode)
[690]57      {
[691]58        if (!(actuallayer->valid (actedge)))
59          {
60            cerr << "The given edge is not in the given network!" << endl;
61            return -1;
62          }
63        else if ((actuallayer->id (actuallayer->tail (actedge)) !=
64                  actuallayer->id (*actuallayernode))
65                 && (actuallayer->id (actuallayer->head (actedge)) !=
66                     actuallayer->id (*actuallayernode)))
67          {
68            cerr << "The given edge does not connect to the given node!" <<
69              endl;
70            return -1;
71          }
[690]72
[691]73        if (!(subnetwork->valid (subnode)))
74          {
75            cerr << "The given node is not in the given network!" << endl;
76            return -1;
77          }
[690]78
[691]79        int i = 0;
[690]80        //while in the array there is valid note that is not equvivalent with the one that would be noted increase i
[691]81        while ((i < edgenumber)
82               && (actuallayer->valid (assignments[i].actedge))
83               && (assignments[i].actedge != actedge))
84          i++;
85        if (assignments[i].actedge == actedge)
86          {
87            cout << "Warning: Redefinement of assigment!!!" << endl;
88          }
89        if (i == edgenumber)
90          {
91            cout <<
92              "This case can't be!!! (because there should be the guven edge in the array already and the cycle had to stop)"
93              << endl;
94          }
[690]95        //if(!(actuallayer->valid(assignments[i].actedge)))   //this condition is necessary if we do not obey redefinition
96        {
[691]97          assignments[i].actedge = actedge;
98          assignments[i].subnode = subnode;
[690]99        }
100
101        /// If to all of the edges a subnode is assigned then the subnetwork is connectable (attachable?)
102        /// We do not need to check for further attributes, because to notice an assignment we need
103        /// all of them to be correctly initialised before.
[691]104        if (i == edgenumber - 1)
105          connectable = 1;
[690]106
107        return 0;
108      }
109
[691]110      int setSubNetwork (Gsub * sn)
[690]111      {
[691]112        subnetwork = sn;
[690]113        return 0;
114      }
115
[691]116      int setActualLayer (Gact * al)
[690]117      {
[691]118        actuallayer = al;
[690]119        return 0;
120      }
121
[691]122      int setActualLayerNode (typename Gact::Node * aln)
[690]123      {
124        typename Gact::InEdgeIt iei;
125        typename Gact::OutEdgeIt oei;
126
[691]127        actuallayernode = aln;
[690]128
[691]129        edgenumber = 0;
[690]130
[691]131        if (actuallayer)
[690]132          {
[691]133            for (iei = actuallayer->first (iei, (*actuallayernode));
134                 ((actuallayer->valid (iei))
135                  && (actuallayer->head (iei) == (*actuallayernode)));
136                 actuallayer->next (iei))
137              {
138                cout << actuallayer->id (actuallayer->
139                                         tail (iei)) << " " << actuallayer->
140                  id (actuallayer->head (iei)) << endl;
141                edgenumber++;
142              }
143            //cout << "Number of in-edges: " << edgenumber << endl;
144            for (oei = actuallayer->first (oei, (*actuallayernode));
145                 ((actuallayer->valid (oei))
146                  && (actuallayer->tail (oei) == (*actuallayernode)));
147                 actuallayer->next (oei))
148              {
149                cout << actuallayer->id (actuallayer->
150                                         tail (oei)) << " " << actuallayer->
151                  id (actuallayer->head (oei)) << endl;
152                edgenumber++;
153              }
154            //cout << "Number of in+out-edges: " << edgenumber << endl;
155            assignments = new actedgesubnodestruct[edgenumber];
156            for (int i = 0; i < edgenumber; i++)
157              {
158                assignments[i].actedge = INVALID;
159                assignments[i].subnode = INVALID;
160              }
[690]161          }
[691]162        else
[690]163          {
[691]164            cerr << "There is no actual layer defined yet!" << endl;
165            return -1;
[690]166          }
167
168        return 0;
169      }
170
[691]171    SubNetwork ():edgenumber (0), connectable (false), actuallayer (NULL),
172        actuallayernode (NULL), subnetwork (NULL),
173        assignments (NULL)
[690]174      {
175      }
176
177    };
178
[691]179    typename Gact::template NodeMap < SubNetwork > subnetworks;
[677]180
181
182    /// Defalult constructor.
183    /// We don't need any extra lines, because the actuallayer
184    /// variable has run its constructor, when we have created this class
185    /// So only the two maps has to be initialised here.
[691]186  HierarchyGraph ():subnetworks (actuallayer)
[677]187    {
188    }
189
190
191    ///Copy consructor.
[691]192  HierarchyGraph (const HierarchyGraph < Gact, Gsub > &HG):actuallayer (HG.actuallayer),
193      subnetworks
194      (actuallayer)
[677]195    {
196    }
197
[690]198
[677]199    /// The base type of the node iterators.
200
201    /// This is the base type of each node iterators,
202    /// thus each kind of node iterator will convert to this.
203    /// The Node type of the HierarchyGraph is the Node type of the actual layer.
204    typedef typename Gact::Node Node;
205
[690]206
[677]207    /// This iterator goes through each node.
208
209    /// Its usage is quite simple, for example you can count the number
210    /// of nodes in graph \c G of type \c Graph like this:
211    /// \code
212    ///int count=0;
213    ///for(Graph::NodeIt n(G);G.valid(n);G.next(n)) count++;
214    /// \endcode
215    /// The NodeIt type of the HierarchyGraph is the NodeIt type of the actual layer.
216    typedef typename Gact::NodeIt NodeIt;
[690]217
218
[677]219    /// The base type of the edge iterators.
220    /// The Edge type of the HierarchyGraph is the Edge type of the actual layer.
[691]221    typedef typename Gact::Edge Edge;
[677]222
[690]223
[677]224    /// This iterator goes trough the outgoing edges of a node.
225
226    /// This iterator goes trough the \e outgoing edges of a certain node
227    /// of a graph.
228    /// Its usage is quite simple, for example you can count the number
229    /// of outgoing edges of a node \c n
230    /// in graph \c G of type \c Graph as follows.
231    /// \code
232    ///int count=0;
233    ///for(Graph::OutEdgeIt e(G,n);G.valid(e);G.next(e)) count++;
234    /// \endcode
235    /// The OutEdgeIt type of the HierarchyGraph is the OutEdgeIt type of the actual layer.
236    typedef typename Gact::OutEdgeIt OutEdgeIt;
237
238
239    /// This iterator goes trough the incoming edges of a node.
240
241    /// This iterator goes trough the \e incoming edges of a certain node
242    /// of a graph.
243    /// Its usage is quite simple, for example you can count the number
244    /// of outgoing edges of a node \c n
245    /// in graph \c G of type \c Graph as follows.
246    /// \code
247    ///int count=0;
248    ///for(Graph::InEdgeIt e(G,n);G.valid(e);G.next(e)) count++;
249    /// \endcode
250    /// The InEdgeIt type of the HierarchyGraph is the InEdgeIt type of the actual layer.
251    typedef typename Gact::InEdgeIt InEdgeIt;
252
253
254    /// This iterator goes through each edge.
255
256    /// This iterator goes through each edge of a graph.
257    /// Its usage is quite simple, for example you can count the number
258    /// of edges in a graph \c G of type \c Graph as follows:
259    /// \code
260    ///int count=0;
261    ///for(Graph::EdgeIt e(G);G.valid(e);G.next(e)) count++;
262    /// \endcode
263    /// The EdgeIt type of the HierarchyGraph is the EdgeIt type of the actual layer.
264    typedef typename Gact::EdgeIt EdgeIt;
265
266
267    /// First node of the graph.
268
269    /// \retval i the first node.
270    /// \return the first node.
[691]271    typename Gact::NodeIt & first (typename Gact::NodeIt & i) const
272    {
273      return actuallayer.first (i);
274    }
[677]275
276
277    /// The first incoming edge.
[691]278    typename Gact::InEdgeIt & first (typename Gact::InEdgeIt & i,
279                                     typename Gact::Node) const
280    {
281      return actuallayer.first (i);
282    }
[677]283
284
285    /// The first outgoing edge.
[691]286    typename Gact::OutEdgeIt & first (typename Gact::OutEdgeIt & i,
287                                      typename Gact::Node) const
288    {
289      return actuallayer.first (i);
290    }
[677]291
292
293    //  SymEdgeIt &first(SymEdgeIt &, Node) const { return i;}
294    /// The first edge of the Graph.
[691]295    typename Gact::EdgeIt & first (typename Gact::EdgeIt & i) const
296    {
297      return actuallayer.first (i);
298    }
[677]299
300
301//     Node getNext(Node) const {}
302//     InEdgeIt getNext(InEdgeIt) const {}
303//     OutEdgeIt getNext(OutEdgeIt) const {}
304//     //SymEdgeIt getNext(SymEdgeIt) const {}
305//     EdgeIt getNext(EdgeIt) const {}
306
307
308    /// Go to the next node.
[691]309    typename Gact::NodeIt & next (typename Gact::NodeIt & i) const
310    {
311      return actuallayer.next (i);
312    }
[677]313    /// Go to the next incoming edge.
[691]314    typename Gact::InEdgeIt & next (typename Gact::InEdgeIt & i) const
315    {
316      return actuallayer.next (i);
317    }
[677]318    /// Go to the next outgoing edge.
[691]319    typename Gact::OutEdgeIt & next (typename Gact::OutEdgeIt & i) const
320    {
321      return actuallayer.next (i);
322    }
[677]323    //SymEdgeIt &next(SymEdgeIt &) const {}
324    /// Go to the next edge.
[691]325    typename Gact::EdgeIt & next (typename Gact::EdgeIt & i) const
326    {
327      return actuallayer.next (i);
328    }
[677]329
330    ///Gives back the head node of an edge.
[691]331    typename Gact::Node head (typename Gact::Edge edge) const
332    {
333      return actuallayer.head (edge);
334    }
[677]335    ///Gives back the tail node of an edge.
[691]336    typename Gact::Node tail (typename Gact::Edge edge) const
337    {
338      return actuallayer.tail (edge);
339    }
[690]340
[677]341    //   Node aNode(InEdgeIt) const {}
342    //   Node aNode(OutEdgeIt) const {}
343    //   Node aNode(SymEdgeIt) const {}
344
345    //   Node bNode(InEdgeIt) const {}
346    //   Node bNode(OutEdgeIt) const {}
347    //   Node bNode(SymEdgeIt) const {}
348
349    /// Checks if a node iterator is valid
350
351    ///\todo Maybe, it would be better if iterator converted to
352    ///bool directly, as Jacint prefers.
[691]353    bool valid (const typename Gact::Node & node) const
354    {
355      return actuallayer.valid (node);
356    }
[677]357    /// Checks if an edge iterator is valid
358
359    ///\todo Maybe, it would be better if iterator converted to
360    ///bool directly, as Jacint prefers.
[691]361    bool valid (const typename Gact::Edge & edge) const
362    {
363      return actuallayer.valid (edge);
364    }
[677]365
366    ///Gives back the \e id of a node.
367
368    ///\warning Not all graph structures provide this feature.
369    ///
[691]370    int id (const typename Gact::Node & node) const
371    {
372      return actuallayer.id (node);
373    }
[677]374    ///Gives back the \e id of an edge.
375
376    ///\warning Not all graph structures provide this feature.
377    ///
[691]378    int id (const typename Gact::Edge & edge) const
379    {
380      return actuallayer.id (edge);
381    }
[677]382
383    //void setInvalid(Node &) const {};
384    //void setInvalid(Edge &) const {};
[690]385
[677]386    ///Add a new node to the graph.
387
388    /// \return the new node.
389    ///
[691]390    typename Gact::Node addNode ()
391    {
392      return actuallayer.addNode ();
393    }
[677]394    ///Add a new edge to the graph.
395
396    ///Add a new edge to the graph with tail node \c tail
397    ///and head node \c head.
398    ///\return the new edge.
[691]399    typename Gact::Edge addEdge (typename Gact::Node node1,
400                                 typename Gact::Node node2)
401    {
402      return actuallayer.addEdge (node1, node2);
403    }
[690]404
[677]405    /// Resets the graph.
406
407    /// This function deletes all edges and nodes of the graph.
408    /// It also frees the memory allocated to store them.
[691]409    void clear ()
410    {
411      actuallayer.clear ();
412    }
[677]413
[691]414    int nodeNum () const
415    {
416      return actuallayer.nodeNum ();
417    }
418    int edgeNum () const
419    {
420      return actuallayer.edgeNum ();
421    }
[677]422
423    ///Read/write/reference map of the nodes to type \c T.
424
425    ///Read/write/reference map of the nodes to type \c T.
[880]426    /// \sa MemoryMap
[677]427    /// \todo We may need copy constructor
428    /// \todo We may need conversion from other nodetype
429    /// \todo We may need operator=
430    /// \warning Making maps that can handle bool type (NodeMap<bool>)
431    /// needs extra attention!
432
[691]433    template < class T > class NodeMap
[677]434    {
435    public:
436      typedef T ValueType;
437      typedef Node KeyType;
438
[691]439      NodeMap (const HierarchyGraph &)
440      {
441      }
442      NodeMap (const HierarchyGraph &, T)
443      {
444      }
[677]445
[691]446      template < typename TT > NodeMap (const NodeMap < TT > &)
447      {
448      }
[677]449
450      /// Sets the value of a node.
451
452      /// Sets the value associated with node \c i to the value \c t.
453      ///
[691]454      void set (Node, T)
455      {
456      }
[677]457      // Gets the value of a node.
458      //T get(Node i) const {return *(T*)0;}  //FIXME: Is it necessary?
[691]459      T & operator[](Node)
460      {
461        return *(T *) 0;
462      }
463      const T & operator[] (Node) const
464      {
465        return *(T *) 0;
466      }
[677]467
468      /// Updates the map if the graph has been changed
469
470      /// \todo Do we need this?
471      ///
[691]472      void update ()
473      {
474      }
475      void update (T a)
476      {
477      }                         //FIXME: Is it necessary
[677]478    };
479
480    ///Read/write/reference map of the edges to type \c T.
481
482    ///Read/write/reference map of the edges to type \c T.
483    ///It behaves exactly in the same way as \ref NodeMap.
484    /// \sa NodeMap
[880]485    /// \sa MemoryMap
[677]486    /// \todo We may need copy constructor
487    /// \todo We may need conversion from other edgetype
488    /// \todo We may need operator=
[691]489    template < class T > class EdgeMap
[677]490    {
491    public:
492      typedef T ValueType;
493      typedef Edge KeyType;
494
[691]495      EdgeMap (const HierarchyGraph &)
496      {
497      }
498      EdgeMap (const HierarchyGraph &, T)
499      {
500      }
[690]501
[677]502      ///\todo It can copy between different types.
503      ///
[691]504      template < typename TT > EdgeMap (const EdgeMap < TT > &)
505      {
506      }
[677]507
[691]508      void set (Edge, T)
509      {
510      }
[677]511      //T get(Edge) const {return *(T*)0;}
[691]512      T & operator[](Edge)
513      {
514        return *(T *) 0;
515      }
516      const T & operator[] (Edge) const
517      {
518        return *(T *) 0;
519      }
[690]520
[691]521      void update ()
522      {
523      }
524      void update (T a)
525      {
526      }                         //FIXME: Is it necessary
[677]527    };
528  };
529
[826]530  /// An empty erasable graph class.
[690]531
[826]532  /// This class provides all the common features of an \e erasable graph
[677]533  /// structure,
534  /// however completely without implementations and real data structures
535  /// behind the interface.
536  /// All graph algorithms should compile with this class, but it will not
537  /// run properly, of course.
538  ///
539  /// \todo This blabla could be replaced by a sepatate description about
[880]540  /// s.
[677]541  ///
542  /// It can be used for checking the interface compatibility,
543  /// or it can serve as a skeleton of a new graph structure.
[690]544  ///
[677]545  /// Also, you will find here the full documentation of a certain graph
546  /// feature, the documentation of a real graph imlementation
547  /// like @ref ListGraph or
548  /// @ref SmartGraph will just refer to this structure.
[826]549template < typename Gact, typename Gsub > class ErasableHierarchyGraph:public HierarchyGraph < Gact,
[691]550    Gsub
551    >
[677]552  {
553  public:
554    /// Deletes a node.
[691]555    void erase (typename Gact::Node n)
556    {
557      actuallayer.erase (n);
558    }
[677]559    /// Deletes an edge.
[691]560    void erase (typename Gact::Edge e)
561    {
562      actuallayer.erase (e);
563    }
[677]564
565    /// Defalult constructor.
[826]566    ErasableHierarchyGraph ()
[691]567    {
568    }
[677]569    ///Copy consructor.
[826]570    ErasableHierarchyGraph (const HierarchyGraph < Gact, Gsub > &EPG)
[691]571    {
572    }
[677]573  };
574
[690]575
[677]576  // @}
577
[691]578}                               //namespace hugo
[677]579
580
581#endif // HUGO_SKELETON_GRAPH_H
Note: See TracBrowser for help on using the repository browser.