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

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...
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.
Explain: Analysis of algorithms with the example. Explain with example: How computational time of an algorithm...
Explain: Analysis of algorithms with the example. Explain with example: How computational time of an algorithm depends on input size? Write an algorithm (using whatever method you prefer) that takes an input of 10 integers, sorts the integers in ascending order and out puts the sorted list. In relation to computational time of your algorithm for part 4, what arrangement of the input numbers will cause the best case computational time? What arrangement will cause the worst case time?
1. Implement the recursive LU factorization algorithm in Python. Use plenty of comments to explain your...
1. Implement the recursive LU factorization algorithm in Python. Use plenty of comments to explain your code. While you are coding, it is helpful to break up your code into sub-functions and test the sub-functions as you go along.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT