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 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
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