Lower Bounds Best-, Average-, and Worst-Case Time Complexity - - PDF document

lower bounds
SMART_READER_LITE
LIVE PREVIEW

Lower Bounds Best-, Average-, and Worst-Case Time Complexity - - PDF document

Lower Bounds Best-, Average-, and Worst-Case Time Complexity Ex. Insertion sort of x 1 , x 2 , , x n . For i = 2, 3, , n , insert x i into x 1 , x 2 , , x i 1 such that these i data items are sorted.


slide-1
SLIDE 1

1

Lower Bounds

  • Best-, Average-, and Worst-Case Time

Complexity

  • Ex. Insertion sort of x1, x2, …, xn.

For i = 2, 3, …, n, insert xi into x1, x2, …, xi−

− − −1

such that these i data items are sorted. input : 7, 5, 1, 4, 3, 2, 6 i = 2 : 5, 7 i = 3 : 1, 5, 7 i = 4 : 1, 4, 5, 7 i = 5 : 1, 3, 4, 5, 7 i = 6 : 1, 2, 3, 4, 5, 7 i = 7 : 1, 2, 3, 4, 5, 6, 7

slide-2
SLIDE 2

2

T(n) : the number of comparisons made best case : T(n) = O(n) worst case : T(n) = O(n2) average case : T(n) = O(n2) Consider the insertion of xi. P(k) : the probability of making k comparisons ⇒ P(1) = P(2) = … = P(i − − − − 2) = 1 i P(i − − − − 1) = 2 i ⇒ the average number of comparisons for xi = (1 + 2 + … + (i − − − − 2)) × × × × 1 i + (i − − − − 1) × × × × 2 i = i

1 2

+

− − − 1 i The average number of comparisons for x2, x3, …, xn is equal to

( ) n i

i i

− =

2

1 1 2

+

= O(n2).

slide-3
SLIDE 3

3

  • Ex. Binary search of a1, a2, …, an.

Assume n = 2k − − − − 1. T(n) : the number of comparisons made best case : T(n) = O(1) worst case : T(n) = O(log n) average case : T(n) = O(log n) P(i) : the probability of making i comparisons for a successful search ⇒ P(i) =

i

n

−1

2

, for i = 1, 2, …, k The average number of comparisons for a successful search is equal to

( ) i k i

n

i

− =

1 1

2

× × × ×

= n

1

×

× × × (2k(k − − − − 1) + 1) = O(log n).

slide-4
SLIDE 4

4

(

( ) k i i

i

− =

1 1

2

× × × ×

= 2k(k −

− − − 1) + 1 can be proved by induction on k) There are k = O(log n) comparisons for each unsuccessful search. Exercise 1. Analyze the time complexity of the quick sort in the best, average, and worst cases. (refer to page 32 of the textbook)

slide-5
SLIDE 5

5

  • Lower Bound for a Problem

A problem has a lower bound of Ω

Ω Ω Ω(g(n)).

⇒ ⇒ ⇒ ⇒ Any algorithm that can solve it takes

Ω Ω Ω Ω(g(n)) time.

For example, sorting n data items requires

Ω Ω Ω Ω(n log n) time.

Unless stated otherwise, “lower bound” means “lower bound in the worst case”. For a problem, if the time complexity of an algorithm matches a lower bound, then the algorithm is time optimal and the lower bound is tight. Otherwise (if the lower bound is lower than the time complexity of the algorithm), the lower bound or the algorithm can be improved.

slide-6
SLIDE 6

6

  • Lower Bound by Comparison Tree

The method of comparison tree is applicable to comparison-based algorithms which make comparisons among input data items. Most of sorting (exclusive of radix sort and bucket sort), searching, selection, and merging algorithms are comparison-based. The execution of a comparison-based algorithm can be described by a comparison tree, and the tree depth is the greatest number of comparisons, i.e., the worst-case time complexity. ⇒ ⇒ ⇒ ⇒ The minimal tree depth of all possible comparison trees is a lower bound.

slide-7
SLIDE 7

