Algorithm with time complexity o(n^2)

Click For Summary
SUMMARY

The discussion focuses on developing an algorithm with a time complexity of \(o(n^2)\) to find two elements \(y_k\) and \(y_l\) from a set \(M = \{y_1, y_2, \dots, y_n\}\) such that \(|y_k - y_l| = \min_{1 \leq i,j \leq n} |y_i - y_j|\). The proposed solution involves first sorting the elements using Heap Sort, which has a time complexity of \(O(n \log n)\), followed by a single pass through the sorted list to identify the minimum difference, resulting in an overall complexity of \(O(n \log n)\). The correctness of the algorithm can be established through mathematical induction.

PREREQUISITES
  • Understanding of time complexity and Big O notation
  • Familiarity with sorting algorithms, specifically Heap Sort
  • Basic knowledge of absolute values and their properties
  • Concepts of mathematical induction for algorithm correctness
NEXT STEPS
  • Study the implementation details of Heap Sort and its time complexity
  • Learn about the properties of absolute differences in sets of real numbers
  • Explore mathematical induction techniques for proving algorithm correctness
  • Investigate alternative algorithms for finding minimum differences, such as using balanced trees
USEFUL FOR

Computer scientists, algorithm designers, and students studying data structures and algorithms, particularly those interested in optimizing search and comparison operations in datasets.

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
1K
Replies
9
Views
10K
Replies
5
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 36 ·
2
Replies
36
Views
6K