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
|
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; } } return false; }
public static <T extends Comparable<T>> boolean binaryContains(List<T> list, T key) { int low = 0; int high = list.size() - 1; while (low <= high) { int middle = (low + high) / 2; int comparison = list.get(middle).compareTo(key); if (comparison < 0) { low = middle + 1; } else if (comparison > 0) { high = middle - 1; } else { return true; } } return false; }
public static class Node<T> implements Comparable<Node<T>> { final T state; Node<T> parent; double cost; double heuristic;
Node(T state, Node<T> parent) { this.state = state; this.parent = parent; }
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) { Stack<Node<T>> frontier = new Stack<>(); frontier.push(new Node<>(initial, null)); Set<T> explored = new HashSet<>(); explored.add(initial);
while (!frontier.isEmpty()) { Node<T> currentNode = frontier.pop(); T currentState = currentNode.state; if (goalTest.test(currentState)) { return currentNode; } for (T child : successors.apply(currentState)) { if (explored.contains(child)) { continue; } explored.add(child); frontier.push(new Node<>(child, currentNode)); } } return null; }
public static <T> List<T> nodeToPath(Node<T> node) { List<T> path = new ArrayList<>(); path.add(node.state); while (node.parent != null) { node = node.parent; path.add(0, node.state); } return path; }
public static <T> Node<T> bfs(T initial, Predicate<T> goalTest, Function<T, List<T>> successors) { Queue<Node<T>> frontier = new LinkedList<>(); frontier.offer(new Node<>(initial, null)); Set<T> explored = new HashSet<>(); explored.add(initial);
while (!frontier.isEmpty()) { Node<T> currentNode = frontier.poll(); T currentState = currentNode.state; if (goalTest.test(currentState)) { return currentNode; } for (T child : successors.apply(currentState)) { if (explored.contains(child)) { continue; } explored.add(child); frontier.offer(new Node<>(child, currentNode)); } } return null; }
public static <T> Node<T> astar(T initial, Predicate<T> goalTest, Function<T, List<T>> successors, ToDoubleFunction<T> heuristic) { PriorityQueue<Node<T>> frontier = new PriorityQueue<>(); frontier.offer(new Node<>(initial, null, 0.0, heuristic.applyAsDouble(initial))); Map<T, Double> explored = new HashMap<>(); explored.put(initial, 0.0); while (!frontier.isEmpty()) { Node<T> currentNode = frontier.poll(); T currentState = currentNode.state; if (goalTest.test(currentState)) { return currentNode; } for (T child : successors.apply(currentState)) { 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; }
public static void main(String[] args) { System.out.println(linearContains(List.of(1, 5, 15, 15, 15, 15, 20), 5)); System.out.println(binaryContains(List.of("a", "d", "e", "f", "z"), "f")); System.out.println(binaryContains(List.of("john", "mark", "ronald", "sarah"), "sheila")); }
}
|