473,657 Members | 2,571 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.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) ;
}



}
Mar 20 '07 #1
1 1062
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
1249
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
2911
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 this? Thank You,
0
8823
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8503
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8605
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6163
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4151
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4301
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
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 we have to send another system
2
1950
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1607
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.