Einfhrung in die Programmierung Introduction to Programming g mm - - PowerPoint PPT Presentation

einf hrung in die programmierung introduction to
SMART_READER_LITE
LIVE PREVIEW

Einfhrung in die Programmierung Introduction to Programming g mm - - PowerPoint PPT Presentation

Chair of Softw are Engineering Einfhrung in die Programmierung Introduction to Programming g mm g Prof. Dr. Bertrand Meyer Prof. Dr. Bertrand Meyer Lecture 13: Recursion Lecture 13: Recursion The story of the universe* *According to


slide-1
SLIDE 1

Chair of Softw are Engineering

Einführung in die Programmierung Introduction to Programming g mm g

  • Prof. Dr. Bertrand Meyer
  • Prof. Dr. Bertrand Meyer

Lecture 13: Recursion Lecture 13: Recursion

slide-2
SLIDE 2

The story of the universe*

*According to Édouard Lucas, Récréations mathématiques, Paris, 1883. This is my translation; the original is on the next page.

In the great temple of Benares, under the dome that marks the center

  • f the world, three diamond needles, a foot and a half high, stand on a

copper base copper base. God on creation strung 64 plates of pure gold on one of the needles, the largest plate at the bottom and the others ever smaller on top of each largest plate at the bottom and the others ever smaller on top of each

  • ther. That is the tower of Brahmâ.

The monks must continuously move the plates until they will be set in The monks must continuously move the plates until they will be set in the same configuration on another needle. The rule of Brahmâ is simple: only one plate at a time, and never a he ru e of Brahmâ s s mp e on y one p ate at a t me, and ne er a larger plate on a smaller one. When they reach that goal, the world will crumble into dust and

2

y g , disappear.

slide-3
SLIDE 3

The story of the universe*

*According to Édouard Lucas, Récréations mathématiques, Paris, 1883.

Dans le grand temple de Bénarès, sous le dôme qui marque le centre du monde, repose un socle de cuivre équipé de trois aiguilles verticales en diamant de 50 cm de haut diamant de 50 cm de haut. A la création, Dieu enfila 64 plateaux en or pur sur une des aiguilles, le plus grand en bas et les autres de plus en plus petits C'est la tour de plus grand en bas et les autres de plus en plus petits. C est la tour de Brahmâ. Les moines doivent continûment déplacer les disques de manière que Les moines doivent continûment déplacer les disques de manière que ceux-ci se retrouvent dans la même configuration sur une autre aiguille. La règle de Brahmâ est simple: un seul disque à la fois et jamais un La règ e de Brahmâ est s mp e un seu d sque à a fo s et jama s un grand plateau sur un plus petit. Arrivé à ce résultat, le monde tombera en poussière et disparaîtra.

3

, p p

slide-4
SLIDE 4

The towers of Hanoi

4

slide-5
SLIDE 5

How many moves?

Assume n disks (n ≥ 0); three needles source, target, other The largest disk can only move from source to target if g y g it’s empty; all the other disks must be on other. So the minimal number of moves for any solution is: So the minimal number of moves for any solution is:

Move n−1 from source to other Move largest from source to target source to target

Hn = + 1 + = 2 ∗ + 1 Hn −1 Hn −1 H

1

= 2 ∗ + 1

Move n−1 from

  • ther to target

Since H1 = 1, this implies: Hn −1

5

Since H1 1, this implies Hn = 2n − 1

slide-6
SLIDE 6

This reasoning gives us an algorithm!

hanoi (n : INTEGER ; source, target, other : CHARACTER)

  • - Transfer n disks from source to target,
  • - using other as intermediate storage
  • - using other as intermediate storage.

require non negative: n >= 0 non_negative: n >= 0 different1: source /= target different2: target /= other diff 3 / h different3: source /= other do if n > 0 then Recursive calls hanoi (n − 1, source, other, target ) move (source, target ) hanoi (n − 1, source, other, target) hanoi (n − 1, other, target, source ) end end hanoi (n − 1, other, target, source)

6

end

slide-7
SLIDE 7

The tower of Hanoi

7

slide-8
SLIDE 8

A possible implementation for move

