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