Docsity

Problem Sheet for Sorting and Searching - COMS21103, Study notes of Introduction to Computers

Toggle sidebar
bg1
Data Structures and Algorithms - COMS21103
Sorting and Searching
Problem Sheet
1. Let Abe a sorted array with nelements, and xan element in the array.
(a) Explain how to perform the operation find(x) in time O(log k), where kis the index of the
cell containing x(that is A[k] = x). If k∈Θ(n), then a standard binary search would do, but
think what to do if kis much smaller than n. Try first narrowing the search to a section of
the array that definitely contains x. You should not use any other data structures.
(b) Improve your find(x) operation so that it takes O(logmin{k , n āˆ’k}) time.
2. Consider the ā€œhistogram pattern matching problemā€. The input is a pattern string pof length m
and a text string tof length n, where m < n. He asks you to devise an algorithm that will tell
whether there is some substring of tof length mwhose histogram matches that of p. The output
should simply be a boolean value indicating whether a histogram match was found anywhere in t.
The histogram of a string simply counts the number of times each letter in the string occurs. For
example, the histogram of the string ababb is (a, 2),(b, 3). Therefore, ababb has a histogram match
with string abbaab starting at position 2. To see why this is, notice that the histogram of abbaa
is (a, 3),(b, 2) so there is no match starting at position 1. However, the histogram of bbaab is
(a, 2),(b, 3) so there is a match starting at position 2. Assume all strings are indexed from 1.
(a) Assuming that the alphabet size for both the pattern and text is binary (i.e. the only charac-
ters are aand b), give an efficient algorithm to solve this problem. You should present your
solution in pseudocode and also explain clearly how it works. You may assume a function
count(c, s) that will return the number of occurrences of character cin string sin linear time.
(b) Give the time complexity of your histogram pattern matching algorithm and explain briefly
why it is so.
(c) Now consider strings over non-binary alphabets. I.e. the characters in the string are no longer
limited to aand bonly. Give a detailed description of an efficient algorithm to solve this new
problem and derive its time complexity. Explain clearly how your algorithm works and what
effect the alphabet size (usually denoted |Ī£|) has, if any, on the overall running time.
3. Using a decision tree or otherwise, derive the Ω(nlog n) lower bound for sorting in the comparison
model.
4. Consider searching for a given integer in a sorted array of nintegers. Find and prove tight upper
and lower bounds for the problem.
1

Partial preview of the text

Download Problem Sheet for Sorting and Searching - COMS21103 and more Study notes Introduction to Computers in PDF only on Docsity!

Data Structures and Algorithms - COMS

Sorting and Searching

Problem Sheet

  1. Let A be a sorted array with n elements, and x an element in the array.

(a) Explain how to perform the operation find(x) in time O(log k), where k is the index of the cell containing x (that is A[k] = x). If k ∈ Θ(n), then a standard binary search would do, but think what to do if k is much smaller than n. Try first narrowing the search to a section of the array that definitely contains x. You should not use any other data structures. (b) Improve your find(x) operation so that it takes O(log min{k, n āˆ’ k}) time.

  1. Consider the ā€œhistogram pattern matching problemā€. The input is a pattern string p of length m and a text string t of length n, where m < n. He asks you to devise an algorithm that will tell whether there is some substring of t of length m whose histogram matches that of p. The output should simply be a boolean value indicating whether a histogram match was found anywhere in t. The histogram of a string simply counts the number of times each letter in the string occurs. For example, the histogram of the string ababb is (a, 2), (b, 3). Therefore, ababb has a histogram match with string abbaab starting at position 2. To see why this is, notice that the histogram of abbaa is (a, 3), (b, 2) so there is no match starting at position 1. However, the histogram of bbaab is (a, 2), (b, 3) so there is a match starting at position 2. Assume all strings are indexed from 1.

(a) Assuming that the alphabet size for both the pattern and text is binary (i.e. the only charac- ters are a and b), give an efficient algorithm to solve this problem. You should present your solution in pseudocode and also explain clearly how it works. You may assume a function count(c, s) that will return the number of occurrences of character c in string s in linear time. (b) Give the time complexity of your histogram pattern matching algorithm and explain briefly why it is so. (c) Now consider strings over non-binary alphabets. I.e. the characters in the string are no longer limited to a and b only. Give a detailed description of an efficient algorithm to solve this new problem and derive its time complexity. Explain clearly how your algorithm works and what effect the alphabet size (usually denoted |Ī£|) has, if any, on the overall running time.

  1. Using a decision tree or otherwise, derive the Ī©(n log n) lower bound for sorting in the comparison model.
  2. Consider searching for a given integer in a sorted array of n integers. Find and prove tight upper and lower bounds for the problem.