简体   繁体   English

我的方法没有达到预期的返回语句

[英]My method doesn't reach the expected return statement

I have been staring at this for hours now and can't figure out what is going wrong,我已经盯着这个看了几个小时,不知道出了什么问题,

I am trying to implement a method that determines whether or not a path exists between two vertices in a graph, using a depth-first search algorithm.我正在尝试实现一种方法,该方法使用深度优先搜索算法来确定图中两个顶点之间是否存在路径。

However, while it seems to succesfully find the vertex it needs to find, it doen't reach the return statement.然而,虽然它似乎成功地找到了它需要找到的顶点,但它没有到达 return 语句。

I've tried using different variables, printing stuff, changing orders, but can't get this to return true.我尝试过使用不同的变量、打印内容、更改订单,但无法使其返回 true。 Any help would be greatly appreciated!任何帮助将不胜感激!

public class Path<V,E> {

private HashMap<V,Boolean> visited = new HashMap<V,Boolean>();

public boolean pathExists(Graph<V,E> graph, V v1, V v2) {

    if (v1.equals(v2)) {
        System.out.println("v1 = v2");
        return true;
    }

    if (!visited.containsKey(v1)) {
        System.out.println("v1 discovered: "+v1);
        visited.put(v1, true);
    }

    for (V v : graph.getNeighbors(v1)) {

        if (v.equals(v2)) {
            visited.clear();
            System.out.println("v discovered: "+v);
            return true;
        }

        if (!visited.containsKey(v)) {
            visited.put(v,true);
            System.out.println("visited doesn't contain v: "+v);
            System.out.println("starting new search with "+v);
            pathExists(graph,v,v2);
        }

    }
    visited.clear();
    return false;
}



public static void main(String[] args) {

    MyGraph<String,String> mg = new MyGraph<String,String>();
    Path<String,String> path = new Path<String,String>();

    mg.insertVertex("utrecht");
    mg.insertVertex("amsterdam");
    mg.insertVertex("maastricht");

    mg.insertEdge("utrecht", "amsterdam", "route1", 2);
    mg.insertEdge("amsterdam", "maastricht", "route2", 1);

    boolean c = path.pathExists(mg, "utrecht", "maastricht");

    System.out.println(c);

}
}

This is the console output.这是控制台输出。

v1 discovered: utrecht<br>
visited doesn't contain v: amsterdam<br>
starting new search with amsterdam<br>
v discovered: maastricht<br>
false<br>

Since it is printing "v discovered: maastricht", I expect it to be returning true, but it returns false instead.由于它正在打印“v发现:马斯特里赫特”,我希望它返回true,但它返回false。

You have two main errors in your program.您的程序中有两个主要错误。

  1. You indeed return true when you have found your vertex, but you don't use the return value.当您找到顶点时,您确实返回true ,但您不使用返回值。 Since your method is recursive, the return value is lost.由于您的方法是递归的,因此返回值丢失。

    Instead of代替

    pathExists(graph,v,v2);

    You should write你应该写

    if(pathExists(graph,v,v2)) return true;
  2. You wrongly clear your visited map during the recursion.您在递归过程中错误地清除了访问过的地图。
    First of all, it is not needed at all except if you need to reuse your visited map after.首先,除非您需要在之后重新使用您访问过的地图,否则根本不需要它。 But even in that case, it's better to clear it outside of your method.但即使在这种情况下,最好在您的方法之外清除它。
    While the visited.clear() under if (v.equals(v2)) is not really an issue (apart from the previous remarks), the second one after your for loop will create bugs.虽然if (v.equals(v2)) visited.clear()下的if (v.equals(v2))不是真正的问题(除了前面的评论),for 循环之后的第二个会产生错误。 For example, consider the following vertices with their adjacency lists:例如,考虑以下顶点及其邻接列表:

     1 -> [2, 4] 2 -> [3] 4 -> [1, 5] 5 -> []

    Let's say that you look for vertex 5 and start from vertex 1 .假设您查找顶点5并从顶点1开始。 You will visit vertex 2 and vertex 3 .您将访问顶点2和顶点3 Your visited map contains [1,2,3] .visited地图包含[1,2,3] A the end of pathExists for vertex 3 , since you haven't found 5 and 3 as no neighbor, you clear your visited map. A the end of pathExists for vertex 3 ,因为你没有找到53作为没有邻居,你清除了你visited地图。 Then you visit vertex 4 , and since you have cleared your map, it only contains [4] .然后您访问顶点4 ,并且由于您已清除地图,因此它仅包含[4] So you visit 1 again, and so on.所以你再次访问1 ,依此类推。

  3. This is not really an error, but there is no need to create a Map<Integer, Boolean> .这并不是一个真正的错误,但没有必要创建一个Map<Integer, Boolean> Just create a Set and check for the presence of the vertex.只需创建一个Set并检查顶点是否存在。

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

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