Algorithm with time complexity o(n^2)

Click For Summary

Discussion Overview

The discussion revolves around finding an algorithm with time complexity o(n^2) that identifies two elements from a set of real numbers such that the absolute difference between them is minimized. The conversation includes algorithm design, time complexity considerations, and correctness proofs.

Discussion Character

  • Exploratory
  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant suggests that using two while loops would lead to a time complexity of at least cn^2, which does not meet the requirement of being less than cn^2.
  • Another participant proposes sorting the values using heapsort, which has a time complexity of n log n, followed by a single pass through the sorted list to find the desired elements, resulting in a total complexity of o(n^2).
  • A similar suggestion is reiterated by another participant, confirming the sorting and single pass approach.
  • A participant questions how to demonstrate the correctness of the proposed algorithm and suggests using induction on i as a potential method for proof.

Areas of Agreement / Disagreement

There is no consensus on the correctness of the algorithm or the method of proof. Participants have differing views on the approach to take for proving correctness.

Contextual Notes

The discussion does not resolve the assumptions regarding the definitions of time complexity or the specifics of the algorithm's correctness proof.

evinda
Gold Member
MHB
Messages
3,741
Reaction score
0
Hi! (Smile)

I am looking at the following exercise:

Let $M=\{ y_1, y_2, \dots, y_n \}$ a set of real numbers, where $n \geq 2$. Describe an algorithm, that has time complexity $o(n^2)$ and that finds and returns two elements $y_k$ and $y_l$ of $M$, such that:

$$|y_k-y_l|=\min_{1 \leq i,j \leq n} |y_i-y_j|$$I think that we cannot do this with the use of two while loops, because the time complexity will be $\leq cn^2$, but we want it to be $<cn^2$, or am I wrong? (Thinking)
How else could we do this? (Worried)
 
Technology news on Phys.org
First sort the values (with say heap sort) of time complexity $n\ln\,n$. Then in the sorted list find the desired elements in one pass of the n elements. Total complexity "little oh" $n^2$.
 
johng said:
First sort the values (with say heap sort) of time complexity $n\ln\,n$. Then in the sorted list find the desired elements in one pass of the n elements. Total complexity "little oh" $n^2$.

So, is it right like that? (Thinking)

Code:
Heapsort(A){
  Buildheap(A);
  for (i=size(A); i>1; --i){
       swap(A[i],A[1]);
       heap_size(A)=heap_size(A)-1;
       Heapify(A,1);
   }
}
 
Elements(S,n){
  int i,min,k;
  Heapsort(S)
  min=abs(S[1]-S[2]);
  for (i=2; i<n; i++){ 
      if (abs(S[i]-S[i+1])<min)
          min=abs(S[i]-S[i+1]);
          k=i;
  }
  return S[k],S[k+1];
}
 
Looks right to me.
 
How could we show the correctness of the algorithm?
Could we prove that it is correct, using induction on [m] i [/m] ?
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 11 ·
Replies
11
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
9
Views
10K
Replies
5
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 36 ·
2
Replies
36
Views
6K