Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-24-2011, 04:09 PM   PM User | #1
antislayer
New to the CF scene

 
Join Date: Apr 2011
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
antislayer is an unknown quantity at this point
DFS in Graphs

This is the implementation of DFS using stacks, but when I try to run this it gives an infinite loop at the first vertex. Graph class has a array of nodes as it's attribute. The node class is used for both Vertices and Edges. I've tested whether the graph is read in correctly or not and it has been. I could provide the input file and the readFile method used to generate the graph. I don't know why it gets stuck in an infinite loop here.

Code:
public static void dfs(Node start)
{
//DFS uses Stack data structure
Stack s=new Stack();
s.push(start);
start.visited=true;
System.out.println(start.item);
while(!s.isEmpty())
{
    Node n= (Node)s.peek();
            Node child=getUnvisitedChildNode(n);
    if(child!=null)
    {
        child.visited=true;
        System.out.println(child.item);
        s.push(child);
    }

    else
    {
        s.pop();
    }
}

}

public static Node getUnvisitedChildNode(Node n){
Node missing_child = new Node();
Node counter = new Node(n.item);
while(counter!=null){
     if (!counter.visited)
         missing_child = counter;
counter = counter.next;

}


return missing_child;
}

Last edited by antislayer; 04-24-2011 at 04:35 PM..
antislayer is offline   Reply With Quote
Old 04-26-2011, 04:38 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,645
Thanks: 4
Thanked 2,450 Times in 2,419 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
The problem will be here:
Code:
Node counter = new Node(n.item);
Counter is a semi-cloned node, and I assume that .item doesn't include an edge or node list. Real problem here is you are not modifying the real node's visited status. So you essentially are trying to fetch the first child of the first child over and over again since it doesn't realize its been visited.
I don't like the idea of having visited incorporated as a part of the Node class. If you do this, it means you need to go through and reset all item's visited status either prior to or after any traversal. A better option is to track the visited status in an array or collection, and compare it when you are iterating the whiles.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:50 PM.


Advertisement
Log in to turn off these ads.