CMSC 203 / 0201 Fall 2002 - PowerPoint PPT Presentation

1 / 14
About This Presentation
Title:

CMSC 203 / 0201 Fall 2002

Description:

Exercise 3.3.5: Give a recursive definition of the sequence {an}, n = 1, 2, 3, ... if ... Exercise 3.3.33: Use Exercise 29 [definition of wi] and mathematical ... – PowerPoint PPT presentation

Number of Views:18
Avg rating:3.0/5.0
Slides: 15
Provided by: Mariedes8
Category:
Tags: cmsc | definition | exercise | fall | of

less

Transcript and Presenter's Notes

Title: CMSC 203 / 0201 Fall 2002


1
CMSC 203 / 0201Fall 2002
  • Week 7 7/9/11 October 2002
  • Prof. Marie desJardins

2
TOPICS
  • Recursion
  • Recursive and iterative algorithms
  • Program correctness

3
MON 10/7RECURSION (3.3)
4
Concepts/Vocabulary
  • Recursive (a.k.a. inductive) function definitions
  • Recursively defined sets
  • Special sequences
  • Factorial F(0)1, F(n) F(n-1)?(n) n!
  • Fibonacci numbers f0 0, f1 1, fn fn-1
    fn-2
  • Strings
  • ? Strings over alphabet ?
  • Empty string ?
  • String length l(s)
  • String concatenation

5
Examples
  • S1 Even positive integers
  • 2 ? S1 x2 ? S1 if x ? S1
  • S2 Even integers
  • 2 ? S2 x2 ? S2 if x ? S2 x-2 ? S2 if x ? S2
  • Prove that S1 is the set of all even positive
    integers
  • Every even positive integer is in S1
  • Every member of S1 is an even positive integer

6
Examples II
  • Exercise 3.3.5 Give a recursive definition of
    the sequence an, n 1, 2, 3, if
  • (a) an 6n
  • (b) an 2n 1
  • (c) an 10n
  • (d) an 5
  • Exercise 3.3.31 When does a string belong to the
    set A of bit strings defined recursively by ? ?
    ? 0x1 ? A if x ? A,where ? is the empty string?

7
Examples III
  • Exercise 3.3.33 Use Exercise 29 definition of
    wi and mathematical induction to show that
    l(wi) i ? l(w),where w is a string and i is
    a nonnegative integer.
  • Exercise 3.3.34 Show that (wR)I (wi)R whenever
    w is a string and I is a nonnegative integer
    that is, show that the ith power of the reversal
    of a string is the reversal of the ith power of
    the string.

8
WED 10/9RECURSIVE ALGORITHMS (3.4)
  • HOMEWORK 4 DUE
  • UNGRADED QUIZ TODAY

9
Concepts / Vocabulary
  • Recursive algorithm
  • Iterative algorithm

10
Examples
  • Algorithm 7 Recursive Fibonacci
  • Complexity of f7 (Exercise 14), fn
  • Algorithm 8 Iterative Fibonacci
  • Complexity f7 (Exercise 14), fn
  • Eercise 3.4.23 Give a recursive algorithm for
    finding the reversal of a bit string.

11
FRI 10/11PROGRAM CORRECTNESS (3.5)
12
Concepts / Vocabulary
  • Initial assertion, final assertion
  • Correctness, partial correctness, termination
  • Partially correct with respect to initial
    assertion p and final assertion q
  • Rules of inference
  • Composition rule
  • Conditional rules
  • Loop invariants

13
Examples
  • Exercise 3.5.3 Verify that the program
    segment x 2 z x y if y gt 0 then z
    z 1 else z 0is correct with respect to
    the initial assertion y3 and the final assertion
    z6.

14
Examples II
  • Exercise 3.5.6 Use the rule of inference
    developed in Exercise 5 if else if else to
    verify that the program if x lt 0 then y
    -2x / x else if x gt 0 then y 2x /
    x else if x 0 then y 2is correct with
    respect to the initial assertion T and the final
    assertion y2.
Write a Comment
User Comments (0)
About PowerShow.com