Given a sorted array of distinct integersA[1,...,n] , you want to find out whether there is an indexi for which A[i]=i. Give a divide-and-conquer algorithm that runs in time O(logn).

Short Answer

Expert verified

By using binary search, only certain that there is no such value to the left of the middle element if array[mid]<=startindex, where mid is index of middle element, and start index is the start of the array. A divide-and-conquer algorithm is constructed with run time O(logn).

Step by step solution

01

Binary search algorithm and Divide-and-conquer algorithm.

Binary Search is used in a sorted array by repeatedly dividing the search interval in half. The idea of binary search is to use the information that the array is sorted and reduce the time complexity toO(logn) .Binary Search also known as half-interval search, logarithmic search, or binary chop.Binary Search Algorithm can be implemented in the following two ways,

1.Iterative Method

2. Recursive Method

02

Sorting algorithm to be use in given situation

Use modification of binary search here :

Basically we have to find index such that a[i]=iinothera[i]-i=0.

Now as the array is sorted , so all the indices j which is less than i will satisfy the property

:a[j]-j<=0

as the array is sorted so a[j] will be at least one less than a[i] and left index will also be at least one less than i hence the conclusion.

Similarly to the right of the last index element .

a[j]-j>=0.

So the key idea is :

mid=left+right/2where left and right are the boundary indices and then check whether mid is the last index ,index we are looking for using the above mentioned property.

For this we compare only its immediate left and right neighbours.

Given that the array is “SORTED”. We can more use of this property to improve the fine complexity to O(logn).

-1

2

5

7

11

20

30

Ex: A

Let’s say we to search “20” in our Array. First we go for middle element i.e.,7

Search if 7>20?? False

If 7>20teen we will only search on the right hand side of 7.

Then, we have to search only in

112030

Here, we are dividing the large problem into a sub-problem with less sited array.

Mid=20

Apply same procedure.

Here, we found the mid value as required 20. This approach is called “Binary Search”

03

Algorithm

Algorithm BinarySearch(arr[],min,max,key)

{

If (max<min)then

Else return false.

Mid=(max+min)/2

If arr[mid]>keythen

Return BinarySearch(arr,min,mid1,key)

Else if arr[mid]<keythen

ReturnBinarySearch(arr,mid+1,max,key)

Else

Return

End if

End if

}

array[mid] <= start index

p[1.n]is array of words frequencies of sizen.

for(s=1ton)for(i=0ton-s)j=i+sif(i=j)

T(i,j)=p[i]for(k=itoj+1)T(i,j)=T(i,j)+p[k]returnT(0,n-1)

This algorithm is constructed or take run time of O(logn).

Here, the analysis of algorithm is given below, binary search, only certain that there is no such value to the left of the middle element ifrole="math" localid="1659776120568" array[mid]<=startindex, where mid is index of middle element, and start index is the start of the array.And divide-and-conqueralgorithm is constructed with run timeO(logn)

Every fine we are dividing the array into half. i.e.,

Ifn=100, next call will be,

Calln=50,25,12,6,3,1base case.

We are performing logn operations.

Hence, time complexity: O(logn)

Unlock Step-by-Step Solutions & Ace Your Exams!

  • Full Textbook Solutions

    Get detailed explanations and key concepts

  • Unlimited Al creation

    Al flashcards, explanations, exams and more...

  • Ads-free access

    To over 500 millions flashcards

  • Money-back guarantee

    We refund you if you fail your exam.

Over 30 million students worldwide already upgrade their learning with Vaia!

One App. One Place for Learning.

All the tools & learning materials you need for study success - in one app.

Get started for free

Most popular questions from this chapter

In Section 2.1 we described an algorithm that multiplies two n-bit binary integers x and y in time na, where a=log23. Call this procedure fast multiply (x,y).

(a) We want to convert the decimal integer 10n(a 1 followed by n zeros) into binary. Here is the algorithm (assume n is a power of 2):

function pwr2bin(n)

if n = 1: return10102

else:

z= ???

return fastmultiply(z,z)

Fill in the missing details. Then give a recurrence relation for the running time of the algorithm, and solve the recurrence.

(b) Next, we want to convert any decimal integer x with n digits (where n is a power of 2) into binary. The algorithm is the following:

function dec2bin(x)

