Home > Back-end >  Conditions of BFS algorithm is wrong, for bosses to help change
Conditions of BFS algorithm is wrong, for bosses to help change

Time:09-17

Excuse me this breadth-first calendar calculation method, what is the problem, I debug seems a for in BFS in the right condition, how to change?

//positioning points before and after the
Int FirstAdjVex (MGraph G, int v) {
//return v first adjacency point
int i;
For (I=0; I & lt; G - & gt; Nv. + + I) {
If (G - & gt; G [v] [I] adj==1 & amp; & Visited [I]==false)
return i;
}
return -1;
}//FirstAdjVex

Int NextAdjVex (MGraph G, int u, int w) {
//return v relative to w next adjacent point
int i;
For (I=w; I & lt; G - & gt; Nv. + + I) {
If (G - & gt; G [u] [I] adj==1 & amp; & Visited [I]==false)
return i;
}
return -1;
}//NextAdjVex


Void BFS (MGraph G, Vertex V, void * (Visit) (Vertex))
{
The Queue Q;
Int u, w;
Visit (V);
Visited [V]=true;
Q=the init ();
AddQ (& amp; Q, V);
while(! IsEmpty (& amp; Q))
{
U=DeleteQ (& amp; Q);

For (w=FirstAdjVex (G, u); W & gt;=0; W=NextAdjVex (G, u, w)) {
//check all of u in turn adjacency point w, modify the statement that will be able to get correct

If (Visited [w]!=1) {//w for u haven't access to adjacent vertex
Visit (w);
Visited [w]=true; Access w//, and access to sign an array of the corresponding weight value is true
AddQ (& amp; Q, w);//w into team
}//if
//printf (" speed ");
}//for

}
}
  • Related