ArticleslgStudy

computer science

Negamax

Negamax is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Negamax rather than just read about it. In short: Negamax search is a variant form of minimax search that relies on the zero-sum property of a two-player game. This algorithm relies on the fact that ⁠ min ( a , b ) = − max ( − b , − a ) {\displaystyle \min(a,b)=-\max(-b,-a)} ⁠ to simplify the implementation of the minimax algorithm.

Negamax — main illustration
Negamax — illustration

Key takeaways

  • Negamax belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Negamax to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Negamax from memory before moving on to harder problems.

Reference excerpt

Negamax search is a variant form of minimax search that relies on the zero-sum property of a two-player game. This algorithm relies on the fact that ⁠ min ( a , b ) = − max ( − b , − a ) {\displaystyle \min(a,b)=-\max(-b,-a)} ⁠ to simplify the implementation of the minimax algorithm. More precisely, the value of a position to player A in such a game is the negation of the value to player B. Thus, the player on move looks for a move that maximizes the negation of the value resulting from the move: this successor position must by definition have been valued by the opponent. The reasoning of the previous sentence works regardless of whether A or B is on move. This means that a single procedure can be used to value both positions. This is a coding simplification over minimax, which requires that A selects the move with the maximum-valued successor while B selects the move with the minimum-valued successor. It should not be confused with negascout, an algorithm to compute the minimax or negamax value quickly by clever use of alpha–beta pruning discovered in the 1980s. Note that alpha–beta pruning is itself a way to compute the minimax or negamax value of a position quickly by avoiding the search of certain uninteresting positions. Most adversarial search engines are coded using some form of negamax search.

Negamax base algorithm

NegaMax operates on the same game trees as those used with the minimax search algorithm. Each node and root node in the tree are game states (such as game board configuration) of a two player game. Transitions to child nodes represent moves available to a player who is about to play from a given node. The negamax search objective is to find the node score value for the player who is playing at the root node. The pseudocode below shows the negamax base algorithm, with a configurable limit for the maximum search depth:

function negamax(node, depth, color) is if depth = 0 or node is a terminal node then return color × the heuristic value of node value := −∞ for each child of node do value := max(value, −negamax(child, depth − 1, −color)) return value

(* Initial call for Player A's root node *) negamax(rootNode, depth, 1)

(* Initial call for Player B's root node *) negamax(rootNode, depth, −1)

The root node inherits its score from one of its immediate child nodes. The child node that ultimately sets the root node's best score also represents the best move to play. Although the negamax function shown only returns the node's best score, practical negamax implementations will retain and return both best move and best score for the root node. Only the node's best score is essential with non-root nodes. And a node's best move isn't necessary to retain nor return for non-root nodes. What can be confusing is how the heuristic value of the current node is calculated. In this implementation, this value is always calculated from the point of view of player A, whose color value is one. In other words, higher heuristic values always represent situations more favorable for player A. This is the same behavior as the normal minimax algorithm. The heuristic value is not necessarily the same as a node's return value due to value negation by negamax and the color parameter. The negamax node's return value is a heuristic score from the point of view of the node's current player. Negamax scores match minimax scores for nodes where player A is about to play, and where player A is the maximizing player in the minimax equivalent. Negamax always searches for the maximum value for all its nodes. Hence for player B nodes, the minimax score is a negation of its negamax score. Player B is the minimizing player in the minimax equivalent.

Negamax variant with no color parameter Negamax can be implemented without the color parameter. In this case, the heuristic evaluation function must return values from the point of view of the node's current player rather than an absolute score. For example, the heuristic evaluation function in chess should return a positive value if the current node's player is black and black is winning.

function negamax(node, depth) is if depth = 0 or node is a terminal node then return evaluatePosition() // From current player's perspective value := −∞ for each child of node do value := max(value, −negamax(child, depth − 1)) return value

// Example picking best move in a chess game using negamax function above function think(boardState) is allMoves := generateLegalMoves(boardState) bestMove := null bestEvaluation := -∞

for each move in allMoves board.apply(move) evaluateMove := -negamax(boardState, depth=3) board.undo(move) if evaluateMove > bestEvaluation bestMove := move bestEvaluation := evaluateMove

return bestMove

Negamax with alpha beta pruning

Algorithm optimizations for minimax are also equally applicable for Negamax. Alpha–beta pruning can decrease the number of nodes the negamax algorithm evaluates in a search tree in a manner similar with its use with the minimax algorithm. The pseudocode for depth-limited negamax search with alpha–beta pruning follows:

function negamax(node, depth, α, β, color) is if depth = 0 or node is a terminal node then return color × the heuristic value of node

childNodes := generateMoves(node) childNodes := orderMoves(childNodes) value := −∞ foreach child in childNodes do value := max(value, −negamax(child, depth − 1, −β, −α, −color)) α := max(α, value) if α ≥ β then break (* cut-off *) return value

(* Initial call for Player A's root node *) negamax(rootNode, depth, −∞, +∞, 1)

… excerpt ends here. Continue reading the full article.

Illustrations

Negamax: An animated pedagogical example showing the negamax algorithm with alpha–beta pruning. The person performing the game tree search is considered to be the one that has to move first from the current state of the game (player in this case)
An animated pedagogical example showing the negamax algorithm with alpha–beta pruning. The person performing the game tree search is considered to be the one that has to move first from the current state of the game (player in this case)

Worked examples

Example 1 — a first encounter with Negamax

Start with the simplest possible case. Write down what Negamax claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Negamax before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Negamax ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Negamax

In research
Negamax appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Negamax in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Negamax is common in secondary-school and first-year university syllabi. It links to neighbouring topics Combinatorial game theory, Game artificial intelligence, Optimization algorithms and methods, so understanding it makes those chapters shorter.
In everyday life
Look for Negamax outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.

Affiliate

Preply — study more efficiently by working with a personal tutor. 50% off.

How to study Negamax in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Negamax means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Negamax out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Negamax in simple terms?

Negamax search is a variant form of minimax search that relies on the zero-sum property of a two-player game. This algorithm relies on the fact that ⁠ min ( a , b ) = − max ( − b , − a ) {\displaystyle \min(a,b)=-\max(-b,-a)} ⁠ to simplify the implementation of the minimax algorithm.

Why does Negamax matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Negamax?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Negamax.

Tags

  • Combinatorial game theory
  • Game artificial intelligence
  • Optimization algorithms and methods

Keep exploring