Professional Documents
Culture Documents
TABLE OF CONTENTS
• Course mechanics
• Models of computation
COURSE MECHANICS
• In person
• Lectures
• “Lab” section is for tutorials
• Course website: https://student.cs.uwaterloo.ca/~cs341/
• Syllabus, calendar, policies, slides, assignments…
• Read this and mark important dates.
COURSE MECHANICS
• Keep up with the lectures: Material builds over time…
• Piazza: For questions and announcements.
3 4
ASSESSMENTS TEXTBOOK
• Available for free via library website!
• All sections have same assignments, midterm and final
• You are expected to know
• Sections are roughly synchronized to ensure necessary
content is taught • entire textbook sections,
as listed on course website
• Tentative plan is 5 assignments, midterm, final
• all the material presented in lectures
• See website for grading scheme, etc.
(unless we explicitly say you aren’t
responsible for it)
• Some other textbooks cover some material better… see www
5 6
1
2023-09-13
WHAT IS AN ALGORITHM?
MODELS OF COMPUTATION • Informally: A well-defined
procedure (sequence of steps)
to solve a computational problem Correctness?
9 10
11 12
2
2023-09-13
13 14
15 16
17 18
3
2023-09-13
Example 3 1 -7 4 0 2 -1 3 -1 Solution: 8
(take A[3..7])
Index 1 2 3 4 5 6 7 8
19 20
1 -7 4 0 2 -1 3 -1
𝒊=𝒋 𝒋 𝒋 𝒋 Case 1: optimal sol’n
A 9 -3 4 -5 -2 -5 3 -1
is entirely in L L 9 -3 4 -5 R -2 -5 3 -1
21 22
23 24
4
2023-09-13
A 9 -3 4 -5 -2 -5 3 -1 A 1 -7 4 0 2 -1 3 0
L 9 -3 4 -5 R -2 -5 3 -1 L 1 -7 4 0 R 2 -1 3 0
How to simplify?
27 28
29 30
5
2023-09-13
𝒏 Difference is due to
Compare to unit cost model:
(1) growth in variable sizes and
• = 𝑂 𝑛 log 𝑛𝑀 𝑂 𝑛3 time
(2) cost of bitwise addition
and analysis looks more like bit complexity model • 0 represents time
under 0.01s
33 34
𝑓 𝑛 ∈ 𝑂(𝑔 𝑛 )
35 36
6
2023-09-13
𝑓 𝑛 𝑓 𝑛 ∈ Θ(𝑔 𝑛 ) 𝑓 𝑛 ∈ O(𝑔 𝑛 )
𝑓 𝑛 ∈ Ω(𝑔 𝑛 ) 𝑔 𝑛
𝑔 𝑛 ∈ Θ(𝑓 𝑛 ) 𝑓 𝑛 ∈ Ω(𝑔 𝑛 )
𝑂+Ω=Θ
37 38
EXERCISE
• Which of the following are true?
𝑓 𝑛 ∈o 𝑔 𝑛 • 𝑛2 ∈ 𝑂(𝑛3 )
But NOT
implies
vice versa
𝑓 𝑛 ∈ O(𝑔 𝑛 ) • 𝑛2 ∈ 𝑜(𝑛3 )
• 𝑛3 ∈ 𝜔(𝑛3 )
• log 𝑛 ∈ 𝑜(𝑛)
𝑓 𝑛 ∈𝜔 𝑔 𝑛 • 𝑛 log 𝑛 ∈ Ω(𝑛)
But NOT
implies
vice versa • 𝑛 log 𝑛2 ∈ 𝜔(𝑛 log 𝑛)
𝑓 𝑛 ∈ Ω(𝑔 𝑛 )
• 𝑛 ∈ Θ(𝑛 log 𝑛)
39 40
EXERCISE
• Which of the following are true?
• 𝑛2 ∈ 𝑂(𝑛3 ) YES
• 𝑛2 ∈ 𝑜(𝑛3 ) YES
• 𝑛3 ∈ 𝜔(𝑛3 ) NO
• log 𝑛 ∈ 𝑜(𝑛) YES
• 𝑛 log 𝑛 ∈ Ω(𝑛) YES COMPARING GROWTH RATES
• 𝑛 log 𝑛2 ∈ 𝜔(𝑛 log 𝑛) NO
• 𝑛 ∈ Θ(𝑛 log 𝑛) NO
41 42
7
2023-09-13
LIMIT TECHNIQUE
FOR COMPARING GROWTH RATES
43 44
45 46
L’HOSPITAL’S RULE
LIMIT RULES 3/3 • Often we take the limit of 𝑔
𝑓 𝑛
where
𝑛
both 𝑓(𝑛) and 𝑔(𝑛) tend to ∞, or
both 𝑓(𝑛) and 𝑔(𝑛) tend to 0
Limit of an Exponential Function • Such limits require L’Hospital’s rule
lim 𝑓 𝑥
lim 𝑏 𝑓 𝑥 = 𝑏 𝑥→𝑎 • This rule says the limit of 𝒇(𝒏)/𝒈(𝒏) in this case
𝑥→𝑎
is the same as the limit of the derivative
Limit of a Logarithm of a Function 𝑓 𝑛
𝑑
𝑓 𝑛
lim log 𝑏 𝑓(𝑥) = log 𝑏 lim 𝑓 𝑥 • In other words, lim = lim 𝑑𝑛
𝑑
𝑥→𝑎 𝑥→𝑎 𝑛→∞ 𝑔 𝑛 𝑛→∞ 𝑔 𝑛
𝑑𝑛
8
2023-09-13
USING THE LIMIT METHOD: EXERCISE 1 USING THE LIMIT METHOD: EXERCISE 2
• Compare growth rate of 𝑛2 and 𝑛2 − 7𝑛 − 30 • Compare growth rate of ln 𝑛 2
and 𝑛1/2
𝑛2 −7𝑛−30 𝑑
• lim ln 𝑛 2 ln 𝑛 2
𝑛→∞ 𝑛2 • lim = lim𝑑𝑛
𝑑 1/2
𝑛→∞ 𝑛1/2 𝑛→∞ 𝑛
7 30 𝑑𝑛
• = lim (1 − 𝑛 − 𝑛2 )
𝑛→∞
• =1
• So 𝑛2 − 7𝑛 − 30 ∈ Θ(𝑛2 )
49 50
51 52
SUMMATIONS
AND SEQUENCES
53 54
9
2023-09-13
SEQUENCES
55 56
57 58
LOGARITHM RULES
59 60
10
2023-09-13
61 62
63 64
65 66
11
2023-09-13
𝑛 𝑛 𝑛 𝑛
𝑛
𝑇 𝑛 ∈ O log 𝑖 ⊆ O log 𝑛 ⊆ 𝑶(𝒏 𝐥𝐨𝐠 𝒏) 𝑇 𝑛 ∈ Ω log 𝑖 ⊆ Ω log ⊆ 𝜴(𝒏 𝐥𝐨𝐠 𝒏)
𝒏
2
𝑖=1 𝑖=1 𝑖=1 𝑖=𝟐
67 68
… ANOTHER EXERCISE
IN LOOP ANALYSIS?
69 70
𝑛
Strategy 1: big-O and big-Ω bounds 𝑂(1)
…
Proving a big-Ω bound…
𝒊=1 𝑛 𝑛
𝑛 𝑛 𝒋
Recall: 𝑇 𝑛 ∈ 𝚯 (𝑗 − 𝑖)
𝑇 𝑛 ∈ Θ 1 + Θ 1 + Θ 1 + Θ(1) 𝑂(1)
𝑖=1 𝑗=𝒊
𝑖=1 𝑗=𝒊 𝑘=𝒊
𝑂(1) 𝑛 𝑛
𝑛 𝑛 𝑛 𝑛
𝑇 𝑛 ∈ 𝛀 (𝑗 − 𝑖)
𝑇 𝑛 ∈ 𝚯 𝒋 − 𝒊 ∈ 𝚯 (𝑗 − 𝑖) Intuition: 𝑗 − 𝑖 is 𝛀(𝒏) in some iterations. How
𝑖=1 𝑗=𝒊
𝑖=1 𝑗=𝒊 𝑖=1 𝑗=𝒊 𝑂(1) many iterations? Lots?
𝒏/𝟐 𝑛
𝑛 𝑛 𝑛 𝑛
≥ Ω (𝑗 − 𝑖) To get a good Ω-bound, we ask questions like:
𝑇 𝑛 ∈ 𝑶 (𝑗 − 𝑖) ≤ 𝑂 𝒏 When do our loops have many iterations?
𝑖=1 𝑗=𝒊
𝑖=1 𝑗=𝒊 𝑖=1 𝑗=𝒊 When is our dominant term large?
𝑛/2 𝑛
𝑛 𝑛
This is the maximum number of ≥ Ω (𝑗 − 𝑖) Many iterations: when our 𝒋 loop does 𝛀(𝒏)
≤ 𝑂 𝑛 iterations that could be iterations! For example, when 𝒊 ≤ 𝒏/𝟐…
𝑖=1 𝑗=𝟑𝒏/𝟒
𝑖=1 𝒋=𝟏 performed in this loop
Large dominant term: when 𝒋 is much
𝑻 𝒏 ∈ 𝑶(𝒏𝟑 ) larger than 𝒊 (i.e., by a factor of n)
71 72
12
2023-09-13
Recall: 𝑇 𝑛 ∈ Ω (𝑗 − 𝑖)
𝑖=1 𝑗=𝟑𝒏/𝟒
𝑛/2 𝑛
𝟑𝒏 𝒏
≥Ω −
𝟒 𝟐
𝑖=1 𝑗=3𝑛/4 Smallest possible value of 𝑗 − 𝑖
𝑛/2 𝑛 for these bounds on 𝒊, 𝒋
= Ω 𝒏/𝟒
We will perform at least this much
𝑖=1 𝑗=3𝑛/4
work in every iteration!
𝒏 𝒏 𝑛
≥Ω ⋅ ⋅ = 𝛀(𝒏𝟑 ) This term does not depend on the
𝟐 𝟒 4
loop indexes, so just multiply by the
total number of loop iterations…
BONUS
• Study-song of the day
• Tool - Descending
• youtu.be/PcSoLwFisaw
75
13