Menu
- 1.Write a Program to Implement Breadth First Search using Python.
- 2.Write a Program to Implement Depth First Search using Python
- 3.Write a Program to Implement Tic-Tac-Toe game using Python.
- 4.Write a Program to Implement 8-Puzzle problem using Python.
- 5.Write a Program to Implement Water-Jug problem using Python.
- 6.Write a Program to Implement Travelling Salesman Problem using Python.
- 7.Write a Program to Implement Tower of Hanoi using Python
- 8.Write a Program to Implement Monkey Banana Problem using Python.
Aim: Write a Program to Implement Alpha-Beta Pruning using Python
Program:
# Initial values of Alpha and Beta
MAX, MIN = 1000, -1000
# Returns optimal value for current player
# (Initially called for root and maximizer)
def minimax(depth, nodeIndex, maximizingPlayer, values, alpha, beta):
# Terminating condition. i.e
# leaf node is reached
if depth == 3:
return values[nodeIndex]
if maximizingPlayer:
best = MIN
# Recur for left and right children
for i in range(0, 2):
val = minimax(depth + 1, nodeIndex * 2 + i, False, values, alpha, beta)
best = max(best, val)
alpha = max(alpha, best)
# Alpha Beta Pruning
if beta <= alpha:
break
return best
else:
best = MAX
# Recur for left and right children
for i in range(0, 2):
val = minimax(depth + 1, nodeIndex * 2 + i, True, values, alpha, beta)
best = min(best, val)
beta = min(beta, best)
# Alpha Beta Pruning
if beta <= alpha:
break
return best
# Driver Code
if __name__ == "__main__":
values = [3, 5, 6, 9, 1, 2, 0, -1]
print("The optimal value is :", minimax(0, 0, True, values, MIN, MAX))
Output:
The optimal value is : 5