src/work/graphdemo.cc
author alpar
Sat, 13 Dec 2003 15:44:50 +0000
changeset 3 272a5677bd6d
parent 2 37117ebbabe2
child 35 65dca0f43fba
permissions -rw-r--r--
- Marci type iterator constructors
- src/demo/bfsdemo.cc: demo for bfs.h
- cosmetical changes
     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.isValid();n++)
    47     for(G.GetFirst(m);m.isValid();++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.isValid();++a)
    54     cout << a->id << ":" << a.From()->id << "->" << a.To()->id << "   ";
    55 
    56   cout << "\n\n\n";
    57   
    58   for(G.GetFirst(n);n.isValid();++n)
    59     {
    60       cout << n->id << "->";
    61       for(G.GetFirst(e,n);e.isValid();++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.isValid();n++)
    74     for(m=G.First();m.isValid();++m)
    75       if(n!=m) G.AddEdge(n,m)->id=++i;
    76    
    77   ;
    78   for(n=G.First();n.isValid();++n) //Demo
    79     {
    80       e=G.First(n);
    81       while(e.isValid())
    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.isValid();++a)
    89     cout << a->id << ": " << a.From()->id << "->" << a.To()->id << "   ";
    90   
    91   cout << "\n\n\n";
    92   
    93   for(n=G.First();n.isValid();++n)
    94     {
    95       cout << n->id << "->";
    96       for(e=G.First(n);e.isValid();++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;
   111       for(TestGraph::NodeIterator n(G);n.isValid();n++)
   112 	for(TestGraph::NodeIterator m(G);m.isValid();++m)
   113 	  if(n!=m) G.AddEdge(n,m)->id=++i;
   114     }
   115     
   116     for(TestGraph::NodeIterator n(G);n.isValid();++n) //Demo
   117       {
   118 	TestGraph::OutEdgeIterator e(G,n);
   119 	while(e.isValid())
   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.isValid();++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.isValid();++n)
   130       {
   131 	cout << n->id << "->";
   132 	for(TestGraph::OutEdgeIterator e(G,n);e.isValid();++e)
   133 	  cout << e->id << ":" << e.To()->id << ' ';
   134 	cout << '\n';
   135       }
   136   }
   137 }