Results 1 to 3 of 3
Thread: Breath First Search
- 01-10-2015, 02:15 PM #1
Senior Member
- Join Date
- Jan 2014
- Posts
- 137
- Rep Power
- 0
Breath First Search
I have been working on this program for a while and I am just about finished. I am just having an issue traversing the graph. I shortened the graph to 10x10 instead of 25x25. As of right now it only searches linearly and not a box method, I attached a picture of what I mean.
I am handling this problem like so:
In the view class I created a matrix of buttons, and placed them on a panel.
Java Code:JPanel matrixPan(){ matrixBtn = new Mybuttons[size][size]; JPanel panel = new JPanel(); panel.setSize(550,550); panel.setLayout(new GridLayout(size,size)); //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 arraySize++; } } System.out.println("Size in view "+size); model.setSize(arraySize); panel.setVisible(true); return panel; }
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) { System.out.println("In second while loop"); child.model.visited = true; // turn child to a color here or print a value q.add(child); } } }
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 else if (view.matrixBtn[x][j].model.getTargetNode() == true) { System.out.println("Found it"); break; } System.out.println("leaving Child Node"); } return null; }
Java Code:package path.finding; import javax.swing.SwingUtilities; public class Main { public static void main(String args[]){ SwingUtilities.invokeLater(new Runnable() { View view =new View(); Model model = new Model(); public void run() { new Controller(view,model); } }); } }
- 01-10-2015, 10:03 PM #2
Re: Breath First Search
Do you have a specific java programming question?
If you don't understand my response, don't ignore it, ask a question.
- 01-10-2015, 10:48 PM #3
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
Re: Breath First Search
I believe you mean breadth, not breath. Normally I wouldn't mention it but since it is also that way in your app I thought you should be aware.
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
Similar Threads
-
Adjacency Graphs and Breath First Search
By jocdrew21 in forum New To JavaReplies: 0Last Post: 12-28-2014, 12:32 PM -
Reading in a file, carrying out linear search, then sorting before Binary search
By JavaNewb26 in forum New To JavaReplies: 1Last Post: 12-01-2012, 03:20 AM -
Any advice - java search algorithms which accept multiple search parameters
By Alfster in forum New To JavaReplies: 4Last Post: 03-24-2011, 11:50 PM -
synchronization kid riding a swing n catching breath
By jodeen20 in forum Threads and SynchronizationReplies: 2Last Post: 09-10-2008, 01:46 PM
Bookmarks