Design and Analysis of Algorithms: Logistics, Introduction, and Multiplication!
Design and Analysis of Algorithms: Logistics, Introduction, and Multiplication!
The
Operating Systems (CS 140) Machine learning (CS 229) Cryptography (CS 255)
Computational
Lens
Can I do better?
Algorithm designer
The algorithm designer’s internal monologue…
What exactly do we Dude, this is just like
mean by better? And that other time. If you
what about that corner do the thing and the
case? Shouldn’t we be Can I do better? stuff like you did then,
zero-indexing? it’ll totally work real fast!
Roadmap
Asymptotic
Analysis
Dynamic
Greedy Algs Programming
MIDTERM
Graphs!
The
Future!
Course elements and resources
• Course website:
• https://elcit.ctu.edu.vn/course/view.php?id=2858
• Lectures
• Textbook
• Homework
• Exams
How to get the most out of lectures
• During lecture:
• Show up, ask questions, put your phone away.
• May be helpful: take notes on printouts of the slides.
• Before lecture:
• Do the pre-lecture exercises listed on the website.
• After lecture:
• Go through the exercises on the slides.
These guys will pop up
on the slides and ask
questions – those
questions are for you!
Siggi the Studious Stork Ollie the Over-achieving Ostrich
(recommended exercises) (challenge questions)
• Do the reading
• either before or after lecture, whatever works best for you.
• do not wait to “catch up” the week before the exam.
Textbook
• CLRS:
• Introduction to Algorithms,
by Cormen, Leiserson,
Rivest, and Stein.
We will also
sometimes refer to
Kleinberg and Tardos
Homework!
Weekly assignments in two parts:
1. Exercises:
• Check-your-understanding and computations
• Should be pretty straightforward
• Do these on your own
2. Problems:
• Proofs and algorithm design
• Not straightforward
• You may collaborate with your classmates…
How to get the most out of homework
• Do the exercises on your own.
1. Work hard
2. Ask for help
3. Work hard
Today
• Why are you here?
• Course overview, logistics, and how to succeed in
this course.
• Some actual computer science.
Course goals
• Think analytically about algorithms
• Flesh out an “algorithmic toolkit”
• Learn to communicate clearly about algorithms
Today’s goals
• Karatsuba Integer Multiplication
• Technique: Divide and conquer
• Meta points:
• How do we measure the speed of an algorithm?
Let’s start at the beginning
Etymology of “Algorithm”
• Al-Khwarizmi (Persian mathematician, lived
around 800 AD) wrote a book about how to
multiply with Arabic numerals.
Dixit algorizmi
(so says Al-Khwarizmi)
44
x 97
Integer Multiplication
1234567895931413
x 4563823520395533
Integer Multiplication
n
1233925720752752384623764283568364918374523856298
x 4562323582342395285623467235019130750135350013753
???
How long would this take you?
?@
Tme(n) = + 100
AB
(I made this up)
Tlaptop(n) =
0.0063 n2 – 0.5 n + 12.7 ms
Asymptotic analysis
• How does the runtime scale with the size of the input?
• Runtime of grade school multiplication scales like n2
𝑛>
𝑇GH 𝑛 = + 100
10
Tlaptop(n) =
0.0063 n2 – 0.5 n + 12.7 ms
Let n get bigger…
Asymptotic analysis
is a useful notion…
• How does the runtime scale with the size of the input?
𝑛
Let’s dig in to our algorithmic toolkit…
Divide and conquer
Break problem up into smaller (easier) sub-problems
Big problem
Smaller Smaller
problem problem
Often recursively!
1 2 3 4
1 2 3 4
See the Lecture 1 Python notebook for actual code! Siggi the Studious Stork
How long does this take?
• Better or worse than the grade school algorithm?
• That is, does the number of operations grow like n2 ?
• More or less than that?
• Proof by meta-reasoning:
It must be faster than the grade school
algorithm, because we are learning it in
an algorithms class.
Not sound logic!
• Claim:
The running time of this algorithm is
AT LEAST n2 operations.
How many one-digit multiplies?
12345678 × 87654321
12 × 87 56 × 87 12 × 43 56 × 43
34 × 87 78 × 87 34 × 43 78 × 43
12 × 65 56 × 65 12 × 21 56 × 21
34 × 65 78 × 65 34 × 21 78 × 21
1×8 … 3×4 …
1×7
2×8 Claim: there are n2 one-digit problems.
2×7 Every pair of digits still gets multiplied together separately.
etc... So the running time is still at least 𝑛> .
Another way to see this*
*we will come back to
this sort of analysis later
and still more rigorously.
1 problem
of size n • If you cut n in half
log2(n) times,
4 problems you get down to 1.
of size n/2
• So we do this
log2(n) times and
get…
4t problems
4log_2(n) = n2
of size n/2t problems of size 1.
𝑛>
But wait!!
𝑛
Divide and conquer can actually make progress
✓ ✓ ✓
What’s the running time?
1 problem • If you cut n in half
of size n log2(n) times, you
get down to 1.
3 problems
of size n/2 • So we do this log2(n)
times and get…
𝑛>
𝑛A.[
𝑛
We can even see it in real life!
Can we do better?
• Toom-Cook (1963): instead of breaking into three n/2-
sized problems, break into five n/3-sized problems.
• This scales like n1.465
Try to figure out how to break Given that you can break an
up an n-sized problem into five n-sized problem into five
n/3-sized problems! (Hint: start n/3-sized problems, where
with nine n/3-sized problems). does the 1.465 come from?
• Schönhage–Strassen (1971):
• Scales like n log(n) loglog(n)
• Furer (2007)
• Scales like n log(n) 2log*(n)
[This is just for fun, you don’t need
to know these algorithms!]
Course goals
• Think analytically about algorithms
• Flesh out an “algorithmic toolkit”
• Learn to communicate clearly about algorithms
Today’s goals
• Karatsuba Integer Multiplication
• Technique: Divide and conquer
• Meta points:
• How do we measure the speed of an algorithm?
Wrap up
Wrap up
• https://elcit.ctu.edu.vn/course/view.php?id=2858
• Algorithms are:
• Fundamental, useful, and fun!
• In this course, we will develop both algorithmic
intuition and algorithmic technical chops
• It might not be easy but it will be worth it!