Click presentation screen icon to the right to see the slide show.
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 ?
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;
Is there another way?
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;
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…..
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?
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?
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?
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.
What would the grammar be?
What would the grammar be?
How about all letters?
Is ?
Would like to do soemthing like
and but not standard grammar notation.
Write a recognition algorithm.
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;
What is a grammar for this?
Think about the palindromes.
What is a grammar for this?
Think about the palindromes. Recurse in the middle.
What is a grammar for this?
Think about the palindromes. Recurse in the middle.
And now the recognition algorithm.
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;
What about the language
What about the language
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;
What about the language
Check it. Can you think of any strings that this grammar can generate that are not in ?
What about the language
Must make choice at beginning of generation.
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;
Infix to Postfix:
Infix to Postfix:
Grammar:
Grammar:
Do number 3. Write a grammar for numbers in scientific notation, like 1.5e-4 and -6.21e20.
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>
Do number 3. Write a grammar for numbers in scientific notation, like 1.5e-4 and -6.21e20.
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.
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
Definition:
Property :
Basis:
:
By definition, .
Base case proved!
Definition:
Property :
Inductive Hypothesis:
is true, or
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.
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.
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)
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?
Oh yeah? Prove it.
Basis
Is true? Yes, because .
Inductive Hypothesis
Assume
Inductive Step
Prove assuming .
Inductive Step
Prove assuming .
which is true because this is how we defined .
Another way is to start with the recurrence relation:
Inductive Step
Prove assuming .
which is the property we want to prove.