BLG 335E – Analysis of Algorithms I
Fall 2013, Recitation 2
23.10.2013
R.A. Atakan Aral
aralat@itu.edu.tr – Research Lab 1
R.A. Doğan Altan
daltan@itu.edu.tr – Research Lab 3
Warm-up Problem
• Order the following functions by
asymptotic growth rate:
– 𝑛2 + 5𝑛 + 7
– log2 𝑛3
– 9517
– 2log2 𝑛
– 𝑛3
– 𝑛𝑙𝑜𝑔2 𝑛 + 9𝑛
– 4 log2 𝑛
– log2 𝑛 + 3𝑛
Warm-up Problem
• Solution:
– 9517
– log2 𝑛3
– 4 log2 𝑛
– 2log2 𝑛
– log2 𝑛 + 3𝑛
– 𝑛𝑙𝑜𝑔2 𝑛 + 9𝑛
– 𝑛2 + 5𝑛 + 7
– 𝑛3
Problem 1
• Give tight asymptotic bounds for 𝑇(𝑛) in
each of the following recurrences.
a. 𝑇 𝑛 = 𝑇 𝑛 − 1 + 𝑛
b. 𝑇 𝑛 = 𝑇
𝑛
2
+ 𝑇
𝑛
4
+ 𝑇
𝑛
8
+ 𝑛
c. 𝑇 𝑛 = 𝑇
9𝑛
10
+ 𝑛
d. 𝑇 𝑛 = 16𝑇
𝑛
4
+ 𝑛2
e. 𝑇 𝑛 = 7𝑇
𝑛
2
+ 𝑛2
Problem 1
• Give tight asymptotic bounds for 𝑇(𝑛) in
each of the following recurrences.
a. 𝑇 𝑛 = 𝑇 𝑛 − 1 + 𝑛
Lower bound (Ω):
𝑇 𝑛 ≥ 𝑐𝑛2 𝑓𝑜𝑟 𝑠𝑜𝑚𝑒 𝑐 > 0
𝑇 𝑛 ≥ 𝑐 𝑛 − 1 2 + 𝑛
= 𝑐𝑛2 − 2𝑐𝑛 + 𝑐 + 𝑛 ≥ 𝑐𝑛2
𝑡𝑟𝑢𝑒 𝑖𝑓 0 < 𝑐 <
1
2
𝑎𝑛𝑑 𝑛 ≥ 0
Problem 1
• Give tight asymptotic bounds for 𝑇(𝑛) in
each of the following recurrences.
a. 𝑇 𝑛 = 𝑇 𝑛 − 1 + 𝑛
Upper bound (O):
𝑇 𝑛 ≤ 𝑐𝑛2 𝑓𝑜𝑟 𝑠𝑜𝑚𝑒 𝑐 > 0
𝑇 𝑛 ≤ 𝑐 𝑛 − 1 2 + 𝑛
= 𝑐𝑛2 − 2𝑐𝑛 + 𝑐 + 𝑛 ≤ 𝑐𝑛2
𝑡𝑟𝑢𝑒 𝑖𝑓 𝑐 = 1 𝑎𝑛𝑑 𝑛 ≥ 1
Problem 1
• Give tight asymptotic bounds for 𝑇(𝑛) in
each of the following recurrences.
b. 𝑇 𝑛 = 𝑇
𝑛
2
+ 𝑇
𝑛
4
+ 𝑇
𝑛
8
+ 𝑛
Problem 1
• Give tight asymptotic bounds for 𝑇(𝑛) in
each of the following recurrences.
b. 𝑇 𝑛 = 𝑇
𝑛
2
+ 𝑇
𝑛
4
+ 𝑇
𝑛
8
+ 𝑛
Upper bound (O):
𝑇 𝑛 ≤
𝑐𝑛
2
+
𝑐𝑛
4
+
𝑐𝑛
8
+ 𝑛 =
7𝑐𝑛
8
+ 𝑛 ≤ 𝑐𝑛
𝑡𝑟𝑢𝑒 𝑖𝑓 𝑐 ≥ 8
Problem 1
• Give tight asymptotic bounds for 𝑇(𝑛) in
each of the following recurrences.
b. 𝑇 𝑛 = 𝑇
𝑛
2
+ 𝑇
𝑛
4
+ 𝑇
𝑛
8
+ 𝑛
Lower bound (Ω):
𝑇 𝑛 ≥
𝑐𝑛
2
+
𝑐𝑛
4
+
𝑐𝑛
8
+ 𝑛 =
7𝑐𝑛
8
+ 𝑛 ≥ 𝑐𝑛
𝑡𝑟𝑢𝑒 𝑖𝑓 0 < 𝑐 ≤ 8
Master Method
Problem 1
• Give tight asymptotic bounds for 𝑇(𝑛) in
each of the following recurrences.
c. 𝑇 𝑛 = 𝑇
9𝑛
10
+ 𝑛
𝑎 = 1, 𝑏 =
10
9
, 𝑓 𝑛 = 𝑛 = 𝛺 n
log10
9
1+1
𝑝𝑜𝑠𝑠𝑖𝑏𝑙𝑦 𝑐𝑎𝑠𝑒 3, 𝑙𝑒𝑡′ 𝑠 𝑐ℎ𝑒𝑐𝑘 𝑐
1
9𝑛
10
≤ 𝑐𝑛 ℎ𝑜𝑙𝑑𝑠 𝑓𝑜𝑟 𝑐 =
9
10
≤ 1
𝑐𝑒𝑟𝑡𝑎𝑖𝑛𝑙𝑦 𝑐𝑎𝑠𝑒 3:
𝑇 𝑛 = Θ(n)
Problem 1
• Give tight asymptotic bounds for 𝑇(𝑛) in
each of the following recurrences.
d. 𝑇 𝑛 = 16𝑇
𝑛
4
+ 𝑛2 𝑎 = 16, 𝑏 = 4, 𝑓 𝑛 = 𝑛2
𝑛2=Θ 𝑛log4 16 , 𝑐𝑎𝑠𝑒 2:
𝑇 𝑛 = Θ(𝑛2 log2 𝑛)
e. 𝑇 𝑛 = 7𝑇
𝑛
2
+ 𝑛2
𝑎 = 7, 𝑏 = 2, 𝑓 𝑛 = 𝑛2
𝑛2 = 𝑂 𝑛log2 7−ε , 𝑐𝑎𝑠𝑒 1:
𝑇 𝑛 = Θ(𝑛log2 7)
Problem 2
• Hat-check problem
• Each of n customers gives a hat to a hat-
check person at a restaurant.
• The hat-check person gives the hats back
to the customers in a random order.
• What is the expected number of customers
that get back their own hat?
Problem 2
• Counting Solution:
– Count number of fixed points in all n!
possible permutations
– Divide by n! To find average number
– Answer is 1
• With Indicator Random Variables:
– 𝑋𝑖 = 𝐼 𝑐𝑢𝑠𝑡𝑜𝑚𝑒𝑟 𝑖 𝑔𝑒𝑡𝑠 ℎ𝑖𝑠 𝑜𝑤𝑛 ℎ𝑎𝑡
𝑓𝑜𝑟 𝑖 = 1,2, … , 𝑛
– 𝑋 = 𝑋1 + 𝑋2 + ⋯ + 𝑋 𝑛
𝑛𝑢𝑚𝑏𝑒𝑟 𝑜𝑓 𝑐𝑢𝑠𝑡𝑜𝑚𝑒𝑟𝑠 𝑡ℎ𝑎𝑡 𝑔𝑒𝑡 𝑡ℎ𝑒𝑖𝑟 𝑜𝑤𝑛 ℎ𝑎𝑡
Problem 2
• 𝐸 𝑋 = 𝐸 𝑖=1
𝑛
𝑋𝑖 = 𝑖=1
𝑛
𝐸[𝑋𝑖]
• 𝐸 𝑋𝑖 =
1
𝑛
• 𝐸 𝑋 = 𝑖=1
𝑛 1
𝑛
= 1
Problem 3
• Searching for a value x in an unsorted
array A consisting of n elements.
• Pick a random index i into A.
• If A[i ] = x, then we terminate;
• Otherwise, we continue the search by
picking a new random index into A.
• We continue picking random indices into
A until we find x or until we have checked
every element of A.
Problem 3
• What is the expected number of indices
into A must be picked before x is found?
(There is exactly one x in A)
• What if there are 𝑘 ≥ 1 x values in A?
• If there is no x in A, what is the expected
number of indices into A that must be
picked before search terminates?

Analysis of Algorithms - 2

  • 1.
    BLG 335E –Analysis of Algorithms I Fall 2013, Recitation 2 23.10.2013 R.A. Atakan Aral aralat@itu.edu.tr – Research Lab 1 R.A. Doğan Altan daltan@itu.edu.tr – Research Lab 3
  • 2.
    Warm-up Problem • Orderthe following functions by asymptotic growth rate: – 𝑛2 + 5𝑛 + 7 – log2 𝑛3 – 9517 – 2log2 𝑛 – 𝑛3 – 𝑛𝑙𝑜𝑔2 𝑛 + 9𝑛 – 4 log2 𝑛 – log2 𝑛 + 3𝑛
  • 3.
    Warm-up Problem • Solution: –9517 – log2 𝑛3 – 4 log2 𝑛 – 2log2 𝑛 – log2 𝑛 + 3𝑛 – 𝑛𝑙𝑜𝑔2 𝑛 + 9𝑛 – 𝑛2 + 5𝑛 + 7 – 𝑛3
  • 4.
    Problem 1 • Givetight asymptotic bounds for 𝑇(𝑛) in each of the following recurrences. a. 𝑇 𝑛 = 𝑇 𝑛 − 1 + 𝑛 b. 𝑇 𝑛 = 𝑇 𝑛 2 + 𝑇 𝑛 4 + 𝑇 𝑛 8 + 𝑛 c. 𝑇 𝑛 = 𝑇 9𝑛 10 + 𝑛 d. 𝑇 𝑛 = 16𝑇 𝑛 4 + 𝑛2 e. 𝑇 𝑛 = 7𝑇 𝑛 2 + 𝑛2
  • 5.
    Problem 1 • Givetight asymptotic bounds for 𝑇(𝑛) in each of the following recurrences. a. 𝑇 𝑛 = 𝑇 𝑛 − 1 + 𝑛 Lower bound (Ω): 𝑇 𝑛 ≥ 𝑐𝑛2 𝑓𝑜𝑟 𝑠𝑜𝑚𝑒 𝑐 > 0 𝑇 𝑛 ≥ 𝑐 𝑛 − 1 2 + 𝑛 = 𝑐𝑛2 − 2𝑐𝑛 + 𝑐 + 𝑛 ≥ 𝑐𝑛2 𝑡𝑟𝑢𝑒 𝑖𝑓 0 < 𝑐 < 1 2 𝑎𝑛𝑑 𝑛 ≥ 0
  • 6.
    Problem 1 • Givetight asymptotic bounds for 𝑇(𝑛) in each of the following recurrences. a. 𝑇 𝑛 = 𝑇 𝑛 − 1 + 𝑛 Upper bound (O): 𝑇 𝑛 ≤ 𝑐𝑛2 𝑓𝑜𝑟 𝑠𝑜𝑚𝑒 𝑐 > 0 𝑇 𝑛 ≤ 𝑐 𝑛 − 1 2 + 𝑛 = 𝑐𝑛2 − 2𝑐𝑛 + 𝑐 + 𝑛 ≤ 𝑐𝑛2 𝑡𝑟𝑢𝑒 𝑖𝑓 𝑐 = 1 𝑎𝑛𝑑 𝑛 ≥ 1
  • 7.
    Problem 1 • Givetight asymptotic bounds for 𝑇(𝑛) in each of the following recurrences. b. 𝑇 𝑛 = 𝑇 𝑛 2 + 𝑇 𝑛 4 + 𝑇 𝑛 8 + 𝑛
  • 8.
    Problem 1 • Givetight asymptotic bounds for 𝑇(𝑛) in each of the following recurrences. b. 𝑇 𝑛 = 𝑇 𝑛 2 + 𝑇 𝑛 4 + 𝑇 𝑛 8 + 𝑛 Upper bound (O): 𝑇 𝑛 ≤ 𝑐𝑛 2 + 𝑐𝑛 4 + 𝑐𝑛 8 + 𝑛 = 7𝑐𝑛 8 + 𝑛 ≤ 𝑐𝑛 𝑡𝑟𝑢𝑒 𝑖𝑓 𝑐 ≥ 8
  • 9.
    Problem 1 • Givetight asymptotic bounds for 𝑇(𝑛) in each of the following recurrences. b. 𝑇 𝑛 = 𝑇 𝑛 2 + 𝑇 𝑛 4 + 𝑇 𝑛 8 + 𝑛 Lower bound (Ω): 𝑇 𝑛 ≥ 𝑐𝑛 2 + 𝑐𝑛 4 + 𝑐𝑛 8 + 𝑛 = 7𝑐𝑛 8 + 𝑛 ≥ 𝑐𝑛 𝑡𝑟𝑢𝑒 𝑖𝑓 0 < 𝑐 ≤ 8
  • 10.
  • 11.
    Problem 1 • Givetight asymptotic bounds for 𝑇(𝑛) in each of the following recurrences. c. 𝑇 𝑛 = 𝑇 9𝑛 10 + 𝑛 𝑎 = 1, 𝑏 = 10 9 , 𝑓 𝑛 = 𝑛 = 𝛺 n log10 9 1+1 𝑝𝑜𝑠𝑠𝑖𝑏𝑙𝑦 𝑐𝑎𝑠𝑒 3, 𝑙𝑒𝑡′ 𝑠 𝑐ℎ𝑒𝑐𝑘 𝑐 1 9𝑛 10 ≤ 𝑐𝑛 ℎ𝑜𝑙𝑑𝑠 𝑓𝑜𝑟 𝑐 = 9 10 ≤ 1 𝑐𝑒𝑟𝑡𝑎𝑖𝑛𝑙𝑦 𝑐𝑎𝑠𝑒 3: 𝑇 𝑛 = Θ(n)
  • 12.
    Problem 1 • Givetight asymptotic bounds for 𝑇(𝑛) in each of the following recurrences. d. 𝑇 𝑛 = 16𝑇 𝑛 4 + 𝑛2 𝑎 = 16, 𝑏 = 4, 𝑓 𝑛 = 𝑛2 𝑛2=Θ 𝑛log4 16 , 𝑐𝑎𝑠𝑒 2: 𝑇 𝑛 = Θ(𝑛2 log2 𝑛) e. 𝑇 𝑛 = 7𝑇 𝑛 2 + 𝑛2 𝑎 = 7, 𝑏 = 2, 𝑓 𝑛 = 𝑛2 𝑛2 = 𝑂 𝑛log2 7−ε , 𝑐𝑎𝑠𝑒 1: 𝑇 𝑛 = Θ(𝑛log2 7)
  • 13.
    Problem 2 • Hat-checkproblem • Each of n customers gives a hat to a hat- check person at a restaurant. • The hat-check person gives the hats back to the customers in a random order. • What is the expected number of customers that get back their own hat?
  • 14.
    Problem 2 • CountingSolution: – Count number of fixed points in all n! possible permutations – Divide by n! To find average number – Answer is 1 • With Indicator Random Variables: – 𝑋𝑖 = 𝐼 𝑐𝑢𝑠𝑡𝑜𝑚𝑒𝑟 𝑖 𝑔𝑒𝑡𝑠 ℎ𝑖𝑠 𝑜𝑤𝑛 ℎ𝑎𝑡 𝑓𝑜𝑟 𝑖 = 1,2, … , 𝑛 – 𝑋 = 𝑋1 + 𝑋2 + ⋯ + 𝑋 𝑛 𝑛𝑢𝑚𝑏𝑒𝑟 𝑜𝑓 𝑐𝑢𝑠𝑡𝑜𝑚𝑒𝑟𝑠 𝑡ℎ𝑎𝑡 𝑔𝑒𝑡 𝑡ℎ𝑒𝑖𝑟 𝑜𝑤𝑛 ℎ𝑎𝑡
  • 15.
    Problem 2 • 𝐸𝑋 = 𝐸 𝑖=1 𝑛 𝑋𝑖 = 𝑖=1 𝑛 𝐸[𝑋𝑖] • 𝐸 𝑋𝑖 = 1 𝑛 • 𝐸 𝑋 = 𝑖=1 𝑛 1 𝑛 = 1
  • 16.
    Problem 3 • Searchingfor a value x in an unsorted array A consisting of n elements. • Pick a random index i into A. • If A[i ] = x, then we terminate; • Otherwise, we continue the search by picking a new random index into A. • We continue picking random indices into A until we find x or until we have checked every element of A.
  • 17.
    Problem 3 • Whatis the expected number of indices into A must be picked before x is found? (There is exactly one x in A) • What if there are 𝑘 ≥ 1 x values in A? • If there is no x in A, what is the expected number of indices into A that must be picked before search terminates?