7

  • Ex. Sequential search and binary search of

A(1), A(2), …, A(n) for x.

: (1) x A

...

Failure Failure Failure Failure : (2) x A : ( ) x A n 1 : ( ) 2 n x A +       1 : ( ) 4 n x A +       3 1 : ( ) 4 n x A +       1 : ( 1) 2 n x A +   −     1 : ( 1) 2 n x A +   +     : (1) x A : ( ) x A n

... ... ... ...

Failure Failure Failure Failure Failure Failure Failure Failure

⇒ ⇒ ⇒ ⇒ Searching has a lower bound of Ω

Ω Ω Ω(log n).

Since binary search takes O(log n) time, the lower bound is tight.

slide-8
SLIDE 8

8

  • Ex. Sorting a1, a2, …, an.

Straight insertion sort of a1, a2, a3 : Sorting 3, 1, 2 : 3 → → → → 3, 1 → → → → 1, 3 → → → → 1, 3, 2 → → → → 1, 2, 3 (a1 : a2) (a2 : a3) (a1 : a2) Sorting 2, 1, 3 : 2 → → → → 2, 1 → → → → 1, 2 → → → → 1, 2, 3 (a1 : a2) (a2 : a3)

slide-9
SLIDE 9

9

Bubble sort of a1, a2, a3 : Sorting 3, 1, 2 : 3, 1, 2 → → → → 1, 3, 2 → → → → 1, 2, 3 (a1 : a2) (a2 : a3) (a1 : a2) Sorting 2, 1, 3 : 2, 1, 3 → → → → 1, 2, 3 (a1 : a2) (a2 : a3)

slide-10
SLIDE 10

10

  • ne-to-one

correspondence n! leaf nodes ← ← ← ←                  → → → → n! possible input sequences When the comparison tree is balanced, the tree depth is    log n!    = Ω

Ω Ω Ω(n log n) (refer to page 47 of the

textbook), which is minimum. Heap sort takes O(n log n) time. ⇒ ⇒ ⇒ ⇒ Ω

Ω Ω Ω(n log n) is tight for sorting.

The worst-case time complexity of sorting was considered above. In what follows, the average- case time complexity of sorting is considered.

slide-11
SLIDE 11

11

Average time complexity of a sorting algorithm can be estimated as L n!, where L is the total length in the comparison tree from the root to each of the leaf nodes. Let Lmin be the minimal L of all comparison trees ⇒ ⇒ ⇒ ⇒ L n

min

! is an average-case lower bound for sorting Lmin occurs when the comparison tree is balanced. For example, The left tree has L = 13, and the right tree has L = 12 (= Lmin for n = 9).

slide-12
SLIDE 12

12

Suppose that T is a balanced comparison tree with n! leaf nodes. Let N = n! + (n! − − − − 1) = 2(n!) − − − − 1. ⇒ ⇒ ⇒ ⇒ T is of height h =    log N   . Assume that there are x1 leaf nodes of depth h − − − − 1 and x2 leaf nodes of depth h. ⇒ ⇒ ⇒ ⇒ x1 + x2 = n! (A) x1 + 1 2 x2 = 2h−

− − −1 (x2 is even)

(B) (A), (B) ⇒ ⇒ ⇒ ⇒ x1 = 2h

− − − n!, x2 = 2(n! − − − − 2h−

− − −1)

⇒ ⇒ ⇒ ⇒ Lmin = (2h

− − − n!)(h − − − − 1) + 2(n! − − − − 2h−

− − −1)h

= (h + 1)n! − − − − 2h Since log N − − − − 1 < h ≤ ≤ ≤ ≤ log N, we have Lmin > (log N)n! − − − − 2log N = (log N − − − − 2)n! + 1. ⇒ ⇒ ⇒ ⇒ L n

min

!

= Ω

Ω Ω Ω(n log n)

slide-13
SLIDE 13

13

For example, n = 3, N = 11, h = 3, x1 = 2, and x2 = 4. ⇒ ⇒ ⇒ ⇒ x1 + 1 2 x2 = 22. Quick sort takes O(n log n) time in the average case. ⇒ ⇒ ⇒ ⇒ Ω Ω Ω Ω(n log n) is the tight average-case lower bound for sorting. Since the heap sort takes O(n log n) time in the worst case, it also takes O(n log n) time in the average case.

slide-14
SLIDE 14

14

  • Ex. Selection from n data items.

Let L(n) denote a lower bound for selecting the greatest data item from a1, a2, …, an. Any comparison tree has leaf nodes labeled with a1, a2, …, an, and each root-to-ai path represents a process to recognize that ai is the greatest element. Since at least n − − − − 1 comparisons are required to find the greatest data item, each root-to-leaf path has length ≥ ≥ ≥ ≥ n − − − − 1. ⇒ ⇒ ⇒ ⇒ L(n) = n − − − − 1 Exercise 2. Let Lk(n) denote a lower bound for selecting the k greatest data items from a1, a2, …, an. Prove that for 2 ≤ ≤ ≤ ≤ k ≤ ≤ ≤ ≤ n, Lk(n) ≥ ≥ ≥ ≥ n − − − − k +    log n(n − − − − 1) … (n − − − − k + 2)   . (Refer to Section 10.1.3 on page 464 of

  • Ref. (2).)
slide-15
SLIDE 15

15

  • Ex. An n-player tournament.

An 8-player tennis tournament.

C C C C A A B D H H E H G E F

C is the best player. Consider each match a comparison. ⇒ finding the best player is equivalent to finding the greatest data item. According to Exercise 2, finding the first two best players requires at least n − − − − 2 +    log n    matches There is an approach to finding the first two best players with exactly n − − − − 2 +    log n    matches.

slide-16
SLIDE 16

16

Consider the 8-player tennis tournament again. The best player can be found with 7 (= n − − − − 1) matches. The second best player is one of 3 (=    log n   ) candidates, i.e., D, A, and H. ⇒ ⇒ ⇒ ⇒ The second best player can be found with    log n    − − − − 1 matches. Therefore, n − − − − 2 +    log n    is a tight lower bound for finding the first two best players.

slide-17
SLIDE 17

17

  • Lower Bound by a Particular Problem

Instance

  • Ex. Merging two sorted sequences a1 ≤

≤ ≤ ≤ a2 ≤ ≤ ≤ ≤ … ≤ ≤ ≤ ≤ an and b1 ≤ ≤ ≤ ≤ b2 ≤ ≤ ≤ ≤ … ≤ ≤ ≤ ≤ bn. Consider a problem instance with a1 < b1 < a2 < b2 < … < an < bn. When a1 < b1 < a2 < b2 < … < ai < bi is obtained, bi+1 must be compared with ai+1 and ai+2 before it is placed properly. ⇒ a lower bound of 2n − − − − 1 comparisons The merging algorithm, which continuously compares the two currently smallest elements

  • f the two sorted lists and outputs the smaller
  • ne, performs 2n −

− − − 1 comparisons. ⇒ the lower bound is tight

slide-18
SLIDE 18

18

  • Ex. Fault diameter of the hypercube

Hn : the n-dimensional hypercube.

01 00 10 11 1

H1 H2

001 000 010 011 101 100 110 111

H3

Dn−

− − −1 : the (n −

− − − 1)-fault diameter of Hn, which is the maximal diameter of Hn with n − − − − 1 edges removed. Consider the following problem instance.

...

10n-1 0n 01n-1

n − − − − 1 edges removed

slide-19
SLIDE 19

19

The distance between 0n and 01n−

− − −1 is n + 1.

⇒ Dn−

− − −1 ≥

≥ ≥ ≥ n + 1 Between any two distinct nodes of Hn, there are n node-disjoint paths whose maximal length is at most n + 1.

... ... ... ...

⇒ Dn−

− − −1 ≤

≤ ≤ ≤ n + 1 ⇒ Therefore, Dn−

− − −1 = n + 1.

slide-20
SLIDE 20

20

There is an advanced technique, named oracles, for deriving lower bounds. In fact, an oracle (e.g., 籤詩

籤詩 籤詩 籤詩) can be considered a

scenario for a particular problem instance. You are suggested to read Sec. 10.2.4 of Ref. (2) (or

  • L. Hyafil, “Bounds for Selection,” SIAM J. Comput.,
  • vol. 5, no. 1, 1976, pp. 109-114), where an example of

selection is illustrated.

slide-21
SLIDE 21

21

  • Lower Bound by State Transition
  • Ex. Finding the maximum and minimum of a1, a2, …,

an. Let (k, k(+), k(−

− − −), k(± ± ± ±)) be a state, where

k : the number of ai’s that are not compared yet; k(+) : the number of ai’s that have won but never lost; k(−

− − −) : the number of ai’s that have lost but never

won; k(±

± ± ±) : the number of ai’s that have both won and

lost. The problem is equivalent to the state transition from (n, 0, 0, 0) to (0, 1, 1, n − − − − 2).

slide-22
SLIDE 22

22

Each comparison induces a state transition from (k, k(+), k(−

− − −), k(± ± ± ±)) to one of the following states:

(1) (k − − − − 2, k(+)

+ 1, k(− − − −) + 1, k(± ± ± ±));

(2) (k − − − − 1, k(+), k(−

− − −) + 1, k(± ± ± ±)) or (k −

− − − 1, k(+) + 1, k(−

− − −), k(± ± ± ±))

  • r (k −

− − − 1, k(+), k(−

− − −), k(± ± ± ±) + 1);

(3) (k, k(+) − − − − 1, k(−

− − −), k(± ± ± ±) + 1);

(4) (k, k(+), k(−

− − −) −

− − − 1, k(±

± ± ±) + 1),

where (1) occurs when k ≥ ≥ ≥ ≥ 2 and two from k are compared; (2) occurs when k ≥ ≥ ≥ ≥ 1 and one from k is compared with one from k(+) or k(−

− − −);

(3) occurs when k(+)

≥ ≥ ≥ 2 and two from k(+) are compared; (4) occurs when k(−

− − −) ≥

≥ ≥ ≥ 2 and two from k(−

− − −) are

compared.

slide-23
SLIDE 23

23

Since the elements of k(±

± ± ±)

come from k(+)

  • r k(−

− − −),

not from k, the quickest way from (n, 0, 0, 0) to (0, 1, 1, n − − − − 2) is as follows. Case 1. n = 2p. (n, 0, 0, 0) → → → →p (0, p, p, 0) → → → →2p−

− − −2 (0, 1, 1, 2p −

− − − 2). (“→ → → →p” means p state transitions.) There are 3p − − − − 2 = (3n/2) − − − − 2 state transitions. Case 2. n = 2p + 1. (n, 0, 0, 0) → → → →p (1, p, p, 0) → → → →1 (0, p, p, 1) → → → →2p−

− − −2

(0, 1, 1, 2p − − − − 1). There are 3p − − − − 1 = (3n/2) − − − − 5/2 state transitions. There is an algorithm that can find the maximum and minimum of n data items with    3n/2    − − − − 2

  • comparisons. (Refer to Sec. 3.3 of Ref. (2))

When n = 2p, (3n/2) − − − − 2 =    3n/2    − − − − 2. When n = 2p + 1, (3n/2) − − − − 5/2 <    3n/2    − − − − 2.

slide-24
SLIDE 24

24

  • Lower Bound by Reduction

A problem P1 reduces to another problem P2, denoted by P1 ∝ ∝ ∝ ∝ P2, if any instance of P1 can be transformed into an instance of P2 such that the solution for P1 can be obtained from the solution for P2. T∝

∝ ∝ ∝ : the reduction time.

T : the time required to obtain the solution for P1 from the solution for P2.

  • Ex. the problem of selection ∝

∝ ∝ ∝ the problem of sorting. T∝

∝ ∝ ∝ : O(1).

T : O(1).

slide-25
SLIDE 25

25

  • Ex. Suppose that S1 and S2 are two sets of n elements

and m elements, respectively. P1 : the problem of determining if S1 ∩ ∩ ∩ ∩ S2 = ∅ ∅ ∅ ∅. P2 : the problem of sorting. T∝

∝ ∝ ∝ : O(n + m).

T : O(n + m). S1 = {a1, a2, …, an}, S2 = {b1, b2, …, bm} : an arbitrary instance of P1. (a1, 1), (a2, 1), …, (an, 1), (b1, 2), (b2, 2), …, (bm, 2) : an instance of P2 created from P1. ⇒ S1 ∩ ∩ ∩ ∩ S2 ≠ ≠ ≠ ≠ ∅ ∅ ∅ ∅ iff the sorted sequence contains two successive elements (ai, 1) and (bj, 2) with ai = bj.

slide-26
SLIDE 26

26

L1 : a lower bound of P1. L2 : a lower bound of P2. ⇒ ⇒ ⇒ ⇒ L1 ≤ ≤ ≤ ≤ T∝

∝ ∝ ∝ + L2 + T

When L1, T∝

∝ ∝ ∝, T are known and T∝ ∝ ∝ ∝ ≤

≤ ≤ ≤ L1, T ≤ ≤ ≤ ≤ L1, we have L1 ≤ ≤ ≤ ≤ L2, i.e., L1 is also a lower bound

  • f P2.
slide-27
SLIDE 27

27

  • Ex. P1 :

the sorting problem. P2 : the convex hull problem. T∝

∝ ∝ ∝ : O(n).

T : O(n). x1, x2, …, xn : an arbitrary instance of P1. (x1, x 2

1 ), (x2, x 2 2 ), …, (xn, n

x 2 ) : an instance of P2

from P1.

(x4, x4

2)

(x3, x3

2)

(x1, x1

2)

(x2, x2

2)

slide-28
SLIDE 28

28

The sorting problem requires Ω Ω Ω Ω(n log n) time. ⇒ The convex hull problem requires Ω Ω Ω Ω(n log n) time. There are O(n log n) time algorithms for the convex hull problem. ⇒ ⇒ ⇒ ⇒ Ω Ω Ω Ω(n log n) is tight for the convex hull problem.

slide-29
SLIDE 29

29

  • Ex. P1 : the sorting problem.

P2 : the Euclidean minimum spanning tree (E-MST) problem. T∝

∝ ∝ ∝ : O(n).

T : O(n). x1, x2, …, xn : an arbitrary instance of P1. (x1, 0), (x2, 0), …, (xn, 0) : an instance of P2 from P1.

...

(x1, 0) (x2, 0) (x3, 0) (xn, 0) (xn-1, 0)

The E-MST problem requires Ω Ω Ω Ω(n log n) time. The E-MST problem can be solved in O(n log n) time. ⇒ ⇒ ⇒ ⇒ Ω Ω Ω Ω(n log n) is tight for the E-MST problem.

slide-30
SLIDE 30

30

Exercise 3. For the example of page 25, prove that P1 has a lower bound of Ω Ω Ω Ω(n log n) by showing P2 ∝ ∝ ∝ ∝ P1. (Refer to Sec. 10.3.2

  • n page 475 of Ref. (2).)

Given n data items at intervals of one time step, the on-line median finding problem is to compute the median of the first i data items at the end of the ith time step, where 1 ≤ ≤ ≤ ≤ i ≤ ≤ ≤ ≤ n. For example, if the input sequence is (7, 15, 3, 17, 8, 11, 5), then the output sequence is (7, 7 or 15, 7, 7 or 15, 8, 8 or 11, 8). Exercise 4. Prove that the on-line median finding problem has a lower bound of Ω Ω Ω Ω(n log n) by showing a reduction from the sorting problem to it. (Refer to Sec. 10.3.3 on page 477 of Ref. (2).)

slide-31
SLIDE 31

31

Project I : Lower Bounds of Some Problems

You are required to survey lower bounds for some problems. You must provide the proofs

  • f lower bounds in your report.