Question

In: Computer Science

(Can you answer my question by explaining, I mean that step by step. I don't want...

(Can you answer my question by explaining, I mean that step by step. I don't want a short answer. Be sure to I like it if you write step by step

For Design and Analysis of the Algorithme lecture)

Peak Finding

Implement the 2-D peak finder mentioned and explain the asymptotic complexity of the algorithm.

Solutions

Expert Solution

An element is a peak element if it is greater than or equal to its four neighbors, left, right, top and bottom. For example neighbors for A[i][j] are A[i-1][j], A[i+1][j], A[i][j-1] and A[i][j+1]. For corner elements, missing neighbors are considered of negative infinite value.

Example:

Input : 10 20 15
21 30 14
7 16 32
Output : 30
30 is a peak element because all its neighbors are smaller or equal to it.
32 can also be picked as a peak.

Algorithm :

1)Consider mid column and find maximum element in it.

2)Let index of mid column be ‘mid’, value of maximum element in mid column be ‘max’ and maximum element be at ‘mat[max_index][mid]’.

3)If max >= A[index][mid-1] & max >= A[index][pick+1], max is a peak, return max.

4)If max < mat[max_index][mid-1], recur for left half of matrix.

5)If max < mat[max_index][mid+1], recur for right half of matrix.

Time Complexity : O(rows * log(columns)). We recur for half the number of columns. In every recursive call, we linearly search for the maximum in the current mid column.

Auxiliary Space: O(columns/2) for Recursion Call Stack.

Code:

// Finding peak element in a 2D Array.
#include <bits/stdc++.h>

using namespace std;

const int MAX = 100;

// Function to find the maximum in column 'mid'
// 'rows' is number of rows.

int findMax(int arr[][MAX], int rows, int mid, int& max)
{

int max_index = 0;

for (int i = 0; i < rows; i++) {

if (max < arr[i][mid]) {

// Saving global maximum and its index

// to check its neighbours

max = arr[i][mid];

max_index = i;

}

}

return max_index;
}

// Function to find a peak element

int findPeakRec(int arr[][MAX], int rows, int columns,

int mid)
{

// Evaluating maximum of mid column. Note max is

// passed by reference.

int max = 0;

int max_index = findMax(arr, rows, mid, max);

// If we are on the first or last column,

// max is a peak

if (mid == 0 || mid == columns - 1)

return max;

// If mid column maximum is also peak

if (max >= arr[max_index][mid - 1] && max >= arr[max_index][mid + 1])

return max;

// If max is less than its left

if (max < arr[max_index][mid - 1])

return findPeakRec(arr, rows, columns, mid - ceil((double)mid / 2));

// If max is less than its left

// if (max < arr[max_index][mid+1])

return findPeakRec(arr, rows, columns, mid + ceil((double)mid / 2));
}

// A wrapper over findPeakRec()

int findPeak(int arr[][MAX], int rows, int columns)
{

return findPeakRec(arr, rows, columns, columns / 2);
}

int main()
{

int arr[][MAX] = { { 10, 8, 10, 10 },

{ 14, 13, 12, 11 },

{ 15, 9, 11, 21 },

{ 16, 17, 19, 20 } };

// Number of Columns

int rows = 4, columns = 4;

cout << findPeak(arr, rows, columns);

return 0;
}

Output :

21.

Please do upvote thank you.


Related Solutions

please answer the question by explaining the step by step process used in the calculation. and...
please answer the question by explaining the step by step process used in the calculation. and please discuss the results. thank you:) Please set-up solution model for the following capital budget problem. Explain the approach you plan to take and why. Then, please perform the calculations of your model and draw conclusions. Capital Budget Problem: This case continues following the new project of the WePPROMOTE Company, that you and your partner own. WePROMOTE is in the promotional materials business. The...
I want my answer typed.                                      I. What i
I want my answer typed.                                      I. What is presbyopia? Hyperopia? Myopia? 2. Which cranial nerves assesses eye function and acoustic function? 3. What is the Rinne test? Weber test? 4. Do you or anyone you know have any of the issues/symptoms/conditions mentioned in the video? Is it due to genetics or aging? How is it being managed?
I JUST WANT AN EXAMPLE ANSWER SO I CAN MAKE MY OWN. THANK YOU!! Conduct research...
I JUST WANT AN EXAMPLE ANSWER SO I CAN MAKE MY OWN. THANK YOU!! Conduct research on New York City's large soda ban. Pretend you work at the Paradise City attorney's office. Draft a law that restricts the sale of large sugary drinks in Paradise City. Make sure to define the types of drinks and types of sellers restricted by the law. Example Statute: Title XXXIV (Links to an external site.)Links to an external site. ALCOHOLIC BEVERAGES AND TOBACCO Chapter...
I don't want explanation , I just want a correct answer, Very quickly. 12- If the...
I don't want explanation , I just want a correct answer, Very quickly. 12- If the slope of the consumption function is equal to one, then Select one: a. The multiplier is undefined b. The multiplier is smaller than one c. The multiplier is just one d. The multiplier is larger than one e. None of the above 11- If the nominal interest rate 8% and expected inflation 5%, the expected real interest rate in year t is approximately Select...
please I want it to step by step and in word posted so I can read...
please I want it to step by step and in word posted so I can read them. Q: If the average realized return of a portfolio is 27.5% per year, the standard deviation of returns is 50%, the portfolio beta is 1.25, the average return of Treasury bills over the same period is 2.5% per year, and the average return on the market is 12.5% per year Calculate i) the Sharpe; ii) Treynor and iii) Jensen    
I saw an answer for this on another question posted, but I don't think the answer...
I saw an answer for this on another question posted, but I don't think the answer was exactly what I was looking for. Can anyone help guide me in the right direction in regards to this question and its subquestions? Thanks. The proton and electron are particles found to have equal and opposite charges to the precision that the measurements have been made so far. Why is it important that the proton and electron have exactly the same magnitude for...
Hi I have an exercise and I want to you to solve it step by step,...
Hi I have an exercise and I want to you to solve it step by step, please.. I found the solution here but it did not have steps .. *Answer the following questions using the information below: Tiger Pride produces two product lines: T-shirts and Sweatshirts. Product profitability is analyzed as follows: T-SHIRTS SWEATSHIRTS Production and sales volume 60,000 units 35,000 units Selling price $16.00 $29.00 Direct material $ 2.00 $ 5.00 Direct labor $ 4.50 $ 7.20 Manufacturing overhead...
I was wondering if I could get a step by step process to answer this question?
I was wondering if I could get a step by step process to answer this question?
I want solve this question step by step, and write by pc Solomon is a principal...
I want solve this question step by step, and write by pc Solomon is a principal engineer at an environmental engineering consulting firm. His main role is to advise clients on what type of action to take when they are faced with risks and liabilities while conducting certain projects. In one case, Solomon had a client that wanted to expand their campus until it was within approximately 50 meters of a marshland. After construction of this extension, however, the client...
please i want solution for this question with algorithm and step by step with c++ language...
please i want solution for this question with algorithm and step by step with c++ language write a program using for loop that calculates the total grade for N classroom exercises as a percentage. The user should input the value for N followed by each of the N scores and totals. Calculate the overall percentage (sum of the total points earned divided by the total points possible) and output it as a percentage. Sample input and output is shown below.How...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT