Professional Documents
Culture Documents
4.7 DynamicProg Minimax Proofs
4.7 DynamicProg Minimax Proofs
Part 1: Naïve to Dynamic
4.7 Dynamic Programming & Programming
Minimax Algorithm & Proofs Solution Example
Online Lesson 10/8
Virtual School Victoria
VSV 2021 VCE Algorithmics HESS Unit 4 VSV 2021 VCE Algorithmics HESS Unit 4
Resource Cutting Problem
Resource Cutting Problem
Given: Optimization problem: Cuts n=4 Price
• A resource of a certain length, 𝑛. There are many combinations
• An array of prices for resource of 1,1,1,1 5
or ways to cut the resource,
sizes between 1 and 𝑛. 1,1,2 7
What is the best way to cut the
you want the one that 1,3 9
resource so that you make the most maximizes the price overall. 2,2 10
money out of selling it? 4 9
Length i 1 2 3 4
Optimization problem:
• There are many ways to cut the Price pi 1 5 8 9
resource, you want the best one.
VSV 2021 VCE Algorithmics HESS Unit 4 VSV 2021 VCE Algorithmics HESS Unit 4
Resource Cutting Problem Resource Cutting Problem
• Recursive solution, a brute force approach.
Length i 1 2 3 4 – Try cutting a first piece of all possible sizes and make a recursive
Cuts n=4 Price call for cutting the remaining piece
Price pi 1 5 8 9
– Decrease & Conquer recursion
1,1,1,1 4 – Add the values (of the first piece and the result of the recursive call)
– Return the max of all these combinations
1 4 1,1,2 7 • Exponential growth of options to check – with overlapping
2 3 sub problems.
1 1,3 9
2 3 1 2 1
2,2 10
1 2 1 1
4 9
1
Length i 1 2 3 4
Price pi 1 5 8 9
Recursion tree n=4
VSV 2021 VCE Algorithmics HESS Unit 4 VSV 2021 VCE Algorithmics HESS Unit 4
1
10/08/2021
Recursion tree n=5
Length i 1 2 3 4
Price pi 1 5 8 9 Recursive Solution – Naïve Brute Force
set globaltally to –infinity (global)
Function CutResource(p,l,n)
// Input p an array of prices
// Input l an array of lengths ascending
// Input n size to cut
Recursion tree n=6
If (n ≤ 0) then
// base case
return 0
Else
for i=1 to length(l) do
if n l i 0 then
tally := p[i]+CutResource(p,l,n‐l[i])
Recursion tree n=7 globalTally=max(globalTally, tally)
end if
end do
End If Length i 1 2 3 4
return tally
Price pi 1 5 8 9
End Function
VSV 2021 VCE Algorithmics HESS Unit 4 VSV 2021 VCE Algorithmics HESS Unit 4
ATTENTION: Overlapping sub‐problems are observed
in naïve solution, this is a candidate for Dynamic
Programming to improve time complexity for finding
the solution.
Length i 1 2 3 4
Price pi 1 5 8 9
VSV 2021 VCE Algorithmics HESS Unit 4 VSV 2021 VCE Algorithmics HESS Unit 4
Dynamic Programming Design Pattern Bottom‐Up Dynamic Programming
Function DP‐CutResource(p,l,n) Length i 1 2 3 4
// Input p an array of prices Price pi 1 5 8 9
Solve for n // Input l an array of lengths ascending
// Input n size to cut
// create a result array using DP bottom up
Build up solutions for 1…n in an array For j = 1 to n do
result[j]:=‐infinity //initialise the result cell
i := 1
Calculate the best solution for 1 to (n‐1) once only while ((l(i) j) AND (i length(l)) do
// try to improve with each available length
if (j‐l(i)) equals to zero then
Calculate the best solution for n once only result[j]:=maximum(result[j],p[i])
else
result[j]:=maximum(result[j],p[i]+result[j‐l(i)])
end if
i := i + 1
end do
End do
Return result
VSV 2021 VCE Algorithmics HESS Unit 4
End Function VSV 2021 VCE Algorithmics HESS Unit 4
2
10/08/2021
Length i 1 2 3 4
Price pi 1 5 8 9
VSV 2021 VCE Algorithmics HESS Unit 4 VSV 2021 VCE Algorithmics HESS Unit 4
Bottom‐Up Dynamic Programming
Function DP‐CutResource(p,l,n)
// Input p an array of prices
Time Complexity of DP solution
Part 2: Minimax Algorithm
// Input l an array of lengths ascending
// Input n size to cut
// create a result array using DP bottom up For j=1 to n
looking ahead a few levels
For j = 1 to n do
result[j]:=‐infinity //initialise the result cell For i=1 to j
i := 1
while ((l(i) j) AND (i length(l)) do Nested Loop
// try to improve with each available length
if (j‐l(i)) equals to zero then to plan a move
result[j]:=maximum(result[j],p[i]) Inner loop actions execute
else
result[j]:=maximum(result[j],p[i]+result[j‐l(i)])
end if 1 + 2 + 3 + 4 + 5 +….n times
i := i + 1
end do
End do
Return result
End Function
𝑛 𝑛 1
𝑛𝑢𝑚𝑏𝑒𝑟 𝑜𝑓 𝑎𝑐𝑡𝑖𝑜𝑛𝑠 𝑗
2
𝑛𝑢𝑚𝑏𝑒𝑟 𝑜𝑓 𝑎𝑐𝑡𝑖𝑜𝑛𝑠 𝑂 𝑛
VSV 2021 VCE Algorithmics HESS Unit 4 VSV 2021 VCE Algorithmics HESS Unit 4
Mathematicians categorize games Zero‐Sum Games
• Perfect information games like checkers and chess; the board
shows both players all the information needed to make the Zero‐sum games are games in which a winner’s payoff
right decision. is equal to a loser’s loss.
• Imperfect information games like poker; no player has For example in chess, one person’s victory is balanced by the
enough information (the other player’s card are hidden) to opponent’s loss. Even if the chess game ends in a stalemate, then
make a guaranteed right decision. neither has gained or lost.
In a poker game your winnings add up to the sum of losses of your
opponents. At the end of the game, poker players as a group are
no wealthier or poorer than when they started.
VSV 2021 VCE Algorithmics HESS Unit 4 VSV 2021 VCE Algorithmics HESS Unit 4
3
10/08/2021
VSV 2021 VCE Algorithmics HESS Unit 4 VSV 2021 VCE Algorithmics HESS Unit 4
VSV 2021 VCE Algorithmics HESS Unit 4 VSV 2021 VCE Algorithmics HESS Unit 4
function minimax(node, depth, maximizingPlayer)
Minimax Algorithm if (depth = 0 or node is a terminal node) then
return the heuristic value of node
• Minimax is a decision algorithm used end if
in decision theory for minimizing the if (maximizingPlayer) then
possible loss for a worst case bestValue := −∞
(maximum loss) scenario. for each child of node do
• Originally formulated for two‐ v := minimax(child, depth − 1, FALSE)
player zero‐sum games where players bestValue := max(bestValue, v)
take alternate moves. end do
return bestValue
function minimax(node, depth, maximizingPlayer) else // minimizing player
if (depth = 0 or node is a terminal node) then
bestValue := +∞
return the heuristic value of node
end if
for each child of node do
if (maximizingPlayer) then v := minimax(child, depth − 1, TRUE)
// maximize gain for player recursively to (depth‐1) bestValue := min(bestValue, v)
else end do
// minimise gain for opponent recursively to (depth‐1) return bestValue
end if end if
End function // Initial call for maximizing player End function // Initial call for maximizing player
minimax(origin, depth, TRUE) minimax(origin, depth, TRUE)
VSV 2021 VCE Algorithmics HESS Unit 4 VSV 2021 VCE Algorithmics HESS Unit 4
4
10/08/2021
Minimax Algorithm Minimax Algorithm
VSV 2021 VCE Algorithmics HESS Unit 4 VSV 2021 VCE Algorithmics HESS Unit 4
Proof of Correctness for Divide &
Part 3: Proof of Correctness by Conquer designs.
Main Steps
Induction & Contradiction 1. Define your recursive non overlapping sub‐problem. Describe in English what
your sub‐problem means.
Divide & Conquer
2. Define the base cases. A recursive algorithm has base cases, and you should
Dynamic Programming state what they are.
Backtracking 3. Present the recursive cases. Give a mathematical definition of your sub‐
problem in terms of “smaller” sub‐problems that don’t overlap. Therefore it is
Divide & Conquer.
4. Prove correctness. This will be an inductive proof that your algorithm does
what it is supposed to. Start by arguing that the base cases return the correct
result, then for the inductive step, argue why the recursive cases combine to
solve the overall problem.
VSV 2021 VCE Algorithmics HESS Unit 4 VSV 2021 VCE Algorithmics HESS Unit 4
Divide & Conquer Proof 1 Divide & Conquer Proof 2
Mergesort Divide & Conquer Mergesort Base Case & Inductive
Design Step
Algorithm Mergesort(Input List: L) Let T(n) denote the running Algorithm Mergesort(Input List: L) As a base case, consider
if |L| = 1 then time of Mergesort(L) when if |L| = 1 then when length of list L = 1.
return L L is a list of n elements return L
else then: else
This one‐element list is
divide L into two halves A and B of size n/2 and n/2 respectively. T(n) = 2T(n/2) + O(n) with divide L into two halves A and B of size n/2 and n/2 respectively.
let A∗ = Mergesort(A) and B∗ = Mergesort(B) base case T(1) = O(1) let A∗ = Mergesort(A) and B∗ = Mergesort(B) already sorted, and our
let L∗ be a new empty list let L∗ be a new empty list algorithm correctly
// merge A* and B* in correct order into L* And therefore the running // merge A* and B* in correct order into L* returns L as the sorted
while both A∗ and B∗ are non‐empty do time is O(n log n). while both A∗ and B∗ are non‐empty do list.
if first element of A∗ < first element of B∗ then if first element of A∗ < first element of B∗ then
append the first element of A∗ onto L∗ and remove it from A∗ Claim. Mergesort(L) append the first element of A∗ onto L∗ and remove it from A∗
else correctly returns the else For the induction step,
append the first element of B∗ onto L∗ and remove it from B∗ elements of L in sorted append the first element of B∗ onto L∗ and remove it from B∗
end if order. end if
end while nd while Suppose for any list L of
if one of A∗ or B∗ is non‐empty then if one of A∗ or B∗ is non‐empty then length < n; Mergesort(L)
append it to the end of L∗ append it to the end of L∗ correctly sorts L.
end if end if
return L∗ return L∗
end if end if
VSV 2021 VCE Algorithmics HESS Unit 4 VSV 2021 VCE Algorithmics HESS Unit 4
5
10/08/2021
VSV 2021 VCE Algorithmics HESS Unit 4 VSV 2021 VCE Algorithmics HESS Unit 4
VSV 2021 VCE Algorithmics HESS Unit 4 VSV 2021 VCE Algorithmics HESS Unit 4
6
10/08/2021
Proof of Correctness for
Backtracking Programming designs.
Using tree terminology, if there is
a decision path from the root to Current
variable
the target where each node is a
partial solution of variables, a tree
traversal algorithm with Dead
End
consistency checks that are Future Future
correct will find the path. variable variable
Consistency checks can be shown to find the solution using
induction methods, extending from a partial solution that is a
current variable to future variables.
VSV 2021 VCE Algorithmics HESS Unit 4