搜索

  • Classic Computer Science Problems in Java

线性搜索

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
public static <T> Node<T> dfs(T initial, Predicate<T> goalTest,
Function<T, List<T>> successors) {
// frontier is where we've yet to go
Stack<Node<T>> frontier = new Stack<>();
frontier.push(new Node<>(initial, null));
// explored is where we've been
Set<T> explored = new HashSet<>();
explored.add(initial);

// keep going while there is more to explore
while (!frontier.isEmpty()) {
Node<T> currentNode = frontier.pop();
T currentState = currentNode.state;
// if we found the goal, we're done
if (goalTest.test(currentState)) {
return currentNode;
}
// check where we can go next and haven't explored
for (T child : successors.apply(currentState)) {
if (explored.contains(child)) {
continue; // skip children we already explored
}
explored.add(child);
frontier.push(new Node<>(child, currentNode));
}
}
return null; // went through everything and never found goal
}
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
public static <T> Node<T> bfs(T initial, Predicate<T> goalTest,
Function<T, List<T>> successors) {
// frontier is where we've yet to go
Queue<Node<T>> frontier = new LinkedList<>();
frontier.offer(new Node<>(initial, null));
// explored is where we've been
Set<T> explored = new HashSet<>();
explored.add(initial);

// keep going while there is more to explore
while (!frontier.isEmpty()) {
Node<T> currentNode = frontier.poll();
T currentState = currentNode.state;
// if we found the goal, we're done
if (goalTest.test(currentState)) {
return currentNode;
}
// check where we can go next and haven't explored
for (T child : successors.apply(currentState)) {
if (explored.contains(child)) {
continue; // skip children we already explored
}
explored.add(child);
frontier.offer(new Node<>(child, currentNode));
}
}
return null; // went through everything and never found goal
}
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
public static <T> Node<T> astar(T initial, Predicate<T> goalTest,
Function<T, List<T>> successors, ToDoubleFunction<T> heuristic) {
// frontier is where we've yet to go
PriorityQueue<Node<T>> frontier = new PriorityQueue<>();
frontier.offer(new Node<>(initial, null, 0.0, heuristic.applyAsDouble(initial)));
// explored is where we've been
Map<T, Double> explored = new HashMap<>();
explored.put(initial, 0.0);
// keep going while there is more to explore
while (!frontier.isEmpty()) {
Node<T> currentNode = frontier.poll();
T currentState = currentNode.state;
// if we found the goal, we're done
if (goalTest.test(currentState)) {
return currentNode;
}
// check where we can go next and haven't explored
for (T child : successors.apply(currentState)) {
// 1 here assumes a grid, need a cost function for more sophisticated apps
double newCost = currentNode.cost + 1;
if (!explored.containsKey(child) || explored.get(child) > newCost) {
explored.put(child, newCost);
frontier.offer(new Node<>(child, currentNode, newCost, heuristic.applyAsDouble(child)));
}
}
}

return null; // went through everything and never found goal
}

完整的代码

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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// GenericSearch.java
// From Classic Computer Science Problems in Java Chapter 2
// Copyright 2020 David Kopec
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package chapter2;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Set;
import java.util.Stack;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.ToDoubleFunction;

