Docsity

Lecture Notes on Yet More Recursion: Minimum and Maximum | CS 112, Study notes of Computer Science

Toggle sidebar
bg1
CS112-2008S-15 Yet More Recursion: Min/Max 1
15-0: Recursion
•What is a really easy (small!) version of the problem, that I could solve immediately? (Base case)
•How can I make the problem smaller?
•Assuming that I could magically solve the smaller problem, how could I use that solution to solve the original
problem (Recursive Case)
15-1: Problems ...
•Some of the problems from this lecture are taken from
•javabat.com
•Really nice way to practice Java pr ogramming, check it out
15-2: Recursion
•sumDigits(n)
•Returns the sum of digits in n
•4321 =¿ 4 + 3 + 2 + 1 = 10
•5042 =¿ 5 + 0 + 4 + 2 = 11
•1776 =¿ 1 + 7 + 7 + 6 = 21
15-3: Recursion
int sumDigits(int n)
{
if (n == 0)
{
return 0;
}
else
{
return n % 10 + sumDigits(n / 10);
}
}
15-4: Recursion – Minimum
•Write a recursive function that returns the smallest value in the first size elements of an array of Comparable
objects
•Comparable minimum(Comparable A[], int size)
15-5: Recursion
Comparable minimum(Comparable A[], int size)
{
if (size == 0)
return null;
if (size == 1)
return A[0];
Comparable smallest = minimum(A, size - 1);
if (smallest.compareTo(A[size - 1]) < 0)
return smallest;
else
return A[size - 1];
}
15-6: Recursion – Minimum
•Write a tail-recursive function that returns the smallest value in the first nelements of an array of Comparable
objects
pf3
pf4
pf5

Partial preview of the text

Download Lecture Notes on Yet More Recursion: Minimum and Maximum | CS 112 and more Study notes Computer Science in PDF only on Docsity!

15-0: Recursion

  • What is a really easy (small!) version of the problem, that I could solve immediately? (Base case)
  • How can I make the problem smaller?
  • Assuming that I could magically solve the smaller problem, how could I use that solution to solve the original

problem (Recursive Case)

15-1: Problems ...

  • Some of the problems from this lecture are taken from
    • javabat.com
  • Really nice way to practice Java programming, check it out

15-2: Recursion

  • sumDigits(n)
    • Returns the sum of digits in n
    • 4321 =¿ 4 + 3 + 2 + 1 = 10
    • 5042 =¿ 5 + 0 + 4 + 2 = 11
    • 1776 =¿ 1 + 7 + 7 + 6 = 21

15-3: Recursion

int sumDigits(int n)

if (n == 0)

return 0;

else

return n % 10 + sumDigits(n / 10);

15-4: Recursion – Minimum

  • Write a recursive function that returns the smallest value in the first size elements of an array of Comparable

objects

  • Comparable minimum(Comparable A[], int size)

15-5: Recursion

Comparable minimum(Comparable A[], int size)

if (size == 0)

return null;

if (size == 1)

return A[0];

Comparable smallest = minimum(A, size - 1);

if (smallest.compareTo(A[size - 1]) < 0)

return smallest;

else

return A[size - 1];

15-6: Recursion – Minimum

  • Write a tail-recursive function that returns the smallest value in the first n elements of an array of Comparable

objects

  • Comparable minimum(Comparable A[], int n)

15-7: Recursion

Comparable minimum(Comparable A[], int n, Comparable smallest)

if (n == 0)

return smallest;

if (smallest.compareTo(A[n-1]) < 0)

return minimum(A, n - 1, smallest);

else

return minimum(A, n - 1, A[n-1]);

Comparable minimum(Comparable A[])

return minimum(A, A.length, null);

15-8: Recursion – Group Sum

  • Input: An array of integers, and a target sum
  • Output: true of a subset of the integers add up to the sum, false otherwise
  • Examples:

[3, 5, 7, 11, 13], 15 ==¿ true

[3, 5, 7, 11, 13], 9 ==¿ false

[3, 5, 7, 11, 13], 23 ==¿ true

[3, 5, 7, 11, 13], 40 ==¿ false

15-9: Recursion – Group Sum

  • Add an extra paramter: Number of elements in array to consider (much like minimum, above)

boolean groupSum(int A[], int size, int target)

15-10: Recursion – Group Sum

boolean groupSum(int A[], int size, int target)

if (size == 0)

return target == 0;

else if (groupSum(A, size - 1, target))

return true;

else

return groupSum(A, size - 1, target - A[size - 1]);

15-11: Recursion – Group Sum

boolean groupSum(int A[], int start, int target)

if (start == A.length)

return target == 0;

else if (groupSum(A, start + 1, target))

return true;

else

return groupSum(A, start + 1, target - A[start]);

15-12: Two player games

14 12 5 13 15 16 3 1 11 19 4 7 10 6 8 2

Max Max Max Max Max Max Max (^) Max

Min (^) Min Min^ Min

Max Max

Min

15-17: Two player games

14 12 5 13 15 16 3 1 11 19 4 7 10 6 8 2

Max Max Max Max Max Max Max Max

Min Min Min Min

Max Max

Min

14 13 16 3 19 7 10 8

(^13 3 7 )

(^138)

8

15-18: Two player games

  • A computer could do this to figure out which move to make
    • Examine all possible moves
    • Examine all possible responses to each move
    • ... all the way to the last move
    • Caclulate the value of each move (assuming opponent plays perfectly)

15-19: Two player games

  • Could we do this for a real game?
    • Checkers / Chess / Conenct-4 / etc

15-20: Two player games

  • Could we do this for a real game?
    • Checkers / Chess / Conenct-4 / etc
  • No! Too many possible games!

15-21: Two player games

  • What can we do instead?
    • Create a “board evauluation function”
  • Positive #’s are good for one player, negative #’s good for the other
  • Checkers: # of red pieces - # of black pieces (Can also take position / piece value into account)
  • Search a set number of spaces ahead, use the board evaluation function

15-22: Two player games

  • Recursion (knew we’d get there eventually ...)
  • Write two recursive functions
    • int min(Board B, int level)
      • Returns the value of the current board, looking level moves ahead, assuming that the minimizer

goes nexdt

  • int max(Board B, int level)
    • Returns the value of the current board, looking level moves ahead, assuming that the maximizer

goes nexdt

15-23: Two player games

int min(Board B, int level)

  • What is the base case?

15-24: Two player games

int min(Board b, int level)

if (level == 0)

return b.evalFunction();

15-25: Two player games

int min(Board b, int level)

if (level == 0)

return b.evalFunction();

best = Integer.MAX_VALUE;

for each possible move n we could make

b.doMove(n);

moveVal = max(b, n - 1);

if (moveVal < best)

best = moveVal;

b.undoMove(n);

return best;

15-26: Two player games

int max(Board b, int level)

if (level == 0)

return b.evalFunction();

best = - Integer.MIN_VALUE;

for each possible move n we could make

b.doMove(n)

moveVal = max(b, n - 1);

if (moveVal > best)

best = moveVal;

b.undoMove(n);

return best;