Are Elements of Sets Close Enough?

Click For Summary
SUMMARY

The discussion focuses on an algorithm to determine if there exists a pair of numbers \(a\) and \(b\) from two sets \(D\) and \(E\) such that the absolute difference \(|a-b|\) is less than or equal to a given threshold \(K\). The proposed algorithm combines both sets into a single array \(A\), sorts it, and checks adjacent elements for the condition. The time complexity is confirmed to be \(O((n+m) \lg (n+m))\). However, participants highlight the need for an additional condition to ensure \(a\) and \(b\) belong to different sets.

PREREQUISITES
  • Understanding of set theory and elements
  • Familiarity with sorting algorithms, specifically HeapSort
  • Knowledge of algorithmic complexity analysis
  • Basic programming skills in a language that supports array manipulation
NEXT STEPS
  • Implement the proposed algorithm in a programming language of choice
  • Research alternative sorting algorithms and their time complexities
  • Explore methods for efficiently checking conditions across multiple sets
  • Study advanced data structures that could optimize set operations
USEFUL FOR

Computer scientists, algorithm developers, and students studying data structures and algorithms who are interested in optimizing set operations and understanding time complexity in algorithm design.

evinda
Gold Member
MHB
Messages
3,741
Reaction score
0
Hello! (Wave)

Consider two sets $D=\{ d_1, d_2, \dots, d_n\}$ and $E=\{ e_1, e_2, \dots, e_m \}$ and consider an other variable $K \geq 0$.
Show that we can answer in time $O((n+m) \lg (n+m))$ the following question:
Is there is a pair of numbers $a,b$ where $a \in D, b \in E$ such that $|a-b| \leq K$?

The algorithm should answer the above question with <<YES>> or <<NO>>.
Describe the algorithm, show its correctness and show that its time complexity is $O((n+m) \lg (n+m))$.I thought that we could do it like that:

We create a new array $A$ that contains both the elements of $D$ and $E$.
Then $A=\{ d_1, d_2, \dots d_n, e_1, e_2, \dots, e_n \}$.
We sort the array $A$.
Then we only look at the differences of the adjacent elements, so at $|d_1-d_2|, |d_2-d_3|$ and so on.. We can do it like that because the array is sorted and if the difference of the adjacent elements isn't less than $D$, neither the difference of the first of these elements with an other element will be.
I wrote this algorithm:
Code:
Algorithm(D,E,K){
   found=0;
   for (i=1; i<=n; i++){
         A[i]=D[i];
   }
   for (i=1; i<=m; i++){
         A[n+i]=E[i];
   }
   HeapSort(A);
   j=1;
   p=2;
   while (j<=n+m and p<=n+m and found==0){
            if (abs(A[j]-A[p]) <= K) found=1;
            j++;
            p=j+1;
   }
   if (found==0) printf("NO\n");
   else printf("YES\n");
}

Could you tell me if my idea is right? (Thinking)
 
Last edited:
Technology news on Phys.org
Hey! (Blush)

I believe the core of your idea is good. (Smile)
... but I think your algorithm needs a little refinement. (Wasntme)

What will for instance be the result of [m]Algorithm({1,2,3}, {10,11,12}, 3)[/m]? (Thinking)
 
I like Serena said:
Hey! (Blush)

I believe the core of your idea is good. (Smile)
... but I think your algorithm needs a little refinement. (Wasntme)

What will for instance be the result of [m]Algorithm({1,2,3}, {10,11,12}, 3)[/m]? (Thinking)

At the if-statement I wrote accidentally [m] D [/m] instead of [m] K [/m]... (Blush) I edited my post...Applying [m]Algorithm({1,2,3}, {10,11,12}, 3)[/m] we will have $j=1, p=2$ and $|A[1]-A[2]|=|1-2|=1 \leq 3$.
So $A[1], A[2]$ satisfy the desired property and so the output will be [m] YES [/m].
Or am I wrong? (Thinking)
 
evinda said:
At the if-statement I wrote accidentally [m] D [/m] instead of [m] K [/m]... (Blush) I edited my post...

Good! ;)

Applying [m]Algorithm({1,2,3}, {10,11,12}, 3)[/m] we will have $j=1, p=2$ and $|A[1]-A[2]|=|1-2|=1 \leq 3$.
So $A[1], A[2]$ satisfy the desired property so the output will be [m] YES [/m].
Or am I wrong? (Thinking)

You wrote:

Is there is a pair of numbers a,b where a∈D,b∈E such that |a−b|≤K?


What are a and b in this case? Do they satisfy the given conditions? (Wondering)
 
I like Serena said:
You wrote:

Is there is a pair of numbers a,b where a∈D,b∈E such that |a−b|≤K?


What are a and b in this case? Do they satisfy the given conditions? (Wondering)


In this case both $a, b \in D$... (Blush)

So do we have to add an if-condition in the while loop that checks if the two elements we are considering belong to different sets and while this isn't satisfied do we have to change the value of [m] p [/m]?
 
evinda said:
In this case both $a, b \in D$... (Blush)

So do we have to add an if-condition in the while loop that checks if the two elements we are considering belong to different sets and while this isn't satisfied do we have to change the value of [m] p [/m]?

Yes, we have to consider the sets the elements belong to. (Nod)

In my example, we would effectively have: ((1,D), (2,D), (3,D), (10,E), (11,E), (12,E)).

Where do we find the a and b that are closest together?
Are they adjacent? (Wondering)
 
I like Serena said:
Yes, we have to consider the sets the elements belong to. (Nod)

In my example, we would effectively have: ((1,D), (2,D), (3,D), (10,E), (11,E), (12,E)).

Where do we find the a and b that are closest together?
Are they adjacent? (Wondering)

No, they aren't adjacent... (Shake)
Since we have sorted the array, if we assume that the first element belongs to the set $D$ we are looking with a while-loop for the first element that belongs to the set $E$ and then we check if their difference is $\leq K$. If not, then we have to look for the next element that belongs to the set $D$ and to find the element of $E$ that is closest to the element of $D$ we found, right? (Thinking)
 
If so, how can we look for the first element that belongs to the set $E$ ?
Do we have to go through the elements of $A$ and $E$ till we find a common element?
But then will the complexity not be greater than the one we want to have? (Worried)
 
Which are the a and b that are closest together in my example? (Wondering)
 
  • #10
I like Serena said:
Which are the a and b that are closest together in my example? (Wondering)

There are no $a,b$ with $a \in D$ and $b \in E$ that satisfy the desired property, right? (Thinking)
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 25 ·
Replies
25
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
9
Views
3K
Replies
47
Views
5K
  • · Replies 25 ·
Replies
25
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
31
Views
3K
  • · Replies 20 ·
Replies
20
Views
4K