Docsity

Notes on Ordered Search Problems - Design and Analysis of Algorithm | CSCI 212, Study notes of Computer Science

Toggle sidebar
bg1
CSCI 212 - Problem Notes
01/27/09
Ordered Search Problem
Given: A list of nelements a1, a2, . . . , anin a non-decreasing order such that a1≤a2≤. . . ≤an
and a number x.
Objective: Check if xis present in the list, i.e., if ai=xfor some i, 1 ≤i≤n.
We apply binary search to get an O(log n) time algorithm. If xis present in the list, the algorithm
returns the position of x. A position of −1 indicates xis not present. Note: a[i] is the ith ele-
ment in the list, lis the left-most element, and ris the rightmost element. The initial call is BS(1,n).
BS(l,r)
1. If l > r, return −1.
2. m←¥l+r
2¦
3. If x=a[m], return m.
If x < a[m], return BS(l,m−1).
If x > a[m], return BS(m+ 1,r).
After each comparison we eliminate half the elements in our list. In the worst case, comparisons
are made until there is one element remaining. The total number of comparisons is equal to the
number of times that we can halve the list, which is the definition of the log function.
Leftmost Ordered Search Problem
Given: A list of nelements a1, a2, . . . , anin a non-decreasing order such that a1≤a2≤. . . ≤an
and a number x.
Objective: Find the left-most occurrence of xin the list.
We apply binary search to get an O(log n) time algorithm. However, we don’t return immediately
after finding an occurrence of x. Instead, we note the “current” left-most position of xand continue
searching for an occurrence that is further left. Note: crefers to the position of current left-most
occurence. The initial call is LBS(1,n,−1).
LBS(l,r,c)
1. If l > r, return c.
2. m←¥l+r
2¦
3. If x=a[m], return LBS(l,m−1,m).
If x < a[m], return LBS(l,m−1,c).
If x > a[m], return LBS(m+ 1,r,c).
1
pf3
pf4
pf5

Partial preview of the text

Download Notes on Ordered Search Problems - Design and Analysis of Algorithm | CSCI 212 and more Study notes Computer Science in PDF only on Docsity!

CSCI 212 - Problem Notes

Ordered Search Problem

Given: A list of n elements a 1 , a 2 ,... , an in a non-decreasing order such that a 1 ≤ a 2 ≤... ≤ an and a number x. Objective: Check if x is present in the list, i.e., if ai = x for some i, 1 ≤ i ≤ n.

We apply binary search to get an O(log n) time algorithm. If x is present in the list, the algorithm returns the position of x. A position of −1 indicates x is not present. Note: a[i] is the ith ele- ment in the list, l is the left-most element, and r is the rightmost element. The initial call is BS(1,n).

BS(l,r)

  1. If l > r, return −1.
  2. m ←

⌊ (^) l+r 2

  1. If x = a[m], return m. If x < a[m], return BS(l,m − 1). If x > a[m], return BS(m + 1,r).

After each comparison we eliminate half the elements in our list. In the worst case, comparisons are made until there is one element remaining. The total number of comparisons is equal to the number of times that we can halve the list, which is the definition of the log function.

Leftmost Ordered Search Problem

Given: A list of n elements a 1 , a 2 ,... , an in a non-decreasing order such that a 1 ≤ a 2 ≤... ≤ an and a number x. Objective: Find the left-most occurrence of x in the list.

We apply binary search to get an O(log n) time algorithm. However, we don’t return immediately after finding an occurrence of x. Instead, we note the “current” left-most position of x and continue searching for an occurrence that is further left. Note: c refers to the position of current left-most occurence. The initial call is LBS(1,n,−1).

LBS(l,r, c)

  1. If l > r, return c.
  2. m ←

⌊ (^) l+r 2

  1. If x = a[m], return LBS(l,m − 1,m). If x < a[m], return LBS(l,m − 1,c). If x > a[m], return LBS(m + 1,r,c).

Occurrences Ordered Search Problem

Given: A list of n elements a 1 , a 2 ,... , an in a non-decreasing order such that a 1 ≤ a 2 ≤... ≤ an and a number x. Objective: Compute the number of occurrences of x in the list.

We apply binary search algorithms to find the leftmost and rightmost occurrences of x in the list. The difference plus one is returned as the count. RBS() finds the rightmost occurrence and OBS() computes the total number of occurrences.

RBS(l,r, c)

  1. If l > r, return c.
  2. m ←

⌊ (^) l+r 2

  1. If x = a[m], return RBS(m + 1,r,m). If x < a[m], return RBS(l,m − 1,c). If x > a[m], return RBS(m + 1,r,c).

OBS(l,r)

  1. m ←LBS(l,r,−1), n ←RBS(l,r,−1)
  2. If (n − m + 1) < 0, return 0. Otherwise, return n − m + 1.

LBS() and RBS() are both O(logn) time algorithms. Therefore, OBS() is O(logn).

Median Ordered Search Problem

