SetComparisons

advertisement
12th December 2006
Elvira Trunau
SET COMPARISONS
I. Purpose
This paper treats the two general set operations:
 Equality
 Subset
and tries to find an algorithm, witch yields the answer if two sets are equal, unequal,
or if one set is a subset of the other one.
II. Definitions
-
Equality
Two relations R and S are equal, if they have the same number of tuples and the
same tuples as well.
-
Subset
If every tuple of the relation R is also a tuple of the relation S, then R is said to be
a (proper) subset of S. Equivalently, the relation S is a superset of R. If R has the
same number of tuples as the relation S, then is R equal to S.
III. Algorithm
Following cases will be treated by this algorithm:




R equal S
R unequal S
R proper subset of S
R subset or equal S
/* This general procedure handles the different cases dependent on the different kinds of set
comparison. It returns a string, which it gets from the appropriate sub function, which is the
result of the comparison. The instruction ‘break’ is used to jump out of the current case
statement. */
string set_comparison (R, S, request)
result <== string;
SWITCH (request)
CASE equalOrNot:
result <-- equalOrNot (R, S, result);
break;
CASE properSubset:
result <-- properSubset (R, S, result);
break;
CASE subsetOrEqual:
result <-- subsetOrEqual (R, S, result);
break;
return result;
END_setComparison
1
12th December 2006
Elvira Trunau
Comment: These three cases are required to supply all the different set comparisons.
The similar cases are combined into one case statement each time it is possible and
convenient.
/* This procedure determines, if R and S are equal or not. It calls a helper function equality,
which compares each tuple of R with each tuple of S. It returns a string, which is ‘equal’ or
‘unequal’. */
string equalOrNot (R, S, result)
found <== bool;
found <-- equality (R, S);
IF (found = true)
result <-- ‘equal’;
ELSE result <-- ‘unequal’;
return result;
END_equalOrNot;
/* This procedure determines, if R is a proper subset of S or if S is a proper subset of R. It
calls the helper function subset to examine the matter exactly. The procedure returns a result
dependend on the determination. */
string properSubset (R, S, result)
found <== bool;
IF (cardinality(R) > cardinality(S)) /* if R has more tuples than S */
found <-- subset(S);
IF (found = true)
result <-- ‘S_properSubsetOfR’;
ELSE result <-- ‘S_notProperSubsetOfR’;
END_IF;
ELSE IF (cardinality(R) < cardinality(S)) /* if S has more tuples than R */
found <-- subset(R);
IF (found = true)
result <-- ‘R_properSubsetOfS’;
ELSE result <-- ‘R_notProperSubsetOfS’;
END_ELSE_IF;
ELSE result <-- ‘notProperSubset’;
return result;
END_properSubset;
/* This procedure determines, if R is a subset of or equal to S and the other way round. It
calls the helper functions equality and properSubset. If the two conditions are satisfied, it
returns the appropriate positive result as a string. If not, it returns the appropriate negative
result as a string. */
string subsetOrEqual (R, S, result)
found <== bool;
temp <== string;
found <-- equality(R, S);
2
12th December 2006
Elvira Trunau
IF (found = true)
temp <-- properSubset (R, S, result);
IF (temp = ‘S_properSubsetOfR’)
result <-- ‘SsubsetOrEqualR’;
ELSE IF (temp = ‘R_properSubsetOfS’)
result <-- ‘RsubsetOrEqualS’;
ELSE result <-- ‘notSubsetOrEqual’;
ELSE result <-- ‘notSubsetOrEqual’;
return result;
END_subsetOrEqual;
/* This procedure determines if S is a subset of R (or R is a subset of S) and returns ’true’, if it
is a subset or ‘false’, if it is not. */
bool subset (RorS)
IF (RorS = S)
IF (R contains S) /* if R contains all the tuples of S */
return true;
ELSE return false;
ELSE IF (RorS = R)
IF (S contains R) /* if S contains all the tuples of R */
return true;
ELSE return false;
END_subset
/* This function compares each tuple of R with each tuple of S and returns ‘true’, if R and S
are equal and ‘false’ if there are not equal. The instruction ‘break’ is used to jump out of the
current loop. */
bool equality (R, S)
i, j <== integer;
IF (cardinality(R) = cardinality(S)) /*if R and S have the same number of tuples*/
FOR (i = 0; i < cardinality(R); i++) /* iterates through relation R */
j = i;
tuple (i) <-- get_next_tuple (R);
tuple (j) <-- get_next_tuple (S);
IF (tuple(i) = tuple(j))*
return true;
ELSE return false;
break;
END_FOR;
END_IF
ELSE return false;
END_equality
*
The tuple comparison does not specify how to take into account the physical storage of tuples. The
attributes could be stored in different physical sequences in the two tuples.
3
Download