Finding the recurrence from an algorithm

  • Context: Undergrad 
  • Thread starter Thread starter Gear2d
  • Start date Start date
  • Tags Tags
    Algorithm Recurrence
Click For Summary
SUMMARY

The discussion centers on determining the recurrence relation for a recursive algorithm in C++ that finds the maximum value in an array. The algorithm, named recursiveMax, operates by comparing the maximum of the first n-1 elements with the n-th element. The established recurrence is f(n) = 3 for n=1 and f(n-1) + 7 for n>1, with the constant 3 representing the steps taken when the array has only one element. The discussion highlights confusion regarding the meaning of f and its measurement.

PREREQUISITES
  • Understanding of recursive algorithms in C++
  • Familiarity with array indexing in programming
  • Knowledge of recurrence relations and their applications
  • Basic concepts of algorithm analysis
NEXT STEPS
  • Study the principles of recurrence relations in algorithm analysis
  • Learn about the Big O notation for analyzing algorithm efficiency
  • Explore C++ recursion techniques and best practices
  • Investigate the implications of recursive calls on stack memory usage
USEFUL FOR

Students, software developers, and computer scientists interested in algorithm design, particularly those focusing on recursive algorithms and performance analysis in C++.

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?
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K