12-13-01ADA11(减治法-减可变规模算法)

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

2012-2013-01 《Design and Analysis of Algorithm》
SCUN
2012-12-27
5
Direct Idea Algorithm
FindkthSmallestBF(A[0…n-1], k) //Input: An array A of orderable elements and integer k (1≤k≤n) //Output: The value of the kth smallest element in A[0…n-1] for i 0 to k-1 smallestLocation n-1 for j n-2 to i if A[j] < A[smallestLocation] smallestLocation = j end if end for swap(A[i], A[smallestLocation]) end for Return A[k-1]
Classical selection problem
• Find the maximum or minimum elements or the both
• Find the median element – The median is used in statistics as a measure of an average value of a sample. In fact, it is a better (more robust) indicator than the mean, which is used for the same purpose. – Example: 4, 1, 10, 9, 7, 12, 8, 2, 15 median = ? • Generally, find the kth smallest element
2012-2013-01 《Design and Analysis of Algorithm》
SCUN
2012-12-27
3
Selection Problem

What’s selection problem?
• Is to find a particular element of an unsorted list
2012-2013-01 《Design and Analysis of Algorithm》 SCUN
2012-12-27 4
Find the

th element k
(direct idea)
To find the kth smallest element, we could move the k smallest elements to the start of the list and then the kth smallest will be in location list[k] This involves finding the smallest element and moving it to the first place, then finding the second smallest element and moving it to the second place and so on

The game of Nim
Goals of the Lecture

At the end of this lecture, you should
• Understand what’s selection problem and master the partition based algorithm for finding the kth smallest elements • Master the basic idea of interpolation search and know the differences between binary search and interpolation search • Know how to solve the game of nim
2012-12-27 11
Worst Case Analysis
According to the Partition algorithm, if the list is divided by n-1 to 0, this will lead to the worst case. The Partition Algorithm will do n-1 comparisons for a list of size n

We need to do k passes, thus giving
T (n)
(n i) n k
i 1
wk.baidu.com
k

k ( k 1) 2
O (n k )
2012-2013-01 《Design and Analysis of Algorithm》
SCUN
2012-12-27
2012-2013-01 《Design and Analysis of Algorithm》 SCUN
2012-12-27 6
Direct Idea Algorithm Analysis
On the first pass, we compare the last element to the rest, doing n - 1 comparisons On the second pass, we compare the last element to the rest (except the first element), doing n - 2 comparisons, and so on

So we can get the number of comparison recurrence relation
TW ( n ) TW ( n 1) n 1

The above recurrence works out to TW(n) = (n+2)*k - k*(k+1)/2 = O(n*k)
7
Algorithms based on Variable-Size-Decrease Technique
A faster algorithm is based on using the quicksort-like partition of the list. Let s be a split position obtained by a partition: all are ≤ A[s] all are ≥ A[s] s Assuming that the list is indexed from 1 to n: If s = k, the problem is solved; if s > k, look for the k-th smallest elem. in the left part; if s < k, look for the (k-s)th smallest elem. in the right part. Note: The same idea can be implemented without recursion as well. For the non recursive version, we need not even adjust the value of k but just continue until s = k.
Chapter 5
Decrease and Conquer (III)
Variable-Size-Decrease Algorithms

Selection Problem th The k smallest element find problem Search Problem Interpolation Search Combinatory Problem
2012-2013-01 《Design and Analysis of Algorithm》 SCUN
2012-12-27 10
Best Case Analysis
According to the Partition algorithm, if the list is divided in half each time, this will lead to the best case. The Partition Algorithm will do n-1 comparisons for a list of size n So we can get the number of comparison recurrence relation n
Solution: median is 8
2012-2013-01 《Design and Analysis of Algorithm》 SCUN
2012-12-27 9
Algorithms based on variable-size-decrease technique
FindkthSmallestVSD_NR(A[0…n-1], k) //Input: An array A of orderable elements and integer k (1≤k≤n) //Output: The value of the kth smallest element in A[0…n-1] l ← 0; r ← n-1 Partition While l ≤ r algorithm //j ← partition2(A, l , r) p ←A[l]; j ← l for i ← l +1 to r j ← partition2(A, l , r) if A[i] ≤ p j←j+1 Thinking: if j≠ i swap(A[j], A[i]) swap(A[l], A[j]) How to rewrite if j > k-1 r ← j-1 this algorithm in else if j < k-1 l ← j+1 recursive format? else return A[k-1]

TB ( n ) TB ( ) n 1 2 T B (1) 1

The above recurrence works out to TB(n) = 2n – 1 + logn = (n)
2012-2013-01 《Design and Analysis of Algorithm》 SCUN
2012-2013-01 《Design and Analysis of Algorithm》 SCUN
8
Tracing the Median / Selection Algorithm
Example: 4 1 10 9 7 12 8 2 15 Here: n = 9, k = 9/2 = 5 array index 1 4 4 2 2 1 1 1 3 10 2 4 4 9 9 9 9 9 8 8 7 5 7 7 7 7 7 7 7 8 6 12 12 12 12 8 9 7 8 8 2 8 10 8 10 8 10 12 10 12 10 9 15 15 15 --- s=3 < k=5 15 15 15 --- s=6 > k=5 --- s=k=5
2012-2013-01 《Design and Analysis of Algorithm》 SCUN
2012-12-27 12
Average-Case Analysis
In each patition, there could be n places for the pivot for a list of size n, We will consider each of these to be equivalent and so will give each a probability of 1/n The Partition Algorithm will do n-1 comparisons for a list of size n So we can get
相关文档
最新文档