public class GenericSearch {

public static <T extends Comparable<T>> boolean linearContains(List<T> list, T key) {
for (T item : list) {
if (item.compareTo(key) == 0) {
return true; // found a match
}
}
return false;
}

// assumes *list* is already sorted
public static <T extends Comparable<T>> boolean binaryContains(List<T> list, T key) {
int low = 0;
int high = list.size() - 1;
while (low <= high) { // while there is still a search space
int middle = (low + high) / 2;
int comparison = list.get(middle).compareTo(key);
if (comparison < 0) { // middle codon is less than key
low = middle + 1;
} else if (comparison > 0) { // middle codon is greater than key
high = middle - 1;
} else { // middle codon is equal to key
return true;
}
}
return false;
}

public static class Node<T> implements Comparable<Node<T>> {
final T state;
Node<T> parent;
double cost;
double heuristic;

// for dfs and bfs we won't use cost and heuristic
Node(T state, Node<T> parent) {
this.state = state;
this.parent = parent;
}

// for astar we will use cost and heuristic
Node(T state, Node<T> parent, double cost, double heuristic) {
this.state = state;
this.parent = parent;
this.cost = cost;
this.heuristic = heuristic;
}

@Override
public int compareTo(Node<T> other) {
Double mine = cost + heuristic;
Double theirs = other.cost + other.heuristic;
return mine.compareTo(theirs);
}
}

public static <T> Node<T> dfs(T initial, Predicate<T> goalTest,
Function<T, List<T>> successors) {
// frontier is where we've yet to go
Stack<Node<T>> frontier = new Stack<>();
frontier.push(new Node<>(initial, null));
// explored is where we've been
Set<T> explored = new HashSet<>();
explored.add(initial);

// keep going while there is more to explore
while (!frontier.isEmpty()) {
Node<T> currentNode = frontier.pop();
T currentState = currentNode.state;
// if we found the goal, we're done
if (goalTest.test(currentState)) {
return currentNode;
}
// check where we can go next and haven't explored
for (T child : successors.apply(currentState)) {
if (explored.contains(child)) {
continue; // skip children we already explored
}
explored.add(child);
frontier.push(new Node<>(child, currentNode));
}
}
return null; // went through everything and never found goal
}

public static <T> List<T> nodeToPath(Node<T> node) {
List<T> path = new ArrayList<>();
path.add(node.state);
// work backwards from end to front
while (node.parent != null) {
node = node.parent;
path.add(0, node.state); // add to front
}
return path;
}

public static <T> Node<T> bfs(T initial, Predicate<T> goalTest,
Function<T, List<T>> successors) {
// frontier is where we've yet to go
Queue<Node<T>> frontier = new LinkedList<>();
frontier.offer(new Node<>(initial, null));
// explored is where we've been
Set<T> explored = new HashSet<>();
explored.add(initial);

// keep going while there is more to explore
while (!frontier.isEmpty()) {
Node<T> currentNode = frontier.poll();
T currentState = currentNode.state;
// if we found the goal, we're done
if (goalTest.test(currentState)) {
return currentNode;
}
// check where we can go next and haven't explored
for (T child : successors.apply(currentState)) {
if (explored.contains(child)) {
continue; // skip children we already explored
}
explored.add(child);
frontier.offer(new Node<>(child, currentNode));
}
}
return null; // went through everything and never found goal
}

public static <T> Node<T> astar(T initial, Predicate<T> goalTest,
Function<T, List<T>> successors, ToDoubleFunction<T> heuristic) {
// frontier is where we've yet to go
PriorityQueue<Node<T>> frontier = new PriorityQueue<>();
frontier.offer(new Node<>(initial, null, 0.0, heuristic.applyAsDouble(initial)));
// explored is where we've been
Map<T, Double> explored = new HashMap<>();
explored.put(initial, 0.0);
// keep going while there is more to explore
while (!frontier.isEmpty()) {
Node<T> currentNode = frontier.poll();
T currentState = currentNode.state;
// if we found the goal, we're done
if (goalTest.test(currentState)) {
return currentNode;
}
// check where we can go next and haven't explored
for (T child : successors.apply(currentState)) {
// 1 here assumes a grid, need a cost function for more sophisticated apps
double newCost = currentNode.cost + 1;
if (!explored.containsKey(child) || explored.get(child) > newCost) {
explored.put(child, newCost);
frontier.offer(new Node<>(child, currentNode, newCost, heuristic.applyAsDouble(child)));
}
}
}

return null; // went through everything and never found goal
}

public static void main(String[] args) {
System.out.println(linearContains(List.of(1, 5, 15, 15, 15, 15, 20), 5)); // true
System.out.println(binaryContains(List.of("a", "d", "e", "f", "z"), "f")); // true
System.out.println(binaryContains(List.of("john", "mark", "ronald", "sarah"), "sheila")); // false
}

}