41
M1 Logic, Algorithms and Data Structures Recursion and Stacks By: Jonas Öberg

Logic, Algorithms and Data Structures · M1 Logic, Algorithms and Data Structures Recursion and Stacks By: Jonas Öberg

  • Upload
    others

  • View
    15

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Logic, Algorithms and Data Structures · M1 Logic, Algorithms and Data Structures Recursion and Stacks By: Jonas Öberg

M1

Logic, Algorithms and Data StructuresRecursion and Stacks

By: Jonas Öberg

Page 2: Logic, Algorithms and Data Structures · M1 Logic, Algorithms and Data Structures Recursion and Stacks By: Jonas Öberg

What is recursion?

Quick answer:

A recursive function is a function which uses itself

Example

f( n ) = 2⋅f (n -1) if n >= 1otherwise1

We define by the use of

Page 3: Logic, Algorithms and Data Structures · M1 Logic, Algorithms and Data Structures Recursion and Stacks By: Jonas Öberg

What is recursion?

What does it do??

f(3)

{ 3 >= 1 }f(3) = 2 * f(3-1) = 2 * f(2)

f(3) = 2 * f(2)f(2)

{ 2 >= 1 }f(2) = 2 * f(2-1) = 2 * f(1)

f(3) = 2 * 2 * f(1)f(1)

{ 1 >= 1}

f(1) = 2 * f(1-1) = 2 * f(0)

f( n ) = 2⋅f (n -1) if n >= 1otherwise1

f(3) = 2 * 2 * 2 * f(0) f(0) { 0 >= 1 } f(0) = 1f(3) = 2 * 2 * 2 * 1f(3) = 8

Page 4: Logic, Algorithms and Data Structures · M1 Logic, Algorithms and Data Structures Recursion and Stacks By: Jonas Öberg

How do we write recursively?

Problem:Write a function that computes the smallest element in a set.

Sample data:A = { 7, 3, 5, 9, 2, 12 }

Stated goal:f({7, 3, 5, 9, 2, 12}) = 2

Page 5: Logic, Algorithms and Data Structures · M1 Logic, Algorithms and Data Structures Recursion and Stacks By: Jonas Öberg

How to think

Assume that the function already does its job for a subset of A, ie. A-{x}

Our parts: f (A-{x}) and x

To this, we only need to add some glue and/or gaff tape

f (A-{x})

Page 6: Logic, Algorithms and Data Structures · M1 Logic, Algorithms and Data Structures Recursion and Stacks By: Jonas Öberg

How to think

Given:A = {7, 3, 5, 9, 2, 12}x = 7

We assume that:f(A-{x}) = 2

We want a function such that:f(A) = f(A-{x}) ... x = 2

Page 7: Logic, Algorithms and Data Structures · M1 Logic, Algorithms and Data Structures Recursion and Stacks By: Jonas Öberg

How to think

So, we want to find a function which solves:f(A) = f(A-{x}) ... x = 2

f(A) = 2 ... 7 = 2

This is what we wanted: write a function that computes the smallest element in a set.

f(A) = min(2, 7) = 2

Page 8: Logic, Algorithms and Data Structures · M1 Logic, Algorithms and Data Structures Recursion and Stacks By: Jonas Öberg

How to think

min(x, y) = if x <= y then x else y

So, min(x, y) will be our glue!

f(A) = min(f(A-{x}), x)

Page 9: Logic, Algorithms and Data Structures · M1 Logic, Algorithms and Data Structures Recursion and Stacks By: Jonas Öberg

How to think

But there's something missing here!

f( A ) = min(f (A-{x}), x)

Page 10: Logic, Algorithms and Data Structures · M1 Logic, Algorithms and Data Structures Recursion and Stacks By: Jonas Öberg

The Base Case

Computing f({3, 5, 7})

f({3, 5, 7}) =

min(3, f({5, 7})) =

min(3, min(5, f({7}))) =

min(3, min(5, min(7, f({})))) =

min(3, min(5, min(7, min({}, f({}))))) =

min(3, min(5, min(7, min({}, min({}, f({}))))))) ...

We need to define f({}) This is called the “Base Case”

f ( A ) = min( x, f(A –{x}))

Page 11: Logic, Algorithms and Data Structures · M1 Logic, Algorithms and Data Structures Recursion and Stacks By: Jonas Öberg

Implementing the base case

What's the smallest value of an empty set?

f( A ) = ∞ , if A = {}min(f (A-{x}), x) , otherwise

Page 12: Logic, Algorithms and Data Structures · M1 Logic, Algorithms and Data Structures Recursion and Stacks By: Jonas Öberg

