bfs.cc

Go to the documentation of this file.
00001 
00002 /*
00003  * bfs.cc
00004  * Copyright (C) 2000 by the University of Southern California
00005  * $Id: bfs.cc,v 1.5 2005/08/25 18:58:11 johnh Exp $
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License,
00009  * version 2, as published by the Free Software Foundation.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License along
00017  * with this program; if not, write to the Free Software Foundation, Inc.,
00018  * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
00019  *
00020  *
00021  * The copyright of this module includes the following
00022  * linking-with-specific-other-licenses addition:
00023  *
00024  * In addition, as a special exception, the copyright holders of
00025  * this module give you permission to combine (via static or
00026  * dynamic linking) this module with free software programs or
00027  * libraries that are released under the GNU LGPL and with code
00028  * included in the standard release of ns-2 under the Apache 2.0
00029  * license or under otherwise-compatible licenses with advertising
00030  * requirements (or modified versions of such code, with unchanged
00031  * license).  You may copy and distribute such a system following the
00032  * terms of the GNU GPL for this module and the licenses of the
00033  * other code concerned, provided that you include the source code of
00034  * that other code when and as the GNU GPL requires distribution of
00035  * source code.
00036  *
00037  * Note that people who make modified versions of this module
00038  * are not obligated to grant this special exception for their
00039  * modified versions; it is their choice whether to do so.  The GNU
00040  * General Public License gives permission to release a modified
00041  * version without this exception; this exception also makes it
00042  * possible to release a modified version which carries forward this
00043  * exception.
00044  *
00045  */
00046 
00047 /*
00048  * Implementation of Breadth First Search algorithm
00049  * contributed to ns
00050  * George Riley, Georgia Tech, Winter 2000
00051  *
00052  */
00053 
00054 #include "config.h"
00055 #ifdef HAVE_STL
00056 #include "routealgo/bfs.h"
00057 #include "routealgo/routealgo.h"
00058 #include "routealgo/rnode.h"
00059 #include "routealgo/tnode.h"
00060 #include "routealgo/rbitmap.h"
00061 #include <stdio.h>
00062 
00063 #ifndef TEST_BFS
00064 
00065 void BFS(
00066          RNodeVec_t& N,
00067          nodeid_t root,
00068          RoutingVec_t& NextHop,
00069          RoutingVec_t& Parent)
00070 {  // Compute shortest path to all nodes from node S using BreadthFirstSearch
00071 BitMap B(N.size()); // Make a bitmap for the colors (grey/white) (white = 0)
00072 RNodeDeq_t Q;       // And a vector for the Q list
00073 DistVec_t  D;       // And the distance vector
00074 
00075  // Fill in all "NONE" in the next hop neighbors and predecessors
00076  NextHop.erase(NextHop.begin(), NextHop.end());
00077  Parent.erase(Parent.begin(), Parent.end());
00078  for (unsigned int i = 0; i < N.size(); i++)
00079    {
00080      NextHop.push_back(NODE_NONE);
00081      Parent.push_back(NODE_NONE);
00082      D.push_back(INF);
00083      // Debug...print adj lists
00084      NodeWeight_t v(NODE_NONE, INF);
00085      if(0)printf("Printing adj for node %ld (addr %p)\n", N[i]->m_id, N[i]);
00086      if(0)while(1)
00087        {
00088          v = N[i]->NextAdj(v);
00089          if (v.first == NODE_NONE) break;
00090          if(0)printf("Found adj %ld\n", v.first);
00091        }
00092    }
00093  B.Set(root); // Color the root grey
00094  if(0)B.DBPrint();
00095  Q.push_back(N[root]); // And put the root in Q
00096  D[root] = 0;
00097  while(Q.size() != 0)
00098    {
00099      RNodeDeq_it it = Q.begin();
00100      NodeWeight_t v(NODE_NONE, INF);
00101      RNode* u = *it;
00102      if(0)printf("Working on node %ld addr %p\n", u->m_id, u);
00103      while(1)
00104        {
00105          v = u->NextAdj(v);
00106          if (v.first == NODE_NONE) break;
00107          if(0)printf("Found adj %ld\n", v.first);
00108          if (B.Get(v.first) == 0)
00109            { // White
00110              Q.push_back(N[v.first]);     // Add to Q set
00111              B.Set(v.first);              // Change to grey
00112              D[v.first] = D[u->m_id] + 1; // Set new distance
00113              Parent[v.first] = u->m_id;   // Set parent
00114              if (u->m_id == root)
00115                { // First hop is new node since this is root
00116                  NextHop[v.first] = v.first;
00117                }
00118              else
00119                { // First hop is same as this one
00120                  NextHop[v.first] = NextHop[u->m_id];
00121                }
00122              if(0)printf("Enqueued %ld\n", v.first);
00123            }
00124        }
00125      Q.pop_front();
00126    }
00127 }
00128 #endif
00129 
00130 #ifdef TEST_BFS
00131 RNodeVec_t Nodes;
00132 
00133 int main()
00134 {
00135   // See the sample BFS search in Fig23.3, p471 CLR Algorithms book
00136 Node N0(0);
00137 Node N1(1);
00138 Node N2(2);
00139 Node N3(3);
00140 Node N4(4);
00141 Node N5(5);
00142 Node N6(6);
00143 Node N7(7);
00144 RoutingVec_t NextHop;
00145 RoutingVec_t Parent;
00146 
00147  N0.AddAdj(1);
00148  N0.AddAdj(2);
00149 
00150  N1.AddAdj(0);
00151 
00152  N2.AddAdj(0);
00153  N2.AddAdj(3);
00154 
00155  N3.AddAdj(2);
00156  N3.AddAdj(4);
00157  N3.AddAdj(5);
00158 
00159  N4.AddAdj(3);
00160  N4.AddAdj(5);
00161  N4.AddAdj(6);
00162 
00163  N5.AddAdj(4);
00164  N5.AddAdj(7); 
00165 
00166  N6.AddAdj(4);
00167  N6.AddAdj(7);
00168 
00169  N7.AddAdj(5);
00170  N7.AddAdj(6);
00171 
00172  Nodes.push_back(&N0);
00173  Nodes.push_back(&N1);
00174  Nodes.push_back(&N2);
00175  Nodes.push_back(&N3);
00176  Nodes.push_back(&N4);
00177  Nodes.push_back(&N5);
00178  Nodes.push_back(&N6);
00179  Nodes.push_back(&N7);
00180 
00181  for (nodeid_t i = 0; i < Nodes.size(); i++)
00182    { // Get shortest path for each root node
00183      printf("\nFrom root %ld\n", i);
00184      BFS(Nodes, i, NextHop, Parent);
00185      PrintParents(Parent);
00186      for (unsigned int k = 0; k < Nodes.size(); k++)
00187        printf("Next hop for node %d is %ld\n", k, NextHop[k]);
00188      printf("Printing paths\n");
00189      for (nodeid_t j = 0; j < Nodes.size(); j++)
00190        {
00191          PrintRoute(i, j, Parent);
00192        }
00193    }
00194  return(0);
00195 }
00196 #endif
00197 
00198 #endif //HAVE_STL

Generated on Tue Mar 6 16:47:43 2007 for ns2 Network Simulator 2.29 by  doxygen 1.4.6