Please give a four part solution for this problem Solution Main idea Do binary

Please give a four part solution for this problem

This preview shows page 6 - 8 out of 10 pages.

(Please give a four part solution for this problem.)Solution:Main idea:Do binary search on both rows and columns. Pick the middle column and use binary search to find themaximum value in this column. If this element is a local maximum, we are done. Otherwise, there is someadjacent element that is larger. If there are larger elements in both adjacent columns, then this would implythe existence of two local maxima. Therefore, only one adjacent column contains a larger element. Recuron that submatrix.Pseudocode:procedureMATRIXMAX(MatrixM)m= number of rows ofMn= number of columns ofMifn=1thenOutput ArrayMax(Mas an array)A= columnbn/2c-1 as an arraye= ArrayMax(A)ifeis a local maximumthenOutputeelse ifeis adjacent to some larger elementfin columnbn/2cthenOutput MatrixMax(the submatrixofMof higher index columns thanA)elseOutput MatrixMax(the submatrix ofMof lower index columns thanA)procedureARRAYMAX(ArrayA)if|A|=1thenOutputA[0]a=A[b|A|/2c-1]ifais to the left of a larger elementbthenOutput ArrayMax(the subarray ofAcontainingband allelements to the right of it)else ifais to the right of a larger elementbthenOutput ArrayMax(the subarray ofAcontainingband all elements to the left of it)elseOutputaRuntime analysis:This algorithm takesO(log2n)time. This is because ArrayMax(A) is just doing binary search and takesO(log|A|)O(logn)time. LetT(m,n)be the runtime of MatrixMax. Notice thatT(m,n)T(m,n/2) +O(logn). By the Master Theorem, the total runtime isO(log2n)sincem=nfor the input matrix.Proof of correctness:First, we show inductively that ArrayMax(A) outputs the maximum entry ofA. SinceAis a column ofM,Ahas only one local maximum. If|A|=1 thenAhas only one element. If|A|>1, letcbe its maximum.Sincecis the only local maximum, the middle elementamust be less than it. Ifais to the left ofc, then theCS 170, Fall 2017, Homework 26
Background image
first ArrayMax call will be executed; otherwise the second one will be executed. Ifc=a, then the algorithmwill outputc. By induction, both ArrayMax calls will returnc, so ArrayMax(A) will returnc.Now, we show that MatrixMax(M) outputs the maximum value ofM. LetMLandMRbe the submatricesto the left and right ofArespectively. We will show that at least one of{ML,MR}only contains elementsthat are less thane. Suppose not, i.e. that bothMLandMRcontain elementseLandeRthat are greater thane. DefinefLandfRas follows. LetfL=eLandfR=eR. Repeatedly replacefLandfRwith any largerneighbors as long as they are not local maxima. SincefLandfRare local maxima,fL=fR. But this meansthat some intermediate value offLorfRmust lie inA, sinceAseparatesMLfromMR. This contradicts thefact that all entries ofAare less thane. This shows that at least one ofMLandMRonly contain elementsless thane.Ifeis not a local maximum, then exactly one ofMLorMRcontains the local maximum. By the previousparagraph, the correctMxwill contain entries adjacent toethat are greater thane. Therefore, the algorithmwill pick the rightMxto recur on.
Background image
Image of page 8

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture