473,503 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ergent plz

12 New Member
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);
}



}
Mar 20 '07 #1
1 1056
alaa123
12 New Member
plz if u know any thing help me

i implement BFS algorithm but i think my run class not correct 100%

plz spot with me what iam wrong with
Mar 20 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
1245
by: mandana | last post by:
hi there I needa load a toolbox inside my application program. it's so ergent.help as soon as possible .tnx
3
2904
by: arial | last post by:
Hi all, I need some ergent help from you expert. I have a MS sql store procedure which I like to get execute autometically everyday at midnight. Can anyone suggest or help on how to accomplish...
0
7261
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6974
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7445
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4991
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4665
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3147
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1492
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
369
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.