开发者

Adjacency List HashMap<String, ArrayList<Edge>> failing to locate its values

I am halfway through debugging a Breadth-First Search algorithm using an adjacency list representation of graph data: HashMap<String, ArrayList<Edge>>. Each String key is the name of an underground station, and each ArrayList is a list of the edges for that station.

I am using a queue to store nodes of the graph in the order they are traversed. So I inspect the next in the queue for it's child's name. I then want to get the child's ArrayList of edges from the adjacencyList by using something like childEdges = stationsAdjacencyList.get(childNodeName);.

My syntax is a little different, but please check the code below.

At the moment the .get() function is not returning an ArrayList but is returning null every time instead. I know that the HashMap lookup is receiving the correct Key. It is just refusing to give me any value from it's associated bucket.

    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&开发者_如何学Pythongt; 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;
                }
            }
        }

    }

Apologies if the code is not so clean or with decent comments, it is changing constantly. Hopefully someone can tell me why the ArrayList<Edge> nextNodeEdges = adjacencyList.get(endpointName); is not fetching anything.

Thanks!!


So a good test is to see if calling adjacencyList.get("valid endpoint"); in the same spot with a hardcoded value will return a non-null list. If it doesn't then adjacencyList is getting destroyed somewhere, if it does then endpointName isn't as correct as you think it is.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