18
Complexity, etc. Homework. Comparison to computability. Big Oh notation. Sorting. Classwork/Homework: prepare presentation on specific sorts. Presentation Day for some.

Complexity, etc. Homework. Comparison to computability. Big Oh notation. Sorting. Classwork/Homework: prepare presentation on specific sorts. Presentation

Embed Size (px)

Citation preview

Page 1: Complexity, etc. Homework. Comparison to computability. Big Oh notation. Sorting. Classwork/Homework: prepare presentation on specific sorts. Presentation

Complexity, etc.

Homework. Comparison to computability.Big Oh notation. Sorting.

Classwork/Homework: prepare presentation on specific sorts.

Presentation Day for some.

Page 2: Complexity, etc. Homework. Comparison to computability. Big Oh notation. Sorting. Classwork/Homework: prepare presentation on specific sorts. Presentation

Homework

• CFG and PDA

• Regular expressions

Page 3: Complexity, etc. Homework. Comparison to computability. Big Oh notation. Sorting. Classwork/Homework: prepare presentation on specific sorts. Presentation

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.

Page 4: Complexity, etc. Homework. Comparison to computability. Big Oh notation. Sorting. Classwork/Homework: prepare presentation on specific sorts. Presentation

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.

Page 5: Complexity, etc. Homework. Comparison to computability. Big Oh notation. Sorting. Classwork/Homework: prepare presentation on specific sorts. Presentation

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.

Page 6: Complexity, etc. Homework. Comparison to computability. Big Oh notation. Sorting. Classwork/Homework: prepare presentation on specific sorts. Presentation

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.

Page 7: Complexity, etc. Homework. Comparison to computability. Big Oh notation. Sorting. Classwork/Homework: prepare presentation on specific sorts. Presentation

Define a CFG for aibj

• Try it!

Page 8: Complexity, etc. Homework. Comparison to computability. Big Oh notation. Sorting. Classwork/Homework: prepare presentation on specific sorts. Presentation

Now, what about aibi

• Write a grammar

Page 9: Complexity, etc. Homework. Comparison to computability. Big Oh notation. Sorting. Classwork/Homework: prepare presentation on specific sorts. Presentation

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.

Page 10: Complexity, etc. Homework. Comparison to computability. Big Oh notation. Sorting. Classwork/Homework: prepare presentation on specific sorts. Presentation

Many similar results

• Pumping lemma for CFL

• Extra credit assignment: look up and present to class.

Page 11: Complexity, etc. Homework. Comparison to computability. Big Oh notation. Sorting. Classwork/Homework: prepare presentation on specific sorts. Presentation

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.

Page 12: Complexity, etc. Homework. Comparison to computability. Big Oh notation. Sorting. Classwork/Homework: prepare presentation on specific sorts. Presentation

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 …..

Page 13: Complexity, etc. Homework. Comparison to computability. Big Oh notation. Sorting. Classwork/Homework: prepare presentation on specific sorts. Presentation

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

Page 14: Complexity, etc. Homework. Comparison to computability. Big Oh notation. Sorting. Classwork/Homework: prepare presentation on specific sorts. Presentation

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

Page 15: Complexity, etc. Homework. Comparison to computability. Big Oh notation. Sorting. Classwork/Homework: prepare presentation on specific sorts. Presentation

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.

Page 16: Complexity, etc. Homework. Comparison to computability. Big Oh notation. Sorting. Classwork/Homework: prepare presentation on specific sorts. Presentation

Common usage of O(n)

• O(c) constant

• O(n) linear

• O(n2) quadratic

• O(np) polynomial

• O(ne) exponential

Page 17: Complexity, etc. Homework. Comparison to computability. Big Oh notation. Sorting. Classwork/Homework: prepare presentation on specific sorts. Presentation

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)

Page 18: Complexity, etc. Homework. Comparison to computability. Big Oh notation. Sorting. Classwork/Homework: prepare presentation on specific sorts. Presentation

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.