Grafalgo
Library of useful data structures and algorithms
/Users/jst/src/grafalgo/cpp/graphAlgorithms/sPath/bfScan.cpp
00001 #include "stdinc.h"
00002 #include "List.h"
00003 #include "Wdigraph.h"
00004 
00005 using namespace grafalgo;
00006 
00007 void bfScan(Wdigraph& dig, vertex s, vertex p[], int d[]) {
00008 // Compute shortest path tree using breadth-first scanning algorithm.
00009         int pass; vertex v,w,last; edge e;
00010         List q(dig.n());
00011 
00012         for (v = 1; v <= dig.n(); v++) { p[v] = 0; d[v] = Util::BIGINT32; }
00013         d[s] = 0; q.addLast(s); pass = 0; last = s;
00014 
00015         while (!q.empty()) {
00016                 v = q.first(); q.removeFirst();
00017                 for (e = dig.firstOut(v); e != 0; e = dig.nextOut(v,e)) {
00018                         w = dig.head(e);
00019                         if (d[v] + dig.length(e) < d[w]) {
00020                                 d[w] = d[v] + dig.length(e); p[w] = v;
00021                                 if (!q.member(w)) q.addLast(w);
00022                         }
00023                 }
00024                 if (v == last && !q.empty()) { pass++; last = q.last(); }
00025                 if (pass == dig.n())
00026                         Util::fatal("bfScan: graph has negative cycle");
00027         }
00028 }
 All Classes Files Functions Variables Typedefs Friends