Breadth First Search (BFS) Depth First Search (DFS) A-Star Search (A ) Minimax Algorithm Alpha-Beta Pruning
Breadth First Search (BFS) Depth First Search (DFS) A-Star Search (A ) Minimax Algorithm Alpha-Beta Pruning
Breadth First Search (BFS) Depth First Search (DFS) A-Star Search (A ) Minimax Algorithm Alpha-Beta Pruning
Topics
Graph Traversal methods have always quite fascinated me. From performing
effective peer to peer communication to finding the nearest restaurants and cafes
using GPS, traversal methods have a varied set of applications in the real-world
scenario. In this blog on Breadth-First Search Algorithm, we will discuss the logic
behind graph traversal methods and use examples to understand the working of the
Breadth-First Search algorithm.
The process of visiting and exploring a graph for processing is called graph
traversal. To be more specific it is all about visiting and exploring each vertex and
edge in a graph such that all the vertices are explored exactly once.
There are several graph traversal techniques such as Breadth-First Search, Depth
First Search and so on. The challenge is to use a graph traversal technique that is
most suitable for solving a particular problem. This brings us to the Breadth-First
Search technique.
Before we get started, you must be familiar with the main data structure involved
in the Breadth-First Search algorithm.
Now let‘s take a look at the steps involved in traversing a graph by using Breadth-
First Search:
Step 2: Select a starting node (visiting a node) and insert it into the Queue.
Step 3: Provided that the Queue is not empty, extract the node from the Queue and
insert its child nodes (exploring a node) into the Queue.
Take a look at the below graph, we will use the Breadth-First Search algorithm to
traverse through the graph.
1. Assign ‗a‘ as the root node and insert it into the Queue.
2. Extract node ‗a‘ from the queue and insert the child nodes of ‗a‘, i.e., ‗b‘ and
‗c‘.
Breadth-first Search is a simple graph traversal method that has a surprising range
of applications. Here are a few interesting ways in which Bread-First Search is
being used:
GPS Navigation systems: Breadth-First Search is one of the best algorithms used
to find neighboring locations by using the GPS system.
Find the Shortest Path & Minimum Spanning Tree for an unweighted
graph: When it comes to an unweighted graph, calculating the shortest path is
quite simple since the idea behind shortest path is to choose a path with the least
number of edges. Breadth-First Search can allow this by traversing a minimum
number of nodes starting from the source node. Similarly, for a spanning tree, we
can use either of the two, Breadth-First Search or Depth-first traversal methods to
find a spanning tree.
A* Search
Reaching a destination via the shortest route is a daily activity we all do. A-
star (also referred to as A*) is one of the most successful search algorithms to find
the shortest path between nodes or graphs. It is an informed search algorithm, as it
uses information about path cost and also uses heuristics to find the solution.
Node (also called State) — All potential position or stops with a unique
identification
Cost — Numerical value (say distance, time, or financial expense) for the
path from a node to another node.
g(n) — this represents the exact cost of the path from the starting node to
any node n
h(n) — this represents the heuristic estimated cost from node n to the goal
node.
Each time A* enters a node, it calculates the cost, f(n)(n being the neighboring
node), to travel to all of the neighboring nodes, and then enters the node with the
lowest value of f(n).
https://www.edureka.co/blog/a-search-algorithm/
https://towardsdatascience.com/a-star-a-search-algorithm-eb495fb156bb
It is used in games such as tic-tac-toe, go, chess, Isola, checkers, and many other two-player
games.
Such games are called games of perfect information because it is possible to see all the possible
moves of a particular game.
There can be two-player games which are not of perfect information such as Scrabble because
the opponent‘s move cannot be predicted.
It is similar to how we think when we play a game: ―if I make this move, then my opponent can
only make only these moves,‖ and so on.
Minimax is called so because it helps in minimizing the loss when the other player chooses the
strategy having the maximum loss.
Terminology
Game Tree: It is a structure in the form of a tree consisting of all the
possible moves which allow you to move from a state of the game to
the next state.
A game can be defined as a search problem with the following components:
Initial state: It comprises the position of the board and showing whose
move it is.
Successor function: It defines what the legal moves a player can make
are.
Terminal state: It is the position of the board when the game gets over.
Utility function: It is a function which assigns a numeric value for the
outcome of a game. For instance, in chess or tic-tac-toe, the outcome is
Dept. of CS , RNLKWC Page 20
either a win, a loss, or a draw, and these can be represented by the
values +1, -1, or 0, respectively. There are games that have a much
larger range of possible outcomes; for instance, the utilities in
backgammon varies from +192 to -192. A utility function can also be
called a payoff function.
Step 1: First, generate the entire game tree starting with the current position of the
game all the way upto the terminal states. This is how the game tree looks like for
the game tic-tac-toe.
1. The initial state is the first layer that defines that the board is blank it‘s
MAX‘s turn to play.
2. Successor function lists all the possible successor moves. It is defined for all
the layers in the tree.
3. Terminal State is the last layer of the tree that shows the final state, i.e
whether the player MAX wins, loses, or ties with the opponent.
4. Utilities in this case for the terminal states are 1, 0, and -1 as discussed
earlier, and they can be used to determine the utilities of the other
nodes as well.
Dept. of CS , RNLKWC Page 22
Step 2: Apply the utility function to get the utility values for all the terminal
states.
Step 3: Determine the utilities of the higher nodes with the help of the
utilities of the terminal nodes. For instance, in the diagram below, we have
the utilities for the terminal states written in the squares.
Step 4: Calculate the utility values with the help of leaves considering one
layer at a time until the root of the tree.
In our example, we only have 3 layers so we immediately reached to the root but in actual
games, there will be many more layers and nodes. So we have to evaluate MAX{3,2} which is 3.
Therefore, the best opening move for MAX is the left node(or the red one). This move is called
the minimax decision as it maximizes the utility following the assumption that the opponent is
To summarize,
= MAX{3,2}
=3
if maximizingPlayer
bestValue := ??
for each child of node
v := minimax(child, depth ? 1, FALSE)
bestValue := max(bestValue, v)
return bestValue
The method that we are going to look in this article is called alpha-beta
pruning.
Let us understand the intuition behind this first and then we will formalize the
algorithm. Suppose, we have the following game tree:
In this case,
Minimax Decision = MAX{MIN{3,5,10}, MIN{2,a,b}, MIN{2,7,3}}
How could we calculate the maximum with a missing value? Here is the
trick. MIN{2,a,b} would certainly be less than or equal to 2, i.e., c<=2 and
hence MAX{3,c,2} has to be 3.
We could have reached a conclusion without looking at those nodes. And this
is where alpha-beta pruning comes into the picture.
A few definitions:
Alpha: It is the best choice so far for the player MAX. We want to get the highest
possible value here.
Beta: It is the best choice so far for MIN, and it has to be the lowest possible
value.
Note: Each node has to keep track of its alpha and beta values. Alpha can be
updated only when it‘s MAX‘s turn and, similarly, beta can be updated only when
it‘s MIN‘s chance.
Pseudocode:
if node is a leaf
return beta
return beta
Dept. of CS , RNLKWC Page 30
if node is a maximizing node
for each child of node
alpha = max (alpha, evaluate (child, alpha, beta))
if beta <= alpha
return alpha
return alpha
References:
http://nptel.ac.in/courses/106105078/8