Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Problem 2 : Peak Finding Problem [ Advanced ] Let us come up with an improved algorithm for Peak Finding. First, let us study the
Problem : Peak Finding Problem Advanced
Let us come up with an improved algorithm for Peak Finding. First, let us study the problem is it possible for an array to not have a peak?
The answer is no Any array will have at least one peak. We can prove this by contradiction.
Proof that an array of numbers will have at least peak.
Assume the statement is not true; in other words, there is an array with no peak. Consider this array of n elements; let us call it as Xn
As X is not a peak, we know that X X
Now, because X is not a peak, and because X X we know that X X
Similarly, go on and we know that X X X X and so on and Xn Xn
However, if that is the case, then Xn is a peak because Xn Xn
Therefore we have proved that an array of n elements MUST have at least peak.
a Answer the following: Come up with an array of elements with peaks.
Below, we give an improved algorithm for peak finding.
Input: Array An indexes x and y and we are finding a peak in Axy
Return: index i x i y such that Ai is a peak
PeakFindingImprovedA x y:
mid x y
if Amid Amid
return PeakFindingImprovedA x mid
else if Amid Amid
return PeakFindingImprovedA mid y
return mid
Proof of Correctness of PeakFindingImproved algorithm
Consider Step This will be executed only if Amid Amid Therefore, any element that is a peak in Axmid will be a peak. For indexes mid it is obvious; if mid is a peak in Axmid then we know mid is a peak in Axy because Amid Amid from Step in Algorithm and Amid Amidfrom Step in Algorithm We can similarly prove for Step We can also prove Step is correct, when Step is executed.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started