05 August 2008
BFS
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
public List<List<String>> findLadders(String beginWord, String endWord, List<String> wordList) {
List<List<String>> ans = new ArrayList<>();
// 如果不含有结束单词,直接结束,不然后边会造成死循环
if (!wordList.contains(endWord)) {
return ans;
}
bfs(beginWord, endWord, wordList, ans);
return ans;
}
public void bfs(String beginWord, String endWord, List<String> wordList, List<List<String>> ans) {
Queue<List<String>> queue = new LinkedList<>();
List<String> path = new ArrayList<>();
path.add(beginWord);
queue.offer(path);
boolean isFound = false;
Set<String> dict = new HashSet<>(wordList);
Set<String> visited = new HashSet<>();
visited.add(beginWord);
while (!queue.isEmpty()) {
int size = queue.size();
Set<String> subVisited = new HashSet<>();
for (int j = 0; j < size; j++) {
List<String> p = queue.poll();
//得到当前路径的末尾单词
String temp = p.get(p.size() - 1);
// 一次性得到所有的下一个的节点
ArrayList<String> neighbors = getNeighbors(temp, dict);
for (String neighbor : neighbors) {
//只考虑之前没有出现过的单词
if (!visited.contains(neighbor)) {
//到达结束单词
if (neighbor.equals(endWord)) {
isFound = true;
p.add(neighbor);
ans.add(new ArrayList<String>(p));
p.remove(p.size() - 1);
}
//加入当前单词
p.add(neighbor);
queue.offer(new ArrayList<String>(p));
p.remove(p.size() - 1);
subVisited.add(neighbor);
}
}
}
visited.addAll(subVisited);
if (isFound) {
break;
}
}
}
private ArrayList<String> getNeighbors(String node, Set<String> dict) {
ArrayList<String> res = new ArrayList<String>();
char chs[] = node.toCharArray();
for (char ch = 'a'; ch <= 'z'; ch++) {
for (int i = 0; i < chs.length; i++) {
if (chs[i] == ch)
continue;
char old_ch = chs[i];
chs[i] = ch;
if (dict.contains(String.valueOf(chs))) {
res.add(String.valueOf(chs));
}
chs[i] = old_ch;
}
}
return res;
}
双向BFS
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
public List<List<String>> findLadders(String beginWord, String endWord, List<String> wordList) {
List<List<String>> ans = new ArrayList<>();
if (!wordList.contains(endWord)) {
return ans;
}
// 利用 BFS 得到所有的邻居节点
HashMap<String, ArrayList<String>> map = new HashMap<>();
bfs(beginWord, endWord, wordList, map);
ArrayList<String> temp = new ArrayList<String>();
// temp 用来保存当前的路径
temp.add(beginWord);
findLaddersHelper(beginWord, endWord, map, temp, ans);
return ans;
}
private void findLaddersHelper(String beginWord, String endWord, HashMap<String, ArrayList<String>> map,
ArrayList<String> temp, List<List<String>> ans) {
if (beginWord.equals(endWord)) {
ans.add(new ArrayList<String>(temp));
return;
}
// 得到所有的下一个的节点
ArrayList<String> neighbors = map.getOrDefault(beginWord, new ArrayList<String>());
for (String neighbor : neighbors) {
temp.add(neighbor);
findLaddersHelper(neighbor, endWord, map, temp, ans);
temp.remove(temp.size() - 1);
}
}
//利用递归实现了双向搜索
private void bfs(String beginWord, String endWord, List<String> wordList, HashMap<String, ArrayList<String>> map) {
Set<String> set1 = new HashSet<String>();
set1.add(beginWord);
Set<String> set2 = new HashSet<String>();
set2.add(endWord);
Set<String> wordSet = new HashSet<String>(wordList);
bfsHelper(set1, set2, wordSet, true, map);
}
// direction 为 true 代表向下扩展,false 代表向上扩展
private boolean bfsHelper(Set<String> set1, Set<String> set2, Set<String> wordSet, boolean direction,
HashMap<String, ArrayList<String>> map) {
//set1 为空了,就直接结束
//比如下边的例子就会造成 set1 为空
/* "hot"
"dog"
["hot","dog"]*/
if(set1.isEmpty()){
return false;
}
// set1 的数量多,就反向扩展
if (set1.size() > set2.size()) {
return bfsHelper(set2, set1, wordSet, !direction, map);
}
// 将已经访问过单词删除
wordSet.removeAll(set1);
wordSet.removeAll(set2);
boolean done = false;
// 保存新扩展得到的节点
Set<String> set = new HashSet<String>();
for (String str : set1) {
//遍历每一位
for (int i = 0; i < str.length(); i++) {
char[] chars = str.toCharArray();
// 尝试所有字母
for (char ch = 'a'; ch <= 'z'; ch++) {
if(chars[i] == ch){
continue;
}
chars[i] = ch;
String word = new String(chars);
// 根据方向得到 map 的 key 和 val
String key = direction ? str : word;
String val = direction ? word : str;
ArrayList<String> list = map.containsKey(key) ? map.get(key) : new ArrayList<String>();
//如果相遇了就保存结果
if (set2.contains(word)) {
done = true;
list.add(val);
map.put(key, list);
}
//如果还没有相遇,并且新的单词在 word 中,那么就加到 set 中
if (!done && wordSet.contains(word)) {
set.add(word);
list.add(val);
map.put(key, list);
}
}
}
}
//一般情况下新扩展的元素会多一些,所以我们下次反方向扩展 set2
return done || bfsHelper(set2, set, wordSet, !direction, map);
}