Alpha-Beta Pruning is an optimization technique for the Minimax algorithm used in two-player games. It reduces the number of nodes that Minimax needs to evaluate by skipping branches that cannot affect the final decision.
How Alpha-Beta Improves Minimax
- Minimax explores the game tree by considering possible moves of both players. For large game trees, evaluating every possible branch can require a large number of computations.
- Alpha-Beta Pruning avoids this unnecessary work.
- Suppose MAX has already found a move that guarantees a score of 3. While exploring another branch, MIN finds a value of 2. Since MIN will choose the smaller value, that branch can give MAX at most 2. MAX already has 3, so there is no need to explore the remaining children of that MIN node.
- This skipped part of the tree is called a pruned branch.
- Therefore, Alpha-Beta Pruning produces the same result as Minimax, but often by evaluating fewer nodes.
Example
Consider the following game tree:

The root is a MAX node, so MAX wants the largest value. The next level contains MIN nodes, so each MIN node chooses the smaller value.
Step 1: Evaluate the left branch
The left MIN node has values 3 and 5.
MIN(3, 5) = 3
So MAX currently has a value of 3.
Step 2: Evaluate the right branch
- The right MIN node first finds 2.
- At this point, MIN already has a value of 2.
MAX already has 3 from the left branch:
MAX already has = 3
MIN has found = 2
- MIN will never choose a value greater than 2 from this branch because it wants the minimum.
- Therefore, the remaining value 9 does not need to be evaluated.

