3

I got a question in my algorithm class and i am unable to solve it. The question states that Theres is a Sorting Algorithm with O(nlogn) and searching is done by Binary search taking O(log n). Two sets are give P &Q and i have to design an algorithm to determine whether the two sets are Disjoint.

Trying
  • 14,004
  • 9
  • 70
  • 110
Shaurya Chaudhuri
  • 3,772
  • 6
  • 28
  • 58

2 Answers2

5

O(N) solution (assuming the two sets are sorted):

  1. merge two sorted sets with information like the element belongs to which set
  2. traverse through the merge list , if you find two equal elements from two sets than not disjoint else if are able to reach till end than disjoint

e.g.

a= 1, 4, 6
b= 2, 4, 7

Now merged set=

elements: 1 2 4 4 6 7

set no(a/b): 1 2 1 2 1 2

Now we can clearly see that two fours are consecutive and both are from two different set, hence not disjoint.

EDIT:

If your need is to find the sets are disjoint or not than simple merge will give you that. As soon as you find both elements in the sets are equal than just return saying not disjoint else if are able to reach till end of both than disjoint.

Question related to container. Below is that:

class Element{
int i;
int setInfo
}

Now declare array as: Element[] e=new Element[X];

Hope i am clear.

Trying
  • 14,004
  • 9
  • 70
  • 110
  • 1
    Assuming two sets are sorted you are right. To make your solution, one can modify the merge algorithm and instead of while merging, only check if two elements are equal of not. – iampat Oct 20 '13 at 03:34
  • @Trying, can you give a practical approach to store the information of set number, I mean providing it's an int array what container will you use? – java_doctor_101 Oct 20 '13 at 03:38
2

Two sets will be disjoint only if there is no common element between these two sets. I am assuming both of these sets have n elements each. This algorithm checks for common element in nlog(n) time. If there is no common element found that means both the sets are disjoint.

For each element in set "P" search whether that number exist in set "Q".
Time complexity - n*log(n) 
Log(n) to search in set Q and this search will be done "n" times.
java_doctor_101
  • 3,287
  • 4
  • 46
  • 78
  • Just make sure to sort the sets first (another O(n log n) operation) so the searches can each be done in O(n) time. – Ted Hopp Oct 20 '13 at 03:23
  • If both the lists are sorted before searching the time complexity will be O(nlogn)+O(nlogn) + O(nlogn) = O(nlogn) [2 sorting and one searching.] Right? @TedHopp - how can the searching be done in O(n) since binary search will take O(logn) and if we use linear search than O(n) but since its inside another loop it will be O(n^2). – Shaurya Chaudhuri Oct 20 '13 at 03:29
  • @Ted , could you please explain how searching for common element in two sorted arrays will only take n amount of time? – java_doctor_101 Oct 20 '13 at 03:29
  • Um, that was supposed to be O(log n). Sorry; typing too fast. – Ted Hopp Oct 20 '13 at 04:20