if n=1: return binary [ x ]

else:

split x into two decimal numbers xt,xRwith n/2 digits each

return ???

Here binary [.] is a vector that contains the binary representation of all one-digit integers. That is, binary role="math" localid="1659333641173" [0]=02, binary [1]=12, up to binary [9]=10012. Assume that a lookup in binary takes 0(1) time. Fill in the missing details. Once again, give a recurrence for the running time of the algorithm, and solve it.

Thesquare of a matrix A is its product with itself, AA.

(a) Show that five multiplications are sufficient to compute the square of a 2 x 2 matrix.

(b) What is wrong with the following algorithm for computing the square of an n x n matrix?

“Use a divide-and-conquer approach as in Strassen’s algorithm, except that instead of getting 7 subproblems of size n2, we now get 5 subproblems of size n2 thanks to part (a). Using the same analysis as in Strassen’s algorithm, we can conclude that the algorithm runs in time O (nc) .”

(c) In fact, squaring matrices is no easier than matrix multiplication. In this part, you will show that if n x n matrices can be squared in time S(n) = O(nc), then any two n x n matrices can be multiplied in time O(nc) .

  1. Given two n x n matrices A and B, show that the matrix AB + BA can be computed in time 3S(n) + O(n2 ) .
  2. Given two n x n matrices X and Y, define the 2n x 2n matrices A and B,L as follows:
    A=X000andB=0Y00
    What is AB + BA, in terms of X and Y?
  3. Using (i) and (ii), argue that the product XY can be computed in time 3S(2n) + O(n2 ). Conclude that matrix multiplication takes time O(nc ).

Section 2.2 describes a method for solving recurrence relations which is based on analyzing the recursion tree and deriving a formula for the work done at each level. Another (closely related) method is to expand out the recurrence a few times, until a pattern emerges. For instance, let’s start with the familiar T(n)=2T(n/2)+o(n). Think of o(n) as being role="math" localid="1658920245976" <cnfor some constant , so: T(n)<2T(n/2)+cn. By repeatedly applying this rule, we can bound T(n) in terms of T(n/2), then T(n/4), then T(n/8), and so on, at each step getting closer to the value of T(.) we do know, namely .

T(1)=0(1).

T(n)2T(n/2)+cn2[2Tn/4+cn/2]+cn=4T(n/4)+2cn4[2Tn/8+cn/4]+2cn=8T(n/8)+3cn8[2Tn/16+cn/8]+3cn=16T(n/16)+4cn

.

.

.

A pattern is emerging... the general term is

T(n)2kT(n/2k)+kcn

Plugging in k=log2n, we get T(n)nT(1)+cnlog2n=0(nlogn).

(a)Do the same thing for the recurrence T(n)=3T(n/2)+0(n). What is the general kth term in this case? And what value of should be plugged in to get the answer?(b) Now try the recurrence T(n)=T(n-1)+0(1), a case which is not covered by the master theorem. Can you solve this too?

In justifying our matrix multiplication algorithm (Section 2.5), we claimed the following block wise property: if X and Y are n×nn matrices, and

X=[ABCD],Y=[EFGH],

where A,B,C,D,E,F,G, and H are n/2×n/2 sub-matrices, then the product XY can be expressed in terms of these blocks:

XY=[ABCD][EFGH]=[AE+BGAF+BHCE+DGCF+DH]

Prove this property.

In Section 1.2.3, we studied Euclid’s algorithm for computing the greatest common divisor (gcd) of two positive integers: the largest integer which divides them both. Here we will look at an alternative algorithm based on divide-and-conquer.

(a) Show that the following rule is true.

gcd(a,b)={2gcd(a2,b2)ifa,bareevengcd(ab2)ifaisodd,bisevengcd(a-b2,b)ifa,bareodd

(b) Give an efficient divide-and-conquer algorithm for greatest common divisor.

(c) How does the efficiency of your algorithm compare to Euclid’s algorithm if a and b are n-bit -bit integers? (In particular, since n might be large you cannot assume that basic arithmetic operations like addition take constant time.)

See all solutions

Recommended explanations on Computer Science Textbooks

View all explanations

What do you think about this solution?

We value your feedback to improve our textbook solutions.

Study anywhere. Anytime. Across all devices.

Sign-up for free