-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinformed_java_BestFirstSearch.java
More file actions
62 lines (53 loc) · 1.98 KB
/
Copy pathinformed_java_BestFirstSearch.java
File metadata and controls
62 lines (53 loc) · 1.98 KB
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
import java.util.*;
public class informed_java_BestFirstSearch {
static List<Integer> greedyBestFirstPath(List<List<Integer>> graph, int[] h, int start, int goal) {
int n = graph.size();
int[] prev = new int[n];
boolean[] visited = new boolean[n];
Arrays.fill(prev, -1);
PriorityQueue<int[]> pq = new PriorityQueue<>(Comparator.comparingInt(a -> a[0])); // {heuristic, node}
pq.add(new int[]{h[start], start});
while (!pq.isEmpty()) {
int[] cur = pq.remove();
int u = cur[1];
if (visited[u]) continue;
visited[u] = true;
if (u == goal) break;
for (int v : graph.get(u)) {
if (!visited[v]) {
if (prev[v] == -1) prev[v] = u;
pq.add(new int[]{h[v], v});
}
}
}
if (!visited[goal]) return Collections.emptyList();
List<Integer> path = new ArrayList<>();
for (int at = goal; at != -1; at = prev[at]) path.add(at);
Collections.reverse(path);
return path;
}
public static void main(String[] args) {
List<List<Integer>> graph = Arrays.asList(
Arrays.asList(1, 2), // 0
Arrays.asList(3, 4), // 1
Arrays.asList(5), // 2
Arrays.asList(6), // 3
Arrays.asList(6), // 4
Arrays.asList(6), // 5
Arrays.asList() // 6
);
int[] h = {7, 6, 5, 1, 4, 2, 0};
int start = 0, goal = 6;
List<Integer> path = greedyBestFirstPath(graph, h, start, goal);
if (path.isEmpty()) {
System.out.println("No path");
return;
}
System.out.print("Best-First (Greedy) Path: ");
for (int i = 0; i < path.size(); i++) {
if (i > 0) System.out.print(" -> ");
System.out.print(path.get(i));
}
System.out.println();
}
}