Implementation

f( A ) = ∞ , if A = {}min(f (A-{x}), x) , otherwise

int setmin(Set a){

if (a.isEmpty()) { return Integer.MAX_VALUE; }

Iterator<Integer> i = a.iterator();int x = i.next(); i.remove();Return Math.min(x, setmin(a));

}

Page 13: Logic, Algorithms and Data Structures · M1 Logic, Algorithms and Data Structures Recursion and Stacks By: Jonas Öberg

Computing faculty

f(5) = 5 * 4 * 3 * 2 * 1 = 120

Assume that f(4) = 24 already works Then,

f(5) = 5 * f(4)

f(x) = x * f(x-1)

Page 14: Logic, Algorithms and Data Structures · M1 Logic, Algorithms and Data Structures Recursion and Stacks By: Jonas Öberg

Faculty base case

f( x ) = 1 , if x = 1x * f (x-1) , otherwise

int faculty(int x){

if (x == 1) { return 1; }

return x * faculty(x - 1);}

f(5) = 5 * f(4) = 5 * 4 * f(3) = 5 * 4 * 3 * f(2) = 5 * 4 * 3 * 2 * f(1) = 5 * 4 * 3 * 2 * 1 = 120

Page 15: Logic, Algorithms and Data Structures · M1 Logic, Algorithms and Data Structures Recursion and Stacks By: Jonas Öberg

Stacks

Page 16: Logic, Algorithms and Data Structures · M1 Logic, Algorithms and Data Structures Recursion and Stacks By: Jonas Öberg

When is the stack used?

“Kalle”

Stack

main()function(1, “Kalle”, 34);

function(a, b, c)List l = new ArrayList();l.add(b);

34

1

b

Page 17: Logic, Algorithms and Data Structures · M1 Logic, Algorithms and Data Structures Recursion and Stacks By: Jonas Öberg

Stacks when recursing

faculty(5)5 * faculty(4)5 * 4 * faculty(3)5 * 4 * 3 * faculty(2)5 * 4 * 3 * 2 * faculty(1)5 * 4 * 3 * 2 * 1

4

Stack

321

5 f( x ) = 1 , if x = 1x * f (x-1) , otherwise

624120

Page 18: Logic, Algorithms and Data Structures · M1 Logic, Algorithms and Data Structures Recursion and Stacks By: Jonas Öberg

public class RecHelloWorld { public static void write_string (String s) { System.out.printf("\n%s\n", s); write_string(s); } public static void main(String[] args) { write_string("Hello World"); }}

Hello WorldHello WorldHello WorldHello WorldHello WorldException in thread "main" java.lang.StackOverflowError

Page 19: Logic, Algorithms and Data Structures · M1 Logic, Algorithms and Data Structures Recursion and Stacks By: Jonas Öberg

Tower of Hanoi

Page 20: Logic, Algorithms and Data Structures · M1 Logic, Algorithms and Data Structures Recursion and Stacks By: Jonas Öberg

Algorithm

Total of rings = n Step 1: Move rings n-1 to Help tower Step 2: Move ring n to Goal tower Step 3: Move rings n-1 to Goal tower

Page 21: Logic, Algorithms and Data Structures · M1 Logic, Algorithms and Data Structures Recursion and Stacks By: Jonas Öberg

Algorithm

Base case: moving ring 1 from Start to Goal Assume that we have a function to move rings

n-1. Solve the problem for moving ring n.

Page 22: Logic, Algorithms and Data Structures · M1 Logic, Algorithms and Data Structures Recursion and Stacks By: Jonas Öberg

Algorithm

public static void move(int nr, String from, String to) { System.out.printf("%2d: Moving ring nr %d from %s to %s\n", i, nr, from, to);}

Page 23: Logic, Algorithms and Data Structures · M1 Logic, Algorithms and Data Structures Recursion and Stacks By: Jonas Öberg

Algorithm

