MHB How many comparisons are required?

  • Thread starter Thread starter mathmari
  • Start date Start date
AI Thread Summary
To find the maximum element of a set of integers S using only addition and comparisons, the initial approach suggested performing n-1 comparisons by comparing each element to a running maximum. However, this method is not the most efficient. An optimized divide and conquer strategy can reduce the number of comparisons to log2(n). By splitting the set into two halves, finding the maximum in each half, and then comparing these two maximums, the process becomes more efficient. Thus, while the initial method is valid, the optimized approach significantly minimizes the number of comparisons required to identify the maximum element in the set.
mathmari
Gold Member
MHB
Messages
4,984
Reaction score
7
Hey! :o

Let $S$ be a set of $n$ integers. Assume you can perform only addition of elements of $S$ and comparisons between sums. Under these conditions how many comparisons are required to find the maximum element of $S$?

I thought that we could find the maximum element as followed:

Code:
max=S(1)+S(1)
k=1
for j=2 to n
     sum=S(1)+S(j)
     if sum>max
           max=sum
           k=j
return S(k)

That means that $n-1$ comparisons are required.

Is this correct?? (Wondering)
 
Technology news on Phys.org


Hello! Your approach to finding the maximum element of $S$ using comparisons and addition is correct. However, the number of comparisons needed may not always be $n-1$. Let me explain why.

In your algorithm, you are comparing each element of $S$ to the current maximum, which is initially set to $S(1)+S(1)$. This means that for each element $S(j)$, you are performing one comparison ($S(1)+S(j)$ vs $max$). Therefore, for $n$ elements in $S$, you would need $n$ comparisons.

However, we can optimize this algorithm by using a divide and conquer approach. We can divide the set $S$ into two halves and find the maximum element in each half. Then, we can compare the maximum elements of the two halves and return the larger one as the maximum element of $S$. This approach would require only $\log_2{n}$ comparisons, which is significantly fewer than $n-1$ comparisons.

In conclusion, your approach is correct but may not always give the minimum number of comparisons needed. The optimized approach would require only $\log_2{n}$ comparisons. I hope this helps!
 
Thread 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
Back
Top