20
software design & management Gachon University Chulyun Kim

Gachon University Chulyun Kim - KOCWcontents.kocw.net/KOCW/document/2014/gacheon/KIMChulyun/... · 2016. 9. 9. · Recursive Definitions and Structural Induction ... Mathematical

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Gachon University Chulyun Kim - KOCWcontents.kocw.net/KOCW/document/2014/gacheon/KIMChulyun/... · 2016. 9. 9. · Recursive Definitions and Structural Induction ... Mathematical

software design & management

Gachon University

Chulyun Kim

Page 2: Gachon University Chulyun Kim - KOCWcontents.kocw.net/KOCW/document/2014/gacheon/KIMChulyun/... · 2016. 9. 9. · Recursive Definitions and Structural Induction ... Mathematical

software design & management

Outline Mathematical Induction

Recursive Definitions and Structural Induction

Recursive Algorithms

Program Correctness

2

Page 3: Gachon University Chulyun Kim - KOCWcontents.kocw.net/KOCW/document/2014/gacheon/KIMChulyun/... · 2016. 9. 9. · Recursive Definitions and Structural Induction ... Mathematical

software design & management

Mathematical Induction

3

Principle

To prove that P(n) is true for all positive integers n

Basis step: verify P(1) is true

Inductive step: show P(k)P(k+1) is true for every integer k

§4

.1 Math

ematical In

du

ction

Page 4: Gachon University Chulyun Kim - KOCWcontents.kocw.net/KOCW/document/2014/gacheon/KIMChulyun/... · 2016. 9. 9. · Recursive Definitions and Structural Induction ... Mathematical

software design & management

Example 1 Show that if n is a positive integer, then

1+2+…+n=n(n+1)/2

Basis step: P(1) is true, because 1 = 1(1+1)/2

Inductive step:

Assume that 1+2+…+k = k(k+1)/2 is true

Now we show 1+2+…+k+1 = (k+1)(k+2)/2 is true under the assumption

4

1+2+…+k+k+1 = k(k+1)/2 + k+1 = k(k+1)/2 + 2(k+1)/2 = (k+1)(k+2)/2

Page 5: Gachon University Chulyun Kim - KOCWcontents.kocw.net/KOCW/document/2014/gacheon/KIMChulyun/... · 2016. 9. 9. · Recursive Definitions and Structural Induction ... Mathematical

software design & management

Example 2 Use mathematical induction to show that

1+2+22+…+2n=2n+1-1

5

Page 6: Gachon University Chulyun Kim - KOCWcontents.kocw.net/KOCW/document/2014/gacheon/KIMChulyun/... · 2016. 9. 9. · Recursive Definitions and Structural Induction ... Mathematical

software design & management

Example 3 Use mathematical induction to prove the inequality n<2n

Basis step: P(1) is true, because 1 < 21

Inductive step:

Assume that P(k) is true for an integer k: k<2k

Show that P(k+1) is true under the assumption

6

k < 2k

k+1 < 2k+1 k+1 < 2k+2k k+1 < 2∙2k=2k+1

Page 7: Gachon University Chulyun Kim - KOCWcontents.kocw.net/KOCW/document/2014/gacheon/KIMChulyun/... · 2016. 9. 9. · Recursive Definitions and Structural Induction ... Mathematical

software design & management

Example 4 Use mathematical induction to show n2+3n+1 ≤ 5n2 for

all positive integers

7

Page 8: Gachon University Chulyun Kim - KOCWcontents.kocw.net/KOCW/document/2014/gacheon/KIMChulyun/... · 2016. 9. 9. · Recursive Definitions and Structural Induction ... Mathematical

software design & management

Recursively Defined Functions Recursive or inductive definition

Basis step: Specify the value of the function at zero

Recursive step: Give a rule for finding its value at an integer from its values at smaller integers

8

§4

.3 Recu

rsive Defin

ition

s and

Stru

ctural In

du

ction

Page 9: Gachon University Chulyun Kim - KOCWcontents.kocw.net/KOCW/document/2014/gacheon/KIMChulyun/... · 2016. 9. 9. · Recursive Definitions and Structural Induction ... Mathematical

software design & management

Example 1 Basis step: f(0)=3

Recursive step: f(n+1) = 2f(n) + 3

f(1) = 2f(0)+3 = 9

f(2) = 2f(1) + 3 = 21

9

Page 10: Gachon University Chulyun Kim - KOCWcontents.kocw.net/KOCW/document/2014/gacheon/KIMChulyun/... · 2016. 9. 9. · Recursive Definitions and Structural Induction ... Mathematical

software design & management

Example 2 Give an inductive definition of the factorial function

F(n)=n!

10

Page 11: Gachon University Chulyun Kim - KOCWcontents.kocw.net/KOCW/document/2014/gacheon/KIMChulyun/... · 2016. 9. 9. · Recursive Definitions and Structural Induction ... Mathematical

software design & management

Fibonacci Numbers f0 = 0, f1 = 1

fn = fn-1 + fn-2 for n = 2, 3, 4, …

f2 = f1 + f0 = 1 + 0 = 1

f3 = f2 + f1 = 1 + 1 = 2

11

Page 12: Gachon University Chulyun Kim - KOCWcontents.kocw.net/KOCW/document/2014/gacheon/KIMChulyun/... · 2016. 9. 9. · Recursive Definitions and Structural Induction ... Mathematical

software design & management

Example 3 Give a recursive definition of L(w), the length of the

string w

Basis step: L(λ)=0 where λ is the empty string

Recursive step: L(wx)=L(w)+1 if w*and x

12

Page 13: Gachon University Chulyun Kim - KOCWcontents.kocw.net/KOCW/document/2014/gacheon/KIMChulyun/... · 2016. 9. 9. · Recursive Definitions and Structural Induction ... Mathematical

software design & management

Example 4 Give a recursive definition of H(n), the number of

Hanoi tower

13

Page 14: Gachon University Chulyun Kim - KOCWcontents.kocw.net/KOCW/document/2014/gacheon/KIMChulyun/... · 2016. 9. 9. · Recursive Definitions and Structural Induction ... Mathematical

software design & management

Recursive Algorithms An algorithm is called recursive if it solves a problem

by reducing it to an instance of the same problem with smaller input

14

§4

.4 R

ecursive A

lgorith

ms

Page 15: Gachon University Chulyun Kim - KOCWcontents.kocw.net/KOCW/document/2014/gacheon/KIMChulyun/... · 2016. 9. 9. · Recursive Definitions and Structural Induction ... Mathematical

software design & management

Example 1

n!

15

procedure factorial(n: nonnegative integer) if n=0 then factorial(n) := 1 else factorial(n) := n∙factorial(n-1)

Page 16: Gachon University Chulyun Kim - KOCWcontents.kocw.net/KOCW/document/2014/gacheon/KIMChulyun/... · 2016. 9. 9. · Recursive Definitions and Structural Induction ... Mathematical

software design & management

Example 2

an

16

Page 17: Gachon University Chulyun Kim - KOCWcontents.kocw.net/KOCW/document/2014/gacheon/KIMChulyun/... · 2016. 9. 9. · Recursive Definitions and Structural Induction ... Mathematical

software design & management

Example 3

Linear search

17

Procedure search(i, j, x: i, j, x integers 1≤i≤n, 1≤j≤n) if ai=x then location := i else if i = j then location := 0 else search(i+1, j, x)

Page 18: Gachon University Chulyun Kim - KOCWcontents.kocw.net/KOCW/document/2014/gacheon/KIMChulyun/... · 2016. 9. 9. · Recursive Definitions and Structural Induction ... Mathematical

software design & management

Example 4

Binary search

18

Page 19: Gachon University Chulyun Kim - KOCWcontents.kocw.net/KOCW/document/2014/gacheon/KIMChulyun/... · 2016. 9. 9. · Recursive Definitions and Structural Induction ... Mathematical

software design & management

Merge Sort

19

Page 20: Gachon University Chulyun Kim - KOCWcontents.kocw.net/KOCW/document/2014/gacheon/KIMChulyun/... · 2016. 9. 9. · Recursive Definitions and Structural Induction ... Mathematical

software design & management

Algorithm

20

Procedure mergesort(L=a1, …, an) if n>1 then m := n/2 L1 := a1, a2, … , am

L2 := am+1, am+2, … , an L := merge(msergesort(L1), mergesort(L2))

Procedure merge(L1, L2) L := empty list while L1 and L2 are both nonempty begin remove smaller of first element of L1 and L2 put it at the right end of L if removal of this element makes one list empty then remove all elements form the other list append them to L end