move (source, target : CHARACTER) P ib f t t t

  • - Prescribe move from source to target.

require different: source /= target different: source /= target do io put character (source) io.put_character (source) io.put_string (“ to “) io.put character (target ) io.put_character (target ) io.put_new_line end

8

slide-9
SLIDE 9

An example

Executing the call hanoi (4, ’A’, ’B’, ’C’) ill i h f fif (24 1) i i will print out the sequence of fifteen (24 -1) instructions A t C B t C B t A A to C B to C B to A A to B A to C C to B C to B A to B A to C C to B A to B A to C A to C C to B A to B B to A C to A C to B B to A C to A C to B

9

slide-10
SLIDE 10

The general notion of recursion A definition for a concept is recursive if it i l s i st f th t its lf if it involves an instance of the concept itself

The definition may use more than one “instance of the

c ncept itself ” concept itself ”

Recursion is the use of a recursive definition

10

slide-11
SLIDE 11

11

slide-12
SLIDE 12

Examples

Recursive routine Recursive grammar Recursively defined programming concept Recursive data structure Recursive proof

12

slide-13
SLIDE 13

Recursive routine

Direct recursion: body includes a call to the routine itself Example: routine hanoi for the preceding solution of the Towers of Hanoi problem

13

slide-14
SLIDE 14

Recursion, direct and indirect

Routine r calls itself

r

r calls s, and s calls r

r s

r calls r calls calls r calls r

rn

r1 calls r2 calls ... calls rn calls r1

rn r1 r2 rn-1

14

slide-15
SLIDE 15

Recursive grammar

Instruction ::= Assignment | Conditional | Compound | ... Conditional Conditional ::= if Expression then Instruction Instruction Conditional ::= if Expression then Instruction else Instruction end Instruction Instruction

15

slide-16
SLIDE 16

Defining lexicographical order

Problem: define the notion that word w1 is “before” word

w2, according to alphabetical order. Conventions: A word is a sequence of zero or more letters. A letter is one of: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z F t l tt it i k hi h i “ ll ” For any two letters it is known which one is “smaller” than the other; the order is that of the preceding list.

16

slide-17
SLIDE 17

Examples

ABC before DEF AB before DEF empty word before ABC A before AB A before ABC

17

slide-18
SLIDE 18

A recursive definition

The word x is “before” the word y if and only if one of the following conditions holds:

x is empty and y is not empty x is empty and y is not empty

Neither x nor y is empty, and the first letter of x Neither x nor y is empty, and the first letter of x

is smaller than the first letter of y

Neither x nor y is empty and:

  • Their first letters are the same
  • The word obtained by removing the first letter
  • The word obtained by removing the first letter
  • f x is before

the word obtained by removing the first letter of y before

18

th f rst tt r of y

slide-19
SLIDE 19

Recursive data structure

A binary tree over a type G is either:

E

t

Empty A node, consisting of three disjoint parts:

A l f t G th t

  • A value of type G the root
  • A binary tree over G, the left subtree

binary tree

19

  • A binary tree
  • ver G, the right subtree

binary tree

slide-20
SLIDE 20

Nodes and trees: a recursive proof

Theorem: to any node of any binary tree, we may associate a binary tree, so that the correspondence is one-to-one Proof:

If tree is empty, trivially holds If non-empty:

  • To root node, associate full tree.
  • Any other node n is in either the left or right

subtree; if B is that subtree, associate with n the node associated with n in B associated with

Consequence: we may talk of the left and right subtrees

  • f a node

20

slide-21
SLIDE 21

Binary tree class skeleton

class BINARY_TREE [G ] feature item : G left : BINARY_TREE [G ] right : BINARY TREE [G ] BINARY_TREE BINARY TREE right : BINARY_TREE [G ] BINARY_TREE ... Insertion and deletion commands ... end

21

slide-22
SLIDE 22

A recursive routine on a recursive data structure

count : INTEGER

  • - Number of nodes

do Result := 1 if left /= Void then Result := Result + left.count left.count Result Result left.count end if right /= Void then left.count if right /= Void then Result := Result + right.count end right.count end end

22

slide-23
SLIDE 23

Children and parents

