Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Data Science
  • Data Science Projects
  • Data Analysis
  • Data Visualization
  • Machine Learning
  • ML Projects
  • Deep Learning
  • NLP
  • Computer Vision
  • Artificial Intelligence
Open In App
Next Article:
Alpha-Beta pruning in Adversarial Search Algorithms
Next article icon

Alpha-Beta pruning in Adversarial Search Algorithms

Last Updated : 13 Jun, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In artificial intelligence, particularly in game playing and decision-making, adversarial search algorithms are used to model and solve problems where two or more players compete against each other. One of the most well-known techniques in this domain is alpha-beta pruning.

This article explores the concept of alpha-beta pruning, its implementation, and its advantages and limitations.

Overview of Adversarial Search

Adversarial search algorithms are used in scenarios where agents (players) have conflicting goals. The classic example is two-player games like chess or tic-tac-toe, where one player's gain is the other's loss. The primary goal of these algorithms is to determine the optimal strategy for a player, assuming the opponent also plays optimally.

The minimax algorithm is a fundamental approach in adversarial search, where the game tree is explored to find the best move by minimizing the possible loss for a worst-case scenario.

The Minimax Algorithm

The Minimax algorithm operates on the principle of minimizing the possible loss for a worst-case scenario. It assumes that both players are playing optimally. The algorithm recursively evaluates all possible moves, constructing a game tree where:

  • Max nodes represent the current player's move, aiming to maximize their advantage.
  • Min nodes represent the opponent's move, aiming to minimize the current player's advantage.

Limitations of Minimax

While effective, the Minimax algorithm can be computationally intensive, particularly for games with large search spaces like chess. The time complexity of Minimax is O(b^d), where b is the branching factor and d is the depth of the tree. This exponential growth makes it impractical for deep searches without optimization techniques like Alpha-Beta pruning.

Explanation of Alpha-Beta Pruning

Alpha-beta pruning is an optimization technique for the minimax algorithm. It reduces the number of nodes evaluated in the game tree by eliminating branches that cannot influence the final decision. This is achieved by maintaining two values, alpha and beta, which represent the minimum score that the maximizing player is assured of and the maximum score that the minimizing player is assured of, respectively.

  • Alpha: The best (highest) value that the maximizer can guarantee given the current state.
  • Beta: The best (lowest) value that the minimizer can guarantee given the current state.

As the algorithm traverses the tree, it updates these values. If it finds a move that is worse than the current alpha for the maximizer or beta for the minimizer, it prunes (cuts off) that branch, as it cannot affect the outcome.

How Alpha-Beta Pruning Works

The Alpha-Beta pruning algorithm traverses the game tree similarly to Minimax but prunes branches that do not need to be explored. The steps are discussed below:

  1. Initialization: Start with alpha set to negative infinity and beta set to positive infinity.
  2. Max Node Evaluation:
    • For each child of a Max node:
      • Evaluate the child node using the Minimax algorithm with Alpha-Beta pruning.
      • Update alpha: \alpha = max(\alpha, \text{child value}.
      • If alpha is greater than or equal to beta, prune the remaining children (beta cutoff).
  3. Min Node Evaluation:
    • For each child of a Min node:
      • Evaluate the child node using the Minimax algorithm with Alpha-Beta pruning.
      • Update beta: β=min⁡(β,child value).
      • If beta is less than or equal to alpha, prune the remaining children (alpha cutoff).

Implementation of Alpha-Beta pruning in Adversarial Search Algorithms

The provided code implements the alpha-beta pruning algorithm, a variant of the minimax algorithm used in decision-making and game theory to minimize the number of nodes evaluated in the game tree. Here is a brief explanation of each part of the code:

  • Node Class: Defines a node in the game tree with name, children, and value.
  • Helper Functions:
    • evaluate(node): Returns node's value.
    • is_terminal(node): Checks if node is a terminal node.
    • get_children(node): Returns node's children.
  • Alpha-Beta Pruning Function:
    • alpha_beta_pruning(node, depth, alpha, beta, maximizing_player):
      • Returns node value if depth is 0 or node is terminal.
      • Maximizing player: Tries to maximize score, updates alpha, checks for beta cut-off.
      • Minimizing player: Tries to minimize score, updates beta, checks for alpha cut-off.
  • Game Tree Creation:
    • Creates terminal nodes (D, E, F, G, H, I) with values.
    • Creates internal nodes (B, C) with children.
    • Creates root node (A) with children B and C.
  • Run Algorithm:
    • Runs alpha-beta pruning on root node A.
    • Sets initial_alpha to -∞ and initial_beta to ∞.
    • Sets depth to 3.
    • Prints optimal value.
Python
class Node:     def __init__(self, name, children=None, value=None):         self.name = name         self.children = children if children is not None else []         self.value = value  def evaluate(node):     return node.value  def is_terminal(node):     return node.value is not None  def get_children(node):     return node.children  def alpha_beta_pruning(node, depth, alpha, beta, maximizing_player):     if depth == 0 or is_terminal(node):         return evaluate(node)          if maximizing_player:         max_eval = float('-inf')         for child in get_children(node):             eval = alpha_beta_pruning(child, depth-1, alpha, beta, False)             max_eval = max(max_eval, eval)             alpha = max(alpha, eval)             if beta <= alpha:                 break  # Beta cut-off         return max_eval     else:         min_eval = float('inf')         for child in get_children(node):             eval = alpha_beta_pruning(child, depth-1, alpha, beta, True)             min_eval = min(min_eval, eval)             beta = min(beta, eval)             if beta <= alpha:                 break  # Alpha cut-off         return min_eval  # Create the game tree 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])  # Run the alpha-beta pruning algorithm maximizing_player = True initial_alpha = float('-inf') initial_beta = float('inf') depth = 3  # Maximum depth of the tree  optimal_value = alpha_beta_pruning(A, depth, initial_alpha, initial_beta, maximizing_player) print(f"The optimal value is: {optimal_value}") 

Output:

The optimal value is: 3

Example of Alpha-Beta Pruning

Consider a simple game tree where the branching factor is 2 and the depth is 3.

  1. Start at the root (Max node). Initialize alpha to -∞ and beta to ∞.
  2. Traverse to the first child (Min node) and evaluate its children (Max nodes).
  3. For each Max node, traverse and evaluate its children (leaf nodes). Update alpha and beta accordingly.
  4. Prune branches where alpha ≥ beta for Max nodes or beta ≤ alpha for Min nodes.

Advantages of Alpha-Beta Pruning

  • Efficiency: Alpha-beta pruning significantly reduces the number of nodes evaluated compared to the basic minimax algorithm, making the search process faster.
  • Optimality: Despite pruning, alpha-beta pruning does not affect the final decision; it still guarantees finding the optimal move.

Limitations of Alpha-Beta Pruning

  • Complexity: While it reduces the number of evaluations, the complexity can still be high for very large game trees.
  • Heuristic Dependency: The effectiveness of alpha-beta pruning can be highly dependent on the order in which nodes are evaluated. Good heuristic functions or move ordering can greatly enhance its performance.

Applications of Alpha-Beta Pruning

Alpha-Beta pruning is widely used in AI applications for two-player games such as:

  • Chess: Enhances the efficiency of chess engines, allowing them to evaluate deeper moves within time constraints.
  • Checkers: Optimizes move evaluation in checkers, making AI opponents more challenging.
  • Othello: Improves decision-making processes in Othello, leading to stronger AI strategies.

Conclusion

Alpha-beta pruning is a powerful optimization technique for adversarial search algorithms, particularly in games and decision-making scenarios. By eliminating branches that do not influence the final outcome, it enhances the efficiency of the minimax algorithm without compromising the optimality of the solution. However, its performance can be influenced by factors such as move ordering and the complexity of the game tree.


Next Article
Alpha-Beta pruning in Adversarial Search Algorithms

V

vishal_shevale
Improve
Article Tags :
  • Blogathon
  • Artificial Intelligence
  • AI-ML-DS
  • Interview-Questions
  • AI-ML-DS With Python
  • Data Science Blogathon 2024

Similar Reads

    Adversarial Search Algorithms in Artificial Intelligence (AI)
    Adversarial search algorithms are the backbone of strategic decision-making in artificial intelligence, it enables the agents to navigate competitive scenarios effectively. This article offers concise yet comprehensive advantages of these algorithms from their foundational principles to practical ap
    15+ min read
    Expectimax Search Algorithm in AI
    In artificial intelligence (AI), decision-making under uncertainty plays a crucial role, especially in games or real-world applications where the outcomes of actions are not deterministic. The Expectimax search algorithm is a variant of the well-known Minimax algorithm, designed to handle such proba
    8 min read
    Explain the role of minimax algorithm in adversarial search for optimal decision-making?
    In the realm of artificial intelligence (AI), particularly in game theory and decision-making scenarios involving competition, the ability to predict and counteract an opponent's moves is paramount. This is where adversarial search algorithms come into play. Among the most prominent and foundational
    11 min read
    AlphaGo Algorithm in Artificial Intelligence
    The emergence of AlphaGo has marked a significant milestone in artificial intelligence (AI), showcasing the power of combining reinforcement learning and deep learning techniques. In this article, we are going to discuss the fundamentals and architecture of AlphaGo algorithm. Table of Content What i
    15 min read
    Neural Architecture Search Algorithm
    Neural Architecture Search (NAS) falls within the realm of automated machine learning (AutoML). AutoML is a comprehensive term encompassing the automation of diverse tasks in the application of machine learning to real-world challenges. The article explores the fundamentals, and applications of the
    11 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences