0

I have a graph which is essentially an ArrayList of Nodes, each of which stores their neighbors.

public class Node {
    ArrayList<Node> neighbors;
    String data;
    public Node() {
        data = null;
        neighbors = new ArrayList<Node>();
    }
}

I print out every path in this graph, but only do it n-levels deep. How should I go about coding this?

Or, if I should store this differently, feel free to let me know. But more importantly I want to know how to print out every path n-levels deep.

4

2 に答える 2

4

グラフの深さ制限されたトラバーサルを実行するだけです。これは深さ優先検索と同じですが、再帰ステップを除いてdepth、深さを下るたびにインクリメントされるという変数も追加します。次に、目的の深さに到達したら、単純に再帰を停止します。

于 2012-04-16T02:39:40.413 に答える
1
  1. Add an extra variable called visited in every node.
  2. Do a breadth first search using a Queue and use the visited to prevent from forming a loop.
  3. Do it for length n.
于 2012-04-16T02:32:38.180 に答える