You get a bonus - 1 coin for daily activity. Now you have 1 coin

Ways to Store Sets in Data Structures and Memory

Lecture



Basic concepts

The creator of set theory, G. Cantor, defined the concept of a set and an element of a set as follows: ”By a set we understand a collection of definite, distinct objects (real or imagined), called elements of the set, taken as a whole”. Usually sets are denoted by uppercase letters of the Latin alphabet, and their elements – by lowercase letters or numbers.

The number of elements in a set A is called the cardinality of the set A and is denoted |A| . If to each element of the set A one can assign a unique element of the set B, and to each element of the set B one can assign a unique element of the set A, then the sets A and B are called equipotent and this is denoted |A|=|B| .

A set consisting of a finite number of elements is called finite .

A set that is not finite is called infinite . An infinite set is called countable if it is equipotent to the set N of all natural numbers. It is said that all elements of a countable set can be enumerated. Otherwise an infinite set is called uncountable .

A set containing no elements is called empty and is denoted Ways to Store Sets in Data Structures and Memory or {}.

Usually in specific reasoning the elements of all sets are taken from some one, sufficiently broad set U (its own for each case), which is called the universe.

Two sets A and B are called equal if they consist of the same elements (A=B).

If M is a set, and a – is an element of it, then a belongs to M (aWays to Store Sets in Data Structures and Memory M). If, however, a is not an element of the set M, then a does not belong to M (aWays to Store Sets in Data Structures and Memory M).

Operations on sets

1. Inclusion of A in B Ways to Store Sets in Data Structures and Memory is true if every element of the set A belongs to the set B. In this case A is called a subset of B, and B – a superset of A.

Sets A and B are equal (A=B) if Ways to Store Sets in Data Structures and Memory. The empty set is a subset of every set.

The universe is a superset of every set.

The set of all subsets of a set M is called the power set and is denoted Ways to Store Sets in Data Structures and Memory:

Ways to Store Sets in Data Structures and Memory

For a finite set Ways to Store Sets in Data Structures and Memory. To each subset A one can assign a binary vector C of |M| digits in which:

Ci=1, if Ways to Store Sets in Data Structures and Memory and

Ci=0, if Ways to Store Sets in Data Structures and Memory, where i – is an element of the set M,

Ci – is the i-th digit of the vector C.

The number of distinct binary vectors of |M| digits equals 2|M|, hence |2M|=2|M|.

2. Strict inclusion of A in B Ways to Store Sets in Data Structures and Memoryis true if Ways to Store Sets in Data Structures and Memory. If Ways to Store Sets in Data Structures and Memory , then A is a proper subset of B.

3. The union of A and B (A U B) is the set consisting of all those and only those elements which belong to A or B, i.e.

Ways to Store Sets in Data Structures and Memory

4. The intersection of A and B (A n B) is the set consisting of all those and only those elements which belong to each of the sets A and B, i.e.

Ways to Store Sets in Data Structures and Memory

5. The difference of A and B (A-B) is the set consisting of all those and only those elements of the set A which do not belong to the set B, i.e.

Ways to Store Sets in Data Structures and Memory

To denote the difference of sets, discrete mathematics usually uses the symbol “\”.

6. The symmetric difference of A and B (AΔB) is the set consisting of all those and only those elements of the set A which do not belong to the set B, and only those elements of the set B which do not belong to the set A, i.e.

Ways to Store Sets in Data Structures and Memory

7. The complement of A to the universe U (Ways to Store Sets in Data Structures and Memory) is the set consisting of all those and only those elements of the universe U which do not belong to the set A, i.e.

Ways to Store Sets in Data Structures and Memory

For any two sets A and B, at least one of the following five cases (possibilities) holds:

Ways to Store Sets in Data Structures and Memory

1) A equals B;

2) A is strictly included in B;

3) B is strictly included in A;

4) A and B do not intersect;

5) A and B are in general position.

If both sets A and B are nonempty, then only one case holds.

Fig.1.1 shows Euler diagrams (Euler (1707-1783) proposed depicting sets as circles even before the creation of set theory by Cantor (1845-1918)), illustrating operations on sets. Sets are depicted as figures (ovals), and the result is highlighted graphically.

Ways to Store Sets in Data Structures and Memory

Ways to Store Sets in Data Structures and Memory

Fig.1.1. Operations on sets

The set of all subsets of a set U (the power set) together with the operations on sets forms an algebra of subsets (an algebra of Cantor) of the set U. An expression made up of subsets of the set U and operations on sets is called an expression in the algebra of subsets. An expression in the algebra of subsets may contain parentheses defining the order of execution of operations. If parentheses are not specified, then the order of execution of operations is determined by their priorities (table 1.1). The value of an expression in the algebra of subsets is a subset of the set U.

Table 1.1 Priorities of operations on sets

Ways to Store Sets in Data Structures and Memory

Properties of operations on sets

1. Idempotence:

