I am in need of some help with an assignment regarding a BFS word chain. The word chain is based on five-letter words, two words are connected when the last four letters in word x are in word y. For example climb and blimp are connected because l, i, m, and b in climb are in the word blimp.
The recommendation is to use directed BFS from Sedgewick's algorithms 4th edition or to modify it. The code can be found here: https://algs4.cs.princeton.edu/40graphs/ and to use the following code to read a data file to the list words:
BufferedReader r =
new BufferedReader(new InputStreamReader(new FileInputStream(fnam)));
ArrayList<String> words = new ArrayList<String>();
while (true) {
String word = r.readLine();
if (word == null) { break; }
assert word.length() == 5; // inputcheck, if you run with assertions
words.add(word);
}
and the following code to read the test cases from a file:
BufferedReader r =
new BufferedReader(new InputStreamReader(new FileInputStream(fnam)));
while (true) {
String line = r.readLine();
if (line == null) { break; }
assert line.length() == 11; // inputcheck, if you run with assertions
String start = line.substring(0, 5);
String goal = line.substring(6, 11);
// ... search path from start to goal here
}
The words in the data file are:
their
moist
other
blimp
limps
about
there
pismo
abcde
bcdez
zcdea
bcdef
fzcde
When the test case file is used...
other there
other their
their other
blimp moist
limps limps
moist limps
abcde zcdea
...the output should be the number of edges between each word pair, and -1 if there is no path between the words.
1
1
-1
3
0
-1
2
I am new to working with graphs and I am not sure how to use Sedgewick's BFS and modify it to read the test cases file. Any help is appreciated.