src/work/marci/merge_node_graph_wrapper_test.cc
author marci
Fri, 19 Nov 2004 17:22:29 +0000
changeset 1009 8cb323dbae93
parent 1008 3fef334f5f37
child 1013 b3bdd856faf4
permissions -rw-r--r--
RoadMap to STGraphWrapper
     1 #include <iostream>
     2 #include <fstream>
     3 
     4 #include <lemon/list_graph.h>
     5 #include <lemon/smart_graph.h>
     6 #include <lemon/dimacs.h>
     7 #include <merge_node_graph_wrapper.h>
     8 
     9 #include<lemon/concept_check.h>
    10 #include<lemon/concept/graph.h>
    11 
    12 using std::cout;
    13 using std::endl;
    14 
    15 using namespace lemon;
    16 using namespace lemon::concept;
    17 
    18 class Graph3 : ListGraph {
    19 public:
    20   class Node : public ListGraph::Node { };
    21   class Edge { };
    22 };
    23 
    24 int main() {
    25   typedef SmartGraph Graph1;
    26   typedef ListGraph Graph2;
    27   
    28   {
    29     checkConcept<StaticGraph, NewEdgeSetGraphWrapper<Graph1, Graph2> >(); 
    30   }
    31   {
    32     checkConcept<StaticGraph, MergeEdgeGraphWrapper<Graph1, Graph2> >(); 
    33   }
    34   
    35   Graph1 g;
    36   Graph2 h;
    37   typedef MergeEdgeGraphWrapper<Graph1, Graph2> GW;
    38   GW gw(g, h);
    39 
    40   std::ifstream f1("graph1.dim");
    41   std::ifstream f2("graph2.dim");
    42   readDimacs(f1, g);
    43   readDimacs(f2, h);
    44   {
    45 
    46 //   Graph1::Node n1=g.addNode();
    47 //   Graph1::Node n2=g.addNode();
    48 //   Graph1::Node n3=g.addNode();
    49 //   Graph2::Node n4=h.addNode();
    50 //   Graph2::Node n5=h.addNode();
    51 //   Graph2::Node n6=h.addNode();
    52 //   Graph1::Edge e1=g.addEdge(n1, n2);
    53 //   Graph1::Edge e2=g.addEdge(n1, n3);
    54 //   Graph2::Edge e3=h.addEdge(n4, n5);
    55 //   Graph2::Edge e4=h.addEdge(n4, n5);
    56   //GW::NodeIt n(gw)
    57   cout << "1st graph" << endl;
    58   cout << " nodes:" << endl;
    59   for (Graph1::NodeIt n(g); n!=INVALID; ++n) { 
    60     cout << "  " << g.id(n) << endl;
    61   }
    62   cout << " edges:" << endl;
    63   for (Graph1::EdgeIt n(g); n!=INVALID; ++n) { 
    64     cout << "  " << g.id(n) << ": " 
    65 	 << g.id(g.source(n)) << "->" << g.id(g.target(n)) << endl;
    66   }
    67   cout << "2nd graph" << endl;
    68   cout << " nodes:" << endl;
    69   for (Graph2::NodeIt n(h); n!=INVALID; ++n) { 
    70     cout << "  " << h.id(n) << endl;
    71   }
    72   cout << " edges:" << endl;
    73   for (Graph2::EdgeIt n(h); n!=INVALID; ++n) { 
    74     cout << "  " << h.id(n) << ": " 
    75 	 << h.id(h.source(n)) << "->" << h.id(h.target(n)) << endl;
    76   }
    77   cout << "merged graph" << endl;
    78   cout << " nodes:" << endl;
    79   for (GW::NodeIt n(gw); n!=INVALID; ++n) { 
    80     cout << "  "<< gw.id(n) << endl;
    81   }
    82   cout << " edges:" << endl;
    83   for (GW::EdgeIt n(gw); n!=INVALID; ++n) { 
    84     cout << "  " << gw.id(n) << ": " 
    85 	 << gw.id(gw.source(n)) << "->" << gw.id(gw.target(n)) << endl;
    86   }
    87 
    88   GW::NodeMap<int> nm(gw);
    89   int i=0;
    90   for (GW::NodeIt n(gw); n!=INVALID; ++n) { 
    91     ++i;
    92     nm.set(n, i);
    93   }
    94   for (Graph1::NodeIt n(g); n!=INVALID; ++n) { 
    95     cout << nm[GW::Node(n,INVALID,false)] << endl;
    96   }
    97   for (Graph2::NodeIt n(h); n!=INVALID; ++n) { 
    98     cout << nm[GW::Node(INVALID,n,true)] << endl;
    99   }
   100 
   101   gw.printNode();
   102 
   103   {
   104 //    typedef SmartGraph Graph1;
   105     typedef ListGraph Graph1;
   106     typedef ListGraph Graph2;
   107     Graph1 g;
   108     Graph2 h;
   109     typedef MergeNodeGraphWrapper<Graph1, Graph2> GW;
   110     GW gw(g, h);    
   111     gw.printNode();
   112   }
   113   {
   114 //    typedef SmartGraph Graph1;
   115     typedef Graph3 Graph1;
   116     typedef ListGraph Graph2;
   117     Graph1 g;
   118     Graph2 h;
   119     typedef MergeNodeGraphWrapper<Graph1, Graph2> GW;
   120     GW gw(g, h);    
   121     gw.printNode();
   122   }
   123   {
   124 //    typedef SmartGraph Graph1;
   125     typedef ListGraph Graph1;
   126     typedef Graph3 Graph2;
   127     Graph1 g;
   128     Graph2 h;
   129     typedef MergeNodeGraphWrapper<Graph1, Graph2> GW;
   130     GW gw(g, h);    
   131     gw.printNode();
   132   }
   133   }
   134   {
   135     Graph1 g;
   136     Graph2 h;
   137     typedef Graph1::Node Node1;
   138     typedef Graph2::Node Node2;
   139     typedef NewEdgeSetGraphWrapper<Graph1, Graph2> GW;
   140     Graph1::NodeMap<Graph2::Node> e_node(g);
   141     Graph2::NodeMap<Graph1::Node> n_node(h);
   142     GW gw(g, h, e_node, n_node);
   143     for (int i=0; i<4; ++i) { 
   144       Node1 n=g.addNode();
   145       e_node.set(n, INVALID);
   146     }
   147     for (int i=0; i<4; ++i) {
   148       Graph1::Node n1=g.addNode();
   149       Graph2::Node n2=h.addNode();
   150       e_node.set(n1, n2);
   151       n_node.set(n2, n1);
   152     }
   153     for (Graph2::NodeIt n(h); n!=INVALID; ++n)
   154       for (Graph2::NodeIt m(h); m!=INVALID; ++m)
   155 	if ((h.id(n)+h.id(m))%3==0) h.addEdge(n, m);
   156 //     Graph1::NodeIt n(g);
   157 //     Node1 n1=n;
   158 //     Node1 n2=++n;
   159 //     Node1 n3=++n;
   160 //     Node1 n4=n;
   161 //     gw.addEdge(n1, n2);
   162 //     gw.addEdge(n1, n2);
   163 //     for (EdgeIt(e))
   164 //   Graph1::Node n1=g.addNode();
   165 //   Graph1::Node n2=g.addNode();
   166 //   Graph1::Node n3=g.addNode();
   167 //   Graph2::Node n4=h.addNode();
   168 //   Graph2::Node n5=h.addNode();
   169     for (GW::EdgeIt e(gw); e!=INVALID; ++e) 
   170       cout << gw.id(e) << endl;
   171     for (GW::NodeIt n(gw); n!=INVALID; ++n) {
   172       if (e_node[n]==INVALID) {
   173  	cout<<gw.id(n)<<" INVALID"<<endl;
   174       } else {
   175 	cout <<gw.id(n)<<" "<<h.id(e_node[n])<<" out_edges: ";
   176 	//	cout << &e_node << endl;
   177 	//cout << &n_node << endl;
   178 	for (GW::OutEdgeIt e(gw, n); e!=INVALID; ++e) {
   179 	  cout<<gw.id(e)<<" ";
   180 	}
   181 	cout<< endl;
   182       }
   183     }
   184   }
   185 }