Ways to Store Sets in Data Structures and Memory

2. Commutativity:

Ways to Store Sets in Data Structures and Memory

3. Associativity:

Ways to Store Sets in Data Structures and Memory

4. Distributivity for intersection and union:

Ways to Store Sets in Data Structures and Memory

5. Distributivity for intersection and difference:

Ways to Store Sets in Data Structures and Memory

6. Distributivity for intersection and symmetric difference:

Ways to Store Sets in Data Structures and Memory

7. Distributivity for difference:

Ways to Store Sets in Data Structures and Memory

8. Absorption:

Ways to Store Sets in Data Structures and Memory

9. Properties of zero:

Ways to Store Sets in Data Structures and Memory

10. Properties of the identity:

Ways to Store Sets in Data Structures and Memory

11. Involution: Ways to Store Sets in Data Structures and Memory

12. De Morgan's laws:

Ways to Store Sets in Data Structures and MemoryWays to Store Sets in Data Structures and Memory

13. De Morgan's laws for difference, intersection, and union:

Ways to Store Sets in Data Structures and Memory

14. Properties of the complement:

Ways to Store Sets in Data Structures and Memory

15. Definition of difference:

Ways to Store Sets in Data Structures and Memory

16. Definition of union:

Ways to Store Sets in Data Structures and Memory

17. Definition of intersection:

Ways to Store Sets in Data Structures and Memory

Ways of specifying sets

1. By enumerating all elements.

A={a,b,c} , B={b,a,c} , C={a} , D={1,2,3,5,9}. From the definition of equality of sets it follows that A=B.

2. By specifying a characteristic property, distinguishing the elements of a given set among the elements of the indicated other set or sets.

A={x | x Ways to Store Sets in Data Structures and MemoryN and x<10} (N – is the set of natural numbers),

B={1,2,3,4,5,6,7,8,9} , A=B.

3. By describing generating procedure with an indication of the set (or sets) over which the parameter (or parameters) of the procedure “ranges”.

A={x2 | xWays to Store Sets in Data Structures and Memory N} – the set of all squares of natural numbers.

By enumeration one can specify only a finite set, while with the help of a characteristic property or a generating procedure one can specify a finite or an infinite set.

Ways of representing and storing a set in the memory of a computer

Various methods can be used to represent a finite set in memory.

1. The elements of the set A are stored in a variable A of array type, and the cardinality of the set A – in a variable KA. The number of elements in the array A equals the cardinality of the universe. The elements of the array A are unordered.

2. The elements of the set A are stored in a variable A of array type, and the cardinality of the set A – in a variable KA. The number of elements in the array A equals the cardinality of the universe. The elements of the array A are ordered in ascending order.

3. The elements of the universe are numbered: U={u1,…,un}. The elements of the set A are stored in a variable A of array type, whose elements are of type boolean. If uiWays to Store Sets in Data Structures and Memory A, then Ai=true, otherwise Ai=false. The number of elements in the array A equals the cardinality of the universe.

4. The elements of the universe are numbered: U={u1,…,un}. The set A is represented by a code C in which: Ci=1, if uiWays to Store Sets in Data Structures and Memory A and Ci=0, if uiWays to Store Sets in Data Structures and Memory A, where Ci – is the i-th digit of the code C. Depending on the cardinality of the universe, the code C can be stored in a simple variable or in an array.

5. A set type can be used to store a set.

There are four main storage options:

  • Array
  • Hash table
  • Trees
  • Lists

Array — is a storage method in which the elements of a set are located sequentially in cells.

The most economical option, if the cardinality of the set is known and the elements are data of one type.

List — a storage method in which memory is allocated for each element separately, and exactly as much as needed.

Disadvantage: it is necessary to store a pointer to the next element and spend time working with it.

The complexity of an array and a list is linear, O(n).

Hash table — is a generalization of the method of storing a set of integers (keys) in the form of a bit vector, for the case when the cardinality of the universe U is very large relative to the cardinality of the sets one needs to work with.

Hash function — converts key values to the interval [0, m – 1], where m — is the size of the hash table, Ways to Store Sets in Data Structures and Memory Obviously, in this case many different key values will correspond to each index of the hash table. Therefore, firstly, the hash table has to store not bits but the key values themselves, and secondly, it is possible to place in it more than one key for each value of the mapping function (to resolve collisions).

If the table is built correctly and is not overloaded, then all functions (deletion, insertion, etc.) are performed in constant time. In the worst case (if there is a collision - repeating elements) the complexity is linear O(n).

Binary search tree (BST) — is a way of storing a set in the form of an extensible ordered list that preserves the ordering when inserting new elements without moving the elements already present.

BST — is a tree with weighted nodes, the weight at any node of which is greater than any weight in its left subtree and no greater than any weight in the right subtree. The number of steps of the algorithm for searching for an element of a set in such a tree does not exceed its height, i.e. it has complexity O(log n) . The operations of inserting a new element into the tree and deleting an element have the same complexity.

A BST can be obtained from an ordered sequence of keys if a binary tree of the corresponding cardinality is labeled by an internal (symmetric) method, and then the node numbers are replaced by the corresponding elements of the sequence. If the sequence is stored in an array, then the BST can be built by the bisection method: place the median element in order at the root of the tree, then recursively create the left subtree from the first half of the sequence, and the right one — from the second.

  Ways to Store Sets in Data Structures and Memory

The disadvantage of a BST — is that it works well only if it is balanced, i.e. the lengths of the paths from the root to any leaf are approximately the same. However, with element-by-element insertion of an ordered sequence into the tree, the tree degenerates, turning into a linear list. Searching, insertion, and deletion in such a tree will be performed not in logarithmic but in linear time. The probability of degeneration is quite high. Thus, out of 7 nodes only one completely balanced tree can be formed, while there are 64 completely degenerate ones. Therefore, algorithms for working with a BST are often supplemented with auto-balancing after insertion and deletion. The most commonly used schemes are the AVL tree, the RB tree, and the 2-3 tree.

Programmatic specification of sets

1. The elements of the set A are stored in a variable A of array type, and the cardinality of the set A – in a variable KA. The number of elements in the array A equals the cardinality of the universe. The elements of the array A are unordered.

Algorithm 1.1 (fig.1.3) for computing the inclusion of A in B (AWays to Store Sets in Data Structures and Memory B).

Input: A-array, storing the elements of the set A, KA=|A|;

B-array, storing the elements of the set B, KB=|B|;

Output: F=true, if AWays to Store Sets in Data Structures and Memory B, otherwise F=false.

Ways to Store Sets in Data Structures and Memory

Fig.1.3. Flowchart of the algorithm for computing the inclusion of A in B

Algorithm 1.2 (fig.1.4) for computing the equality of A and B (A=B).

Input: A-array, storing the elements of the set A, KA=|A|;

B-array, storing the elements of the set B, KB=|B|;

Output: F=true, if A=B, otherwise F=false.

Ways to Store Sets in Data Structures and Memory

Fig.1.4. Flowchart of the algorithm for computing the equality of A and B

Algorithm 1.3 (fig.1.5) for computing the union of A and B (A Ways to Store Sets in Data Structures and Memory B).

Input: A-array, storing the elements of the set A, KA=|A|;

B-array, storing the elements of the set B, KB=|B|;

Output: C-array, storing the union of the sets A and B, KC=|C|;

Ways to Store Sets in Data Structures and Memory

Fig.1.5. Flowchart of the algorithm for computing the union of A and B

Algorithm 1.4 (fig.1.6) for computing the intersection of A and B (A Ways to Store Sets in Data Structures and Memory B).

Input: A-array, storing the elements of the set A, KA=|A|;

B-array, storing the elements of the set B, KB=|B|;

let KA Ways to Store Sets in Data Structures and Memory KB;

Output: C-array, storing the intersection of the sets A and B, KC=|C|;

Ways to Store Sets in Data Structures and Memory

Fig.1.6. Flowchart of the algorithm for computing the intersection of A and B

Algorithm 1.5 (fig.1.7) for computing the difference of A and B (A-B).

Input: A-array, storing the elements of the set A, KA=|A|;

B is an array storing the elements of set B, KB=|B|;

Output: C is an array storing the difference of sets A and B, KC=|C|;

Ways to Store Sets in Data Structures and Memory

Fig.1.7. Flowchart of the algorithm for computing the difference of A and B

Algorithm 1.6 for computing the symmetric difference of A and B (AΔ B).

Input: A is an array storing the elements of set A, KA=|A|;

B is an array storing the elements of set B, KB=|B|;

Output: C is an array storing the symmetric difference

of sets A and B, KC=|C|;

1. C:=A-B Ways to Store Sets in Data Structures and Memory B-A; {or C:=(A Ways to Store Sets in Data Structures and Memory B)-(A Ways to Store Sets in Data Structures and MemoryB) }

2. End.

Algorithm 1.7 (fig.1.8) for computing the complement of A (Ways to Store Sets in Data Structures and Memory).

Input: A is an array storing the elements of set A, KA=|A|;

Output: C is an array storing the complement of set A, KC=|C|;

Ways to Store Sets in Data Structures and Memory

Fig.1.8. Flowchart of the algorithm for computing the complement of A

See also

  • [[b8510]]
  • [[b4506]]
  • [[b6192]]
  • [[b4323]]
  • [[b8941]]
  • Set
  • Array
  • Hash table
  • Trees
  • Lists

See also

created: 2022-02-17
updated: 2026-03-08
230



Was this answer useful?
Choose a quick rating so we can improve the next answer for you.
How satisfied are you?


Comments

To leave a comment

If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Lectures and tutorial on "Structures and data processing algorithms."

Terms: Structures and data processing algorithms.