src/work/bfsdemo2.cc
changeset 1080 568ce2be7fe0
parent 6 b63d1bc367f7
equal deleted inserted replaced
0:b47cd414a51f 1:4fb0daefddbb
    13 {
    13 {
    14   TestGraph G;
    14   TestGraph G;
    15   
    15   
    16   TestGraph::NodeIterator tn,n2;
    16   TestGraph::NodeIterator tn,n2;
    17   
    17   
    18   for(int i=1;i<=5000;i++)
    18   cout << "Create nodes\n";
       
    19 
       
    20   for(int i=1;i<=500;i++)
    19     {
    21     {
    20       *(tn=G.AddNode())=i;
    22       *(tn=G.AddNode())=i;
    21       if(i==2) n2=tn;
    23       if(i==2) n2=tn;
    22     }
    24     }
    23   
    25   
       
    26   cout << "Create Edges\n";
       
    27   
    24   for(TestGraph::NodeIterator n(G);n.isValid();++n)
    28   for(TestGraph::NodeIterator n(G);n.isValid();++n)
    25     for(TestGraph::NodeIterator m(G);m.isValid();++m)
    29     for(TestGraph::NodeIterator m(G);m.isValid();++m) if(n!=m)
    26       if(gcd(*n,*m)>1) G.AddEdge(n,m);
    30       if(gcd(*n,*m)>1) G.AddEdge(n,m);
    27   
    31   
       
    32   
       
    33   cout << "Run BFS\n";
       
    34 
    28   Bfs<default_bfs_T<TestGraph> > bfs;
    35   Bfs<default_bfs_T<TestGraph> > bfs;
    29 
    36 
    30   bfs.SetG(G);
    37   bfs.SetG(G);
    31   bfs.Init(n2);
    38   bfs.Init(n2);
    32   bfs.Run();
    39   bfs.Run();
    33 
    40 
    34   for(TestGraph::NodeIterator n(G);n.isValid();++n)
    41   for(TestGraph::NodeIterator n(G);n.isValid();++n)
    35     cout << Get(bfs.tree_map,n).From() << "->" << Get(bfs.tree_map,n).To()
    42     if((*n)!=2)
    36 	 << '\n';
    43       cout << (Get(bfs.dist_map,n)) << '\n';
    37 
    44 
       
    45   for(TestGraph::NodeIterator n(G);n.isValid();++n)
       
    46     if(Get(bfs.dist_map,n))
       
    47       cout << *(Get(bfs.tree_map,n).From()) << "->"
       
    48 	   << *(Get(bfs.tree_map,n).To())
       
    49 	   << '\n';
    38 }
    50 }