Combinatorial Game Theory | Set 4 (Sprague - Grundy Theorem)
Last Updated :
07 Jan, 2024
Prerequisites : Grundy Numbers/Numbers and Mex
We have already seen in Set 2 (https://www.geeksforgeeks.org/dsa/combinatorial-game-theory-set-2-game-nim/), that we can find who wins in a game of Nim without actually playing the game.
Suppose we change the classic Nim game a bit. This time each player can only remove 1, 2 or 3 stones only (and not any number of stones as in the classic game of Nim). Can we predict who will win?
Yes, we can predict the winner using Sprague-Grundy Theorem.
What is Sprague-Grundy Theorem?
Suppose there is a composite game (more than one sub-game) made up of N sub-games and two players, A and B. Then Sprague-Grundy Theorem says that if both A and B play optimally (i.e., they don’t make any mistakes), then the player starting first is guaranteed to win if the XOR of the grundy numbers of position in each sub-games at the beginning of the game is non-zero. Otherwise, if the XOR evaluates to zero, then player A will lose definitely, no matter what.
How to apply Sprague Grundy Theorem ?
We can apply Sprague-Grundy Theorem in any impartial game and solve it. The basic steps are listed as follows:
- Break the composite game into sub-games.
- Then for each sub-game, calculate the Grundy Number at that position.
- Then calculate the XOR of all the calculated Grundy Numbers.
- If the XOR value is non-zero, then the player who is going to make the turn (First Player) will win else he is destined to lose, no matter what.
Example Game : The game starts with 3 piles having 3, 4 and 5 stones, and the player to move may take any positive number of stones upto 3 only from any of the piles [Provided that the pile has that much amount of stones]. The last player to move wins. Which player wins the game assuming that both players play optimally?
How to tell who will win by applying Sprague-Grundy Theorem?
As, we can see that this game is itself composed of several sub-games.
First Step : The sub-games can be considered as each piles.
Second Step : We see from the below table that
Grundy(3) = 3
Grundy(4) = 0
Grundy(5) = 1

We have already seen how to calculate the Grundy Numbers of this game in the previous article.
Third Step : The XOR of 3, 0, 1 = 2
Fourth Step : Since XOR is a non-zero number, so we can say that the first player will win.
Below is the program that implements above 4 steps.
C++
/* Game Description-
"A game is played between two players and there are N piles
of stones such that each pile has certain number of stones.
On his/her turn, a player selects a pile and can take any
non-zero number of stones upto 3 (i.e- 1,2,3)
The player who cannot move is considered to lose the game
(i.e., one who take the last stone is the winner).
Can you find which player wins the game if both players play
optimally (they don't make any mistake)? "
A Dynamic Programming approach to calculate Grundy Number
and Mex and find the Winner using Sprague - Grundy Theorem. */
#include<bits/stdc++.h>
using namespace std;
/* piles[] -> Array having the initial count of stones/coins
in each piles before the game has started.
n -> Number of piles
Grundy[] -> Array having the Grundy Number corresponding to
the initial position of each piles in the game
The piles[] and Grundy[] are having 0-based indexing*/
#define PLAYER1 1
#define PLAYER2 2
// A Function to calculate Mex of all the values in that set
int calculateMex(unordered_set<int> Set)
{
int Mex = 0;
while (Set.find(Mex) != Set.end())
Mex++;
return (Mex);
}
// A function to Compute Grundy Number of 'n'
int calculateGrundy(int n, int Grundy[])
{
Grundy[0] = 0;
Grundy[1] = 1;
Grundy[2] = 2;
Grundy[3] = 3;
if (Grundy[n] != -1)
return (Grundy[n]);
unordered_set<int> Set; // A Hash Table
for (int i=1; i<=3; i++)
Set.insert (calculateGrundy (n-i, Grundy));
// Store the result
Grundy[n] = calculateMex (Set);
return (Grundy[n]);
}
// A function to declare the winner of the game
void declareWinner(int whoseTurn, int piles[],
int Grundy[], int n)
{
int xorValue = Grundy[piles[0]];
for (int i=1; i<=n-1; i++)
xorValue = xorValue ^ Grundy[piles[i]];
if (xorValue != 0)
{
if (whoseTurn == PLAYER1)
printf("Player 1 will win\n");
else
printf("Player 2 will win\n");
}
else
{
if (whoseTurn == PLAYER1)
printf("Player 2 will win\n");
else
printf("Player 1 will win\n");
}
return;
}
// Driver program to test above functions
int main()
{
// Test Case 1
int piles[] = {3, 4, 5};
int n = sizeof(piles)/sizeof(piles[0]);
// Find the maximum element
int maximum = *max_element(piles, piles + n);
// An array to cache the sub-problems so that
// re-computation of same sub-problems is avoided
int Grundy[maximum + 1];
memset(Grundy, -1, sizeof (Grundy));
// Calculate Grundy Value of piles[i] and store it
for (int i=0; i<=n-1; i++)
calculateGrundy(piles[i], Grundy);
declareWinner(PLAYER1, piles, Grundy, n);
/* Test Case 2
int piles[] = {3, 8, 2};
int n = sizeof(piles)/sizeof(piles[0]);
int maximum = *max_element (piles, piles + n);
// An array to cache the sub-problems so that
// re-computation of same sub-problems is avoided
int Grundy [maximum + 1];
memset(Grundy, -1, sizeof (Grundy));
// Calculate Grundy Value of piles[i] and store it
for (int i=0; i<=n-1; i++)
calculateGrundy(piles[i], Grundy);
declareWinner(PLAYER2, piles, Grundy, n); */
return (0);
}
Java
import java.util.*;
/* Game Description-
"A game is played between two players and there are N piles
of stones such that each pile has certain number of stones.
On his/her turn, a player selects a pile and can take any
non-zero number of stones upto 3 (i.e- 1,2,3)
The player who cannot move is considered to lose the game
(i.e., one who take the last stone is the winner).
Can you find which player wins the game if both players play
optimally (they don't make any mistake)? "
A Dynamic Programming approach to calculate Grundy Number
and Mex and find the Winner using Sprague - Grundy Theorem. */
class GFG {
/* piles[] -> Array having the initial count of stones/coins
in each piles before the game has started.
n -> Number of piles
Grundy[] -> Array having the Grundy Number corresponding to
the initial position of each piles in the game
The piles[] and Grundy[] are having 0-based indexing*/
static int PLAYER1 = 1;
static int PLAYER2 = 2;
// A Function to calculate Mex of all the values in that set
static int calculateMex(HashSet<Integer> Set)
{
int Mex = 0;
while (Set.contains(Mex))
Mex++;
return (Mex);
}
// A function to Compute Grundy Number of 'n'
static int calculateGrundy(int n, int Grundy[])
{
Grundy[0] = 0;
Grundy[1] = 1;
Grundy[2] = 2;
Grundy[3] = 3;
if (Grundy[n] != -1)
return (Grundy[n]);
// A Hash Table
HashSet<Integer> Set = new HashSet<Integer>();
for (int i = 1; i <= 3; i++)
Set.add(calculateGrundy (n - i, Grundy));
// Store the result
Grundy[n] = calculateMex (Set);
return (Grundy[n]);
}
// A function to declare the winner of the game
static void declareWinner(int whoseTurn, int piles[],
int Grundy[], int n)
{
int xorValue = Grundy[piles[0]];
for (int i = 1; i <= n - 1; i++)
xorValue = xorValue ^ Grundy[piles[i]];
if (xorValue != 0)
{
if (whoseTurn == PLAYER1)
System.out.printf("Player 1 will win\n");
else
System.out.printf("Player 2 will win\n");
}
else
{
if (whoseTurn == PLAYER1)
System.out.printf("Player 2 will win\n");
else
System.out.printf("Player 1 will win\n");
}
return;
}
// Driver code
public static void main(String[] args)
{
// Test Case 1
int piles[] = {3, 4, 5};
int n = piles.length;
// Find the maximum element
int maximum = Arrays.stream(piles).max().getAsInt();
// An array to cache the sub-problems so that
// re-computation of same sub-problems is avoided
int Grundy[] = new int[maximum + 1];
Arrays.fill(Grundy, -1);
// Calculate Grundy Value of piles[i] and store it
for (int i = 0; i <= n - 1; i++)
calculateGrundy(piles[i], Grundy);
declareWinner(PLAYER1, piles, Grundy, n);
/* Test Case 2
int piles[] = {3, 8, 2};
int n = sizeof(piles)/sizeof(piles[0]);
int maximum = *max_element (piles, piles + n);
// An array to cache the sub-problems so that
// re-computation of same sub-problems is avoided
int Grundy [maximum + 1];
memset(Grundy, -1, sizeof (Grundy));
// Calculate Grundy Value of piles[i] and store it
for (int i=0; i<=n-1; i++)
calculateGrundy(piles[i], Grundy);
declareWinner(PLAYER2, piles, Grundy, n); */
}
}
// This code is contributed by PrinciRaj1992
Python3
''' Game Description-
"A game is played between two players and there are N piles
of stones such that each pile has certain number of stones.
On his/her turn, a player selects a pile and can take any
non-zero number of stones upto 3 (i.e- 1,2,3)
The player who cannot move is considered to lose the game
(i.e., one who take the last stone is the winner).
Can you find which player wins the game if both players play
optimally (they don't make any mistake)? "
A Dynamic Programming approach to calculate Grundy Number
and Mex and find the Winner using Sprague - Grundy Theorem.
piles[] -> Array having the initial count of stones/coins
in each piles before the game has started.
n -> Number of piles
Grundy[] -> Array having the Grundy Number corresponding to
the initial position of each piles in the game
The piles[] and Grundy[] are having 0-based indexing'''
PLAYER1 = 1
PLAYER2 = 2
# A Function to calculate Mex of all
# the values in that set
def calculateMex(Set):
Mex = 0;
while (Mex in Set):
Mex += 1
return (Mex)
# A function to Compute Grundy Number of 'n'
def calculateGrundy(n, Grundy):
Grundy[0] = 0
Grundy[1] = 1
Grundy[2] = 2
Grundy[3] = 3
if (Grundy[n] != -1):
return (Grundy[n])
# A Hash Table
Set = set()
for i in range(1, 4):
Set.add(calculateGrundy(n - i,
Grundy))
# Store the result
Grundy[n] = calculateMex(Set)
return (Grundy[n])
# A function to declare the winner of the game
def declareWinner(whoseTurn, piles, Grundy, n):
xorValue = Grundy[piles[0]];
for i in range(1, n):
xorValue = (xorValue ^
Grundy[piles[i]])
if (xorValue != 0):
if (whoseTurn == PLAYER1):
print("Player 1 will win\n");
else:
print("Player 2 will win\n");
else:
if (whoseTurn == PLAYER1):
print("Player 2 will win\n");
else:
print("Player 1 will win\n");
# Driver code
if __name__=="__main__":
# Test Case 1
piles = [ 3, 4, 5 ]
n = len(piles)
# Find the maximum element
maximum = max(piles)
# An array to cache the sub-problems so that
# re-computation of same sub-problems is avoided
Grundy = [-1 for i in range(maximum + 1)];
# Calculate Grundy Value of piles[i] and store it
for i in range(n):
calculateGrundy(piles[i], Grundy);
declareWinner(PLAYER1, piles, Grundy, n);
''' Test Case 2
int piles[] = {3, 8, 2};
int n = sizeof(piles)/sizeof(piles[0]);
int maximum = *max_element (piles, piles + n);
// An array to cache the sub-problems so that
// re-computation of same sub-problems is avoided
int Grundy [maximum + 1];
memset(Grundy, -1, sizeof (Grundy));
// Calculate Grundy Value of piles[i] and store it
for (int i=0; i<=n-1; i++)
calculateGrundy(piles[i], Grundy);
declareWinner(PLAYER2, piles, Grundy, n); '''
# This code is contributed by rutvik_56
C#
using System;
using System.Linq;
using System.Collections.Generic;
/* Game Description-
"A game is played between two players and there are N piles
of stones such that each pile has certain number of stones.
On his/her turn, a player selects a pile and can take any
non-zero number of stones upto 3 (i.e- 1,2,3)
The player who cannot move is considered to lose the game
(i.e., one who take the last stone is the winner).
Can you find which player wins the game if both players play
optimally (they don't make any mistake)? "
A Dynamic Programming approach to calculate Grundy Number
and Mex and find the Winner using Sprague - Grundy Theorem. */
class GFG
{
/* piles[] -> Array having the initial count of stones/coins
in each piles before the game has started.
n -> Number of piles
Grundy[] -> Array having the Grundy Number corresponding to
the initial position of each piles in the game
The piles[] and Grundy[] are having 0-based indexing*/
static int PLAYER1 = 1;
//static int PLAYER2 = 2;
// A Function to calculate Mex of all the values in that set
static int calculateMex(HashSet<int> Set)
{
int Mex = 0;
while (Set.Contains(Mex))
Mex++;
return (Mex);
}
// A function to Compute Grundy Number of 'n'
static int calculateGrundy(int n, int []Grundy)
{
Grundy[0] = 0;
Grundy[1] = 1;
Grundy[2] = 2;
Grundy[3] = 3;
if (Grundy[n] != -1)
return (Grundy[n]);
// A Hash Table
HashSet<int> Set = new HashSet<int>();
for (int i = 1; i <= 3; i++)
Set.Add(calculateGrundy (n - i, Grundy));
// Store the result
Grundy[n] = calculateMex (Set);
return (Grundy[n]);
}
// A function to declare the winner of the game
static void declareWinner(int whoseTurn, int []piles,
int []Grundy, int n)
{
int xorValue = Grundy[piles[0]];
for (int i = 1; i <= n - 1; i++)
xorValue = xorValue ^ Grundy[piles[i]];
if (xorValue != 0)
{
if (whoseTurn == PLAYER1)
Console.Write("Player 1 will win\n");
else
Console.Write("Player 2 will win\n");
}
else
{
if (whoseTurn == PLAYER1)
Console.Write("Player 2 will win\n");
else
Console.Write("Player 1 will win\n");
}
return;
}
// Driver code
static void Main()
{
// Test Case 1
int []piles = {3, 4, 5};
int n = piles.Length;
// Find the maximum element
int maximum = piles.Max();
// An array to cache the sub-problems so that
// re-computation of same sub-problems is avoided
int []Grundy = new int[maximum + 1];
Array.Fill(Grundy, -1);
// Calculate Grundy Value of piles[i] and store it
for (int i = 0; i <= n - 1; i++)
calculateGrundy(piles[i], Grundy);
declareWinner(PLAYER1, piles, Grundy, n);
/* Test Case 2
int piles[] = {3, 8, 2};
int n = sizeof(piles)/sizeof(piles[0]);
int maximum = *max_element (piles, piles + n);
// An array to cache the sub-problems so that
// re-computation of same sub-problems is avoided
int Grundy [maximum + 1];
memset(Grundy, -1, sizeof (Grundy));
// Calculate Grundy Value of piles[i] and store it
for (int i=0; i<=n-1; i++)
calculateGrundy(piles[i], Grundy);
declareWinner(PLAYER2, piles, Grundy, n); */
}
}
// This code is contributed by mits
JavaScript
<script>
/* Game Description-
"A game is played between two players and there are N piles
of stones such that each pile has certain number of stones.
On his/her turn, a player selects a pile and can take any
non-zero number of stones upto 3 (i.e- 1,2,3)
The player who cannot move is considered to lose the game
(i.e., one who take the last stone is the winner).
Can you find which player wins the game if both players play
optimally (they don't make any mistake)? "
A Dynamic Programming approach to calculate Grundy Number
and Mex and find the Winner using Sprague - Grundy Theorem. */
/* piles[] -> Array having the initial count of stones/coins
in each piles before the game has started.
n -> Number of piles
Grundy[] -> Array having the Grundy Number corresponding to
the initial position of each piles in the game
The piles[] and Grundy[] are having 0-based indexing*/
let PLAYER1 = 1;
let PLAYER2 = 2;
// A Function to calculate Mex of all the values in that set
function calculateMex(Set)
{
let Mex = 0;
while (Set.has(Mex))
Mex++;
return (Mex);
}
// A function to Compute Grundy Number of 'n'
function calculateGrundy(n,Grundy)
{
Grundy[0] = 0;
Grundy[1] = 1;
Grundy[2] = 2;
Grundy[3] = 3;
if (Grundy[n] != -1)
return (Grundy[n]);
// A Hash Table
let Set = new Set();
for (let i = 1; i <= 3; i++)
Set.add(calculateGrundy (n - i, Grundy));
// Store the result
Grundy[n] = calculateMex (Set);
return (Grundy[n]);
}
// A function to declare the winner of the game
function declareWinner(whoseTurn,piles,Grundy,n)
{
let xorValue = Grundy[piles[0]];
for (let i = 1; i <= n - 1; i++)
xorValue = xorValue ^ Grundy[piles[i]];
if (xorValue != 0)
{
if (whoseTurn == PLAYER1)
document.write("Player 1 will win<br>");
else
document.write("Player 2 will win<br>");
}
else
{
if (whoseTurn == PLAYER1)
document.write("Player 2 will win<br>");
else
document.write("Player 1 will win<br>");
}
return;
}
// Driver code
// Test Case 1
let piles = [3, 4, 5];
let n = piles.length;
// Find the maximum element
let maximum = Math.max(...piles)
// An array to cache the sub-problems so that
// re-computation of same sub-problems is avoided
let Grundy = new Array(maximum + 1);
for(let i=0;i<maximum+1;i++)
Grundy[i]=0;
// Calculate Grundy Value of piles[i] and store it
for (let i = 0; i <= n - 1; i++)
calculateGrundy(piles[i], Grundy);
declareWinner(PLAYER1, piles, Grundy, n);
/* Test Case 2
int piles[] = {3, 8, 2};
int n = sizeof(piles)/sizeof(piles[0]);
int maximum = *max_element (piles, piles + n);
// An array to cache the sub-problems so that
// re-computation of same sub-problems is avoided
int Grundy [maximum + 1];
memset(Grundy, -1, sizeof (Grundy));
// Calculate Grundy Value of piles[i] and store it
for (int i=0; i<=n-1; i++)
calculateGrundy(piles[i], Grundy);
declareWinner(PLAYER2, piles, Grundy, n); */
// This code is contributed by avanitrachhadiya2155
</script>
Output :
Player 1 will win
Time complexity : O(n^2), where n is the maximum number of stones in a pile.
Space complexity :O(n), as the Grundy array is used to store the results of subproblems to avoid redundant computations and it takes O(n) space.
References :
https://en.wikipedia.org/wiki/Sprague%E2%80%93Grundy_theorem
Exercise to the Readers: Consider the below game.
“A game is played by two players with N integers A1, A2, .., AN. On his/her turn, a player selects an integer, divides it by 2, 3, or 6, and then takes the floor. If the integer becomes 0, it is removed. The last player to move wins. Which player wins the game if both players play optimally?”
Hint : See the example 3 of previous article.
Similar Reads
Basics & Prerequisites
Data Structures
Array Data StructureIn this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous
3 min read
String in Data StructureA string is a sequence of characters. The following facts make string an interesting data structure.Small set of elements. Unlike normal array, strings typically have smaller set of items. For example, lowercase English alphabet has only 26 characters. ASCII has only 256 characters.Strings are immut
2 min read
Hashing in Data StructureHashing is a technique used in data structures that efficiently stores and retrieves data in a way that allows for quick access. Hashing involves mapping data to a specific index in a hash table (an array of items) using a hash function. It enables fast retrieval of information based on its key. The
2 min read
Linked List Data StructureA linked list is a fundamental data structure in computer science. It mainly allows efficient insertion and deletion operations compared to arrays. Like arrays, it is also used to implement other data structures like stack, queue and deque. Hereâs the comparison of Linked List vs Arrays Linked List:
2 min read
Stack Data StructureA Stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO implies that the element that is inserted last, comes out first and FILO implies that the element that is inserted first
2 min read
Queue Data StructureA Queue Data Structure is a fundamental concept in computer science used for storing and managing data in a specific order. It follows the principle of "First in, First out" (FIFO), where the first element added to the queue is the first one to be removed. It is used as a buffer in computer systems
2 min read
Tree Data StructureTree Data Structure is a non-linear data structure in which a collection of elements known as nodes are connected to each other via edges such that there exists exactly one path between any two nodes. Types of TreeBinary Tree : Every node has at most two childrenTernary Tree : Every node has at most
4 min read
Graph Data StructureGraph Data Structure is a collection of nodes connected by edges. It's used to represent relationships between different entities. If you are looking for topic-wise list of problems on different topics like DFS, BFS, Topological Sort, Shortest Path, etc., please refer to Graph Algorithms. Basics of
3 min read
Trie Data StructureThe Trie data structure is a tree-like structure used for storing a dynamic set of strings. It allows for efficient retrieval and storage of keys, making it highly effective in handling large datasets. Trie supports operations such as insertion, search, deletion of keys, and prefix searches. In this
15+ min read
Algorithms
Searching AlgorithmsSearching algorithms are essential tools in computer science used to locate specific items within a collection of data. In this tutorial, we are mainly going to focus upon searching in an array. When we search an item in an array, there are two most common algorithms used based on the type of input
2 min read
Sorting AlgorithmsA Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ
3 min read
Introduction to RecursionThe process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. A recursive algorithm takes one step toward solution and then recursively call itself to further move. The algorithm stops once we reach the solution
14 min read
Greedy AlgorithmsGreedy algorithms are a class of algorithms that make locally optimal choices at each step with the hope of finding a global optimum solution. At every step of the algorithm, we make a choice that looks the best at the moment. To make the choice, we sometimes sort the array so that we can always get
3 min read
Graph AlgorithmsGraph is a non-linear data structure like tree data structure. The limitation of tree is, it can only represent hierarchical data. For situations where nodes or vertices are randomly connected with each other other, we use Graph. Example situations where we use graph data structure are, a social net
3 min read
Dynamic Programming or DPDynamic Programming is an algorithmic technique with the following properties.It is mainly an optimization over plain recursion. Wherever we see a recursive solution that has repeated calls for the same inputs, we can optimize it using Dynamic Programming. The idea is to simply store the results of
3 min read
Bitwise AlgorithmsBitwise algorithms in Data Structures and Algorithms (DSA) involve manipulating individual bits of binary representations of numbers to perform operations efficiently. These algorithms utilize bitwise operators like AND, OR, XOR, NOT, Left Shift, and Right Shift.BasicsIntroduction to Bitwise Algorit
4 min read
Advanced
Segment TreeSegment Tree is a data structure that allows efficient querying and updating of intervals or segments of an array. It is particularly useful for problems involving range queries, such as finding the sum, minimum, maximum, or any other operation over a specific range of elements in an array. The tree
3 min read
Pattern SearchingPattern searching algorithms are essential tools in computer science and data processing. These algorithms are designed to efficiently find a particular pattern within a larger set of data. Patten SearchingImportant Pattern Searching Algorithms:Naive String Matching : A Simple Algorithm that works i
2 min read
GeometryGeometry is a branch of mathematics that studies the properties, measurements, and relationships of points, lines, angles, surfaces, and solids. From basic lines and angles to complex structures, it helps us understand the world around us.Geometry for Students and BeginnersThis section covers key br
2 min read
Interview Preparation
Practice Problem