Sunday, December 28, 2014

Adjacency Graphs and Breath First Search
































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































I have been working on this program for a while, and other ones too. I put it down for a little bit and decided to tackle it again. I found where I am having the issue. It is not a syntax issue rather as it is implementation. I know how to create a Graph, much like:
































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































Java Code:










































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































//just an example
int i,j;
adj[i++][j++]= true;
adj[j++][i++]=true;








































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































I am trying to make GUI of BFS in action. There is a starting position where a Jbutton is selected to green, and end position where a JButton is selected to Red and barriers which are yellow. The object is to find the shortest path from the starting position to the ending position while going around the barriers. BFS will search level by level of the graph.































































































































































































































































































































































































































































































































I did not create a graph just a 2D matrix buttons and the issue I am having is if I implement my adj[i++][j++] concept eventually all the buttons will be set to true and I will be having the same issue. My main question is I need some advice on creating a graph via a 2D matrix of JButtons so that I can traverse them it with the following code.
































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































Java Code:










































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































//Breath First Search. The first node passed to it is the start button
void bfs(Mybuttons button){
Queue<Mybuttons> q = new LinkedList<Mybuttons>();
q.add(button);
button.setIcon(button.setButtonToBlueWhileSearched());
button.model.visited=true;
Mybuttons child = null;

while(!q.isEmpty()){
System.out.println("In while loop");
Mybuttons b = (Mybuttons)q.remove();

if(b.model.getTargetNode()==true){
System.out.println("Found it");
break;
}
b.setIcon(button.setButtonToBlueWhileSearched());

while((child=getUnvisitedChildNode(b))!=null){
child.model.visited=true;
//turn child to a color here or print a value
q.add(child);
System.out.println("In second while loop");

}

}

}








































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































Java Code:










































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































private Mybuttons getUnvisitedChildNode(GraphMethods g){

int x= g.getxCoordinate();
//int y= g.getyCoordinate();
int j=0;

System.out.println("In get Child Node");

for(int i=x; i<model.arraySize();i++,j++){

if(view.matrixBtn[x][j].model.getVisited()==false &&
view.matrixBtn[x][j].model.getBarrier()==false &&
view.matrixBtn[x][j].model.getTargetNode()==false){

view.matrixBtn[x][j].model.setVisited(true);
view.matrixBtn[x][j].setButtonToBlueWhileSearched();
System.out.println("in if statement");
return view.matrixBtn[x][j];
}
//if target node is found while in target node stop
//searching
if(view.matrixBtn[x][j].model.getTargetNode()==true){
System.out.println("Found it");
break;
}

System.out.println("leaving Child Node");
}

return null;
}








































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































This is how I am creating the matrix in the View Class:































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































Java Code:












































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































JPanel matrixPan(){
matrixBtn = new Mybuttons[25][25];
JPanel panel = new JPanel();
panel.setSize(550,550);
panel.setLayout(new GridLayout(25,25));
//creating a 25x25 matrix of buttons
for(int i=0; i<model.arraySize(); i++){
for(int j=0;j<model.arraySize();j++)
{
//use myButtons to create the matrix of buttons
panel.add(matrixBtn[i][j] = new Mybuttons());
matrixBtn[i][j].setxCoordinate(i);
matrixBtn[i][j].setyCoordinate(j);
//setting the size of the matrix
size++;
}
}
System.out.println("Size in view "+size);
model.setSize(size);
panel.setVisible(true);

return panel;
}








































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































I hope I was not confusing with my questions.






























































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































No comments:

Post a Comment