Finding the recurrence from an algorithm

AI Thread Summary
The discussion revolves around understanding the recurrence relation for a recursive maximum search algorithm in C++. The algorithm, named recursiveMax, searches for the maximum value in an array by comparing elements recursively. It is noted that the recurrence is defined as f(n) = 3 when n=1 and f(n-1) + 7 otherwise, with the initial value representing the base case. The confusion arises regarding the meaning of "f" and how it quantifies the steps involved in the algorithm, particularly the additional operations performed in the recursive case. Clarification is sought on how to interpret the recurrence and its implications for algorithm analysis.
Gear2d
Messages
49
Reaction score
0
I need some help. I am having a hard time find the recurrence when given an algorithm in c++. The algorithm is a Max search: Its where the program goes through the array from first element to last, or last to first (depends on how you program it) to look for the largest value. In this case it says if you give the program n elements (1...n), the program goes from (0...n-1) because array indexing starts from 0 until n-1. So say you have:
1 2 3 4 5 6, and you want the element at A[0], you get 1, say A[5] = 6, and so on. Here is the algorithm goes n-1 to 0, and where "A" is the array:

Algorithm recursiveMax(A,n)
Input: Array A storing n>= 1 integers
Output: The Maximum element in A

if n = 1 then //if there is only 1 element in array
return A[0]

else
return max{recursiveMax(A,n-1), A[n-1]}

The book says the recurrence is:

f(n) = 3 when n=1 and f(n-1) + 7 otherwise

Now I understand where the three is coming from, its when you have the best case where there is only 1 element in the array so it preforms 3 steps too do the program. But how about the second part?
 
Mathematics news on Phys.org
Gear2d said:
I need some help. I am having a hard time find the recurrence when given an algorithm in c++. The algorithm is a Max search: Its where the program goes through the array from first element to last, or last to first (depends on how you program it) to look for the largest value. In this case it says if you give the program n elements (1...n), the program goes from (0...n-1) because array indexing starts from 0 until n-1. So say you have:
1 2 3 4 5 6, and you want the element at A[0], you get 1, say A[5] = 6, and so on. Here is the algorithm goes n-1 to 0, and where "A" is the array:

Algorithm recursiveMax(A,n)
Input: Array A storing n>= 1 integers
Output: The Maximum element in A

if n = 1 then //if there is only 1 element in array
return A[0]

else
return max{recursiveMax(A,n-1), A[n-1]}

The book says the recurrence is:

f(n) = 3 when n=1 and f(n-1) + 7 otherwise

Now I understand where the three is coming from, its when you have the best case where there is only 1 element in the array so it preforms 3 steps too do the program. But how about the second part?
Since there is no "f" any where in what you say previous to "f(n)= 3 ..." and it clearly is not the output of the algorithm, I don't see how anyone can figure that out! What is f supposed to measure?
 
Fermat's Last Theorem has long been one of the most famous mathematical problems, and is now one of the most famous theorems. It simply states that the equation $$ a^n+b^n=c^n $$ has no solutions with positive integers if ##n>2.## It was named after Pierre de Fermat (1607-1665). The problem itself stems from the book Arithmetica by Diophantus of Alexandria. It gained popularity because Fermat noted in his copy "Cubum autem in duos cubos, aut quadratoquadratum in duos quadratoquadratos, et...
Insights auto threads is broken atm, so I'm manually creating these for new Insight articles. In Dirac’s Principles of Quantum Mechanics published in 1930 he introduced a “convenient notation” he referred to as a “delta function” which he treated as a continuum analog to the discrete Kronecker delta. The Kronecker delta is simply the indexed components of the identity operator in matrix algebra Source: https://www.physicsforums.com/insights/what-exactly-is-diracs-delta-function/ by...
Thread 'Imaginary Pythagorus'
I posted this in the Lame Math thread, but it's got me thinking. Is there any validity to this? Or is it really just a mathematical trick? Naively, I see that i2 + plus 12 does equal zero2. But does this have a meaning? I know one can treat the imaginary number line as just another axis like the reals, but does that mean this does represent a triangle in the complex plane with a hypotenuse of length zero? Ibix offered a rendering of the diagram using what I assume is matrix* notation...
Back
Top