Given: Two lists of n elements a 1 , a 2 ,... , an and b 1 , b 2 ,... , bn in non-decreasing orders such that a 1 ≤ a 2 ≤... ≤ an and b 1 ≤ b 2 ≤... ≤ bn. Objective: Compute the median of the two lists, i.e., the nth smallest element.

MS(l 1 ,r 1 ,l 2 ,r 2 ,n)

  1. m 1 ←

l 1 +r 1 2

, m 2 ←

l 2 +r 2 2

  1. If n = 1, return min(a[m 1 ],b[m 2 ]).
  2. If a[m 1 ] = b[m 2 ], return a[m 1 ].
  3. If a[m 1 ] < b[m 2 ], l 1 = l 1 +

⌊ (^) n 2

, r 2 = m 2.

  1. If a[m 1 ] > b[m 2 ], l 2 = l 2 +

⌊ (^) n 2

, r 1 = m 1.

  1. n ←

⌊ (^) n+ 2

return MS(l 1 ,r 1 ,l 2 ,r 2 ,n)

We compare the medians of each list. Based on the result,

⌊ (^) n 2

elements are removed from each list. The

⌊ (^) n+ 2

smallest element is searched for among the remaining elements. The key observation is that the

⌊ (^) n 2

elements before the lesser median and the

⌊ (^) n 2

elements after the greater median may be discarded. The time complexity is O(log n).

3 log(n)+1^ = 3 · 3 log(n)^ = 3 · nlog(3)^ = O(nlog(3)) < O(n^2 ). When n is an odd number the sub- matrices will not be square. In such cases, the last row and the last column of the original matrix should be searched for x and removed in O(n) time prior to calling MS on the remaining square matrix. The sub-quadratic bound on the running time is not affected.

Infinity Ordered Search Problem

Given: A number x and a list of numbers such that the first n numbers are not equal to infin- ity and are in a non-decreasing order while all remaining numbers are equal to infinity. That is, a 1 ≤ a 2 ≤ · · · ≤ an < ∞ and an+1 = an+2 = · · · = ∞. Objective: Check if x is present in the list.

We find an infinity by iteratively checking the 2ith element beginning with i = 0. We then apply binary search on the list from 1 to 2i.

IS()

  1. i ← 0
  2. While (a[2i] 6 = ∞), i ← i + 1.
  3. Return BS(1,2i).

By construction, the last element that is not equal to infinity is between 2i−^1 and 2i, i.e., 2i−^1 ≤ n < 2 i. Therefore, 2i^ ≤ 2 n and our algorithm has O(logn) time complexity.

Shifted Ordered Search Problem

Given: An integer k and a list of numbers x 1 , x 2 ,... , xn such that xi < xi+1 < xi+2 < · · · < xn < x 1 < x 2 < · · · < xi− 1 for some i, 1 ≤ i ≤ n. Objective: Find the kth smallest element.

We apply a type of binary search to find the smallest element xi in the list. The key observation is that xl < xb l+r 2 c^ implies that xi is to the right of the median xb l+r 2 c

. The index of the kth smallest

element is returned by adding an offset to i.

SBS(l,r)

  1. If (xl < xr) ∨ (l = r) ∨ ((r − l = 1) ∧ (xl < xr)), return (l + k − 1)%n.
  2. If (r − l = 1) ∧ (xr < xl), return (r + k − 1)%n.
  3. m ←

⌊ (^) l+r 2

  1. If xl < xm, return SBS(m + 1,r).
  2. If xl > xm, return SBS(l,m).

We use a special remainder operator that has n%n = n. The time complexity is O(logn) because we do a binary search over the n elements.

Sum Ordered Search Problem

Given: A number x and a list of numbers a 1 , a 2 ,... , an in a non-decreasing order such that a 1 ≤ a 2 ≤ · · · ≤ an. Objective: Find a pair of numbers ai and aj such that ai + aj = x.

With each comparison we eliminate one of the numbers in the list. When the sum is too low (high), the lowest (highest) number is removed.

SS(l,r)

  1. If (r − l) = 0, return (−1,−1).
  2. If (a[l] + a[r]) = x, return (l,r).
  3. If (a[l] + a[r]) < x, return SS(l + 1,r).
  4. If (a[l] + a[r]) > x, return SS(l,r − 1).

The time complexity is O(n) because one comparison is made each call and one element is removed each call until only one element remains.

Mod Problem

Given: Integers B, n, and M. Objective: Compute Bn^ mod M in O(log n) time.

We rely on the property that A × B mod M = (A mod M ) × (B mod M ) mod M. Below, % is the well-known mod operator (i.e., not the previously used special one).

MP(n)

  1. If n = 1, return B.
  2. If n%2 = 0, return (MP( n 2 ))^2.
  3. IF n%2 = 1, return B × (MP( n− 2 1 ))^2.

The time complexity is O(logn) because there is one comparison for each recursive call until the exponent is reduced (by reducing by one half each time) to one.