Theorem: Single Parent Every node in a binary tree has exactly one parent, except for the root which has no parent.

23

slide-24
SLIDE 24

More binary tree properties and terminology

A node of a binary tree may have: Both a left child and a right child Only a left child Only a right child y g No child

24

slide-25
SLIDE 25

More properties and terminology

Upward path:

Sequence of zero or more

nodes, where any node in the sequence is the parent of the q p previous one if any. Th : R t P th Theorem: Root Path

From any node of a binary tree, there is a

single upward path to the root. g p p Theorem: Downward Path F d f b h l

For any node of a binary tree, there is a single

downward path connecting the root to the node through successive applications of left

25

g pp and right links.

slide-26
SLIDE 26

Height of a binary tree Maximum numbers of nodes on a downward path from the root to a leaf from the root to a leaf

height : INTEGER Maximum number of nodes

  • - Maximum number of nodes
  • - on a downward path

local local lh, rh : INTEGER do do if left /= Void then lh := left.height height end if right /= Void then rh := right height end left.height right height if right /= Void then rh := right.height end Result := 1 + lh.max (rh) end right.height

26

end

slide-27
SLIDE 27

Binary tree operations

add_left (x : G )

  • - Create left child of value x.

require require no_left_child_behind: left = Void do create left make (x) create left.make (x) end add right (x : G ) Same model add_right (x : G ) ...Same model... make (x : G ) ( )

  • - Initialize with item value x.

do item := x item := x ensure set: item = x end

27

end

slide-28
SLIDE 28

Binary tree traversals

print_all

  • - Print all node values

Print all node values. do if left /= Void then left.print_all end print (item) left.print_all print (item) if right /= Void then right.print_all end right.print_all end

28

slide-29
SLIDE 29

Binary tree traversals

Inorder: traverse left subtree

traverse left subtree

visit root traverse right subtree

traverse right subtree

Preorder: visit root

traverse left traverse right

Postorder:

traverse right traverse left traverse right

29

visit root

traverse right

slide-30
SLIDE 30

Binary search tree

A binary tree over a sorted set G is a binary search tree if for is a binary search tree if for every node n :

For every node x of the left

subtree of n : x.item ≤ n.item

For every node x of the right

subtree of n : x.item ≥ n.item

30

slide-31
SLIDE 31

Printing elements in order

class BINARY_SEARCH_TREE [G ...] feature

item : G left right : BINARY SEARCH TREE BINARY SEARCH TREE [G ] left, right : BINARY_SEARCH_TREE

print_sorted

BINARY_SEARCH_TREE [G ]

  • - Print element values in order

do if left /= Void then left print sorted end left.print sorted if left / Void then left.print_sorted end print (item) left.print_sorted if right /= Void then right.print_sorted end end right.print_sorted end

end

31

slide-32
SLIDE 32

Searching in a binary search tree

class BINARY_SEARCH_TREE [G ...] feature item : G left, right : BINARY_SEARCH_TREE [G] has (x : G ): BOOLEAN BINARY_SEARCH_TREE [G ] ( )

  • - Does x appear in any node?

require argument_exists: x /= Void g do if x = item then Result := True Result : True elseif x < item and left /= Void then Result := left.has (x)

left.has (x )

elseif x > item and right /= Void then Result := right.has (x) end d

right.has (x )

32

end end

slide-33
SLIDE 33

Insertion into a binary search tree

Do it as an exercise!

33

slide-34
SLIDE 34

Why binary search trees?

Linear structures: insertion, search and deletion are O (n) Binary search tree: average behavior for insertion, deletion and search is O (log (n)) But: worst-time behavior is O (n)!

Improvement: Red-Black Trees

Note measures of complexity: best case, average, worst case.

34

slide-35
SLIDE 35

Well-formed recursive definition

A f l i d fi iti h ld th t A useful recursive definition should ensure that: R1 Th i t l t i b h

R1

There is at least one non-recursive branch

R2

Every recursive branch occurs in a context that differs from the original that differs from the original

R3

For every recursive branch, the change of context (R2) brings it closer to at least one context (R2) brings it closer to at least one

  • f the non-recursive cases (R1)

35

slide-36
SLIDE 36

