/*! This file is auto-generated */ .wp-block-button__link{color:#fff;background-color:#32373c;border-radius:9999px;box-shadow:none;text-decoration:none;padding:calc(.667em + 2px) calc(1.333em + 2px);font-size:1.125em}.wp-block-file__button{background:#32373c;color:#fff;text-decoration:none} Q17E Given a sorted array of distinct... [FREE SOLUTION] | 91影视

91影视

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 鈥淪ORTED鈥. 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鈥檚 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 鈥淏inary 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 91影视!

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

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鈥檚 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?

Practice with the fast Fourier transform.

(a) What is the FFT of (1,0,0,0)? What is the appropriate value of in this case? And of which sequence is (1,0,0,0)the FFT?

(b)Repeat for (1,0,1,-1).

You are given two sorted lists of size mandn. Give an O(logm+logn)time algorithm for computing the k th smallest element in the union of the two lists.

The Hadamard matricesH0,H1,H2, are defined as follows:

  • H0 is the 11matrix[1]
  • For k>0,Hkisthe2k2k matrix

localid="1658916810283" Hk=[Hk-1|Hk-1Hk-1|-Hk-1]

Show that if is a column vector of lengthlocalid="1658916598888" n=2k, then the matrix-vector product localid="1658916618774" Hkvcan be calculated using localid="1658916637767" O(nlogn) operations. Assume that all the numbers involved are small enough that basic arithmetic operations like addition and multiplication take unit time.

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?

鈥淯se a divide-and-conquer approach as in Strassen鈥檚 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鈥檚 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 ).
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.