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.mainProgram = mainProgram;
//ensure nothing is already highlighted
anim.deemphasizeAll();
}
public void run(){
AlgNode currentAN=null;
boolean found = false;
if((startv==null)||(endv==null)){
return;
}
currentAN = start;
st.addAction("Breadth First Search from "+startv.getNodeName()+" to " +endv.getNodeName());
//check in case start and end vertex are the same
if (startv.equals(endv)){
dpp.emphasizeLine(BFSPseudoPanel.ELSE_RETURN_TRUE) ;
pauseIfStepByStep();
if(!stepByStep) try{Thread.sleep(pauseTime);}catch(Exception e){};
st.addAction("The start vertex is also the destination.");
st.addAction("We do not need to carry out the seach.");
found = true;
}else{
dpp.emphasizeLine(BFSPseudoPanel.IF_START_NOT);
pauseIfStepByStep();
if(!stepByStep)try{Thread.sleep(pauseTime);}catch( Exception e){};
dpp.emphasizeLine(BFSPseudoPanel.MARK_START);
st.addAction("Mark the start vertex as visited.");
startv.mark();
markedVs.add(startv);
//this.q.add(start);
anim.lowlight(startv);
st.addAlgorithmState("", strMarkedVs(), q.strContents());
if(!stepByStep)try{Thread.sleep(pauseTime/3);}catch(Exception e){};
pauseIfStepByStep();
//Add the AlgNode containing the start vertex to the queue
dpp.emphasizeLine(BFSPseudoPanel.PUSH_START);
st.addAction("Add it on to the queue.");
this.q.add(start);
st.addAlgorithmState("", strMarkedVs(), q.strContents());
found = false;
pauseIfStepByStep();
}
Vertex v = null;
search:
while(!q.isEmpty()){
// Vertex u = (Vertex) q.firstElement();
q.add(startv);
q.remove();
//peek at the vertex on the stack
dpp.emphasizeLine(BFSPseudoPanel.WHILE);
pauseIfStepByStep();
if(!stepByStep) try{Thread.sleep(pauseTime);}catch(Exception ex){};
if (v!=null) anim.lowlightNoDelay(v);
// dpp.emphasizeLine(BFSPseudoPanel.STACK_PEEK);
// st.addAction("Peek at the stack, selecting the vertex at the top.");
currentAN = q.remove();
v = currentAN.getVertex();
//tells the animator to colour the node as selected
anim.select(v);
st.addAlgorithmState(v.getNodeName(), strMarkedVs(), q.strContents());
pauseIfStepByStep();
//find the edge which leads to the current vertex's first unmarked neighbour
Edge e = currentAN.getEdgeToFirstUnmarkedNeighour();
//find the current vertex's first unmarked neighbour
AlgNode fun = currentAN.getFirstUnmarkedNeighbour();
st.addAction("Does the selected vertex have at least one unmarked neighbour?");
if(fun!=null){
dpp.emphasizeLine(DFSPseudoPanel.IF_UNMARKED_NEIGH BOURS);
//pauseIfStepByStep();
st.addAction("It does. Find its first unmarked neighbour.");
//animate the edge extending from the current node to its first unmarked neighbour
anim.highlightAndExpand(e, currentAN.getVertex());
pauseIfStepByStep();
dpp.emphasizeLine(BFSPseudoPanel.NEIGHBOUR_BECOMES );
//highlight the first unmarked neighbour
anim.highlight(fun.getVertex());
pauseIfStepByStep();
st.addAction("Is this neighbour ("+ fun.getVertex().getNodeName()+") the vertex we are searching for?");
//check whether this neighbour is the vertex we are looking for
if(fun.getVertex().equals(endv)){
dpp.emphasizeLine(BFSPseudoPanel.IF_NEIGHBOUR_IS_E ND);
st.addAction("It is.");
found = true;
currentAN = fun;
pauseIfStepByStep();
break search;
}
//if it wasn't the one we were looking for
dpp.emphasizeLine(BFSPseudoPanel.ELSE_NOT_END);
pauseIfStepByStep();
if(!stepByStep) try{Thread.sleep(pauseTime);}catch(Exception ex){};
st.addAction("No it isn't.");
//mark it
dpp.emphasizeLine(BFSPseudoPanel.MARK_NEIGHBOUR);
st.addAction("Mark 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(fun.getVertex());
//colour the edge as lowlighted.
//Not sure if i like this. Maybe just better to call anim.deemphasize(e) instead
//anim.lowlight(e);
anim.deemphasize(e);
//colour the vertex so that the user can see it is marked
anim.lowlight(fun.getVertex());
pauseIfStepByStep();
st.addAlgorithmState(v.getNodeName(), strMarkedVs(), q.strContents());
//add the neighbour to the stack
dpp.emphasizeLine(BFSPseudoPanel.PUSH_NEIGHBOUR);
st.addAction("Add it to the queue.");
q.add(fun);
st.addAlgorithmState(v.getNodeName(), strMarkedVs(), q.strContents());
pauseIfStepByStep();
//dpp.emphasizeLine(DFSPseudoPanel.CLOSE_ELSE_NOT_EN D);
}//there were no unmarked neighbours for the current vertex
//remove it from the stack
else{st.addAction("No it does not.");
//dpp.emphasizeLine(BFSPseudoPanel.REMOVE_QUEUE);
st.addAction("Remove the vertex from the queue");
q.remove();
st.addAlgorithmState(v.getNodeName(), strMarkedVs(), q.strContents());
pauseIfStepByStep();
}
//unselect the current node. We know that the node must have been marked, so here
//we ensure that that it is coloured appropriately.
//anim.lowlightNoDelay(v);
dpp.emphasizeLine(BFSPseudoPanel.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.getAncestors();
for(int i =0; i<as.length; i++){
path = path+ as[i].getNodeName()+"-";
}
path = path + endv.getNodeName() +".";
st.addAction("The search was succesful.");
st.addAction("Breadth First Search found the path "+path);
}
else{
dpp.emphasizeLine(BFSPseudoPanel.RETURN_FALSE);
st.addAction("The queue is empty. No path exists between " + startv.getNodeName()+ " and "+endv.getNodeName()+".");
}
//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.restoreOriginal();
mainProgram.afterEx();
}
private String strMarkedVs(){
String strMarked = "";
for(int i=0; i<markedVs.size();i++){
strMarked = strMarked + markedVs.get(i).getNodeName()+",";
}
if (strMarked.length()>0) strMarked = strMarked.substring(0, strMarked.length()-1);
return strMarked;
}
public void setAnimatorStepByStep(boolean stepByStep){
anim.setStepByStep(stepByStep);
}
}