“Hanoi” is well-formed

hanoi (n : INTEGER ; source, target, other : CHARACTER)

  • - Transfer n disks from source to target,
  • - using other as intermediate storage
  • - using other as intermediate storage.

require non negative: n >= 0 non_negative: n >= 0 different1: source /= target different2: target /= other diff 3 / h different3: source /= other do if n > 0 then hanoi (n − 1, source, other, target ) move (source, target ) hanoi (n − 1, source, other, target) hanoi (n − 1, other, target, source) end end hanoi (n − 1, other, target, source)

36

end

slide-37
SLIDE 37

What we have seen so far

A definition is recursive if it takes advantage of the notion itself, on a smaller target What can be recursive: a routine, the definition of a concept… Still some mystery left: isn’t there a danger of a cyclic d fi iti ? definition?

37

slide-38
SLIDE 38

Recursion variant

Every recursive routine should use a recursion variant, an i t tit i t d ith ll h th t

The variant is always >= 0 (from precondition)

integer quantity associated with any call, such that:

The variant is always

0 (from precondition)

If a routine execution starts with variant value v,

the value v‘ for any recursive call satisfies y 0 ≤ v‘ < v

38

slide-39
SLIDE 39

Hanoi: what is the variant?

hanoi (n : INTEGER ; source, target, other : CHARACTER)

  • - Transfer n disks from source to target,

i th i t di t t

  • - using other as intermediate storage.

require q … do if n > 0 then if n > 0 then hanoi (n − 1, source, other, target ) hanoi (n − 1, source, other, target) move (source, target ) hanoi (n − 1, other, target, source ) hanoi (n − 1, other, target, source) end end

39

slide-40
SLIDE 40

Printing: what is the variant?

class BINARY_SEARCH_TREE [G ...] feature

item : G left right : BINARY SEARCH TREE BINARY SEARCH TREE [G ] left, right : BINARY_SEARCH_TREE

print_sorted

BINARY_SEARCH_TREE [G ]

  • - Print element values in order

do if left /= Void then left print sorted end left.print sorted if left / Void then left.print_sorted end print (item) left.print_sorted if right /= Void then right.print_sorted end end right.print_sorted end

end

40

slide-41
SLIDE 41

Contracts for recursive routines

hanoi (n : INTEGER; source, target, other : CHARACTER)

  • - Transfer n disks from source to target,

usin

  • ther as intermediate stora e
  • - using other as intermediate storage.
  • - variant: n
  • - invariant: disks on each needle are piled in

decreasing size

  • - decreasing size

require … do if n > 0 then hanoi (n 1 source other target ) h i ( 1 th t t) hanoi (n − 1, source, other, target ) move (source, target ) hanoi (n − 1, source, other, target) hanoi (n − 1, other, target, source ) end end hanoi (n − 1, other, target, source)

41

end

slide-42
SLIDE 42

McCarthy’s 91 function

M (n) =

n – 10

if n > 100

  • M ( M (n + 11))

if n ≤ 100 M M

42

slide-43
SLIDE 43

Another function

bizarre (n) =

1

if n = 1

bizarre

(n / 2) if n is even bizarre (n / 2 )

bizarre

((3 ∗ n + 1) / 2) if n > 1 and n is odd bizarre ((3 ∗ n + 1) / 2)

43

slide-44
SLIDE 44

Fibonacci numbers fib (1) = 0 fib (2) = 1 fib (2) = 1 fib ( ) fib ( 2) fib ( 1) f 2 fib (n) = fib (n − 2) + fib (n − 1) for n > 2

44

slide-45
SLIDE 45

Factorial function

0 ! = 1 n ! = n ∗ (n − 1) ! for n > 0 Recursive definition is interesting for demonstration purposes only; practical implementation will use loop (or p p y p p p table)

45

slide-46
SLIDE 46

Our original example of a loop

highest_name : STRING

  • - Alphabetically greatest station name of line f

p y g do from f st t ; R sult : "" f.start ; Result := "" until f.after loop Result := greater (Result, f.item.name) f.forth end end

item after before

end

item count 1

46

count 1 forth start

slide-47
SLIDE 47

A recursive equivalent

