is there any thing wrong with this Breadth First search algorithm
plz help me
import java.util.*;
public class BFS extends GraphAlgorithm {
Vertex startv;
Vertex endv;
//I use this to keep track of vertices which we mark, as we need to unmark them at the end of the algorithm
Vector<Vertex> markedVs = new Vector<Vertex>( );
SearchTable st;
QueuePanel q;//sorry about calling our stack q-wasn't thinking at the time!
GraphPanel gp;
AlgNode start;
Animator anim;
BFSPseudoPanel dpp;
//public boolean wait = true;
private int pauseTime = 2000;
public BFS(Vertex startv, Vertex endv, QueuePanel q, SearchTable st, BFSPseudoPanel dpp, GraphPanel gp,
boolean stepByStep, Algorithms mainProgram){
this.startv = startv;
this.endv = endv;
this.st = st;
this.start = new AlgNode(startv) ;
this.q = q;
this.stepByStep = stepByStep;
anim = new Animator(gp, stepByStep);
this.dpp = dpp;
this.mainProgra m = mainProgram;
//ensure nothing is already highlighted
anim.deemphasiz eAll();
}
public void run(){
AlgNode currentAN=null;
boolean found = false;
if((startv==nul l)||(endv==null )){
return;
}
currentAN = start;
st.addAction("B readth First Search from "+startv.getNod eName()+" to " +endv.getNodeNa me());
//check in case start and end vertex are the same
if (startv.equals( endv)){
dpp.emphasizeLi ne(BFSPseudoPan el.ELSE_RETURN_ TRUE);
pauseIfStepBySt ep();
if(!stepByStep) try{Thread.slee p(pauseTime);}c atch(Exception e){};
st.addAction("T he start vertex is also the destination.");
st.addAction("W e do not need to carry out the seach.");
found = true;
}else{
dpp.emphasizeLi ne(BFSPseudoPan el.IF_START_NOT );
pauseIfStepBySt ep();
if(!stepByStep) try{Thread.slee p(pauseTime);}c atch(Exception e){};
dpp.emphasizeLi ne(BFSPseudoPan el.MARK_START);
st.addAction("M ark the start vertex as visited.");
startv.mark();
markedVs.add(st artv);
//this.q.add(star t);
anim.lowlight(s tartv);
st.addAlgorithm State("", strMarkedVs(), q.strContents() );
if(!stepByStep) try{Thread.slee p(pauseTime/3);}catch(Excep tion e){};
pauseIfStepBySt ep();
//Add the AlgNode containing the start vertex to the queue
dpp.emphasizeLi ne(BFSPseudoPan el.PUSH_START);
st.addAction("A dd it on to the queue.");
this.q.add(star t);
st.addAlgorithm State("", strMarkedVs(), q.strContents() );
found = false;
pauseIfStepBySt ep();
}
Vertex v = null;
search:
while(!q.isEmpt y()){
// Vertex u = (Vertex) q.firstElement( );
q.add(startv);
q.remove();
//peek at the vertex on the stack
dpp.emphasizeLi ne(BFSPseudoPan el.WHILE);
pauseIfStepBySt ep();
if(!stepByStep) try{Thread.slee p(pauseTime);}c atch(Exception ex){};
if (v!=null) anim.lowlightNo Delay(v);
// dpp.emphasizeLi ne(BFSPseudoPan el.STACK_PEEK);
// st.addAction("P eek at the stack, selecting the vertex at the top.");
currentAN = q.remove();
v = currentAN.getVe rtex();
//tells the animator to colour the node as selected
anim.select(v);
st.addAlgorithm State(v.getNode Name(), strMarkedVs(), q.strContents() );
pauseIfStepBySt ep();
//find the edge which leads to the current vertex's first unmarked neighbour
Edge e = currentAN.getEd geToFirstUnmark edNeighour();
//find the current vertex's first unmarked neighbour
AlgNode fun = currentAN.getFi rstUnmarkedNeig hbour();
st.addAction("D oes the selected vertex have at least one unmarked neighbour?");
if(fun!=null){
dpp.emphasizeLi ne(DFSPseudoPan el.IF_UNMARKED_ NEIGHBOURS);
//pauseIfStepBySt ep();
st.addAction("I t does. Find its first unmarked neighbour.");
//animate the edge extending from the current node to its first unmarked neighbour
anim.highlightA ndExpand(e, currentAN.getVe rtex());
pauseIfStepBySt ep();
dpp.emphasizeLi ne(BFSPseudoPan el.NEIGHBOUR_BE COMES);
//highlight the first unmarked neighbour
anim.highlight( fun.getVertex() );
pauseIfStepBySt ep();
st.addAction("I s this neighbour ("+ fun.getVertex() .getNodeName()+ ") the vertex we are searching for?");
//check whether this neighbour is the vertex we are looking for
if(fun.getVerte x().equals(endv )){
dpp.emphasizeLi ne(BFSPseudoPan el.IF_NEIGHBOUR _IS_END);
st.addAction("I t is.");
found = true;
currentAN = fun;
pauseIfStepBySt ep();
break search;
}
//if it wasn't the one we were looking for
dpp.emphasizeLi ne(BFSPseudoPan el.ELSE_NOT_END );
pauseIfStepBySt ep();
if(!stepByStep) try{Thread.slee p(pauseTime);}c atch(Exception ex){};
st.addAction("N o it isn't.");
//mark it
dpp.emphasizeLi ne(BFSPseudoPan el.MARK_NEIGHBO UR);
st.addAction("M ark the neighbour as visited.");
fun.getVertex() .mark();
//add it to the vector of marked vertices (as at the end we will need to unmark it)
markedVs.add(fu n.getVertex());
//colour the edge as lowlighted.
//Not sure if i like this. Maybe just better to call anim.deemphasiz e(e) instead
//anim.lowlight(e );
anim.deemphasiz e(e);
//colour the vertex so that the user can see it is marked
anim.lowlight(f un.getVertex()) ;
pauseIfStepBySt ep();
st.addAlgorithm State(v.getNode Name(), strMarkedVs(), q.strContents() );
//add the neighbour to the stack
dpp.emphasizeLi ne(BFSPseudoPan el.PUSH_NEIGHBO UR);
st.addAction("A dd it to the queue.");
q.add(fun);
st.addAlgorithm State(v.getNode Name(), strMarkedVs(), q.strContents() );
pauseIfStepBySt ep();
//dpp.emphasizeLi ne(DFSPseudoPan el.CLOSE_ELSE_N OT_END);
}//there were no unmarked neighbours for the current vertex
//remove it from the stack
else{st.addActi on("No it does not.");
//dpp.emphasizeLi ne(BFSPseudoPan el.REMOVE_QUEUE );
st.addAction("R emove the vertex from the queue");
q.remove();
st.addAlgorithm State(v.getNode Name(), strMarkedVs(), q.strContents() );
pauseIfStepBySt ep();
}
//unselect the current node. We know that the node must have been marked, so here
//we ensure that that it is coloured appropriately.
//anim.lowlightNo Delay(v);
dpp.emphasizeLi ne(BFSPseudoPan el.CLOSE_WHILE) ;
}
//if we found the vertex that we were looking for, print out the route to the command line
//obviously, we will not do this in the finished version, but it's a good way to
//check that the algorithm functions as expected
if (found == true){
String path = "";
Vertex[] as = currentAN.getAn cestors();
for(int i =0; i<as.length; i++){
path = path+ as[i].getNodeName()+ "-";
}
path = path + endv.getNodeNam e() +".";
st.addAction("T he search was succesful.");
st.addAction("B readth First Search found the path "+path);
}
else{
dpp.emphasizeLi ne(BFSPseudoPan el.RETURN_FALSE );
st.addAction("T he queue is empty. No path exists between " + startv.getNodeN ame()+ " and "+endv.getNodeN ame()+".");
}
//Very important to make sure that we unmark all of the vertices which we marked.
//otherwise future algorithms will not run properly
for(int i=0; i<markedVs.size ();i++){
markedVs.get(i) .unmark();
}
anim.restoreOri ginal();
mainProgram.aft erEx();
}
private String strMarkedVs(){
String strMarked = "";
for(int i=0; i<markedVs.size ();i++){
strMarked = strMarked + markedVs.get(i) .getNodeName()+ ",";
}
if (strMarked.leng th()>0) strMarked = strMarked.subst ring(0, strMarked.lengt h()-1);
return strMarked;
}
public void setAnimatorStep ByStep(boolean stepByStep){
anim.setStepByS tep(stepByStep) ;
}
}