Are Elements of Sets Close Enough?

Click For Summary

Discussion Overview

The discussion revolves around an algorithmic problem involving two sets, D and E, and a variable K. Participants are tasked with determining if there exists a pair of elements, one from each set, such that the absolute difference between them is less than or equal to K. The focus is on the algorithm's design, correctness, and time complexity, with an emphasis on refining the initial proposal.

Discussion Character

  • Technical explanation, Debate/contested, Mathematical reasoning

Main Points Raised

  • One participant proposes an algorithm that involves merging the two sets into a single array, sorting it, and then checking adjacent elements for the desired property.
  • Another participant questions the algorithm's correctness by providing a specific example and suggesting that the algorithm needs refinement.
  • There is a discussion about ensuring that the elements being compared come from different sets, leading to suggestions for additional conditions in the algorithm.
  • Participants explore the implications of sorting and adjacency in the context of finding the closest elements from different sets.
  • Concerns are raised about maintaining the desired time complexity while implementing the necessary checks for set membership.
  • Some participants express uncertainty about whether any pairs meet the required conditions in the provided examples.

Areas of Agreement / Disagreement

Participants generally agree on the need for refinement of the proposed algorithm, but there is no consensus on the correct implementation or whether the current approach can achieve the desired time complexity. Multiple competing views remain regarding the handling of element comparisons and set membership.

Contextual Notes

Participants note potential limitations in the algorithm's design, particularly regarding the handling of elements from the same set and the implications for time complexity. There are unresolved questions about the correctness of the proposed checks and the overall approach.

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
4K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
31
Views
4K
  • · Replies 20 ·
Replies
20
Views
4K