English 中文(简体)
未加权图的短途(西端)
原标题:Shortest path (fewest nodes) for unweighted graph
  • 时间:2009-10-16 17:38:04
  •  标签:

我试图用一种方法,将一条最短的道路从一条 no到另一条不加权图。 我考虑了Dijkstra的使用问题,但这似乎是一种超高的技巧,因为我只想一对。 相反,我进行了广泛的第一次搜索,但麻烦在于我的返回名单包含了我不希望的一些要点——我如何能够修改我的法典以实现我的目标?

public List<Node> getDirections(Node start, Node finish){
    List<Node> directions = new LinkedList<Node>();
    Queue<Node> q = new LinkedList<Node>();
    Node current = start;
    q.add(current);
    while(!q.isEmpty()){
        current = q.remove();
        directions.add(current);
        if (current.equals(finish)){
            break;
        }else{
            for(Node node : current.getOutNodes()){
                if(!q.contains(node)){
                    q.add(node);
                }
            }
        }
    }
    if (!current.equals(finish)){
        System.out.println("can t reach destination");
    }
    return directions;
}
最佳回答

实际上,你的法典将不以 cycl语写成,考虑图1-> 2-> 1. 你们必须有一些阵列,可以悬挂你们已经来的旗帜。 此外,就每个节点而言,你可以节省你们的以往节点。 因此,这里是正确的法典:

private Map<Node, Boolean>> vis = new HashMap<Node, Boolean>();

private Map<Node, Node> prev = new HashMap<Node, Node>();

public List getDirections(Node start, Node finish){
    List directions = new LinkedList();
    Queue q = new LinkedList();
    Node current = start;
    q.add(current);
    vis.put(current, true);
    while(!q.isEmpty()){
        current = q.remove();
        if (current.equals(finish)){
            break;
        }else{
            for(Node node : current.getOutNodes()){
                if(!vis.contains(node)){
                    q.add(node);
                    vis.put(node, true);
                    prev.put(node, current);
                }
            }
        }
    }
    if (!current.equals(finish)){
        System.out.println("can t reach destination");
    }
    for(Node node = finish; node != null; node = prev.get(node)) {
        directions.add(node);
    }
    directions.reverse();
    return directions;
}
问题回答

感谢你

我改写了这段话,对以下几点做了一些推论:

  • The collection of visited nodes doesn t have to be a map.
  • For path reconstruction, the next node could be looked up, instead of the previous node, eliminating the need for reversing the directions.
public List<Node> getDirections(Node sourceNode, Node destinationNode) {
    //Initialization.
    Map<Node, Node> nextNodeMap = new HashMap<Node, Node>();
    Node currentNode = sourceNode;

    //Queue
    Queue<Node> queue = new LinkedList<Node>();
    queue.add(currentNode);

    /*
     * The set of visited nodes doesn t have to be a Map, and, since order
     * is not important, an ordered collection is not needed. HashSet is 
     * fast for add and lookup, if configured properly.
     */
    Set<Node> visitedNodes = new HashSet<Node>();
    visitedNodes.add(currentNode);

    //Search.
    while (!queue.isEmpty()) {
        currentNode = queue.remove();
        if (currentNode.equals(destinationNode)) {
            break;
        } else {
            for (Node nextNode : getChildNodes(currentNode)) {
                if (!visitedNodes.contains(nextNode)) {
                    queue.add(nextNode);
                    visitedNodes.add(nextNode);

                    //Look up of next node instead of previous.
                    nextNodeMap.put(currentNode, nextNode);
                }
            }
        }
    }

    //If all nodes are explored and the destination node hasn t been found.
    if (!currentNode.equals(destinationNode)) {
        throw new RuntimeException("No feasible path.");
    }

    //Reconstruct path. No need to reverse.
    List<Node> directions = new LinkedList<Node>();
    for (Node node = sourceNode; node != null; node = nextNodeMap.get(node)) {
        directions.add(node);
    }

    return directions;
}

当你把他们放到你的位置时,你必须把他们的亲子告诉你们。 然后,你只能重新阅读该名单的道路。

页: 1

     /B------C------D
   /                |
 A                 /
                /
     E---------

Each time you enqueue a node, keep track of the way you got here. So in step 1 B(A) E(A) is put on the queue. In step two B gets dequeued and C(B) is put on the queue etc. Its then easy to find your way back again, by just recursing "backwards".

最好的办法可能是,只要有点点,保持其联系(通常就是这样做)。 Dijkstra s。

每次通过你来临,你都会打电话。

directions.Add(current);

相反,你们应该把这转移到你真正知道你想要进入的地方。

仅仅为一对一对一对一对一对一对一对一对一对一的回答,真是简单不过了。 计算一条最短道路的通常方式是,开始与你一样,但当你遇到新的 no点并记录过去的道路。 然后,当你达到目标节点时,你可以追随与来文方的后向联系,走路。 因此,将<代码>directions.add(目前)从该行文中删除,并添加如下的代码。

Map<Node,Node> backlinks = new HashMap<Node,Node>();

in the the

if (!backlinks.containsKey(node)) {
    backlinks.add(node, current);
    q.add(node);
}

之后,使用<条码>后背图绘制<条码>直接<>>清单。





相关问题
热门标签