Seminar 1 - Introduction (Handout)
Seminar 1 - Introduction (Handout)
Seminar 1 - Introduction
Rafael Dowsley
3 March 2025
Agenda
1 Unit Information
2 Divide-and-Conquer
3 Complexity Analysis
1/49
What is this unit about?
I Python used for assignments, but the subject is really language agnostic.
2/49
Is this unit important?
I Companies actively hunt for people good at algorithms and data structures.
I Many job interview questions are based on algorithms from this unit.
I Many applied class questions are in fact very similar to questions you could
be asked in job interviews.
I You are the future of CS and will make great contributions to field.
What you learn in this unit will greatly help you throughout your career.
3/49
Overview of the contents
I Divide-and-Conquer
I Greedy algorithms
I Dynamic Programming
I Network Flow
• Analysis of algorithms.
4/49
Expectations
I You have to be on top of it from Week 1. You will very likely not pass if
you think “I can brush up on the material close to the assessment
deadlines”.
I The best way to spend “the first 60 hours” is by attending and engaging in
the Seminars and Applied Classes.
5/49
Good news
I Notes for all 12 weeks are available in a single PDF file on Moodle (click
on “Learning”, then “Additional Information and Resources” and scroll
down to “Unit Resources”).
• Majority of the students that regularly engage with the classes end
up getting D/HD.
6/49
Support
7/49
Additional references
• This subject has huge value for your professional development into
careers in computer science. Some books you might want to refer to
(from time-to-time, even beyond this unit) include:
8/49
Assessment
I In-semester hurdle: 45% of the in-semester marks (i.e., at least 22.5 out of
total 50 marks for Quizzes and Assignments).
I Exam hurdle: 45% (i.e., at least 22.5 out of the 50 Exam marks).
9/49
How to succeed in this unit?
• During Week x, read the corresponding parts of the unit notes and
clarify any questions in the consultations or Ed forum.
I The Preparation problems will likely be helpful to get you ready for the
Quiz as the Quiz covers contents from classes up to Week x − 1.
I The Assignments and the Exam assess the concepts explored in the
Applied Classes.
10/49
How to get into trouble in this unit?
I If you spend time understanding the content taught in the classes, the
assignments will be far easier.
I Those students normally struggle a lot with exam questions that are taken
almost “as is” from Applied Classes and Seminars.
11/49
Academic integrity
12/49
How to avoid academic integrity issues
• https://www.monash.edu/students/study-support/
academic-integrity
• What can you do? Share test cases! Feel free to post your test cases
on the Ed post that will be created for that, and to use other’s.
13/49
Give us feedback!
14/49
Your first algorithm
I In fact, you learned in school an algorithm that is more than 2000 years
old: Euclid’s algorithm for computing the greatest common divisor.
I Even in your first school years you already learned the “grade school”
multiplication algorithm.
15/49
Grade school multiplication algorithm
• Did your teacher talk about the efficiency of this algorithm? Showed
the correctness proof?
16/49
Grade school multiplication algorithm
17/49
Divide-and-Conquer
Divide-and-Conquer paradigm
18/49
First improvement idea
19/49
Are we making progress?
• Not really! Intuition: 4 instances that will each take about 14 of the
time that was necessary to solve the original problem, so the overall
time stays in the same order. You can later check that by solving
the recurrence T (n) = 4 · T (n/2) + c · n.
21/49
Karatsuba’s algorithm
• Given the results of (1), (2) and (3), if we can trivially obtain
(aM · bL + aL · bM ) then we are done computing a · b with only 3
recursive calls.
22/49
The trick
• Note that
23/49
Where does this trick come from?
24/49
Is that a big improvement?
Merge sort
27/49
Other Divide-and-Conquer examples
28/49
Complexity Analysis
Time complexity
I Best-case complexity.
I Average-case complexity.
29/49
Asymptotic notation
Big-O Notation
It is said that f (n) = O(g (n)) if there are constants c and n0 such that
f (n) ≤ c · g (n) for all n ≥ n0 .
Big-Ω Notation
It is said that f (n) = Ω(g (n)) if there are constants c and n0 such that
f (n) ≥ c · g (n) for all n ≥ n0 .
Big-Θ Notation
It is said that f (n) = Θ(g (n)) if, and only if, f (n) = O(g (n)) and
f (n) = Ω(g (n)).
30/49
Space complexity
I Many textbooks and online resources do not distinguish between the above
two terms and use the term “space complexity” when they are in fact
referring to auxiliary space complexity.
31/49
In-place algorithm
I Be mindful that some books use a different definition (e.g., space taken by
recursion may be ignored). For the sake of this unit, we will use the above
definition.
32/49
Time complexity of Binary Search
I O(1)
33/49
Space complexity of Binary Search
• Space complexity?
Binary Search
I O(n)
1: function binary search(array [1..n], key )
2: lo = 1 and hi = n + 1
3: while lo < hi − 1 do • Auxiliary space complexity?
4: mid = b(lo + hi)/2)c
5: if key ≥ array [mid] then lo = mid I O(1)
6: else hi = mid
7: if array [lo] = key then return lo
8: else return null • It is an in-place algorithm!
34/49
What is the time complexity?
35/49
Output-sensitive time complexity
x = 23, y = 35
1 5 8 17 22 27 31 32 36 41
36/49
Solving Recurrence Relations
Recurrence relation
T (1) = b
T (n) = T (n − 1) + c
37/49
Solving a simple recurrence relation for time complexity
• Cost when n = 1:
Power1 I T (1) = b for constant b
1: function power1(x, n)
• Cost for general case:
2: if n = 0 then return 1
I T (n) = T (n − 1) + c for constant c
3: else if n = 1 then return x
I T (n) = T (n − 2) + 2c
4: else return x · power1(x, n − 1)
T (n) = T (n − 3) + 3c
I Pattern?
Goal I T (n) = T (n − k) + c · k
Reduce general case to be in terms of the I Set k = n − 1 to get base case.
base case.
Solution
T (n) = b + c · (n − 1)
= c ·n+b−c
= O(n)
38/49
Checking solution by substitution
• Cost when n = 1:
Power1 I T (1) = b for constant b
1: function power1(x, n)
2: if n = 0 then return 1 • We have that:
3: else if n = 1 then return x
T (1) = c ·1+b−c
4: else return x · power1(x, n − 1)
= b
base case.
• We have that:
Solution T (n − 1) + c = c · (n − 1) + b − c + c
T (n) = b + c · (n − 1) = c ·n+b−c
= c ·n+b−c = T (n)
= O(n)
39/49
Space complexity of this power function
• Space complexity?
Power1 I Total space usage = local space
used by the function * maximum
1: function power1(x, n)
depth of recursion
2: if n = 0 then return 1
I O(n)
3: else if n = 1 then return x
4: else return x · power1(x, n − 1)
• We will not discuss tail-recursion in
this unit because it is language
specific, e.g., Python doesn’t utilise
Power2
tail-recursion.
1: function power2(x, n)
2: result = 1
• Auxiliary Space Complexity?
3: for i ← 1 to n do
4: result = result · x • power1(x, n) is not in-place.
5: return result
• Iterative version power2(x, n) is
in-place.
40/49
Yet another power function
• Cost when n = 1:
Power3 I T (1) = b for constant b
1: function power3(x, n)
• Cost for general case:
2: if n = 0 then return 1
I T (n) = T (n/2) + c for constant c
3: else if n = 1 then return x
I T (n) = T (n/4) + 2c
4: y = power3(x · x, bn/2c)
T (n) = T (n/8) + 3c
5: if n even then return y
I Pattern?
6: else return x · y
n
I T (n) = T 2k
+c ·k
I Set k = log n to get base case.
Goal
Reduce general case to be in terms of the
base case.
Solution
T (n) = b + c · log n
= O(log n)
41/49
Another check by substitution
• Cost when n = 1:
Power3 I T (1) = b for constant b
1: function power3(x, n)
2: if n = 0 then return 1 • We have that:
3: else if n = 1 then return x
T (1) = b + c · log 1
4: y = power3(x · x, bn/2c)
5: if n even then return y = b
6: else return x · y
• Cost for general case:
I T (n) = T (n/2) + c for constant c
Goal
Reduce general case to be in terms of the • We have that:
base case.
T (n/2) + c = b + c · log(n/2) + c
= b + c · (log n − log 2) + c
Solution
= b + c · log n
T (n) = b + c · log n
= T (n)
= O(log n)
42/49
Recurrence and complexity
• Recurrence relation:
T (n) = T (n/2) + c
T (1) = b
• Algorithmic example?
I Binary search
• Asymptotic complexity?
I O(log n)
43/49
Recurrence and complexity
• Recurrence relation:
T (n) = T (n − 1) + c
T (1) = b
• Algorithmic example?
I Linear search
• Asymptotic complexity?
I O(n)
44/49
Recurrence and complexity
• Recurrence relation:
T (n) = 2 · T (n/2) + c · n
T (1) = b
• Algorithmic example?
I Merge Sort
• Asymptotic complexity?
I O(n log n)
45/49
Recurrence and complexity
• Recurrence relation:
T (n) = T (n − 1) + c · n
T (1) = b
• Algorithmic example?
I Selection Sort
• Asymptotic complexity?
I O(n2 )
46/49
Recurrence and complexity
• Recurrence relation:
T (n) = 2 · T (n − 1) + c
T (0) = b
• Algorithmic example?
• Asymptotic complexity?
I O(2n )
47/49
Reading
I Rou: Chapters 1 to 4
48/49
Concluding remarks
• Coming up next:
I Analysis of algorithms
49/49