Difference of elements of Sets

In summary: Struggling)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
  • #1
evinda
Gold Member
MHB
3,836
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
  • #2
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)
 
  • #3
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)
 
  • #4
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)
 
  • #5
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]?
 
  • #6
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)
 
  • #7
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)
 
  • #8
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)
 
  • #9
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)
 

Related to Difference of elements of Sets

What is a "difference of elements of sets"?

The difference of elements of sets refers to the elements that are present in one set but not in another. It is also known as the relative complement of sets.

What is the notation used to represent the difference of sets?

The difference of sets is represented using the symbol "∖" or "-". For example, if set A = {1, 2, 3} and set B = {2, 3, 4}, then A ∖ B = {1} or A - B = {1}.

Can the difference of elements of sets result in an empty set?

Yes, if the two sets have the same elements, then the difference of sets will result in an empty set. For example, if set A = {1, 2, 3} and set B = {1, 2, 3}, then A ∖ B = {} or A - B = {}.

What is the relationship between the difference of sets and the intersection of sets?

The difference of sets and the intersection of sets are complementary operations. The difference of sets gives the elements that are present in one set but not in the other, while the intersection of sets gives the elements that are common to both sets.

In what situations is the concept of difference of sets useful?

The difference of sets is useful when dealing with data that belongs to different categories or groups. It helps to identify the unique elements in each group and can be used for data analysis, data comparison, and classification.

Similar threads

  • Programming and Computer Science
Replies
25
Views
2K
Replies
9
Views
1K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
2
Views
985
  • Programming and Computer Science
Replies
31
Views
2K
  • Programming and Computer Science
2
Replies
47
Views
4K
  • Set Theory, Logic, Probability, Statistics
2
Replies
36
Views
3K
  • Programming and Computer Science
Replies
9
Views
1K
  • Programming and Computer Science
Replies
1
Views
627
  • Programming and Computer Science
Replies
1
Views
1K
Back
Top