Results 1 to 5 of 5
  1. #1
    Blacky777 is offline Member
    Join Date
    Feb 2010
    Posts
    11
    Rep Power
    0

    Question Breadth First Search

    Hi folks

    I'm trying to develop a breadth first search method but having some problems. I'm trying to add a string (MI) as the lone member of a queue, and expand on that string using my nextStates( that string ) method if it doesnt match a pre-determined string (b) defined in the main. Below is my code, so far it doesn't return anything even though b is a next state of MI. Was wondering if anyone could point out why this is and a possible fix. Any help would be appreciated. Thanks

    Java Code:
    	public static boolean breadthFirstSearch (String b)
    	{
    		boolean flag = false;
    		Queue<String> qe=new LinkedList<String>();	//sets up queue
    		qe.add("MI");								//adds MI to queue
    		while(flag = false)						
    		{
    			qe.addAll(nextStates(qe.element())); 	//add all the nextStates of b to the queue  
    
    			if(qe.poll()== b)
    			{
    				flag = true;
    				System.out.println("found!");	//found the state therefore 
    			}
    		}
    
    
    		return true;    
    	}

  2. #2
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: Breadth First Search

    Your comparison of "qe.poll()== b" is incorrect - you cannot compare two string like that.
    Use e.g. "qe.poll().equals(b)" for that purpose or the case insensitive equivalent.

  3. #3
    kosmos890 is offline Member
    Join Date
    Apr 2012
    Posts
    40
    Rep Power
    0

    Default Re: Breadth First Search

    while(flag = false){ }
    boolean flag=true;
    ...
    while(flag) or while(flag==true)

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: Breadth First Search

    Quote Originally Posted by kosmos890 View Post
    boolean flag=true;
    ...
    while(flag)
    Good.

    Quote Originally Posted by kosmos890 View Post
    or while(flag==true)
    Bad. If you support comparing a boolean variable with a boolean literal for equivalence, why stop there? Why not
    Java Code:
    while ((((flag == true) == true) == true) == true) {
    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    kosmos890 is offline Member
    Join Date
    Apr 2012
    Posts
    40
    Rep Power
    0

    Default Re: Breadth First Search

    @DarrylBurke
    Thanks. It was my mistake!

Similar Threads

  1. Replies: 4
    Last Post: 03-24-2011, 11:50 PM
  2. Question with a Tree and Breadth First Search
    By Ing. Balderas in forum New To Java
    Replies: 1
    Last Post: 05-17-2010, 05:46 AM
  3. Graph - Breadth First search for searching a node ?
    By JavaInLove in forum Advanced Java
    Replies: 1
    Last Post: 10-06-2008, 10:17 PM
  4. Replies: 5
    Last Post: 08-26-2008, 03:43 PM
  5. Breadth First Search and Friends!
    By nokomis in forum New To Java
    Replies: 0
    Last Post: 11-09-2007, 08:16 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
  •