2011-01-11 18 views
0

グラフデータの隣接リスト表現を使用して、幅広い検索アルゴリズムをデバッグする途中です:HashMap<String, ArrayList<Edge>>。各Stringキーは地下ステーションの名前で、各ArrayListはそのステーションのエッジのリストです。隣接リストHashMap <String、ArrayList <Edge>>値の検索に失敗しました

グラフのノードを横断する順序でキューを使用しています。だから私はそれが子供の名前であることを待ち行列の次のものを調べる。私はの子供のadjacencyListからの辺のArrayListを得るには、childEdges = stationsAdjacencyList.get(childNodeName);のようなものを使用します。

私の構文は少し異なりますが、以下のコードを確認してください。

現在、.get()関数はArrayListを返さず、毎回nullを返しています。私は、HashMap参照が正しいKeyを受け取っていることを知っています。それは、関連するバケツから私に何か価値を与えることを拒否しているだけです。

while (!q.empty()) { // 

     String endpointName; // the Key part for the next node lookup 

     // get next node (single entry of adjacency list) 
     Map<String, ArrayList<Edge>> currentNode = (Map<String, ArrayList<Edge>>) q.deque(); 

     HashMap<String, ArrayList<Edge>> nextNode = new HashMap<String, ArrayList<Edge>>(); 

     for (Map.Entry<String, ArrayList<Edge>> node : currentNode.entrySet()) { // there is only one node 

      ++levelCount; // next node iteration is one level down the tree 

      for (Edge edge : node.getValue()) { // for each of this nodes Edges 

       endpointName = edge.getEndpoint(); // retrieve the name of adjacent 

       if (!endpointName.equals(destination)) { // if it's not the destination 



        levelTracker.put(edge.getParent(), levelCount); // record the level in the tree of this node 

        ArrayList<Edge> nextNodeEdges = adjacencyList.get(endpointName); 

        nextNode.put(endpointName, nextNodeEdges); // create child node from endpoint 

        q.enqueue(nextNode); // add child to queue 

       } 
       else if (endpointName.equals(destination)) { // if we're done 

        path.add(endpointName); // record the destination in the path (reverse order) 

        getPathBack(edge, levelCount + 1); // + 1 levelCount to indicate destination level in tree 

        break; 
       } 
      } 
     } 

    } 

コードがきれいでないかまともなコメントでない場合は、絶えず変化しています。うまくいけば、誰かがなぜArrayList<Edge> nextNodeEdges = adjacencyList.get(endpointName);が何も取得していないと私に言うことができます。

ありがとうございます!

+0

ここで、 'adjacencyList'を定義していますか、それとも' q'ですか? –

+0

また、ここではエラーがあります: 'endpointName == destination'(無効な文字列比較です)。それ以外に、 'else if'の中に' if'を指定する必要はありません。なぜなら、それらのものが等しくなければ、それらは等しく、等価性のチェックは不当であるからです。 –

+0

'adjacencyList'は以前に定義されていますが、キューではありません。 'adjacencyList'はString-> EdgeListのペアで構成されています。これらはそれぞれ一度に1つのキューに置かれ、一度に1つずつ削除されます。私はそれからそれぞれのエッジをループしています。 – Alex

答えて

1

したがって、ハードコードされた値を持つ同じ場所にあるadjacencyList.get("valid endpoint");を呼び出すと、null以外のリストが返されるかどうかを確認することをお勧めします。それではadjacencyListがどこかで破壊されている場合は、endpointNameはそれが正しいとは限りません。

関連する問題