Question

5. Design an algorithm that takes two arrays, and returns true if the arrays are disjoint, i.e. have no elements in common. W

0 0
Add a comment Improve this question Transcribed image text
Answer #1

SOLUTION:

NOTE:  I have written an executable python program since I need to show an executable I have written this. There is no difference in terms of pseudocode and program in pseudo-code we will write English like statements here we will keep syntax for that there is no difference in that.

CODE:

def disjointSet(list1,list2):
##we need to iterate through all elements in the list so we have written this loop

for i in range(len(list1)):
##we need to iterate through list2 to check for matching for that
##we have written another loop
for j in range(len(list2)):
if(list1[i]==list2[j]):
##if any element is matched then it is not an disjoint set so
## we will return false
return False

##if no elements are then only this statement will execute this meansthat
## the two sets are disjoint so we will return True
return True

##call the function to test
print(disjointSet([1,2,3,4,8],[5,6,7,8]))

IMAGE:

le disjoint.py - C:/Users/vinod/AppData/Local/Programs/Python/Python37-32/disjoint.py (3.7.2) File Edit Format Run Options WiOUTPUT:

RESTART: C:/Users/vinod/AppData/Local/Programs/Python/Python37-32/disjoint.py False

TIME COMPLEXITY:

HERE n-> Number of elements in list1

m-> Number of elements in list2

BEST CASE COMPLEXITY: O(n)-> this is when we have the first element is found in the next list we will find that the list is disjoint in only one parse of the second list its complexity is "n"

WORST CASE COMPLEXITY: \Omega(n*m)This is when a list is disjoint we need to check each element in the list1 with list2 so the complexity here is like each element is check for each element in list2 so it is (n*m)

Add a comment
Know the answer?
Add Answer to:
5. Design an algorithm that takes two arrays, and returns true if the arrays are disjoint,...
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
  • Question 4 6 points You have to design an algorithm that verify if an array contains...

    Question 4 6 points You have to design an algorithm that verify if an array contains duplicate values. This algorithm simply returns true as soon as two identical values are found. To perform this task you must not change the order of the elements in the array (so you cannot sort it) or use an auxiliary array. Write the pseudo-code of this algorithm and give the Big-2 complexity in the worst case.

  • Exercise 1 Use Top-Down Design to “design” a set of instructions to write an algorithm for...

    Exercise 1 Use Top-Down Design to “design” a set of instructions to write an algorithm for “travel arrangement”. For example, at a high level of abstraction, the algorithm for “travel arrangement” is: book a hotel buy a plane ticket rent a car Using the principle of stepwise refinement, write more detailed pseudocode for each of these three steps at a lower level of abstraction. Exercise 2 Asymptotic Complexity (3 pts) Determine the Big-O notation for the following growth functions: 1....

  • pseudo code only no coding in necessary (a) Design a variant "binary" search algorithm which splits...

    pseudo code only no coding in necessary (a) Design a variant "binary" search algorithm which splits the set not into 2 sets of equal sizes (½ and ½), but into 2 sets of sizes one quarter (14) and three quarters (3/4) (b) Give the recurrence relations of your algorithm. (c) How does this algorithm compare with the original binary search in both the best case complexity and the worst case complexity?

  • Introduction to the Design and Analysis of Algorithms Note: Present Algorithms in Pseudocode Give an algorithm...

    Introduction to the Design and Analysis of Algorithms Note: Present Algorithms in Pseudocode Give an algorithm to print all the common elements in two sorted arrays of integers, A and B of sizes m and n, respectively. Assume that the numbers in each of the arrays are distinct. Input: Sorted arrays A[m] and B [n]. Output: Common elements. ExA:3.5 6 9 12 17 23; B:2581011 12 15 16 17 19 22 25; Output: 5 12 17

  • problem 2 can use Det-Selection(A, p, q, r) as a sub-routine (i.e, you don't need to...

    problem 2 can use Det-Selection(A, p, q, r) as a sub-routine (i.e, you don't need to write its pseudo-code). To sort an array A, you will then call Det-QuickSort(A, 1, n). You also need to provide the worst case time complexity analysis of your algorithm. 2. (20 points) Given a set of n distinct numbers, we wish to find the k largest in sorted order using a comparison-based algorithm. Give an algorithm that implements each of the following methods, and...

  • Give an efficient algorithm that takes a directed graph G = (V, E) and two vertices...

    Give an efficient algorithm that takes a directed graph G = (V, E) and two vertices u, v E V, and determines if there are at least two edge-disjoint paths in G from u to v. i.e., your algorithm should determine whether there are at least two paths from u to v in G that have no edges in common. Argue your algorithm's correctness and analyze its time complexity.

  • Practice: Two-dimensional arrays Design an algorithm that takes two locations from the user and outputs the...

    Practice: Two-dimensional arrays Design an algorithm that takes two locations from the user and outputs the correct points, as shown in the table above. For this problem you only need to output what is on the white lines of the table. You must declare the 2D array and give it a suitable name, but for initialization you can simply write input mySuitablyNamedArray from points table. The user enters the two locations as numbers, for example 0 would refer to Main,...

  • Your task is to design algorithms to solve the following problems. For full credit, your algorithm...

    Your task is to design algorithms to solve the following problems. For full credit, your algorithm must run in logarithmic time. Given a number n greaterthanorequalto 1 and a (user-specified) error tolerance e, you want to approximate the squareroot of n to within error tolerance e. Specifically, you want to return an x = Squareroot n that satisfies |x^2 - n| greaterthanorequalto e. For example, to compute the squareroot of n = 2 with e = 0.01, an acceptable answer...

  • Problem 3: (5 2 points) Design an algorithm that takes an array of positive integers A...

    Problem 3: (5 2 points) Design an algorithm that takes an array of positive integers A of length n and a positive integer T as an input and finds the largest N < T such that N can be written as a sum of some elements of A and returns such a representation of N. The complexity of the algorithms has to be O(nT). For example, for A 3,7, 10 and T 19, the output is 17 7+10, because we...

  • In pseudocode only Design (pseudocode) a program (name it Occurrences) to determine whether two two-dimensional arrays...

    In pseudocode only Design (pseudocode) a program (name it Occurrences) to determine whether two two-dimensional arrays are equivalent or not. Two arrays are equivalent if they contain the same values in any order. The program main method defines two two-dimensional array of size 3-by-3 of type integer, and prompts the user to enter integer values to initialize the arrays. The main method calls method isEquivalent()that takes two two-dimensional arrays of integer and returns true (boolean value) if the arrays contain...

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