Upload
hedya
View
25
Download
7
Tags:
Embed Size (px)
DESCRIPTION
Review for Quiz 2. Starring. Starring Bonzo Jr… (251 student). and Lucky (the turtle who introduced the magic square). Disclaimer. - PowerPoint PPT Presentation
Citation preview
Review for Quiz 2
Great Theoretical Ideas In Computer Science
Quiz Review CS 15-251 Spring 2005
March 17, 2005
Carnegie Mellon University
StarringStarring Bonzo Jr…(251 student)
and Lucky(the turtle who introduced the magic square)
Disclaimer
This set of lecture slides is intended as an OVERVIEW of the things you should know; it is insufficient to simply glance over these slides 10 minutes before the quiz.
Moreover, this quiz review is NOT a substitute for the lectures; if you missed any lecture, watching the video is highly recommended.
Administrative Stuff
First, a quick review of the administrative stuff.
– 50 minutes long– Divided into 4 conceptual
sections– Practice quizzes have been/will
be uploaded.
Section 1: Repeat After me
Yay! Free points just for going to lecture and skimming over
the notes
This section contains about 6-8 questions based on quick facts covered in lecture. If
you went to each lecture and paid attention, you should be fine. It is recommended that you skim over the lecture notes before the quiz to refresh your memory on the subject
matter.
Section 2: Reading Solutions
You are expected to read the given solutions to all homework and recitation
problems - this section is comprised of one or two questions that are literally identical to questions that we've given you before. Note
that the problems we ask can come from any previous assignment. (its even possible
that we'll ask for the same problem's solution on all three quizzes).
Section 3: Basic Techniques
These problems are based on techniques taught during class, and practiced in
HWs/recitations. If you practice the ideas taught in lecture, you should have very little trouble with this section (problems vary only
slightly from results derived in lecture).
HINT: Look at the stuff from Monday’s recitation and the practice quizzes.
Section 4: A Moment’s Thought
These problems are similar to problems given on homework assignments, albeit
easier. If you do well on homework assignments, you shouldn't have too much
trouble with this section.
Who named this section…?
Time managementDon’t forget about time management!
The first two sections are designed to be able to be
completed relatively quickly - don’t spend all your time on a
Repeat After Me question that is worth only 5 points!
Beginning of Material Review
Enough; I know all this! What’s going to be on the quiz?
Stuff on quizThe new material for Quiz 2 is from Lecture 9: Counting III
to Lecture 17: On Time and
Input Size.
We are assuming that you already have an understanding of the
material from lectures 1-8. While we won’t explicitly test on that, you may need the knowledge
you’ve learned from those lectures in answering the
questions.
Key Areas
The following areas are rated as very likely to appear on your quiz.
– Finite State Machines (DFAs).– Counting and Binomials.– Matchings (including the TMA).– GCDs and Continued Fractions– Fibonacci and Fibonacci-like
series.– Asymptotic notation and runtime
analysis.
– Finite State Machines (DFAs).– Counting and Binomials.– Matchings (including the TMA).– GCDs and Continued Fractions– Fibonacci and Fibonacci-like
series.– Asymptotic notation and runtime
analysis.
Table of contents
Finite set of states
A start state
A set of accepting states
A finite alphabet a b #
x 1
State transition instructions
1 2{ , , , , }o kQ q q q q
Finite Automaton – quick review
oq
1 2, , ,
ri i iF q q q
:
( , )i j
Q Q
q a q
iq jq
a
Pumping Lemma
In simple terms, the pumping lemma says that for any string that’s long enough, there’s a
partof it we can repeat as many times as we want (or even
delete, if we let i = 0), and still get
a string that’s accepted.
Formal definition of pumping lemma
The pumping lemma states that if there exists a DFA accepting a certain language, thenthere is a positive integer k (called the pumping length) so that any string s whose lengthis greater than or equal to k can be split up into three strings, u, v, and w, with certain properties:
1. v is not empty; 2. |uv| k, and 3. The string uviw is also accepted by the DFA.
What strings are accepted by the following regexes?
– Ø– ε– 1001– 10*1– (0+10+110+111)*– 1(0+1)*0(0+1)*1
Regular Expressions
Important ConceptsCan you do the following? - Construct a DFA that accepts all strings with a length that is a multiple of 2 or 3.- Prove that the language of palindromes is not regular (use of pumping lemma is sufficient but not necessary).- Convert a simple regex to a DFA and vice versa?
– Finite State Machines (DFAs).– Counting and Binomials.– Matchings (including the TMA).– GCDs and Continued Fractions– Fibonacci and Fibonacci-like
series.– Asymptotic notation and runtime
analysis.
Table of contents
1 X 1 X1 X 1 X
1 X 1 X
1 X
Choice tree for terms of (1+X)3
1 X X X2 X X2 X2 X3
Combine like terms to get 1 + 3X + 3X2 + X3
The Binomial Formula
nkn xn
nx
k
nx
nx
nnx
......
110)1( 2
binomial expression
Binomial Coefficients
0
(1 )n
n k
k
nx x
k
The Binomial Formula
Be sure you can handle variants on this (remember the
problem from Monday’s recitation)
Pascal’s Triangle
11
1 11 1
1 2 11 2 1
1 3 3 11 3 3 1
1 4 6 4 11 4 6 4 1
1 5 10 10 5 11 5 10 10 5 1
1 6 15 20 15 6 11 6 15 20 15 6 1
So very many
properties…
Manhattan
k’th Avenue01
23
4
j’th Street 01
23
4
– Finite State Machines (DFAs).– Counting and Binomials.– Matchings (including the TMA).– GCDs and Continued Fractions– Fibonacci and Fibonacci-like
series.– Asymptotic notation and runtime
analysis.
Table of contents
Rogue Couples
Suppose we pair off all the boys and girls. Now suppose that some boy and some girl prefer each other to the people to whom they are paired. They will be called a rogue couple.
Stable Pairings
A pairing of boys and girls is called stable if it contains no rogue couples.
3,5,2,1,4
15,2,1,4,3
4,3,5,1,2
31,2,3,4,5
42,3,4,1,5
5
1
3,2,5,1,4
2
1,2,5,3,4
3
4,3,2,1,5
4
1,3,4,2,5
5
1,2,4,5,3
2
Traditional Marriage Algorithm
For each day that some boy gets a “No” do:• MorningMorning
– Each girl stands on her balcony– Each boy proposes under the balcony of the best
girl whom he has not yet crossed off• Afternoon (for those girls with at least one Afternoon (for those girls with at least one
suitor)suitor)– To today’s best suitor: “Maybe, come back “Maybe, come back
tomorrow”tomorrow”– To any others: “No, I will never marry you” “No, I will never marry you”
• EveningEvening– Any rejected boy crosses the girl off his list
Each girl marries the boy to whom she just said “maybe”
Opinion Poll
Who is better off
in traditio
nal
dating, the boys
or the girls
?
– Finite State Machines (DFAs).– Counting and Binomials.– Matchings (including the TMA).– GCDs and Continued Fractions– Fibonacci and Fibonacci-like
series.– Asymptotic notation and runtime
analysis.
Table of contents
Euclid’s GCD Formula
Note: GCD(67, 29) = 1
Euclid(67,29) 67 mod 29 = 9Euclid(29,9) 29 mod 9 = 2
Euclid(9,2) 9 mod 2 = 1 Euclid(2,1) 2 mod 1
= 0 Euclid(1,0) outputs 1
Euclid(A,B) // requires AB0 If B=0 then return A
else return Euclid(B, A mod B)
Extended GCD Algorithm
The lecture also covered Euclid’s Extended GCD
Algorithm – that might be something you should take a
look at.
The multiplicative inverse of x ≡ Zn* is
the unique y є Zn* such that
x *n y ≡ n 1. TO QUICKLY COMPUTE Y FROM X:
Run Extended_Euclid(x,n). It returns a,b, and d such that ax+bn = dBut d = GCD(x,n) = 1, so ax + bn = 1Hence MODULO n: ax = 1 (mod n)Thus, a is the multiplicative inverse of x.
A (Simple) Continued Fraction Is Any Expression Of The Form:
111111111
....
ab
cd
ef
gh
ij
where a, b, c, … are whole numbers.
A Finite Continued Fraction
12
13
142
Denoted by [2,3,4,2,0,0,0,…]
An Infinite Continued Fraction
11
12
12
12
12
12
12
12
122 ....
Denoted by [1,2,2,2,…]
An infinite continued fraction
12 1
12
12
12
12
12
12
12
122 ....
Phi!
Φ
– Finite State Machines (DFAs).– Counting and Binomials.– Matchings (including the TMA).– GCDs and Continued Fractions– Fibonacci and Fibonacci-like
series.– Asymptotic notation and runtime
analysis.
Table of contents
Check HW/Recitation
The best way to review for this particular section is to look over the Fibonacci homework.
Also, the recitation problems from Monday on Fibonacci are EXCELLENT practice.
Beginning of Material Review
How did we get the closed form for Fibonacci-like series?
A technique to derive the formula for the Fibonacci
numbersFn is defined by two conditions:
Base condition: F0=0, F1=1
Inductive condition: Fn=Fn-1+Fn-2
Forget the base condition and concentrate on satisfying the inductive condition
Inductive condition: Fn=Fn-1+Fn-2
Consider solutions of the form: Fn= cn for some complex constant c
C must satisfy: cn - cn-1 - cn-2 = 0
cn - cn-1 - cn-2 = 0
iff cn-2(c2 - c1 - 1) = 0
iff c=0 or c2 - c1 - 1 = 0
Iff c = 0, c = , or c = -(1/)
c = 0, c = , or c = -(1/)
So for all these values of c the inductive condition is satisfied:
cn - cn-1 - cn-2 = 0
Do any of them happen to satisfy the base condition as well? c0=0 and c1=1?
ROTTEN LUCK
Insight: if 2 functions g(n) and h(n) satisfy the inductive condition then so does
a g(n) + b h(n) for all complex a and b
(a g(n) + b h(n)) + (a g(n-1) + b h(n-1)) + (a g(n-2) + b h(n-2)) = 0
g(n)-g(n-1)-g(n-2)=0ag(n)-ag(n-1)-ag(n-2)=0
h(n)-h(n-1)-h(n-2)=0bh(n)-bh(n-1)-bh(n-2)=0
a,b a n + b (-1/ )n satisfies the inductive condition
Set a and b to fit the base conditions.
n=0 : a + b = 0n=1 : a 1 + b (-1/ )1 = 1
Two equalities in two unknowns (a and b).Now solve for a and b:
this gives a = 1/5 b = -1/5
– Finite State Machines (DFAs).– Counting and Binomials.– Matchings (including the TMA).– GCDs and Continued Fractions– Fibonacci and Fibonacci-like
series.– Asymptotic notation and runtime
analysis.
Table of contents
Useful notation to discuss growth rates
For any two monotonic functions f and g from the positive integers to the positive integers, we say
“f = O(g)” or “f is O(g)”if
Some constant times g eventually dominates f
[Formally: there exists a constant c such that for all sufficiently large n: f(n) ≤ c g(n) ]
O(n) graph
# of bits in numbers
time
f = O(g) means that there is some constant c such that c g(n) stays above f(n) from some point
on.
fg
1.5g
More useful notation: Ω
For any two monotonic functions f and g from the positive integers to the positive integers, we say
“f = Ω(g)” or “f is Ω(g)”if:
f eventually dominates some constant times g
[Formally: there exists a constant c such that for all sufficiently large n: f(n) ≥ c g(n) ]
Yet more useful notation: Θ
For any two monotonic functions f and g from the positive integers to the positive integers, we say
“f = Θ(g)” or “f is Θ(g)”if:
f = O(g) and f = Ω(g)
Quickies
• n = O(n2) ?– YES
• n = O(√n) ?– NO
• 3n2 + 4n + = O(n2) ?– YES
• 3n2 + 4n + = Ω(n2) ?– YES
• n2 = Ω(n log n) ?– YES
• n2 log n = Θ(n2)– NO
3n2 + 4n + = Θ(n2)
Final Thoughts
Do the practice quizzes (or at least look over them).
Don’t forget that the processor lab is due on Tuesday at the beginning of class; make sure to schedule everything appropriately.
Good luck!
Fin