在我的代码中发出问题,不确定为什么用单词构建的图形中找不到连接。
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似乎在这里的代码,尤其是图形有麻烦。我不知道具体为什么,但是,我敢肯定有这样做的人。
我想你正在使用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
我介绍的主要更改是与start
和goal
变量的计算有关的代码:
String[] tokens = line.split(" ");
String start = tokens[0];
String goal = tokens[1];
我假设你正在使用另一个文本文件,也许是另一个代码。与一个提供的断言将失败或StringIndexOutOfBounds
当你计算将引发异常goal
的substring
从指数6
到11
。
除此之外,该算法应该可以正常工作。
话虽这么说,请注意,你正在构造一个图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
要获得O(V + E),我假设我必须在顶点和边上循环?
对不起,@ AlbinM的回复很晚。抱歉,但我不明白这个问题。是的,该算法将为您提供O(V + E)的时间复杂度,其中V表示顶点数,E表示边数,我认为您的情况就是O(V <sup> 2 </ sup>)提供每个单词的工作组合。拜托,你能澄清你的问题吗?
嗨,约翰。是的,我想是的,如果您想在整个图中查找所有连接。本
BreadthFirstPaths
类提供一个构造函数,可以用来指示要检查源顶点和节点Graph
。如果仅想验证该顶点是否与其他顶点连接,则可以使用方法hasPathTo
。但是,您需要对要查找连接是否存在的每个顶点以及将其当作源的每个源顶点重复此过程。@JohnSmith可以,我想我现在已经了解您的问题:请注意,您的问题并不表示您要解决的实际问题。该算法有任何限制吗?我的意思是,您能用整个单词吗?他们是否必须共享共同的角色?有哪些规则?
我意识到Albin的注释引用了Algorithms 4th ed book中介绍的WordLadder示例。那是您想要实现的目标,但也许是整个图中的每个单词?