src/work/graphdemo.cc
changeset 2 37117ebbabe2
parent 1 207fb3c727cb
child 3 272a5677bd6d
equal deleted inserted replaced
0:a80cd5fb9704 1:609ad50d4314
     1 #include <iostream>
     1 #include <iostream>
     2 #include <graph.h>
     2 #include <graph.h>
       
     3 #include <bfs.h>
     3 
     4 
     4 using namespace NEGRO;
     5 using namespace NEGRO;
     5 using namespace std;
     6 using namespace std;
     6 
     7 
     7 class NodeData;
     8 class NodeData;
    12 class NodeData
    13 class NodeData
    13 {
    14 {
    14 public:
    15 public:
    15   int id;
    16   int id;
    16   bool isVis;
    17   bool isVis;
       
    18   bfs_node_data<TestGraph> bfs;
    17 };
    19 };
    18 
    20 
    19 class EdgeData
    21 class EdgeData
    20 {
    22 {
    21 public:
    23 public:
    22   int id;
    24   int id;
    23 };
    25 };
    24 
    26 
    25 
       
    26 typedef Graph<NodeData,EdgeData> TestGraph;
    27 typedef Graph<NodeData,EdgeData> TestGraph;
       
    28 
       
    29 /*
       
    30 struct isVis_map {};
       
    31 bool Get(isVis_map p,TestGraph::NodeIterator i) { return i->isVis;}
       
    32 void Set(isVis_map p,TestGraph::NodeIterator i,bool b) {  i->isVis=b;}
       
    33 */
       
    34 
       
    35 class my_bfs_maps
       
    36 {
       
    37 public:
       
    38   //isVis_map visited;  //node->bool (RW)
       
    39   class_element_map<TestGraph::NodeIterator,
       
    40 		    TestGraph::NodeType,
       
    41 		    bool,
       
    42 		    &NodeData::isVis> visited;
       
    43   struct _tree_map_t {
       
    44     typedef TestGraph::EdgeIterator value_type;
       
    45     void Set(const TestGraph::NodeIterator &n,const value_type &t)
       
    46     {
       
    47       cout << t.From()->id << "->" << t.To()->id << '\n';
       
    48     }
       
    49   } tree;
       
    50   do_nothing_map dist;   //node->int (W)
       
    51   do_nothing_map priority; //node->int (W)
       
    52   //priority_map priority; //node->int (W)
       
    53 };
       
    54 
       
    55 
       
    56 
       
    57 
       
    58 class IGraph 
       
    59 {
       
    60 public:
       
    61 
       
    62   //  struct NodeType {bfs_node_data<TestGraph> bfs;};
       
    63   struct NodeType {bool isVis;};
       
    64 
       
    65   vector<NodeType> nodes;
       
    66   
       
    67   class NodeIterator 
       
    68   {
       
    69   public:
       
    70     IGraph *G;
       
    71     int n;
       
    72     NodeIterator &operator ++() { n++; return *this;}
       
    73     NodeType &operator *() const { return G->nodes[n];}
       
    74     NodeType *operator ->() const { return &(G->nodes[n]);}
       
    75     bool isValid() const {return n<=5000;}
       
    76     int Index() {return n;} //csak a kiirashoz kell
       
    77   };
       
    78 
       
    79   void GetFirst(NodeIterator &i) {i.G=this;i.n=1;}
       
    80 
       
    81   class OutEdgeIterator 
       
    82   {    
       
    83   public:
       
    84     IGraph *G;
       
    85     int f,t;
       
    86     int gcd() { int a=f;int b=t;int c;while(c=a%b) {a=b;b=c;} ; return b;}
       
    87     OutEdgeIterator &operator ++() {while(++t<=5000&&gcd()==1);return *this;}
       
    88     bool isValid() const {return t<=5000;}
       
    89     NodeIterator From() const {NodeIterator i; i.G=G;i.n=f;return i;}
       
    90     NodeIterator To() const {NodeIterator i; i.G=G;i.n=t;return i;}
       
    91     NodeIterator Anode() const {return From();}
       
    92     NodeIterator Bnode() const {return To();}
       
    93   };
       
    94 
       
    95   typedef OutEdgeIterator EdgeIterator;
       
    96   void GetFirst(OutEdgeIterator &i,const NodeIterator &n)
       
    97   {i.G=this;i.f=n.n;i.t=0;++i;}
       
    98 
       
    99   IGraph() : nodes(5000) {}
       
   100 };
       
   101 
       
   102 class IMaps_t
       
   103 {
       
   104 public:
       
   105 //     class_element_map<IGraph::NodeIterator,
       
   106 //   		    IGraph::NodeType,
       
   107 //   		    bool,
       
   108 //   		    &IGraph::NodeType::isVis> visited;
       
   109   struct _visited_map_t {
       
   110     typedef bool value_type;
       
   111     void Set(const IGraph::NodeIterator &n,const value_type &t) { n->isVis=t; }
       
   112     value_type Get(const IGraph::NodeIterator &n) const { return n->isVis; }
       
   113   } visited;
       
   114   struct _tree_map_t {
       
   115     typedef IGraph::EdgeIterator value_type;
       
   116     void Set(const IGraph::NodeIterator &n,const value_type &t)
       
   117     { cout << t.From().Index() << "->" << t.To().Index() << '\n'; }
       
   118   } tree;
       
   119   do_nothing_map dist;   //node->int (W)
       
   120   do_nothing_map priority; //node->int (W)
       
   121 };
    27 
   122 
    28 void main()
   123 void main()
    29 {
   124 {
    30   TestGraph G;
   125   TestGraph G;
    31   TestGraph::NodeIterator n,m,o,p,q;
   126   TestGraph::NodeIterator n,m,o,p,q;
    80       while(e.isValid())
   175       while(e.isValid())
    81 	if((e->id)%2) G.Delete(e++);
   176 	if((e->id)%2) G.Delete(e++);
    82 	else ++e;
   177 	else ++e;
    83     }
   178     }
    84   
   179   
       
   180   // cout << "Number of edges: " << i << "\n\n";
       
   181 
    85   for(a=G.First();a.isValid();++a)
   182   for(a=G.First();a.isValid();++a)
    86     cout << a->id << ": " << a.From()->id << "->" << a.To()->id << "   ";
   183     cout << a->id << ": " << a.From()->id << "->" << a.To()->id << "   ";
    87   
   184   
    88   cout << "\n\n\n";
   185   cout << "\n\n\n";
    89   
   186   
    93       for(e=G.First(n);e.isValid();++e)
   190       for(e=G.First(n);e.isValid();++e)
    94 	cout << e->id << ":" << e.To()->id << ' ';
   191 	cout << e->id << ":" << e.To()->id << ' ';
    95       cout << '\n';
   192       cout << '\n';
    96     }
   193     }
    97   
   194   
       
   195   n=G.First();
       
   196 
       
   197 
       
   198   //G.Clean();
       
   199 
       
   200   cout << "\n\n\n BFS \n\n\n";
       
   201   //my_bfs_maps Maps;
       
   202   //  bfs_static_maps<TestGraph> Maps(&NodeData::bfs);
       
   203   
       
   204   /// bfs(G,n,Maps);
       
   205 
       
   206   cout << '\n';
       
   207 
       
   208   IGraph IG;
       
   209   IMaps_t IMaps;
       
   210 
       
   211   IGraph::NodeIterator in;
       
   212   IG.GetFirst(in);
       
   213   ++in;
       
   214   bfs(IG,in,IMaps);
       
   215   
       
   216   
    98 }
   217 }