Question

In: Computer Science

Implement 2-D peak finder and explain the asymptotic complexity of the algorithm For Design and Analysis...

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

For Design and Analysis of the Algorithme lecture

Solutions

Expert Solution

Peak finder in a 2D array means to find out the largest or equal values of an element when comparing with the neighboring elements lying in top,bottom,left and right in the matrix.For corner elements neighbour values are considered nil for the missing ones.

for eg: 20 40 4

2 9 6

4 11 9

here peak value is 40 and 11 because all its neigbours are smaller or equal to it.

Here goes the c++ code:

using namespace std;

const int ROWS=10; //initialise

const int COLS=10;

void init_array(int arr[][COLS]);

void print_array(int arr[][COLS]);

void find_peak(int arr[][COLS]);

int main()

{

int myArr[ROWS][COLS];

init_array(myArray);

print_array(myArray);

find_peak(myArray);

system("pause");

return 0;

}

void init_array(int arr[][COLS])

{

srand(time(0));

for(int i=0;i<ROWS;i++)

for(int j=0;i<COLS;j++)

arr[i][j]=rand();

}

void print_array(int arr[][COLS])

{

  for(int i=0;i<ROWS;i++)

{

for(int j=0;i<COLS;j++)

cout<<arr[i][j]<<"\t";

cout<<endl

}

}

void find_peak(int arr[][COLS]) //finding peakvalue function

{

int current=0;

int peakCounter=0;

int minPeakCounter=0;

for(int i=1;i<ROWS-1;i++)

for (j=1;j<COLS-1;j++)

{

current=arr[i][j];

for(int k=i-1;k<=i+1;k++)

{

for(int l=j-1;l<=j+1;l++)

{

if (arr[k][l]>current)

minPeakCounter++;

}

}

if (peak counter==8)

count<<"Peak at("<<i<<","<<j<<")"<<endl;

if(minPeak counter==8)

vcount<<"minPeak at("<<i<<","<<j<<")"<<endl;

peakCounter=0;

minpeakCounter=0;

}}

YOu can refer to the attached diagram to understand better.


Related Solutions

Q1) (1 point) Design an efficient algorithm (in terms of asymptotic complexity in the worst case)...
Q1) (1 point) Design an efficient algorithm (in terms of asymptotic complexity in the worst case) to determine if two students in a class of n students have the same height. What is the complexity of your algorithm? a.Provide the pseudo-code of that algorithm. b.Implement the algorithm in a language of your choice and provide a sample run with meaningful input.
Give the asymptotic (“big-Oh”) running time complexity of the following algorithm, show all the work you...
Give the asymptotic (“big-Oh”) running time complexity of the following algorithm, show all the work you have done. Algorithm: ArrayMangle(A[ ], int n) Input: an array A, an integer n x = 0; for (i=0; i<=n-1; i++) { for (j=i; j<=n-1; j++) { x = x + A[j]; } for (k=0; k<= n-1; k++) { for (j=0; j< =n-1; j++) { x = x + A[j]*A[k]; } } }
In all algorithm, always explain how and why they work. ALWAYS, analyze the complexity of your...
In all algorithm, always explain how and why they work. ALWAYS, analyze the complexity of your algorithms. In all algorithms, always try to get the fastest possible. A correct algorithm with slow running time may not get full credit. In all data structures, try to minimize as much as possible the running time of any operation. 1.Show that if a binary tree has a vertex with a single child, then this can not be an optimal Huffman tree. 2. Question...
Java Programm please! Design and implement an algorithm using recursion and backtracking to sort an array...
Java Programm please! Design and implement an algorithm using recursion and backtracking to sort an array of integers into ascending order. Consider the given array as input and produce a sorted array as output. Each time you take an integer from the input array, place it at the end of the output array. If the result is unsorted, backtrack.
Explain the failure criteria in 2-D stress-strain analysis.
Explain the failure criteria in 2-D stress-strain analysis.
Design a bus arbiter; A.) Plot the algorithm state chart. B.)Draw the state table C.) implement...
Design a bus arbiter; A.) Plot the algorithm state chart. B.)Draw the state table C.) implement suitable hardware
IN JAVA: Implement an algorithm to check whether a LinkedList is a palindrome. 2 methods should...
IN JAVA: Implement an algorithm to check whether a LinkedList is a palindrome. 2 methods should be implemented: Constant space, i.e. without creating extra copies of the linked list Unrestricted space q1: / For this implementation you should use constant space   // Note: you are free to add any extra methods,   // but this method has to be used   public static boolean isPalindromeRestricted(ListNode head) {     // Your code goes here     return false;   } q2: // For this implementation the space...
Write pseudocode to implement the paranoid algorithm. (You vs 2 other players)
Write pseudocode to implement the paranoid algorithm. (You vs 2 other players)
Cross Over design 2) Explain the power analysis for cross-over designs.
Cross Over design 2) Explain the power analysis for cross-over designs.
Cross Over design 2) Explain the power analysis for cross-over designs.
Cross Over design 2) Explain the power analysis for cross-over designs.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT