Results 1 to 3 of 3
  1. #1
    jocdrew21 is offline Senior Member
    Join Date
    Jan 2014
    Posts
    137
    Rep Power
    0

    Default 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;
    	}
    In the controller class I implement BFS:

    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;
    	}
    Main:
    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);
    		    }
    		    });
    		
    	}
    
    }
    I have also attached a picture of what I am trying to achieve.
    Attached Thumbnails Attached Thumbnails Breath First Search-bfs-issue.png   Breath First Search-correct-bfs.png  

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default 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.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default 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,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Adjacency Graphs and Breath First Search
    By jocdrew21 in forum New To Java
    Replies: 0
    Last Post: 12-28-2014, 12:32 PM
  2. Replies: 1
    Last Post: 12-01-2012, 03:20 AM
  3. Replies: 4
    Last Post: 03-24-2011, 11:50 PM
  4. synchronization kid riding a swing n catching breath
    By jodeen20 in forum Threads and Synchronization
    Replies: 2
    Last Post: 09-10-2008, 01:46 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •