Upload
colleen-phelps
View
212
Download
0
Embed Size (px)
Citation preview
Complexity, etc.
Homework. Comparison to computability.Big Oh notation. Sorting.
Classwork/Homework: prepare presentation on specific sorts.
Presentation Day for some.
Homework
• CFG and PDA
• Regular expressions
Hierarchy
• There is a "proper" hierarchy concerning languages FSA and PDA and TM.
meaning• If a language is recognized by an FSA, then we
can define a PDA. If a language is recognized by a PDA, we can define a TM.
AND• There are languages for which there is not FSA,
but there is a PDA and similarly for PDA and TM.
Trick question
• Define a FSA for the language aibj – some a's and then some b's.
• Define a FSA for the language aibi – some a's and then the same number of b's.
Pumping lemma for FSA
• If A is a regular language, then there is a number p where if s is in A, and the length of s is at least p, then we can write s ass = xyz, – length of y is greater than 0– length of xy is less than p and– xyiz is also in A.
• General idea: finite number of states. Set p to be number of states. So if there a string in A of length at least p, then the string s must re-visit at least 1 state. Call one such state q, then the substring from q and then back to q can be repeated.
Example of language that is not regular
• Alphabet {a,b}. All strings of the formaibi
– some number of a's followed by the same number of b's.
– NOTE: the language of strings aibj is regular.
Define a CFG for aibj
• Try it!
Now, what about aibi
• Write a grammar
Answer
• S => aSb
• S => ab
Notice: this grammar is not regular.
It better not be, because an application of the pumping lemma indicated that this language could not be produced by a FSA.
Many similar results
• Pumping lemma for CFL
• Extra credit assignment: look up and present to class.
Complexity
• calculate limit (maximum, minimum, worst case, average) amount of resources required to perform specific task– generally specific algorithm– time
• count of a specific type of operation
– space
• Actual usage in any specific case may be different.
Comparison
• Computability looks at whether or not task can be done using a certain type of machine or grammar or definition of functions
• Complexity assumes that something can be done and complexity assessment generally looks at specific method– Note: there are proofs that say (something
like) Any method will take at least …..
Bubble sort
Class example
• Sort subset of students by height.
• Designate someone as counter of compare operations
• Designate someone as counter of swap operations
• Designate someone as counter of rounds
Bubble sort
Worst case. n elements in set• Round 1: n-1 compares• Round 2: n-2 compares• …• Round n-1: 1 compare
So, average number of compares per round is (n-1)/2
Total number of compares is (rough)(n-1) * (n-1)/2
Measure
• generally given in terms of the size of the problem and given as a bound
• called Big Oh notation• A function f(n) is O(g(n)), n integer, if there is an
integer n0 and a constant c such thatf(n) <= c*g(n) if n>=n0
Conceptually, if n is big enough, than the function f is bounded by a multiple of the function g.
Common usage of O(n)
• O(c) constant
• O(n) linear
• O(n2) quadratic
• O(np) polynomial
• O(ne) exponential
Easy results
• f(n) = n IS O(n) and also O(n2) and O(np)Let c = 1, and n0 = 1, then n <= n2 for all values of n>=1
but
• f(n2) is NOT O(n)
Sorts
There are sorting algorithms that are better than bubble sort, that is, time is less than O(n2)
• heap sort
• quick sort
• merge sort
Assign: be able to demonstrate with a deck of cards. Sketch proof on complexity.