public static void tower(int nr, String start, String goal, String help) { if (nr == 1) //Base case move(1, start, goal); else { tower(nr – 1, start, help, goal); move(nr, start, goal); tower(nr – 1, help, goal, start); }}

Step 1: Move rings n-1 to Help tower Step 2: Move ring n to Goal tower Step 3: Move rings n-1 to Goal tower

Page 24: Logic, Algorithms and Data Structures · M1 Logic, Algorithms and Data Structures Recursion and Stacks By: Jonas Öberg
Page 25: Logic, Algorithms and Data Structures · M1 Logic, Algorithms and Data Structures Recursion and Stacks By: Jonas Öberg

public class Hanoi { public static int i = 0; // global variable to keep track of method calls public static void move(int nr, String from, String to) { i++; System.out.printf("%2d: Moving ring nr %d from %s to %s\n", i, nr, from, to); }

public static void tower(int nr, String start, String goal, String help) { if (nr == 1) //Base case move(1, start, goal); else { tower(nr – 1,start, help, goal); move(nr, start, goal); tower(nr – 1, help, goal, start); } }

public static void main(String[] args) { int nr = Integer.parseInt(args[0]); tower(nr,"Start Tower", "Goal Tower", "Help Tower"); }}

Page 26: Logic, Algorithms and Data Structures · M1 Logic, Algorithms and Data Structures Recursion and Stacks By: Jonas Öberg

jonas@anheg:~> java Hanoi 3 1: Moving ring nr 1 from Start Tower to Goal Tower 2: Moving ring nr 2 from Start Tower to Help Tower 3: Moving ring nr 1 from Goal Tower to Help Tower 4: Moving ring nr 3 from Start Tower to Goal Tower 5: Moving ring nr 1 from Help Tower to Start Tower 6: Moving ring nr 2 from Help Tower to Goal Tower 7: Moving ring nr 1 from Start Tower to Goal Tower

Page 27: Logic, Algorithms and Data Structures · M1 Logic, Algorithms and Data Structures Recursion and Stacks By: Jonas Öberg

Call tree

Page 28: Logic, Algorithms and Data Structures · M1 Logic, Algorithms and Data Structures Recursion and Stacks By: Jonas Öberg

Labyrinth

Page 29: Logic, Algorithms and Data Structures · M1 Logic, Algorithms and Data Structures Recursion and Stacks By: Jonas Öberg

Code snippet

Page 30: Logic, Algorithms and Data Structures · M1 Logic, Algorithms and Data Structures Recursion and Stacks By: Jonas Öberg
Page 31: Logic, Algorithms and Data Structures · M1 Logic, Algorithms and Data Structures Recursion and Stacks By: Jonas Öberg

RPN calculator

2 1 + 4 * 72 / 5 + 1

Stack

23412726511

Page 32: Logic, Algorithms and Data Structures · M1 Logic, Algorithms and Data Structures Recursion and Stacks By: Jonas Öberg

RPN calculator

2 1 5 6 * * +

Stack

2156303032

Page 33: Logic, Algorithms and Data Structures · M1 Logic, Algorithms and Data Structures Recursion and Stacks By: Jonas Öberg

Koch snowflakes1. Start with an equilateral triangle2. Divide each line in turn in three parts3. Draw another equilateral triangle extending from the middle4. Repeat 2

Page 34: Logic, Algorithms and Data Structures · M1 Logic, Algorithms and Data Structures Recursion and Stacks By: Jonas Öberg

Fractal grammar

F = “Move forward+ = turn left 60 degrees- = turn right 60 degrees

Page 35: Logic, Algorithms and Data Structures · M1 Logic, Algorithms and Data Structures Recursion and Stacks By: Jonas Öberg

Turtle graphics

Imagine that you're a turtle!

F + + F + + F

Page 36: Logic, Algorithms and Data Structures · M1 Logic, Algorithms and Data Structures Recursion and Stacks By: Jonas Öberg

Fractal grammar

F = Move forward+ = turn left 60 degrees- = turn right 60 degrees

Koch snowflakeAxiom: F + + F + + FRules: F := F – F + + F - F

Page 37: Logic, Algorithms and Data Structures · M1 Logic, Algorithms and Data Structures Recursion and Stacks By: Jonas Öberg

Alternatives to Koch

Cesaro fractal

Page 38: Logic, Algorithms and Data Structures · M1 Logic, Algorithms and Data Structures Recursion and Stacks By: Jonas Öberg

Serpinski triangle

A, B = Move forward+ = turn left 60 degrees- = turn right 60 degrees

Serpinski triangleAxiom: ARules: A := B – A – B and B := A + B + A

Page 39: Logic, Algorithms and Data Structures · M1 Logic, Algorithms and Data Structures Recursion and Stacks By: Jonas Öberg

Fractal plant

F = Move forward-/+ = turn right/left 25 degreesX = noop[ = save position and angle to stack] = restore position and angle from stack

Fractal plantAxiom: XRules: X := F-[[X]+X]+F[+FX]-X and F := FF

Page 40: Logic, Algorithms and Data Structures · M1 Logic, Algorithms and Data Structures Recursion and Stacks By: Jonas Öberg

Fractal plant result

Page 41: Logic, Algorithms and Data Structures · M1 Logic, Algorithms and Data Structures Recursion and Stacks By: Jonas Öberg

Of course, there's work to do..