Professional Documents
Culture Documents
1
The Joy of Algorithms
2
Etymology [Knuth, TAOCP]
True origin: Abu 'Abd Allah Muhammad ibn Musa al-Khwarizm was a
famous 9th century Persian textbook author who wrote Kitab al-
jabr wa'l-muqabala, which evolved into today's high school algebra
text.
3
What you will learn in this course
Prerequisites:
CS 330: Discrete Structures https://moss.cs.iit.edu/cs330
CS 331: Data Structures and Algorithms https://moss.cs.iit.edu/cs331
4
… and how
• Attending lectures
5
Administrative matters
6
You’ve already learnt many algorithms in the past …
7
Algorithms in daily lives …
Ex: 34¢.
Ex: $2.89.
8
Algorithms in daily lives …
7 8
7 5 6 8
1 7 3 5 2 6 4 8
9
Recreational puzzles
@wiki/Balance_puzzle
10
If you are bored, try…
Q1: Given n coins, one of them fake and lighter, find the minimum
number of weighings that will guarantee finding the fake coin.
Q2: Given n 3 coins, one of them fake and lighter or heavier, find the
minimum number of weighings that will guarantee finding the fake coin
and figuring out whether it is heavier or lighter .
11
Appetizer: elevator movements
12
Counting using potential/state function
UPs DOWNs
Counting:
13
Gambler's losing bets
Suppose that
John starts with at most
the number of times that he wins bets is at most .
What is an upper bound on the number of times that John loses a bet?
Losses .
14
Five Representative Problems [KT 1.2]
Interval scheduling
h Time
0 1 2 3 4 5 6 7 8 9 10 11
16
Weighted interval scheduling
Input. Set of jobs with start times, finish times, and weights.
Goal. Find maximum weight subset of non-overlapping jobs.
23
12
20
26
13
20
11
16 Time
0 1 2 3 4 5 6 7 8 9 10 11
17
Assignment of jobs to machines
A 1
B 2
C 3
D 4
E 5
18
Selection of conflict-free jobs
1 2
4 5
3
6 7
19
Competitive job selection
10 1 5 15 5 1 5 1 15 10
20
Five Representative Problems
21
Asymptotic Order of Growth [KT 2.2]
Suppress constant factors and lower-order terms
23
Semantics of asymptotic order
24
Properties
Transitivity.
If f = O(g) and g = O(h) then f = O(h).
If f = (g) and g = (h) then f = (h).
If f = (g) and g = (h) then f = (h).
Additivity.
If f = O(h) and g = O(h) then f + g = O(h).
If f = (h) and g = (h) then f + g = (h).
If f = (h) and g = O(h) then f + g = (h).
25
Asymptotic bounds for some common functions
26
Quickly growing function and its slowly growing inverse
27
The tower function and iterated logarithm
n T(n) n log*n
1 2 [0,2] 1
2 4 [3,4] 2
3 16 [ 5 , 16 ] 3
4 65,536 [ 17 , 65,536 ] 4
5 265,536 [ 65,537 , 265,536 ] 5
28
Nesting / Repeated application
29
Ackermann’s function (one of many variations)
30
Ackermann’s function vs. Tower function
31
Inverse Ackermann function
32
In-class exercise: Sort in ascending order
log*n
comparison strategy:
• divide and conquer
• take the logarithm
33
Stirling formula
Solution.
36
Proof by recursion tree
0, if 𝑛 = 1
𝑇 𝑛 =
2𝑇 𝑛/2 + 𝑛, otherwise
n n
...
2 2 2 2 2 2 2 2 n/2 (2)
n log2n
37
Proof by telescoping
0, if 𝑛 = 1
𝑇 𝑛 =
2𝑇 𝑛/2 + 𝑛, otherwise
For n > 1:
38
Proof by induction
0, if 𝑛 = 1
𝑇 𝑛 =
2𝑇 𝑛/2 + 𝑛, otherwise
Base case: n = 1.
Inductive hypothesis: T(n) = n log2 n.
Goal: show that T(2n) = 2n log2 (2n).
39
Induction with floor/ceiling
Base case: n = 1.
Define n1 = n / 2 , n2 = n / 2.
Induction step: assume true for 1, 2, ... , n–1.
40
Recurrence Relation: Increasing total subproblem sizes
0, if 𝑛 = 1
𝑇 𝑛 ≤
3𝑇 𝑛/2 + 𝑛, otherwise
n n
log2n n/4 n/4 n/4 n/4 n/4 n/4 n/4 n/4 n/4 9(n/4)
... ...
n / 2k
... ...
2 2 2 2 2 2 2 2 3 lg n (2)
41
The master theorem for balanced subsproblems
Suppose , where
• is the number of subproblems
• is the factor by which input size shrinks
• is the work to create and combine all the subproblems.
42
Unbalanced subproblems
n/5 7n/10
...
Key fact: the total subproblem size drops at the factor 9/10, so does
the total work at each level.
43
CS 330 Topics
• Introductory Logic propositional logic, predicate logic, proof
methodologies, examples of algorithm correctness
https://moss.cs.iit.edu/cs331/syllabus.html
45