View page as slide show

Click presentation screen icon to the right to see the slide show.

Chapter 6

  • Formal languages
  • Mathematical Induction

Formal Languages

A language is a set of strings

Is the string in this language? Is ?

How would you write an algorithm to decide if string of characters, , is in ?

Formal Languages

A language is a set of strings

Is the string in this language? Is ?

How would you write an algorithm to decide if string of characters, , is in ?

if (w.equals("a"))
    return true;
else if (w.equals("b"))
    return true;
else if (w.equals("aa"))
    return true;
 . . .
else
    return false;

Formal Languages

Is there another way?

Formal Languages

Is there another way?

if (w[0] is 'a' or 'b')
    if (w.length() == 1)
       return true;
    if (w[1] is 'a' or 'b') 
        if (w.length() == 2)
	    return true;
} 
return false;

Formal Languages

Is there another way?

if (w[0] is 'a' or 'b')
    if (w.length() == 1)
       return true;
    if (w[1] is 'a' or 'b') 
        if (w.length() == 2)
	    return true;
} 
return false;

Now, what if

More nested if's, or…..

Formal Languages

More nested if's, or….. Yes! Recursion!

isInL(w):
    if (w[0] is 'a' or 'b')
        if (w.length() == 1)
            return true;
        if (w[1] is 'a' or 'b') 
            if (w.length() == 2)
              return true;
} 
return false;
isInL(w):
    if (w[0] is 'a' or 'b')
        return isInL(w.substring(1));
} 
return false;

What is missing?

Formal Languages

isInL(w):
    if (w.length() == 0)
        return true;
    if (w[0] is 'a' or 'b')
        return isInL(w.substring(1));
} 
return false;

But only if . is the empty string.

What else is in now that we didn't write?

Formal Languages

isInL(w):
    if (w.length() == 0)
        return true;
    if (w[0] is 'a' or 'b')
        return isInL(w.substring(1));
} 
return false;

But only if . is the empty string.

What else is in now that we didn't write?

Grammars

So, has infinite number of strings.

Specify with a grammar.

Looks very much like the recursive procedure.

Often start with partial enumeration of , then define the grammar, then write the recognition code.

Palindromes

What would the grammar be?

Palindromes

What would the grammar be?

How about all letters?

Palindromes

Is ?

Palindromes

Would like to do soemthing like

and but not standard grammar notation.

Palindromes

Write a recognition algorithm.

Palindromes

Write a recognition algorithm.

pal(w):
    if (w.length() == 0)
        return true;
    if (w[0] == w[last])
        return pal(w.substring(1,last-1));
    return false;

Counting

What is a grammar for this?

Think about the palindromes.

Counting

What is a grammar for this?

Think about the palindromes. Recurse in the middle.

Counting

What is a grammar for this?

Think about the palindromes. Recurse in the middle.

And now the recognition algorithm.

Counting

And now the recognition algorithm.

anbn(w):
    if (w.length() == 0)
        return true;
    if (w[0] == 'a' and w[last] == 'b') 
        return anbn(w.substring(1,last-1));
    return false;

Counting

What about the language

Counting

What about the language

Counting

What about the language

a2nbn(w):
    if (w.length() == 0)
        return true;
    if (w[0] == 'a' and w[1] == 'a' and w[last] == 'b') 
        return a2nbn(w.substring(2,last-1));
    return false;

Counting

What about the language

Check it. Can you think of any strings that this grammar can generate that are not in ?

Counting

What about the language

Must make choice at beginning of generation.

Counting

What about the language

Must make choice at beginning of generation.

a2nb2n(w):
    if (w.length() == 0)
        return true;
    if (w[0] == 'a' and w[1] == 'a' and w[last] == 'b') 
        return a2nbn(w.substring(2,last-1));
    if (w[0] == 'a' and w[last-1] == 'b' and w[last] == 'b')
        return anb2n(w.substring(1,last-2));
    return false;

Algebraic Expressions

Infix to Postfix:

  • Fully parenthesize
  • Move operator to corresponding right parenthesis

Algebraic Expressions

Infix to Postfix:

  • Fully parenthesize
  • Move operator to correponsing right parenthesis

Grammar:

Algebraic Expressions

Grammar:

Self-Test Exercises

Do number 3. Write a grammar for numbers in scientific notation, like 1.5e-4 and -6.21e20.

Self-Test Exercises

Do number 3. Write a grammar for numbers in scientific notation, like 1.5e-4 and -6.21e20.

<n> = <s> <d> . <ds> e <s> <ds>

Self-Test Exercises

Do number 3. Write a grammar for numbers in scientific notation, like 1.5e-4 and -6.21e20.

Mathematical Induction

Remember this? We will use it to prove things about recursive programs.

We want to prove some property is true for all .

Prove assuming is true, and assume is true.

Basis:

Prove or , or .

Inductive Hypothesis

is assumed true.

Inductive Step

Prove . If is true, then is true.

Mathematical Induction

Basis: Prove or , or .
Inductive Hypothesis: is assumed true.
Inductive Step: Prove . If is true, then is true.

Let's try using this to prove the property that the function is equal to

Mathematical Induction

Definition:

Property :

Basis:
:
By definition, .
Base case proved!

Mathematical Induction

Definition:

Property :

Inductive Hypothesis:
is true, or

Mathematical Induction

Definition:

Property :

Inductive Step:
Prove using the Inductive Hypothesis that is true. Using the Inductive Hypothesis, we get which is what we wanted to prove. So we are done.

Mathematical Induction

Here is the Inductive Step again. We started with the function's definition, and used the Inductive Hypothesis for to prove .

Inductive Step:
which is what we wanted to prove.

An alternative is to start with the statement of and use the Inductive Hypothesis to replace part of the equation and end up with the definition of the function.

Inductive Step:
which is exactly our definition of the factorial function, so it is true.

Towers of Hanoi Moves

How many moves does a solution for disks take?

Remember the solution?

solve(3,A,B,C) =  solve(2,A,C,B), solve(1,A,B,C), solve(2,C,B,A)

So

nMoves(3) = nMoves(2) + nMoves(1) + nMoves(2)
          = 2 nMoves(2) + nMoves(1)

and

nMoves(2) = nMoves(1) + nMoves(1) + nMoves(1)
          = 2 nMoves(1) + nMoves(1)

Towers of Hanoi Moves

How many moves does a solution for disks take?

In general,

nMoves(n) = nMoves(n-1) + nMoves(1) + nMoves(n-1)
          = 2 nMoves(n-1) + nMoves(1)

Can we come up with a simpler, nonrecursive way to calculate this?

Towers of Hanoi Moves

Oh yeah? Prove it.

Towers of Hanoi Moves

Basis

Is true? Yes, because .

Inductive Hypothesis

Assume

Inductive Step

Prove assuming .

Towers of Hanoi Moves

Inductive Step

Prove assuming .

which is true because this is how we defined .

Towers of Hanoi Moves

Another way is to start with the recurrence relation:

Inductive Step

Prove assuming .

which is the property we want to prove.

Recent changes RSS feed CC Attribution-Share Alike 3.0 Unported Driven by DokuWiki