-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphBFSNavigator.java
More file actions
159 lines (128 loc) · 5.02 KB
/
GraphBFSNavigator.java
File metadata and controls
159 lines (128 loc) · 5.02 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
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
import java.util.*;
public class GraphBFSNavigator {
// A Node in our Graph Traversal
private static class SearchState {
List<int[]> reachableVertices;
int depth;
SearchState(List<int[]> vertices, int depth) {
this.reachableVertices = vertices;
this.depth = depth;
}
}
private final MastermindEngine engine;
private Backtracking backtracking;
private Queue<SearchState> bfsQueue;
private SearchState currentState;
public GraphBFSNavigator(MastermindEngine engine) {
this.engine = engine;
this.backtracking = new Backtracking(engine);
reset();
}
public void reset() {
List<int[]> allVertices = engine.generateAllGraphVertices();
this.bfsQueue = new LinkedList<>();
this.currentState = new SearchState(allVertices, 0);
bfsQueue.add(currentState);
}
// BFS STEP: Moves to the next level in the Graph
public void traverseToNextState(int[] lastGuess, int[] feedback) {
List<int[]> filteredVertices = new ArrayList<>();
// 1. PRUNING: Only keep vertices that satisfy the graph edges (constraints)
//Before backtracking :
/*
for (int[] vertex : currentState.reachableVertices) {
//int[] testFb = engine.evaluateGuess(lastGuess, vertex); -> before DP
int[] testFb = engine.getFeedbackDP(lastGuess, vertex);
if (testFb[0] == feedback[0] && testFb[1] == feedback[1]) {
filteredVertices.add(vertex);
}
}*/
//BACKTRACKING:
backtracking.addConstraint(lastGuess, feedback);
filteredVertices = backtracking.generateCandidates();
// 2. SORTING: Order the reachable vertices lexicographically
// This ensures the greedy choice is deterministic and prioritized
/*
filteredVertices.sort((a, b) -> {
for (int i = 0; i < a.length; i++) {
if (a[i] != b[i])
return Integer.compare(a[i], b[i]);
}
return 0;
});
*/
filteredVertices = mergeSort(filteredVertices);
// 3. BFS QUEUE UPDATE: Move to the next level of the search
this.currentState = new SearchState(filteredVertices, currentState.depth + 1);
bfsQueue.clear(); // Clear old level
bfsQueue.add(currentState); // Add new level
}
private List<int[]> mergeSort(List<int[]> list) {
if (list.size() <= 1)
return list;
int mid = list.size() / 2;
List<int[]> left = mergeSort(new ArrayList<>(list.subList(0, mid)));
List<int[]> right = mergeSort(new ArrayList<>(list.subList(mid, list.size())));
return merge(left, right);
}
private List<int[]> merge(List<int[]> left, List<int[]> right) {
List<int[]> result = new ArrayList<>();
int i = 0, j = 0;
while (i < left.size() && j < right.size()) {
if (lexCompare(left.get(i), right.get(j)) <= 0) {
result.add(left.get(i++));
} else {
result.add(right.get(j++));
}
}
while (i < left.size())
result.add(left.get(i++));
while (j < right.size())
result.add(right.get(j++));
return result;
}
private int lexCompare(int[] a, int[] b) {
for (int i = 0; i < a.length; i++) {
if (a[i] != b[i]) {
return Integer.compare(a[i], b[i]);
}
}
return 0;
}
// GREEDY SELECTION: Pick the first vertex from the sorted list
/*
public int[] getGreedyMove() {
if (currentState.reachableVertices.isEmpty())
return null;
return currentState.reachableVertices.get(0);
}
*/
public int[] getImprovedGreedyMove() {
List<int[]> candidates = currentState.reachableVertices;
if (candidates.isEmpty())
return null;
int bestScore = Integer.MAX_VALUE;
int[] bestGuess = null;
for (int[] guess : candidates) {
// Map feedback -> count
Map<String, Integer> buckets = new HashMap<>();
for (int[] secret : currentState.reachableVertices) {
//int[] fb = engine.evaluateGuess(guess, secret); -> before DP
int[] fb = engine.getFeedbackDP(guess, secret);
String key = fb[0] + "," + fb[1];
buckets.put(key, buckets.getOrDefault(key, 0) + 1);
}
// Worst-case remaining possibilities
int worstCase = 0;
for (int size : buckets.values()) {
worstCase = Math.max(worstCase, size);
}
// Minimize worst-case
if (worstCase < bestScore) {
bestScore = worstCase;
bestGuess = guess;
}
}
return bestGuess;
}
}