The hill-climbing step: move to the best neighbour, else stop
The hill-climbing step: move to the best neighbour, else stop
Answer
best, best_val = x, f(x) for nxt in (x - 1, x + 1): if f(nxt) > best_val: best, best_val = nxt, f(nxt) if best == x: return x, best_val # local/global maximum x = best
No frontier, no visited set — only the single best child survives. When no neighbour beats the current value, the search is stuck (it may be a local maximum).