The result is still:
MAX (3, 2) = 3
So Alpha-Beta Pruning gives the same answer as Minimax while evaluating fewer nodes.
Alpha and Beta
To determine when a branch can be skipped, Alpha-Beta Pruning maintains two values:
- Alpha (α): The best value that MAX can guarantee so far.
- Beta (β): The best value that MIN can guarantee so far.
Initially:
As the tree is explored:
- At a MAX node, update Alpha with the largest value found.
- At a MIN node, update Beta with the smallest value found.
When:
The remaining children of the current node can be pruned because they cannot change the final Minimax decision.
Working
1. Initialize Alpha and Beta
Start with:
These represent the initial best values for MAX and MIN.
2. At a MAX Node
MAX looks for the largest value.
- Evaluate each child one by one.
- Update Alpha with the largest value found:
α = max(α, value)
- If α ≥ β, stop evaluating the remaining children because they cannot affect the final decision.
3. At a MIN Node
MIN looks for the smallest value.
- Evaluate each child one by one.
- Update Beta with the smallest value found:
β = min(β, value)
- If α ≥ β, stop evaluating the remaining children because they cannot affect the final decision.
Thus, the main pruning condition is: α ≥ β
When this condition is reached, the unexplored branches of the current node are pruned.
Implementation
The following example builds a game tree, applies Alpha-Beta Pruning and visualizes the search. Green branches represent visited nodes, while red dashed branches represent pruned branches.
Step 1: Import the Required Libraries
matplotlib is used to create and display the game-tree visualization.
import matplotlib.pyplot as plt
from matplotlib.patches import Circle, FancyArrowPatch
Step 2: Define the Node Class
The Node class represents a node in the game tree. It stores the node name, its children and its value if it is a terminal node.
class Node:
def __init__(self, name, children=None, value=None):
self.name = name
self.children = children or []
self.value = value
Step 3: Build the Game Tree
The leaf nodes are assigned evaluation values, while B and C contain these leaves and A is the root node.
D = Node("D", value=3)
E = Node("E", value=5)
F = Node("F", value=6)
G = Node("G", value=9)
H = Node("H", value=1)
I = Node("I", value=2)
B = Node("B", children=[D, E, F])
C = Node("C", children=[G, H, I])
A = Node("A", children=[B, C])
Step 4: Implement Alpha-Beta Pruning
The alpha_beta() function recursively evaluates the tree. It updates α at MAX nodes and β at MIN nodes. When α ≥ β, the remaining children are recorded as pruned.
visited_edges = []
pruned_edges = []
node_info = {}
def alpha_beta(node, depth, alpha, beta, maximizing):
if depth == 0 or node.value is not None:
return node.value
children = node.children
if maximizing:
best_value = float("-inf")
for i, child in enumerate(children):
visited_edges.append((node.name, child.name))
value = alpha_beta(
child,
depth - 1,
alpha,
beta,
False
)
best_value = max(best_value, value)
alpha = max(alpha, best_value)
if alpha >= beta:
for remaining_child in children[i + 1:]:
pruned_edges.append(
(node.name, remaining_child.name)
)
break
node_info[node.name] = {
"type": "MAX",
"value": best_value,
"alpha": alpha,
"beta": beta
}
return best_value
else:
best_value = float("inf")
for i, child in enumerate(children):
visited_edges.append((node.name, child.name))
value = alpha_beta(
child,
depth - 1,
alpha,
beta,
True
)
best_value = min(best_value, value)
beta = min(beta, best_value)
if alpha >= beta:
for remaining_child in children[i + 1:]:
pruned_edges.append(
(node.name, remaining_child.name)
)
break
node_info[node.name] = {
"type": "MIN",
"value": best_value,
"alpha": alpha,
"beta": beta
}
return best_value
Step 5: Run Alpha-Beta Pruning
Start the search from the root node with α = -∞ and β = +∞.
best_value = alpha_beta(
A,
3,
float("-inf"),
float("inf"),
True
)
print("Optimal Value =", best_value)
Step 6: Define Positions for the Game Tree
The following coordinates determine where each node appears in the visualization.
positions = {
"A": (0, 3),
"B": (-2, 2),
"C": (2, 2),
"D": (-3, 1),
"E": (-2, 1),
"F": (-1, 1),
"G": (1, 1),
"H": (2, 1),
"I": (3, 1)
}
leaf_values = {
"D": 3,
"E": 5,
"F": 6,
"G": 9,
"H": 1,
"I": 2
}
Step 7: Draw the Game Tree
The following code draws the tree and uses different styles to distinguish visited and pruned branches.
fig, ax = plt.subplots(figsize=(12, 7))
all_edges = [
("A", "B"),
("A", "C"),
("B", "D"),
("B", "E"),
("B", "F"),
("C", "G"),
("C", "H"),
("C", "I")
]
for parent, child in all_edges:
x1, y1 = positions[parent]
x2, y2 = positions[child]
if (parent, child) in pruned_edges:
edge_color = "red"
edge_width = 3
edge_style = "--"
elif (parent, child) in visited_edges:
edge_color = "green"
edge_width = 3
edge_style = "-"
else:
edge_color = "gray"
edge_width = 2
edge_style = "--"
ax.add_patch(
FancyArrowPatch(
(x1, y1 - 0.12),
(x2, y2 + 0.12),
arrowstyle="-",
linewidth=edge_width,
linestyle=edge_style,
color=edge_color
)
)
Step 8: Add Node Values and Alpha-Beta Information
The nodes are labeled with their type and calculated values. MAX nodes also show α, while MIN nodes show β.
for name, (x, y) in positions.items():
if name in node_info:
info = node_info[name]
label = (
f"{name}\n"
f"{info['type']}\n"
f"Value = {info['value']}\n"
)
if info["type"] == "MAX":
label += f"α = {info['alpha']}"
else:
label += f"β = {info['beta']}"
else:
label = f"{name}\nValue = {leaf_values[name]}"
if any(child == name for _, child in pruned_edges):
face_color = "mistyrose"
edge_color = "red"
else:
face_color = "lightblue"
edge_color = "black"
circle = Circle(
(x, y),
0.38,
facecolor=face_color,
edgecolor=edge_color,
linewidth=2
)
ax.add_patch(circle)
ax.text(
x,
y,
label,
ha="center",
va="center",
fontsize=9
)
Step 9: Mark Pruned Branches and Display the Output
Finally, the pruned branches are labeled and the completed visualization is displayed.
for parent, child in pruned_edges:
x, y = positions[child]
ax.text(
x + 0.45,
y,
"PRUNED",
color="red",
fontsize=10,
fontweight="bold",
va="center"
)
ax.set_title(
f"Alpha-Beta Pruning\nOptimal Value = {best_value}",
fontsize=16,
fontweight="bold"
)
ax.text(
-3.8,
0.35,
"Green = Visited Red dashed = Pruned",
fontsize=11
)
ax.set_xlim(-4, 4)
ax.set_ylim(0.2, 3.7)
ax.set_aspect("equal")
ax.axis("off")
plt.tight_layout()
plt.show()
Output:

The visualization shows the Alpha-Beta traversal of the game tree. Green branches represent visited nodes, while the red dashed branch represents the branch that was pruned. The node labels show the calculated values and the relevant Alpha or Beta values.
You can download full code from here.

