Question

In: Computer Science

Provide pseudo-code to compute the difference of two sets, S - T, using: a. sets represented...

Provide pseudo-code to compute the difference of two sets, S - T, using:

a. sets represented by linked-lists of characters.

b. sets represented by bit strings. (hint - what is "exclusive or?")

Solutions

Expert Solution

If the sets are represented as sorted linked lists. The running time is O(|S|+|T|)O(|S|+|T|).

We don't need to compare every element of AA against every element of BB, pairwise. That would lead to a runtime of O(|S|×|T|)O(|S|×|T|), which is much worse. Instead, to compute the symmetric difference of these two sets, we can use a technique similar to the "merge" operation in mergesort, suitably modified to omit values that are common to both sets.

In more detail, we can build a recursive algorithm like the following to compute S∖TS∖T, assuming SS and TT are represented as linked lists with their values in sorted order:

  1. difference(S, T):
  2.     if len(T)=0:
  3.         return S
  4.     if len(S)=0:
  5.         return S
  6.     if S[0] < T[0]:
  7.         return [S[0]] + difference(S[1:], T)
  8.     elsif S[0] = T[0]:
  9.         return difference(S[1:], T[1:]) # omit the common element
  10.     else:
  11.         return [T[0]] + difference(S, T[1:])

Thank you.


Related Solutions

Given two sets S and T, the direct product of S and T is the set...
Given two sets S and T, the direct product of S and T is the set of ordered pairs S × T = {(s, t)|s ∈ S, t ∈ T}.Let V, W be two vector spaces over F. (a) Prove that V × W is a vector space over F under componentwise addition and scalar multiplication (i.e. if (v1, w1),(v2, w2) ∈ V × W, then (v1, w1) + (v2, w2) = (v1+w1, v2+w2) and a(v, w) = (av, aw)...
Write a recursive algorithm in pseudo-code to compute the “power list” of a given list of...
Write a recursive algorithm in pseudo-code to compute the “power list” of a given list of integers. Assume that the List type has members: int List.length returns the length of the list. void List.push(T n) pushes an element n to the front of the list T List.pop() pops an element from the front of the list. List$$ List$$.concat(List$$ other) returns the concatenation of this list with other. Explain in plain English the reasoning behind your algorithm. Power Lists should be...
Using the set of scores below, determine if the difference between the two sets of scores...
Using the set of scores below, determine if the difference between the two sets of scores is significant at alpha .05, two tailed.  Determine this through the paired t-test.  Show the 5 steps of hypothesis testing. Solve manually, by hand showing all steps. Do not use SPSS or excel. X                     Y             3                      8             8                      7             5                      6             7                      7             6                      6             8                      9
Consider two sets of integers represented in arrays, X = [x1, x2, . . . ,...
Consider two sets of integers represented in arrays, X = [x1, x2, . . . , xn] and Y = [y1, y2, . . . , yn]. Write two versions of a FindSetUnion(X, Y ) algorithm to find the union of X and Y as an array. An element is in the union of X and Y if it appears in at least one of X and Y . You may make use any algorithm introduced in the lectures to...
Using pseudocode or C++ code, write a function to compute and return the product of two...
Using pseudocode or C++ code, write a function to compute and return the product of two sums. The first is the sum of the elements of the first half of an array A. The second is the sum of the elements of the second half of A. The function receives A and N ≥ 2, the size of A, as parameters. (in c++)
Cyphers are hundred of years old. Using pseudo code and the a substitution cypher write a...
Cyphers are hundred of years old. Using pseudo code and the a substitution cypher write a function to decode the following message and give the expected solution for the this message             IUHHKIN NAHKK KBNHT VUOYNG T Z I J K M W A O P Q R S Y U V L H G N E C D B F X A B C D E F G H I J K L M N O P Q R...
2.6 Consider all the possible sets of two square roots s, t of 1 (mod 35)...
2.6 Consider all the possible sets of two square roots s, t of 1 (mod 35) where s ≢ t (mod 35) (there are six of them, since addition is commutative (mod 35). For all possible combinations, compute gcd(s + t, 35). Which combinations give you a single prime factor of 35? 2.7 Using CRT notation, show what is going on for all the combinations you considered in #2.6. Explain why gcd(s + t, 35) sometimes gave you a factor,...
S = {(2,5,3)} and T = {(2,0,5)} are two clusters. Two clusters that S and T...
S = {(2,5,3)} and T = {(2,0,5)} are two clusters. Two clusters that S and T spans are L(S) and L(T) . Is the intersection of L (S) and L (T) a vector space? If yes, find this vector space. If no, explain why there is no vector space.
(General guidance: in responding to the following four questions, do not simply provide pseudo-code. Rather explain...
(General guidance: in responding to the following four questions, do not simply provide pseudo-code. Rather explain the main insight in such a way that an experienced programmer could devise an implementation of the algorithm on the basis of your explanation. Indeed, pseudo-code may not be necessary at all.) Give an O(n) algorithm that determines (1) the number of subsequences of an input vector that sum to an even number and (2) ) the number of subsequences of an input vector...
(General guidance: in responding to the following four questions, do not simply provide pseudo-code. Rather explain...
(General guidance: in responding to the following four questions, do not simply provide pseudo-code. Rather explain the main insight in such a way that an experienced programmer could devise an implementation of the algorithm on the basis of your explanation. Indeed, pseudo-code may not be necessary at all.) Give an algorithm that takes as input a string and returns the length of its longest palindromic subsequence.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT