Skip to content
geeksforgeeks
  • 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
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Practice Game Theory
  • Game Theory in DSA
  • Nash Equilibrium
  • Normal form games
  • Minimax Algorithm
  • Impartial games
  • Combinatorial Game Theory
  • Nim Game
  • Grundy Numbers
  • Sprague Grundy Theorem
  • Minimax Algo
  • Alpha Beta Pruning
  • Zorbist Hashing
  • Coin Game
  • DP in Game Theory
  • Problems on Game Theory for CP
Open In App
Next Article:
Introduction to Evaluation Function of Minimax Algorithm in Game Theory
Next article icon

Expectimax Algorithm in Game Theory

Last Updated : 25 Oct, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

The Expectimax search algorithm is a game theory algorithm used to maximize the expected utility. It is a variation of the Minimax algorithm. While Minimax assumes that the adversary(the minimizer) plays optimally, the Expectimax doesn’t. This is useful for modelling environments where adversary agents are not optimal, or their actions are based on chance.
Expectimax vs Minimax 
Consider the below Minimax tree:
 

As we know that the adversary agent(minimizer) plays optimally, it makes sense to go to the left. But what if there is a possibility of the minimizer making a mistake(or not playing optimally). Therefore going right might sound more appealing or may result in a better solution. 
In the below Expectimax tree, we have replaced minimizer nodes by chance nodes.
 

The Chance nodes take the average of all available utilities giving us the ‘expected utility’. Thus the expected utilities for left and right sub-trees are (10+10)/2=10 and (100+9)/2=54.5. The maximizer node chooses the right sub-tree to maximize the expected utilities.
Advantages of Expectimax over Minimax: 
 

  • Expectimax algorithm helps take advantage of non-optimal opponents.
  • Unlike Minimax, Expectimax ‘can take a risk’ and end up in a state with a higher utility as opponents are random(not optimal).

Disadvantages: 
 

  • Expectimax is not optimal. It may lead to the agent losing(ending up in a state with lesser utility)
  • Expectimax requires the full search tree to be explored. There is no type of pruning that can be done, as the value of a single unexplored utility can change the expectimax value drastically. Therefore it can be slow.
  • It is sensitive to monotonic transformations in utility values. 
    For minimax, if we have two states S1 and S2, if S1 is better than S2, the magnitudes of the evaluation function values f(S1) and f(S2) don’t matter as along as f(S1)>f(S2). 
    For expectimax, magnitudes of the evaluation function values matter.

Algorithm: Expectimax can be implemented using recursive algorithm as follows, 
 

  1. If the current call is a maximizer node, return the maximum of the state values of the nodes successors.
  2. If the current call is a chance node, then return the average of the state values of the nodes successors(assuming all nodes have equal probability). If different nodes have different probabilities the expected utility from there is given by ∑ixipi
  3. We call the function recursively until we reach a terminal node(the state with no successors). Then return the utility for that state.

Implementation: 
 

C++




// C++ program to illustrate
// Expectimax Algorithm
 
#include <iostream>
using namespace std;
 
// Structure to declare
// left and right nodes
struct Node {
    int value;
    struct Node *left, *right;
};
 
// Initializing Nodes to NULL
Node* newNode(int v)
{
    Node* temp = new Node;
    temp->value = v;
    temp->left = NULL;
    temp->right = NULL;
    return temp;
}
 
// Getting expectimax
float expectimax(Node* node, bool is_max)
{
    // Condition for Terminal node
    if (node->left == NULL
        && node->right == NULL) {
        return node->value;
    }
 
    // Maximizer node. Chooses the max from the
    // left and right sub-trees
    if (is_max) {
        return max(
            expectimax(
                node->left, false),
            expectimax(node->right, false));
    }
 
    // Chance node. Returns the average of
    // the left and right sub-trees
    else {
        return (
                   expectimax(node->left, true)
                   + expectimax(node->right, true))
               / 2.0;
    }
}
 
// Driver code
int main()
{
    // Non leaf nodes.
    // If search is limited
    // to a given depth,
    // their values are
    // taken as heuristic value.
    // But because the entire tree
    // is searched their
    // values don't matter
    Node* root = newNode(0);
    root->left = newNode(0);
    root->right = newNode(0);
 
    // Assigning values to Leaf nodes
    root->left->left = newNode(10);
    root->left->right = newNode(10);
    root->right->left = newNode(9);
    root->right->right = newNode(100);
 
    float res = expectimax(root, true);
    cout << "Expectimax value is "
<< res << endl;
    return 0;
}
 
 

Java




// Java program to illustrate
// Expectimax Algorithm
class GFG{
  
// Structure to declare
// left and right nodes
static class Node {
    int value;
    Node left, right;
};
  
// Initializing Nodes to null
static Node newNode(int v)
{
    Node temp = new Node();
    temp.value = v;
    temp.left = null;
    temp.right = null;
    return temp;
}
  
// Getting expectimax
static float expectimax(Node node, boolean is_max)
{
    // Condition for Terminal node
    if (node.left == null
        && node.right == null) {
        return node.value;
    }
  
    // Maximizer node. Chooses the max from the
    // left and right sub-trees
    if (is_max) {
        return Math.max(
            expectimax(
                node.left, false),
            expectimax(node.right, false));
    }
  
    // Chance node. Returns the average of
    // the left and right sub-trees
    else {
        return (float) ((
                   expectimax(node.left, true)
                   + expectimax(node.right, true))
               / 2.0);
    }
}
  
// Driver code
public static void main(String[] args)
{
    // Non leaf nodes.
    // If search is limited
    // to a given depth,
    // their values are
    // taken as heuristic value.
    // But because the entire tree
    // is searched their
    // values don't matter
    Node root = newNode(0);
    root.left = newNode(0);
    root.right = newNode(0);
  
    // Assigning values to Leaf nodes
    root.left.left = newNode(10);
    root.left.right = newNode(10);
    root.right.left = newNode(9);
    root.right.right = newNode(100);
  
    float res = expectimax(root, true);
    System.out.print("Expectimax value is "
+ res +"\n");
}
}
 
// This code is contributed by PrinciRaj1992
 
 

Python3




# Python3 program to illustrate
# Expectimax Algorithm
  
# Structure to declare
# left and right nodes
class Node:
     
    def __init__(self, value):
         
        self.value = value
        self.left = None
        self.right = None
     
# Initializing Nodes to None
def newNode(v):
 
    temp = Node(v);
    return temp;
 
# Getting expectimax
def expectimax(node, is_max):
 
    # Condition for Terminal node
    if (node.left == None and node.right == None):
        return node.value;
     
    # Maximizer node. Chooses the max from the
    # left and right sub-trees
    if (is_max):
        return max(expectimax(node.left, False), expectimax(node.right, False))
  
    # Chance node. Returns the average of
    # the left and right sub-trees
    else:
        return (expectimax(node.left, True)+ expectimax(node.right, True))/2;
     
# Driver code
if __name__=='__main__':
     
    # Non leaf nodes.
    # If search is limited
    # to a given depth,
    # their values are
    # taken as heuristic value.
    # But because the entire tree
    # is searched their
    # values don't matter
    root = newNode(0);
    root.left = newNode(0);
    root.right = newNode(0);
  
    # Assigning values to Leaf nodes
    root.left.left = newNode(10);
    root.left.right = newNode(10);
    root.right.left = newNode(9);
    root.right.right = newNode(100);
  
    res = expectimax(root, True)
    print("Expectimax value is "+str(res))
     
# This code is contributed by rutvik_56
 
 

C#




// C# program to illustrate
// Expectimax Algorithm
using System;
 
class GFG{
   
// Structure to declare
// left and right nodes
class Node {
    public int value;
    public Node left, right;
};
   
// Initializing Nodes to null
static Node newNode(int v)
{
    Node temp = new Node();
    temp.value = v;
    temp.left = null;
    temp.right = null;
    return temp;
}
   
// Getting expectimax
static float expectimax(Node node, bool is_max)
{
    // Condition for Terminal node
    if (node.left == null
        && node.right == null) {
        return node.value;
    }
   
    // Maximizer node. Chooses the max from the
    // left and right sub-trees
    if (is_max) {
        return Math.Max(
            expectimax(
                node.left, false),
            expectimax(node.right, false));
    }
   
    // Chance node. Returns the average of
    // the left and right sub-trees
    else {
        return (float) ((
                   expectimax(node.left, true)
                   + expectimax(node.right, true))
               / 2.0);
    }
}
   
// Driver code
public static void Main(String[] args)
{
    // Non leaf nodes.
    // If search is limited
    // to a given depth,
    // their values are
    // taken as heuristic value.
    // But because the entire tree
    // is searched their
    // values don't matter
    Node root = newNode(0);
    root.left = newNode(0);
    root.right = newNode(0);
   
    // Assigning values to Leaf nodes
    root.left.left = newNode(10);
    root.left.right = newNode(10);
    root.right.left = newNode(9);
    root.right.right = newNode(100);
   
    float res = expectimax(root, true);
    Console.Write("Expectimax value is "
+ res +"\n");
}
}
 
// This code is contributed by sapnasingh4991
 
 

Javascript




<script>
    // Javascript program to illustrate Expectimax Algorithm
     
    // Structure to declare
    // left and right nodes
    class Node
    {
        constructor(v) {
           this.left = null;
           this.right = null;
           this.value = v;
        }
    }
     
    // Initializing Nodes to null
    function newNode(v)
    {
        let temp = new Node(v);
        return temp;
    }
 
    // Getting expectimax
    function expectimax(node, is_max)
    {
        // Condition for Terminal node
        if (node.left == null
            && node.right == null) {
            return node.value;
        }
 
        // Maximizer node. Chooses the max from the
        // left and right sub-trees
        if (is_max) {
            return Math.max(
                expectimax(
                    node.left, false),
                expectimax(node.right, false));
        }
 
        // Chance node. Returns the average of
        // the left and right sub-trees
        else {
            return ((
                       expectimax(node.left, true)
                       + expectimax(node.right, true))
                   / 2.0);
        }
    }
     
    // Non leaf nodes.
    // If search is limited
    // to a given depth,
    // their values are
    // taken as heuristic value.
    // But because the entire tree
    // is searched their
    // values don't matter
    let root = newNode(0);
    root.left = newNode(0);
    root.right = newNode(0);
    
    // Assigning values to Leaf nodes
    root.left.left = newNode(10);
    root.left.right = newNode(10);
    root.right.left = newNode(9);
    root.right.right = newNode(100);
    
    let res = expectimax(root, true);
    document.write("Expectimax value is " + res);
     
    // This code is contributed by mukesh07.
</script>
 
 
Output: 
Expectimax value is 54.5

 

Time complexity: O(bm) 
Space complexity: O(b*m), where b is branching factor and m is the maximum depth of the tree.
Applications: Expectimax can be used in environments where the actions of one of the agents are random. Following are a few examples, 
 

  1. In Pacman, if we have random ghosts, we can model Pacman as the maximizer and ghosts as chance nodes. The utility values will be the values of the terminal states(win, lose or draw) or the evaluation function value for the set of possible states at a given depth.
  2. We can create a minesweeper AI by modelling the player agent as the maximizer and the mines as chance nodes.

 



Next Article
Introduction to Evaluation Function of Minimax Algorithm in Game Theory

A

AftaabZia
Improve
Article Tags :
  • Algorithms
  • DSA
  • Game Theory
  • Recursion
Practice Tags :
  • Algorithms
  • Game Theory
  • Recursion

Similar Reads

  • Minimax Algorithm in Game Theory | Set 4 (Alpha-Beta Pruning)
    Prerequisites: Minimax Algorithm in Game Theory, Evaluation Function in Game TheoryAlpha-Beta pruning is not actually a new algorithm, but rather an optimization technique for the minimax algorithm. It reduces the computation time by a huge factor. This allows us to search much faster and even go in
    11 min read
  • Backtracking Algorithm in Python
    Backtracking is a problem-solving algorithmic technique that involves finding a solution incrementally by trying different options and undoing them if they lead to a dead end. The backtracking algorithm is a recursive algorithm that is used to solve problems by making a series of choices, and if a c
    4 min read
  • Minimax Algorithm in Game Theory | Set 1 (Introduction)
    Minimax is a kind of backtracking algorithm that is used in decision making and game theory to find the optimal move for a player, assuming that your opponent also plays optimally. It is widely used in two player turn-based games such as Tic-Tac-Toe, Backgammon, Mancala, Chess, etc.In Minimax the tw
    9 min read
  • What is Greedy Algorithm in DSA?
    A Greedy Algorithm is defined as a problem-solving strategy that makes the locally optimal choice at each step of the algorithm, with the hope that this will lead to a globally optimal solution. In other words, a greedy algorithm always chooses the option that seems the best at the moment, without c
    4 min read
  • Introduction to Evaluation Function of Minimax Algorithm in Game Theory
    Prerequisite: Minimax Algorithm in Game TheoryAs seen in the above article, each leaf node had a value associated with it. We had stored this value in an array. But in the real world when we are creating a program to play Tic-Tac-Toe, Chess, Backgammon, etc. we need to implement a function that calc
    8 min read
  • Elo Rating Algorithm
    The Elo Rating Algorithm is a widely used rating algorithm used to rank players in many competitive games. Players with higher ELO ratings have a higher probability of winning a game than players with lower ELO ratings. After each game, the ELO rating of players is updated. If a player with a higher
    7 min read
  • Greedy Best first search algorithm
    What is the Greedy-Best-first search algorithm?Greedy Best-First Search is an AI search algorithm that attempts to find the most promising path from a given starting point to a goal. It prioritizes paths that appear to be the most promising, regardless of whether or not they are actually the shortes
    5 min read
  • Greedy Algorithm Notes for GATE Exam [2024]
    In the dynamic landscape of algorithmic design, Greedy Algorithms stand out as powerful tools for solving optimization problems. Aspirants preparing for the GATE Exam 2024 are poised to encounter a range of questions that test their understanding of Greedy Algorithms. These notes aim to provide a co
    15+ min read
  • Scheduling in Greedy Algorithms
    In this article, we will discuss various scheduling algorithms for Greedy Algorithms. Many scheduling problems can be solved using greedy algorithms. Problem statement: Given N events with their starting and ending times, find a schedule that includes as many events as possible. It is not possible t
    2 min read
  • Minimax Algorithm in Game Theory | Set 5 (Zobrist Hashing)
    Previous posts on this topic : Minimax Algorithm in Game Theory, Evaluation Function in Game Theory, Tic-Tac-Toe AI – Finding optimal move, Alpha-Beta Pruning.Zobrist Hashing is a hashing function that is widely used in 2 player board games. It is the most common hashing function used in transpositi
    12 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