Question

(1) Describe (in words) an algorithm that checks if two given lists are disjoint, meaning that they have no element in common
0 0
Add a comment Improve this question Transcribed image text
Answer #1
  • Below is the detailed explanation of the algorithm and pseudo code for the problem mentioned.

  • Algorithm:

To check if two lists are disjoint or not we can:

Start traversing list1 i.e, element by element and for each element of this list1 start traversing the other list2 and check if element of list1 can be found in list2, if we found that element in list2 then we will return False(as lists are not disjoint) and otherwise we move to next element of list1 and do the same process till all the elements of list1 are checked to be in list2 or we return false somewhere.

After we come out of the loops we know that lists are disjoint as if it was not then we have returned false from there only, so return True here and end function.

  • Pseudo code(function disjoint returns True if lists passed are disjoint otherwise returns False):

Procedure disjoint(a1,.........,an, b1,.........,bm : elements):

for element1 in (a1,.........,an):

  for element2 in (b1,.........,bm):

check if element1 == element2 then:

return False

else continue checking

return True

  • If we call above function as disjoint([1,2,3], [4,5,6]) then it will return True as both lists are disjoint but if we call above function as disjoint([1,2,3], [2,5,6,8]) then it will return False as the two lists mentioned have element 2 as common so are not disjoint.

  • The complexity of above mentioned algorithm is O(n*m) where n is size of first list and m is size of other list as we are checking each element of list1 for every other element of list2, so 2 ested loops are required .Hence worst/average case complexity comes out to be O(n*m)


answered by: ANURANJAN SARSAM
Add a comment
Answer #2
  • Below is the detailed explanation of the algorithm and pseudo code for the problem mentioned.
  • Algorithm:

To check if two lists are disjoint or not we can:

Start traversing list1 i.e, element by element and for each element of this list1 start traversing the other list2 and check if element of list1 can be found in list2, if we found that element in list2 then we will return False(as lists are not disjoint) and otherwise we move to next element of list1 and do the same process till all the elements of list1 are checked to be in list2 or we return false somewhere.

After we come out of the loops we know that lists are disjoint as if it was not then we have returned false from there only, so return True here and end function.

  • Pseudo code(function disjoint returns True if lists passed are disjoint otherwise returns False):

Procedure disjoint(a1,.........,an, b1,.........,bm : elements):

for element1 in (a1,.........,an):

  for element2 in (b1,.........,bm):

check if element1 == element2 then:

return False

else continue checking

return True

  • If we call above function as disjoint([1,2,3], [4,5,6]) then it will return True as both lists are disjoint but if we call above function as disjoint([1,2,3], [2,5,6,8]) then it will return False as the two lists mentioned have element 2 as common so are not disjoint.
  • The complexity of above mentioned algorithm is O(n*m) where n is size of first list and m is size of other list as we are checking each element of list1 for every other element of list2, so 2 ested loops are required .Hence worst/average case complexity comes out to be O(n*m).

So if you still have any doubt regarding this solution please feel free to ask it in the comment section below and if it is helpful then please upvote this solution, THANK YOU.

Add a comment
Know the answer?
Add Answer to:
(1) Describe (in words) an algorithm that checks if two given lists are disjoint, meaning that...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 5. Design an algorithm that takes two arrays, and returns true if the arrays are disjoint,...

    5. Design an algorithm that takes two arrays, and returns true if the arrays are disjoint, i.e. have no elements in common. Write down your algorithm as pseudocode. You don't need to write a python code. Give the asymptotic analysis for time complexity in best-case and worst-case scenario.

  • Describe an algorithm that takes as input two sorted lists of length n and m and...

    Describe an algorithm that takes as input two sorted lists of length n and m and an integer k and outputs the kst smallest element in their union. You can assume both lists contain integers and all entries are different.

  • Given two lists, write python code to print “True” if the two lists have at least...

    Given two lists, write python code to print “True” if the two lists have at least one common element. For example, x = [1,2,3], y=[3,4,5], then the program should print “True” since there is a common element 3.

  • Suppose that we have two unsorted lists of integers A and B. The lists are the...

    Suppose that we have two unsorted lists of integers A and B. The lists are the same size, n. a) Write an algorithm that outputs how many integers occur in both lists. An integer occurs at most once in each given list. For example: if A = [1,2,5,7,13,19] and B = [2,9,13,14,19,22], then we can see that elements {2, 13, 19} occur in both lists, so the output will be 3. b) If the lists were sorted, say how we...

  • Assume you are given two linear lists of size n each; consider the problem of determining...

    Assume you are given two linear lists of size n each; consider the problem of determining whether any element of one list is an element of the other (not value, element). Derive a lower bound for this problem and design an algorithm for this problem. Derive its time complexity. It should be as close the lower bound as possible. My work so far: //Copy list n1 into n3 int n3[n]; for(int I = 0; I < n; i++)n3[i] = n1[i];...

  • 1. Design an algorithm to find all the non-common elements in two sorted lists of numbers....

    1. Design an algorithm to find all the non-common elements in two sorted lists of numbers. What is the maximum number of comparisons your algorithm makes if the lengths of the two given lists are m and n, ?respectively 2. Estimate how many times faster it will be to find ged(98765, 56789) by Euclid's algorithm compared with the algorithm based on checking consecutive integers from min{m, n} down to gcd(m, n). 3. For each of the following functions, indicate how...

  • JAVA Objectives: 1. Apply linear search algorithm 2. Apply select sort algorithm 3. Apply array iteration...

    JAVA Objectives: 1. Apply linear search algorithm 2. Apply select sort algorithm 3. Apply array iteration skill Problem description: Write the following eight methods and write a main function to test these methods // return the index of the first occurrence of key in arr // if key is not found in arra, return -1 public static int linearSearch(int arr[], int key) // sort the arr from least to largest by using select sort algorithm public stati void selectSort(int arr[])...

  • (25pts) You are given two sorted lists of size m and n. Give an O(log m...

    (25pts) You are given two sorted lists of size m and n. Give an O(log m log n) time algorithm for computing the k-th smallest element in the union of the two lists Note that the only way you can access these values is through queries to the databases. Ina single query, you can specify a value k to one of the two databases, and the chosen database will return the k-th smallest value that it contains. Since queries are...

  • Q.(1)Describe the algorithm and java implementation for the following operations A. Create a singly linked list...

    Q.(1)Describe the algorithm and java implementation for the following operations A. Create a singly linked list L1 with 4 nodes. You can use insert operation to add nodes to the list. Each element represent an airport code (e.g. BOS, ATL, JFK, MSP, etc.). Display the list L1 after it is created. B. Given singly linked list L1, create another singly linked list L2 that contains the same elements but in the reverse order. Display the content of both L1 and...

  • 1. Please write a Divide-and-Conquer Java algorithm solving the following problem: Given an "almost sorted" array...

    1. Please write a Divide-and-Conquer Java algorithm solving the following problem: Given an "almost sorted" array of distinct integers, and an integer x, return the index of x in the array. If the element x is not present in the array, return -1. "Almost sorted" means the following. Assume you had a sorted array A[0…N], and then split it into two pieces A[0…M] and A[M+1…N], and move the second piece upfront to get the following: A[M+1]…A[N]A[0]…A[M]. Thus, the "almost sorted"...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT