Warm tip: This article is reproduced from serverfault.com, please click

java-使用BFS时,为什么我的单词没有连接在无向/无权图中?

(java - Why aren't my Words connecting in Undirected/Unweighted Graph when using BFS?)

发布于 2020-11-26 14:07:19

在我的代码中发出问题,不确定为什么用单词构建的图形中找不到连接。

ArrayList<String> words = new ArrayList<String>();
words.add("hello");
words.add("there");
words.add("here");
words.add("about");
                
Graph g = new Graph(words.size());
for(String word: words) {
  for(String word2: words){
       g.addEdge(words.indexOf(word), words.indexOf(word2));
  }
}

BufferedReader readValues = 
    new BufferedReader(new InputStreamReader(new FileInputStream("values.txt")));

while(true)
{
    String line = readTestFile.readLine();
    if (line == null) { break; }
    assert line.length() == 11; 
    String start = line.substring(0, 5);
    String goal = line.substring(6, 11);
            
    BreadthFirstPaths bfs = new BreadthFirstPaths(g, words.indexOf(start));
    if (bfs.hasPathTo(words.indexOf(goal))) {
    System.out.println(bfs.distTo(words.indexOf(goal)));
     for (int v : bfs.pathTo(words.indexOf(goal))) {
               System.out.println(v);
     }
    }
    else System.out.println("Nothing");
}

文本文件的内容:

hello there
hello here
about here

我似乎得到:

Nothing
Nothing
Nothing
Nothing
Nothing 

不知道为什么?

编辑:OP似乎在这里的代码,尤其是图形有麻烦。我不知道具体为什么,但是,我敢肯定有这样做的人。

Questioner
user14368465
Viewed
0
jccampanero 2020-11-29 19:35:46

我想你正在使用Robert Sedgewick和Kevin Wayne的优秀著作中有关Java算法第四版中算法实现的源代码

没有理由为什么你的代码不能正常工作。请根据你的代码考虑以下测试:

public static void main(String... args) throws IOException {
  ArrayList<String> words = new ArrayList<String>();
  words.add("hello");
  words.add("there");
  words.add("here");
  words.add("about");

  Graph g = new Graph(words.size());
  for(String word: words) {
    for(String word2: words){
      g.addEdge(words.indexOf(word), words.indexOf(word2));
    }
  }

  BufferedReader readValues = null;

  try {

    readValues =
        new BufferedReader(new InputStreamReader(new FileInputStream("values.txt")));

    String line = null;
    while ((line = readValues.readLine()) != null) {
      // assert line.length() == 11;
      String[] tokens = line.split(" ");
      String start = tokens[0];
      String goal = tokens[1];

      BreadthFirstPaths bfs = new BreadthFirstPaths(g, words.indexOf(start));
      if (bfs.hasPathTo(words.indexOf(goal))) {
        System.out.println("Shortest path distance from " + start + " to " + goal + " = " + bfs.distTo(words.indexOf(goal)));
        StringBuilder path = new StringBuilder();
        String sep = "";
        for (int v : bfs.pathTo(words.indexOf(goal))) {
          path.append(sep).append(v);
          sep = " -> ";
        }

        System.out.println("Shortest path = " + path.toString());
      } else System.out.println("Nothing");
    }

  } finally {
    if (readValues != null) {
      try {
        readValues.close();
      } catch (Throwable t) {
        t.printStackTrace();
      }
    }
  }
}

如果使用你指示的文本文件运行该程序,它将产生类似于以下内容的输出:

Shortest path distance from hello to there = 1
Shortest path = 0 -> 1
Shortest path distance from hello to here = 1
Shortest path = 0 -> 2
Shortest path distance from about to here = 1
Shortest path = 3 -> 2

我介绍的主要更改是与startgoal变量的计算有关的代码

String[] tokens = line.split(" ");
String start = tokens[0];
String goal = tokens[1];

我假设你正在使用另一个文本文件,也许是另一个代码。与一个提供的断言将失败或StringIndexOutOfBounds当你计算将引发异常goalsubstring从指数611

除此之外,该算法应该可以正常工作。

话虽这么说,请注意,你正在构造一个图hyperconnected,其中每个节点都有一个指向不同节点及其本身的直接路径。也许这可能是你的目标,但请注意,当你执行其他操作时,事情会变得很有趣。

例如,如果使用以下代码代替:

for(String word: words) {
  for(String word2: words){
    g.addEdge(words.indexOf(word), words.indexOf(word2));
  }
}

你尝试这样的事情:

g.addEdge(words.indexOf("hello"), words.indexOf("there"));
g.addEdge(words.indexOf("hello"), words.indexOf("about"));
g.addEdge(words.indexOf("about"), words.indexOf("here"));

该算法的输出更具意义:

Shortest path distance from hello to there = 1
Shortest path = 0 -> 1
Shortest path distance from hello to here = 2
Shortest path = 0 -> 3 -> 2
Shortest path distance from about to here = 1
Shortest path = 3 -> 2