src/work/graphdemo.cc
author marci
Mon, 23 Aug 2004 11:26:09 +0000
changeset 769 eb61fbc64c16
parent 3 272a5677bd6d
permissions -rw-r--r--
.
     1 #include <iostream>
     2 #include <graph.h>
     3 #include <bfs.h>
     4 
     5 using namespace NEGRO;
     6 using namespace std;
     7 
     8 class NodeData;
     9 class EdgeData;
    10 
    11 typedef Graph<NodeData,EdgeData> TestGraph;
    12 
    13 class NodeData
    14 {
    15 public:
    16   int id;
    17   bool isVis;
    18   bfs_node_data<TestGraph> bfs;
    19 };
    20 
    21 class EdgeData
    22 {
    23 public:
    24   int id;
    25 };
    26 
    27 typedef Graph<NodeData,EdgeData> TestGraph;
    28 
    29 int main()
    30 {
    31   TestGraph G;
    32   TestGraph::NodeIterator n,m;
    33   TestGraph::OutEdgeIterator e;
    34   int i;
    35 
    36   
    37   //for(i=1;i<=10;i++) G.AddNode().n=i; //Ez nagyon rossz!!!!!!!!
    38   for(i=1;i<=10;i++) G.AddNode()->id=i; //Ez a jo!!!!!!!!
    39 
    40   //n=G.AddNode();
    41   
    42    //for(i=1;i<=10;i++) cout << (G.AddNode()->n=i) << ' ';
    43    //cout << '\n';
    44  
    45   i=0;
    46   for(G.GetFirst(n);n.Valid();n++)
    47     for(G.GetFirst(m);m.Valid();++m)
    48       if(n!=m) G.AddEdge(n,m)->id=++i;
    49    
    50   cout << "Number of edges: " << i << "\n\n";
    51 
    52   TestGraph::AllEdgeIterator a;
    53   for(G.GetFirst(a);a.Valid();++a)
    54     cout << a->id << ":" << a.From()->id << "->" << a.To()->id << "   ";
    55 
    56   cout << "\n\n\n";
    57   
    58   for(G.GetFirst(n);n.Valid();++n)
    59     {
    60       cout << n->id << "->";
    61       for(G.GetFirst(e,n);e.Valid();++e)
    62 	cout << e->id << ":" << e.To()->id << ' ';
    63       cout << '\n';
    64     }
    65   
    66   cout << "\n\n\n\nB-verzio:\n\n\n";
    67   
    68   G.Clean();
    69 
    70   for(i=1;i<=10;i++) G.AddNode()->id=i;
    71   
    72   i=0;
    73   for(n=G.First();n.Valid();n++)
    74     for(m=G.First();m.Valid();++m)
    75       if(n!=m) G.AddEdge(n,m)->id=++i;
    76    
    77   ;
    78   for(n=G.First();n.Valid();++n) //Demo
    79     {
    80       e=G.First(n);
    81       while(e.Valid())
    82 	if((e->id)%2) G.Delete(e++);  //it may be nice to have a postfix ++
    83 	else ++e;
    84     }
    85   
    86   // cout << "Number of edges: " << i << "\n\n";
    87 
    88   for(a=G.First();a.Valid();++a)
    89     cout << a->id << ": " << a.From()->id << "->" << a.To()->id << "   ";
    90   
    91   cout << "\n\n\n";
    92   
    93   for(n=G.First();n.Valid();++n)
    94     {
    95       cout << n->id << "->";
    96       for(e=G.First(n);e.Valid();++e)
    97 	cout << e->id << ":" << e.To()->id << ' ';
    98       cout << '\n';
    99     }
   100   
   101   // For Marci's sake
   102   
   103   {
   104     G.Clean();
   105     
   106     for(int i=1;i<=10;i++) G.AddNode()->id=i;
   107     
   108     
   109     {  //I would'n say I'm really happy with this.
   110       int i=0;
   111       for(TestGraph::NodeIterator n(G);n.Valid();n++)
   112 	for(TestGraph::NodeIterator m(G);m.Valid();++m)
   113 	  if(n!=m) G.AddEdge(n,m)->id=++i;
   114     }
   115     
   116     for(TestGraph::NodeIterator n(G);n.Valid();++n) //Demo
   117       {
   118 	TestGraph::OutEdgeIterator e(G,n);
   119 	while(e.Valid())
   120 	  if((e->id)%2) G.Delete(e++);  //it may be nice to have a postfix ++
   121 	  else ++e;
   122       }
   123     
   124     for(TestGraph::AllEdgeIterator a(G);a.Valid();++a)
   125       cout << a->id << ": " << a.From()->id << "->" << a.To()->id << "   ";
   126     
   127     cout << "\n\n\n";
   128     
   129     for(TestGraph::NodeIterator n(G);n.Valid();++n)
   130       {
   131 	cout << n->id << "->";
   132 	for(TestGraph::OutEdgeIterator e(G,n);e.Valid();++e)
   133 	  cout << e->id << ":" << e.To()->id << ' ';
   134 	cout << '\n';
   135       }
   136   }
   137 }