Sieve Technique - Design and Analysis - Study Notes, Study notes of Digital Systems Design

Sieve Technique, Applying the Sieve to Selection, Partitioned about the pivot x, Selection Algorithm, Sieve example, Pivot element, Constant fraction, Recursive solutions are the key points in this study notes file.

Typology: Study notes

2011/2012

Uploaded on 11/03/2012

ankitay
ankitay 🇮🇳

4.4

(50)

106 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Lecture No. 10
3.2.1 Sieve Technique
The reason for introducing this algorithm is that it illustrates a very important special case
of divide-and-conquer, which I call the sieve technique. We think of divide-and-conquer
as breaking the problem into a small number of smaller subproblems, which are then
solved recursively. The sieve technique is a special case, where the number of
subproblems is just 1.
The sieve technique works in phases as follows. It applies to problems where we are
interested in finding a single item from a larger set of n items. We do not know which
item is of interest, however after doing some amount of analysis of the data, taking say
Θ(nk) time, for some constant k, we find that we do not know what the desired the item
is, but we can identify a large enough number of elements that cannot be the desired
value, and can be eliminated from further consideration. In particular “large enough”
means that the number of items is at least some fixed constant fraction of n (e.g. n/2,
n/3). Then we solve the problem recursively on whatever items remain. Each of the
resulting recursive solutions then do the same thing, eliminating a constant fraction of the
remaining set.
3.2.2 Applying the Sieve to Selection
To see more concretely how the sieve technique works, let us apply it to the selection
problem. We will begin with the given array A[1..n]. We will pick an item from the
array, called the pivot element which we will denote by x. We will talk about how an item
is chosen as the pivot later; for now just think of it as a random element of A.
We then partition A into three parts.
1. A[q] contains the pivot element x,
2. A[1..q - 1] will contain all the elements that are less than x and
3. A[q + 1..n] will contains all elements that are greater than x.
Within each sub array, the items may appear in any order. The following figure shows a
partitioned array:
Figure 3.4: A[p..r] Partitioned about the pivot x
Docsity.com
pf2

Partial preview of the text

Download Sieve Technique - Design and Analysis - Study Notes and more Study notes Digital Systems Design in PDF only on Docsity!

Lecture No. 10

3.2.1 Sieve Technique

The reason for introducing this algorithm is that it illustrates a very important special case of divide-and-conquer, which I call the sieve technique. We think of divide-and-conquer as breaking the problem into a small number of smaller subproblems, which are then solved recursively. The sieve technique is a special case, where the number of subproblems is just 1.

The sieve technique works in phases as follows. It applies to problems where we are interested in finding a single item from a larger set of n items. We do not know which item is of interest, however after doing some amount of analysis of the data, taking say Θ(nk) time, for some constant k, we find that we do not know what the desired the item is, but we can identify a large enough number of elements that cannot be the desired value, and can be eliminated from further consideration. In particular “large enough” means that the number of items is at least some fixed constant fraction of n (e.g. n/2, n/3). Then we solve the problem recursively on whatever items remain. Each of the resulting recursive solutions then do the same thing, eliminating a constant fraction of the remaining set.

3.2.2 Applying the Sieve to Selection

To see more concretely how the sieve technique works, let us apply it to the selection problem. We will begin with the given array A[1..n]. We will pick an item from the array, called the pivot element which we will denote by x. We will talk about how an item is chosen as the pivot later; for now just think of it as a random element of A. We then partition A into three parts.

  1. A[q] contains the pivot element x,
  2. A[1..q - 1] will contain all the elements that are less than x and
  3. A[q + 1..n] will contains all elements that are greater than x.

Within each sub array, the items may appear in any order. The following figure shows a partitioned array:

Figure 3.4: A[p..r] Partitioned about the pivot x

Docsity.com

3.2.3 Selection Algorithm

It is easy to see that the rank of the The rank of the pivot x is q - p + 1 in A[p..r]. Let rank x = q - p + 1. If k = rank x then the pivot is kth smallest. If k < rank x then search A[p..q - 1] recursively. If k > rank x then search A[q + 1..r] recursively. Find element of rank (k - q) because we eliminated q smaller elements in A.

SELECT( array A, int p, int r, int k) 1 if (p = r) 2 then return A[p] 3 else x ← CHOOSE PIVOT(A, p, r) 4 q ← PARTITION(A, p, r, x) 5 rank x ← q - p + 1 6 if k = rank x 7 then return x 8 if k < rank x 9 then return SELECT(A, p, q - 1, k) 10 else return SELECT(A, q + 1, r, k - q)

Example: select the 6 th smallest element of the set {5, 9, 2, 6, 4, 1, 3, 7}

Figure 3.5: Sieve example: select 6 th smallest element

Docsity.com