src/lemon/graph_wrapper.h
changeset 923 acbef5dd0e65
parent 921 818510fa3d99
child 930 e89f3bd26fd4
equal deleted inserted replaced
0:455e234b7e4d 1:1688aff13989
    33 namespace lemon {
    33 namespace lemon {
    34 
    34 
    35   // Graph wrappers
    35   // Graph wrappers
    36 
    36 
    37   /// \addtogroup gwrappers
    37   /// \addtogroup gwrappers
    38   /// A main parts of LEMON are the different graph structures, 
    38   /// The main parts of LEMON are the different graph structures, 
    39   /// generic graph algorithms, graph concepts which couple these, and 
    39   /// generic graph algorithms, graph concepts which couple these, and 
    40   /// graph wrappers. While the previous ones are more or less clear, the 
    40   /// graph wrappers. While the previous ones are more or less clear, the 
    41   /// latter notion needs further explanation.
    41   /// latter notion needs further explanation.
    42   /// Graph wrappers are graph classes which serve for considering graph 
    42   /// Graph wrappers are graph classes which serve for considering graph 
    43   /// structures in different ways. A short example makes the notion much 
    43   /// structures in different ways. A short example makes the notion much 
    97   ///Base type for the Graph Wrappers
    97   ///Base type for the Graph Wrappers
    98 
    98 
    99   ///\warning Graph wrappers are in even more experimental state than the other
    99   ///\warning Graph wrappers are in even more experimental state than the other
   100   ///parts of the lib. Use them at you own risk.
   100   ///parts of the lib. Use them at you own risk.
   101   ///
   101   ///
   102   ///This is the base type for the Graph Wrappers.
   102   /// This is the base type for most of LEMON graph wrappers. 
   103   ///\todo Some more docs... 
   103   /// This class implements a trivial graph wrapper i.e. it only wraps the 
       
   104   /// functions and types of the graph. The purpose of this class is to 
       
   105   /// make easier implementing graph wrappers. E.g. if a wrapper is 
       
   106   /// considered which differs from the wrapped graph only in some of its 
       
   107   /// functions or types, then it can be derived from GraphWrapper, and only the 
       
   108   /// differences should be implemented.
   104   ///
   109   ///
   105   ///\author Marton Makai 
   110   ///\author Marton Makai 
   106   template<typename Graph>
   111   template<typename Graph>
   107   class GraphWrapper {
   112   class GraphWrapper {
   108   protected:
   113   protected:
   234   /// A graph wrapper which reverses the orientation of the edges.
   239   /// A graph wrapper which reverses the orientation of the edges.
   235 
   240 
   236   ///\warning Graph wrappers are in even more experimental state than the other
   241   ///\warning Graph wrappers are in even more experimental state than the other
   237   ///parts of the lib. Use them at you own risk.
   242   ///parts of the lib. Use them at you own risk.
   238   ///
   243   ///
   239   /// A graph wrapper which reverses the orientation of the edges.
   244   /// Let \f$G=(V, A)\f$ be a directed graph and 
   240   /// Thus \c Graph have to be a directed graph type.
   245   /// suppose that a graph instange \c g of type 
   241   ///
   246   /// \c ListGraph implements \f$G\f$.
       
   247   /// \code
       
   248   /// ListGraph g;
       
   249   /// \endcode
       
   250   /// For each directed edge 
       
   251   /// \f$e\in A\f$, let \f$\bar e\f$ denote the edge obtained by 
       
   252   /// reversing its orientation. 
       
   253   /// Then RevGraphWrapper implements the graph structure with node-set 
       
   254   /// \f$V\f$ and edge-set 
       
   255   /// \f$\{\bar e : e\in A \}\f$, i.e. the graph obtained from \f$G\f$ be 
       
   256   /// reversing the orientation of its edges. The following code shows how 
       
   257   /// such an instance can be constructed.
       
   258   /// \code
       
   259   /// RevGraphWrapper<ListGraph> gw(g);
       
   260   /// \endcode
   242   ///\author Marton Makai
   261   ///\author Marton Makai
   243   template<typename Graph>
   262   template<typename Graph>
   244   class RevGraphWrapper : public GraphWrapper<Graph> {
   263   class RevGraphWrapper : public GraphWrapper<Graph> {
   245   public:
   264   public:
   246     typedef GraphWrapper<Graph> Parent; 
   265     typedef GraphWrapper<Graph> Parent; 
   312   
   331   
   313   ///\warning Graph wrappers are in even more experimental state than the other
   332   ///\warning Graph wrappers are in even more experimental state than the other
   314   ///parts of the lib. Use them at you own risk.
   333   ///parts of the lib. Use them at you own risk.
   315   ///
   334   ///
   316   /// This wrapper shows a graph with filtered node-set and 
   335   /// This wrapper shows a graph with filtered node-set and 
   317   /// edge-set. Given a bool-valued map on the node-set and one on 
   336   /// edge-set. 
   318   /// the edge-set of the graphs, the iterators show only the objects 
   337   /// Given a bool-valued map on the node-set and one on 
   319   /// having true value. 
   338   /// the edge-set of the graph, the iterators show only the objects 
   320   /// The quick brown fox iterators jump over 
   339   /// having true value. We have to note that this does not mean that an 
   321   /// the lazy dog nodes or edges if their values for are false in the 
   340   /// induced subgraph is obtained, the node-iterator cares only the filter 
   322   /// corresponding bool maps. 
   341   /// on the node-set, and the edge-iterators care only the filter on the 
       
   342   /// edge-set.
       
   343   /// \code
       
   344   /// typedef SmartGraph Graph;
       
   345   /// Graph g;
       
   346   /// typedef Graph::Node Node;
       
   347   /// typedef Graph::Edge Edge;
       
   348   /// Node u=g.addNode(); //node of id 0
       
   349   /// Node v=g.addNode(); //node of id 1
       
   350   /// Node e=g.addEdge(u, v); //edge of id 0
       
   351   /// Node f=g.addEdge(v, u); //edge of id 1
       
   352   /// Graph::NodeMap<bool> nm(g, true);
       
   353   /// nm.set(u, false);
       
   354   /// Graph::EdgeMap<bool> em(g, true);
       
   355   /// em.set(e, false);
       
   356   /// typedef SubGraphWrapper<Graph, Graph::NodeMap<bool>, Graph::EdgeMap<bool> > SubGW;
       
   357   /// SubGW gw(g, nm, em);
       
   358   /// for (SubGW::NodeIt n(gw); n!=INVALID; ++n) std::cout << g.id(n) << std::endl;
       
   359   /// std::cout << ":-)" << std::endl;
       
   360   /// for (SubGW::EdgeIt e(gw); e!=INVALID; ++e) std::cout << g.id(e) << std::endl;
       
   361   /// \endcode
       
   362   /// The output of the above code is the following.
       
   363   /// \code
       
   364   /// 1
       
   365   /// :-)
       
   366   /// 1
       
   367   /// \endcode
       
   368   /// Note that \c n is of type \c SubGW::NodeIt, but it can be converted to
       
   369   /// \c Graph::Node that is why \c g.id(n) can be applied.
   323   ///
   370   ///
   324   ///\author Marton Makai
   371   ///\author Marton Makai
   325   template<typename Graph, typename NodeFilterMap, 
   372   template<typename Graph, typename NodeFilterMap, 
   326 	   typename EdgeFilterMap>
   373 	   typename EdgeFilterMap>
   327   class SubGraphWrapper : public GraphWrapper<Graph> {
   374   class SubGraphWrapper : public GraphWrapper<Graph> {
   609   /// bidirected graph made from a directed one. 
   656   /// bidirected graph made from a directed one. 
   610   ///
   657   ///
   611   ///\warning Graph wrappers are in even more experimental state than the other
   658   ///\warning Graph wrappers are in even more experimental state than the other
   612   ///parts of the lib. Use them at you own risk.
   659   ///parts of the lib. Use them at you own risk.
   613   ///
   660   ///
   614   /// Suppose that for a directed graph $G=(V, A)$, 
   661   /// Let \f$G=(V, A)\f$ be a directed graph and for each directed edge 
   615   /// two bool valued maps on the edge-set, 
   662   /// \f$e\in A\f$, let \f$\bar e\f$ denote the edge obtained by
   616   /// $forward_filter$, and $backward_filter$ 
   663   /// reversing its orientation. We are given moreover two bool valued 
   617   /// is given, and we are dealing with the directed graph
   664   /// maps on the edge-set, 
   618   /// a $G'=(V, \{uv : uv\in A \mbox{ and } forward_filter(uv) \mbox{ is true}\}+\{vu : uv\in A \mbox{ and } backward_filter(uv) \mbox{ is true}\})$. 
   665   /// \f$forward\_filter\f$, and \f$backward\_filter\f$. 
       
   666   /// SubBidirGraphWrapper implements the graph structure with node-set 
       
   667   /// \f$V\f$ and edge-set 
       
   668   /// \f$\{e : e\in A \mbox{ and } forward\_filter(e) \mbox{ is true}\}+\{\bar e : e\in A \mbox{ and } backward\_filter(e) \mbox{ is true}\}\f$. 
   619   /// The purpose of writing + instead of union is because parallel 
   669   /// The purpose of writing + instead of union is because parallel 
   620   /// edges can arose. 
   670   /// edges can arise. (Similarly, antiparallel edges also can arise).
   621   /// In other words, a subgraph of the bidirected graph obtained, which 
   671   /// In other words, a subgraph of the bidirected graph obtained, which 
   622   /// is given by orienting the edges of the original graph in both directions.
   672   /// is given by orienting the edges of the original graph in both directions.
   623   /// An example for such a construction is the \c RevGraphWrapper where the 
   673   /// As the oppositely directed edges are logically different, 
       
   674   /// the maps are able to attach different values for them. 
       
   675   ///
       
   676   /// An example for such a construction is \c RevGraphWrapper where the 
   624   /// forward_filter is everywhere false and the backward_filter is 
   677   /// forward_filter is everywhere false and the backward_filter is 
   625   /// everywhere true. We note that for sake of efficiency, 
   678   /// everywhere true. We note that for sake of efficiency, 
   626   /// \c RevGraphWrapper is implemented in a different way. 
   679   /// \c RevGraphWrapper is implemented in a different way. 
   627   /// But BidirGraphWrapper is obtained from 
   680   /// But BidirGraphWrapper is obtained from 
   628   /// SubBidirGraphWrapper by considering everywhere true 
   681   /// SubBidirGraphWrapper by considering everywhere true 
   630   /// Finally, one of the most important applications of SubBidirGraphWrapper 
   683   /// Finally, one of the most important applications of SubBidirGraphWrapper 
   631   /// is ResGraphWrapper, which stands for the residual graph in directed 
   684   /// is ResGraphWrapper, which stands for the residual graph in directed 
   632   /// flow and circulation problems. 
   685   /// flow and circulation problems. 
   633   /// As wrappers usually, the SubBidirGraphWrapper implements the 
   686   /// As wrappers usually, the SubBidirGraphWrapper implements the 
   634   /// above mentioned graph structure without its physical storage, 
   687   /// above mentioned graph structure without its physical storage, 
   635   /// that is the whole stuff eats constant memory. 
   688   /// that is the whole stuff is stored in constant memory. 
   636   /// As the oppositely directed edges are logically different, 
       
   637   /// the maps are able to attach different values for them. 
       
   638   template<typename Graph, 
   689   template<typename Graph, 
   639 	   typename ForwardFilterMap, typename BackwardFilterMap>
   690 	   typename ForwardFilterMap, typename BackwardFilterMap>
   640   class SubBidirGraphWrapper : public GraphWrapper<Graph> {
   691   class SubBidirGraphWrapper : public GraphWrapper<Graph> {
   641   public:
   692   public:
   642     typedef GraphWrapper<Graph> Parent; 
   693     typedef GraphWrapper<Graph> Parent;