highest_name : STRING

  • - Alphabetically greatest station name

p y g

  • - of line f

require q not f.is_empty do do f.start Result := f highest from cursor f hi h st f m s Result := f.highest_from_cursor end f.highest_from_cursor

47

slide-48
SLIDE 48

Auxiliary function for recursion

highest_from_cursor : STRING

  • - Alphabetically greatest name of stations of

li f t ti t t iti

  • - line f starting at current cursor position

require f /= Void; not f off f /= Void; not f.off do Result := f.item.name f.forth if not f.after then f n t f.aft r th n Result := greater (Result, highest_from_cursor ) end f b k

item after

highest_from_cursor f.back end

item count 1

48

forth

slide-49
SLIDE 49

Loop version using arguments

maximum (a : ARRAY [STRING]): STRING

  • - Alphabetically greatest item in a

require q a.count >= 1 local i : INTEGER do from i := a.lower + 1; Result := a.item (a.lower) ( ) invariant i > a.lower ; i <= a.upper + 1

  • - Result is the maximum element of a [a.lower .. i − 1]

f [ . ] until i > a.upper loop if a.item (i ) > Result then Result := a.item (i ) end i := i + 1 end

49

end

slide-50
SLIDE 50

Recursive version

maxrec (a : ARRAY [STRING]): STRING Alphabetically greatest item in a

  • - Alphabetically greatest item in a

require a.count >= 1 do Result := max_sub_array (a, a.lower) end max_sub_array (a : ARRAY [STRING]; i : INTEGER): STRING

  • - Alphabetically greatest item in a starting from index i

require require i >= a.lower; i <= a.upper do Result : item (i ) Result := a.item (i ) if i < a.upper then Result := greater (Result, max_sub_array (a, i + 1)) end

50

end end

slide-51
SLIDE 51

Recursion elimination

Recursive calls cause (in a default implementation without

  • ptimization) a run-time penalty: need to maintain stack of

d l preserved values Various optimizations are possible Sometimes a recursive scheme can be replaced by a loop; this is known as recursion elimination “Tail recursion” (last instruction of routine is recursive ll) s ll b li i t d call) can usually be eliminated

51

slide-52
SLIDE 52

Recursion elimination

r (n ) do ... Some instructions ... n := n’ goto start_of_r r (n’ ) ( 1) ... More instructions ...

  • - e.g. r (n – 1)

end May need n ! After call, need to revert to previous values of arguments and other context information

52

and other context information

slide-53
SLIDE 53

Using a stack

Queries:

Is the stack empty? is_empty Top element, if any: item

“Top” position

Commands:

Push an element on top: put Pop top element, if any: remove

p p , y Before a call: push on stack a “frame” containing values of l c l v ri bl s r um nts nd r turn inf rm ti n local variables, arguments, and return information After a call: pop frame from stack, restore values (or terminate if stack is empty)

53

terminate if stack is empty)

slide-54
SLIDE 54

Recursion elimination

r (n ) do start: ... Some instructions ... Push frame n := n’ t t t r (n’ ) goto start after: ... More instructions ... if stack not empty then Pop frame end Pop frame goto after end

54

slide-55
SLIDE 55

Minimizing stack use

r (n ) do Push frame n := n’ goto start start: ... Some instructions ... ( ’ ) goto start r (n 1 ) r (n’ ) after: ... More instructions ...

  • - e.g. r (n – 1 )

after ... More instructions ... if stack not empty then P f end Pop frame goto after end

No need to store or retrieve from the stack for simple

55

transformations, e.g. n := n – 1 , inverse is n := n + 1

slide-56
SLIDE 56

Recursion as a problem-solving technique

Applicable if you have a way to construct a solution pp y y to the problem, for a certain input set, from solutions for one or more smaller input sets p

56

slide-57
SLIDE 57

What we have seen

The notion of recursive definition Lots of recursive routines Lots of recursive routines Recursive data structures Recursive proofs Recursive proofs The anatomy of a recursive algorithm: the

Tower of Hanoi Tower of Hanoi

What makes a recursive definition “well-

behaved” behaved

Binary trees

Bi h

Binary search trees Applications of recursion

57

Basics of recursion implementation