Inductive Definitions and Algorithms in Discrete Mathematical Structures, Slides of Discrete Mathematics

Examples of inductive definitions, such as the set of multiples of 3 and well-formed formulas, and the inductive definition of the length and reversal of strings. It also includes algorithms, such as max and bubble, and their complexity analysis. The document also discusses the binary search algorithm and its running time.

Typology: Slides

2012/2013

Uploaded on 04/27/2013

aslesha
aslesha 🇮🇳

4.4

(14)

160 documents

1 / 30

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS 173:
Discrete Mathematical Structures
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e

Partial preview of the text

Download Inductive Definitions and Algorithms in Discrete Mathematical Structures and more Slides Discrete Mathematics in PDF only on Docsity!

CS 173:

Discrete Mathematical Structures

Inductive Definitions

Our examples so far have been

inductively defined functions.

Sets can be defined inductively, too.

Recursive Cases

Base Case

Give an inductive definition of S = {x: x is a multiple of 3}

  1. 3 ∈ S
  2. x,y ∈ S → x + y ∈ S
  3. x,y ∈ S → x - y ∈ S
  4. No other numbers are in S.

“I LOVE my definition. It’s perfect!” “Oh yeah? Prove it!” Docsity.com

Inductive Definitions

We start with T ⊆ S.

If x ∈ T, then x = 3k for some

integer k. We show by induction on

|k| that 3k ∈ S.

Base Case (k = 0): 0 ∈ S since 3 ∈ S

by rule 1, and 3 - 3 ∈ S by rule 3.

Assume 3k, -3k ∈ S, show that 3(k+1), -3(k+1) ∈ S.

Inductive Definitions

We start with T ⊆ S.

If x ∈ T, then x = 3k for some

integer k. We show by induction on

|k| that 3k ∈ S.

Assume 3k, -3k ∈ S, show that 3(k+1), -3(k+1) ∈ S.

3k ∈ S by inductive hypothesis. 3 ∈ S by rule 1. 3k + 3 = 3(k+1) ∈ S by rule 2.

0 ∈ S by base case. 0 - 3(k+1) = -3(k+1) ∈ S by rule 3.

Inductive Definitions

Next we show that S ⊆ T.

That is, if an number x is described

by S, then it is a multiple of 3.

Base Case (k=1): If x ∈ S by 1 rule application, then it must be rule 1 and x = 3, which is clearly a multiple of 3.

Inductive Definitions

Next we show that S ⊆ T.

That is, if an number x is described

by S, then it is a multiple of 3.

Assume any number described by k or fewer applications of the rules in S is a multiple of 3 and prove that any number described by (k+1) applications of the rules is also a multiple of 3.

Suppose the (k+1)st rule applied is rule 3, and it results in value x = a + b. Then a and b are multiples of 3 by inductive hypothesis, and thus x is a multiple of 3. (^) The argument is the same if rule 2 is last instead.

Strings and Inductive Definitions

Let Σ be a finite set called an

alphabet.

The set of strings on Σ, denoted Σ* is

defined as:

  • λ ∈ Σ*, where λ denotes the null or

empty string.

  • If x ∈ Σ, and w ∈ Σ, then wx ∈ Σ,

where wx is the concatenation of

string w with symbol x.

Countably infinite

Example: Let Σ = {a, b, c}. Then Σ* = {λ, a, b, c, aa, ab, ac, ba, bb, bc, ca, cb, cc, aaa, aab,…}

How big is Σ*?

Are there any infinite strings in Σ*? No.

Is there a largest string in Σ*? No.

Strings and Inductive Definitions

Inductive definition of the length of

strings (the length of string w is

|w|.):

• If x ∈ Σ, and w ∈ Σ*, then |wx| =

|w| + 1

I point this out because the length of strings is something we might like to use for an inductive argument.

Strings and Inductive Definitions

A Theorem: ∀x,y ∈ Σ*

(xy)R^ = y R^ x R

FruitLoopsR LoopsR^ Fruit R spooLtiurF

This is an example of how an inductive defn can be used in a proof. The thm itself isn’t compelling. Proof (by induction on |y|):

Base Case (|y| = 0): If |y| = 0, y = λ, then (xy)R^ = (xλ)R^ = xR^ = λxR^ = yR^ xR^.

IH: If |y| ≤ n, then ∀x ∈ Σ*, (xy)R^ = yR^ xR^.

Prove: If |y| = n+1, then ∀x ∈ Σ*, (xy)R^ = yR^ xR^.

Strings and Inductive Definitions

IH: If |y| ≤ n, then ∀x ∈ Σ*, (xy)R^ =

yR^ x R^.

Prove: If |y| = n+1, then ∀x ∈ Σ*,

(xy) R^ = y R^ x R^.

If |y| = n+1, then ∃a ∈ Σ, u ∈ Σ*, so that y = ua, and |u| = n. Then, (xy)R^ = (x(ua))R^ by substitution = ((xu)a)R^ by assoc. of concatenation = a(xu)R^ by inductive defn of reversal = auR^ xR^ by IH = (ua)R^ xR^ by inductive defn of reversal = yR^ xR^ by substitution

Algorithms

No one knows if this algorithm halts

on all inputs:

Algorithm weird Input: x integer Output: y integer

  1. y=
  2. if x = 1, output y and halt.
  3. elseif x is even
  4. x = x/2; y++;
  5. elseif x is odd
  6. x = 3x + 1; y++;
  7. goto 2

5 → 16,8,4,2, 3 → 10,5,16,8,4,2, 6 → 3,10,5,16,8,4,2,

7 → 22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,

It halts on input x ≤ billions

Algorithms

An iterative algorithm is one that repeats the same sequence of steps a number of times.

for loops while loops repeat loops goto??

The running time of an iterative algorithm depends on the number of times the loop is invoked.

Algorithms

How many times does “twiddle-thumbs”

happen?

1. for i = 1 to n

2. for j = 1 to m

3. twiddle-thumbs

The “time complexity” of an algorithm

is a measure of its running time.

But different machines run at different speeds!

So we give running times in terms of big-oh, since different machines affect run times by constant factors.

Complexity is O(mn)

Algorithms

Algorithm MAX

Input: x 1 , x 2 , …, x n, an array of numbers

Output: y, the maximum of x 1 , x 2 , …, x n

1. for j = 1 to n-

2. if x j > x j+1 then

3. temp = x j+

4. x j+1 = xj

5. xj = temp

Complexity is O(n)

vars x 1 x 2 x 3 x (^4) input 3 2 4 1 j = 1 3  2  4 1 j = 2 2 3  4  1 j = 3 2 3 4  1  final 2 3 1 4