简体   繁体   English

我的广度优先搜索算法不起作用

[英]My Breadth-First-Search algorithm not working

Can anyone help me with the BFS algorithm, I don't know what is wrong : 谁能帮助我使用BFS算法,但我不知道这是怎么回事:

    public class Graph {

    private final List<City> cities;
    private final List<Route> routes;
    private final Map<City, List<Route>> myGraph = new HashMap<City,List<Route>>();       

      public Map<String,City> BFS(Graph graph, City from, City to) {
          boolean found = false;
          ArrayList<City> visited = new ArrayList<City>();
          Queue<City> toVisit = new LinkedList<City>();
          Map<String,City> rute = new HashMap<String,City>();
          toVisit.add(from);
          City node;
          while (!toVisit.isEmpty()&&(!found)) {
              node = toVisit.remove();
              visited.add(node);
              if (node.equals(to)) {
                  rute.put(node.getId(), node);
                  found = true;
              }
              else {
                  List<City> aux = new ArrayList<City>();
                  List<City> neighbors = new ArrayList<City>();
                  neighbors = this.getNeighbors(node);
                  for (City n : neighbors) 
                          aux.add(n);
                  for (City n : aux)
                      if (!visited.contains(n)&&(!n.equals(to))) {
                          rute.put(n.getId(), node);   // <--- this
                          toVisit.add(n);
                      }
             }  
          } 
//        for (int j = 0;j<rute.size();j++)
//            System.out.println(rute.get(j));
          String current = to.getId();
          StringBuffer route = new StringBuffer();
          route.append(current);
          while(current != null) {
            if (rute.containsKey(current)) {
               current = rute.get(current).getId();
               route.insert(0, current +"-->");
            } else {
               current = null;
            }
          }
          System.out.println(route);
          return rute;
      }

I need to find the shortest path between 2 cities. 我需要找到两个城市之间的最短路径。 I would like to save all the routes in the rute map and then just check which one has the shortest distance. 我想所有路由保存在rute地图,然后只检查哪一个具有最短距离。 The problem is that after the algorithm ends rute is empty. 问题在于算法结束后, rute为空。 I can't figure it out what is wrong. 我不知道这是什么问题。 Please help me 请帮我

Your problem is the visited, it is a List of Cities and you do contains in it. 您的问题是访问者,它是城市列表,您确实包含其中。 A Map is faster for this kind of things, and also will ensure to compare the value of the String as key. Map在这种情况下速度更快,并且还将确保将String的值作为键进行比较。

In the Graph.BFS method replace the visited from a List by a Map: 在Graph.BFS方法中,用Map替换从List访问的列表:

Map visited = new HashMap(); 已访问的地图=新的HashMap();

And then: 接着:

...
while (!toVisit.isEmpty()&&(!found)) {
          node = toVisit.remove();
          visited.put(node.getId(), true); // <--- this was changed

and: 和:

for (City n : aux)
                  if (!visited.containsKey(n.getId())&&(!n.equals(to))) {  // <-- this was changed

You should hold a reference to each node's parent. 您应该保留对每个节点的父节点的引用。 That way you can traverse back up the Graph when you find your destination. 这样,您可以在找到目的地时遍历图。

 public class Node {
   String id;
   Node parent;
   ArrayList<Node> children;
 }

When you start at your from destination set from.parent = null . 当您从目标位置开始时,设置from.parent = null When you find your destination, traverse back via 找到目的地后,通过

StringBuffer path = new StringBuffer(destination.id);
Node current = destination;
while (current.parent != null) {
   path.append(current.id);
   current = current.parent;
}

This will give you the reverse path of the shortest distance between two cities. 这将为您提供两个城市之间最短距离的反向路径。 This is, of course, assuming that all of your edges have uniform costs. 当然,这是假设您的所有优势都费用统一的情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM