lec5-order_statistics.pptx
- Количество слайдов: 13
Order statistics Выбрать i-й наименьший из n элементов (rank i). • i = 1: minimum; • i = n: maximum; • i = (n+1)/2 или (n+1)/2 : median. Простейший способ : Отсортировать, i-й. Worst-case = Θ(n lg n) + Θ(1) = Θ(n lg n), September 28, 2005 Copyright © 2001 -5 by Erik D. Demaine and Charles E. Leiserson L 6. 2
Randomized divide-andconquer algorithm RAND-SELECT(A, p, q, i) ⊳ ith smallest of A[p. . q] if p = q then return A[ p] r ← RAND-PARTITION(A, p, q) k←r-p+ 1 ⊳ k = rank(A[r]) if i = k then return A[ r] if i < k then return RAND-SELECT( A, p, r - 1, i ) else return RAND-SELECT(A, r + 1, q, i - k) k ≤ A[r] ≥ A[r] p r q September 28, 2005 Copyright © 2001 -5 by Erik D. Demaine and Charles E. Leiserson L 6. 3
Пример Выбрать 7 -й по величине: 6 10 13 pivot 5 8 Партиционирование: 2 5 3 6 8 3 2 11 i=7 13 10 11 k=4 Выбрать 7 - 4 = 3 -й по величине рекурсивно. September 28, 2005 Copyright © 2001 -5 by Erik D. Demaine and Charles E. Leiserson L 6. 4
Соображения Lucky: T(n) = T(9 n/10) + Θ(n) = Θ(n) Unlucky: T(n) = T(n - 1) + Θ(n) = Θ(n 2) nlog 10/9 1 =n 0 =1 CASE 3 Арифметическая последовательность Хуже сортировки! September 28, 2005 Copyright © 2001 -5 by Erik D. Demaine and Charles E. Leiserson L 6. 5
Итого • Быстро: линейное время (ожидаемое). • Хорошо на практике. • Но в худшем случае очень плох: Θ(n 2). Q. Существует ли лучший алгоритм? A. Да, Blum, Floyd, Pratt, Rivest, и Tarjan [1973]. Идея: Генерировать хороший pivot рекурсивно. September 28, 2005 Copyright © 2001 -5 by Erik D. Demaine and Charles E. Leiserson L 6. 18
Worst-case linear-time order statistics SELECT(i, n) 1. Divide the n elements into groups of 5. Find the median of each 5 -element group by rote. 2. Recursively SELECT the median x of the n/5 group medians to be the pivot. 3. Partition around the pivot x. Let k = rank(x). 4. if i = k then return x Same as elseif i < k then recursively SELECT the ith RANDsmallest element in the lower part SELECT else recursively SELECT the (i-k)th smallest element in the upper part September 28, 2005 Copyright © 2001 -5 by Erik D. Demaine and Charles E. Leiserson L 6. 19
Choosing the pivot September 28, 2005 Copyright © 2001 -5 by Erik D. Demaine and Charles E. Leiserson L 6. 20
Choosing the pivot 1. Divide the n elements into groups of 5. September 28, 2005 Copyright © 2001 -5 by Erik D. Demaine and Charles E. Leiserson L 6. 21
Choosing the pivot 1. Divide the n elements into groups of 5. Find lesser the median of each 5 -element group by rote. greater September 28, 2005 Copyright © 2001 -5 by Erik D. Demaine and Charles E. Leiserson L 6. 22
Choosing the pivot x 1. Divide the n elements into groups of 5. Find lesser the median of each 5 -element group by rote. 2. Recursively SELECT the median x of the n/5 group medians to be the pivot. greater September 28, 2005 Copyright © 2001 -5 by Erik D. Demaine and Charles E. Leiserson L 6. 23
Analysis x lesser At least half the group medians are ≤ x, which is at least n/5 /2 = n/10 group medians. greater September 28, 2005 Copyright © 2001 -5 by Erik D. Demaine and Charles E. Leiserson L 6. 24
Analysis (Assume all elements are distinct. ) x lesser At least half the group medians are ≤ x, which is at least n/5 /2 = n/10 group medians. • Therefore, at least 3 n/10 elements are ≤ x. greater September 28, 2005 Copyright © 2001 -5 by Erik D. Demaine and Charles E. Leiserson L 6. 25
Analysis (Assume all elements are distinct. ) x lesser At least half the group medians are ≤ x, which is at least n/5 /2 = n/10 group medians. • Therefore, at least 3 n/10 elements are ≤ x. • Similarly, at least 3 n/10 elements are ≥ x. greater September 28, 2005 Copyright © 2001 -5 by Erik D. Demaine and Charles E. Leiserson L 6. 26
lec5-order_statistics.pptx