Data Structures and Algorithms (Chapter 3): Sets

Set is the basic structure of data structure, it is completely consistent with the set in mathematics, then let's see what is the set in mathematics?

1. What is a collection

A collection refers to a collective of concrete or abstract objects with a certain nature. Among them, these objects constituting the set are called the elements of the set.

To cite a few examples, if humans are taken as a set, the Chinese are an element in the set. If the 0-100number of nature as a collection, 0is an element in the set, denoted 0∈100, 101not the elements of the collection, it is denoted by 0∉100. For more details, please open the first chapter of Gaoshu (1) and take a closer look.

Second, the characteristics of the collection

The collection has the following three characteristics:

  • Certainty: Given a set, any given element, the element belongs to or does not belong to the set, the two must be one of the two, no ambiguity is allowed.

  • Mutuality: In a set, any two elements are considered different, that is, each element can only appear once. Sometimes it is necessary to characterize the situation where the same element appears multiple times, and multiple sets can be used, in which elements are allowed to appear multiple times.

  • Disorder: In a set, the status of each element is the same, and the elements are disordered. The order relationship can be defined on the set. After the order relationship is defined, the elements can be sorted according to the order relationship. But as far as the characteristics of the set itself are concerned, there is no inevitable order among the elements.

Three, the type of collection

1. empty set

If a set does not contain any elements, it is called an empty set and recorded as .

Insert picture description here

The code can be represented as an empty array (for ease of understanding, are to demonstrate all of the following Pythonexample to explain).

In [1]: data = set()                                                                                               

In [2]: data                                                                                                       
Out[2]: set()

2. Subset

If the set of Aall elements belonging to the set B, then the Aset is a Bsubset of the set.

Insert picture description here

In [3]: data = set({1, 2, 3, 4, 5})                                                                                

In [4]: data                                                                                                       
Out[4]: {1, 2, 3, 4, 5}

In [5]: 1 in data                                                                                                  
Out[5]: True

In [6]: 0 in data                                                                                                  
Out[6]: False

3. Intersection

All belong to the set Aand the set belonging to Ba collection of elements called the composition of the intersection, denoted by A∩B.

Insert picture description here

In [7]: data = set({1, 2, 3, 4, 5})                                                                                

In [8]: s = set({2, 4, 6})                                                                                         

In [9]: data & s                                                                                                   
Out[9]: {2, 4}

4. Union

Belonging to the set of all Aor part of the set of Bset of elements consisting of called and set, denoted by A∪B.

Insert picture description here

In [10]: data | s                                                                                                   
Out[10]: {1, 2, 3, 4, 5, 6}

5. Subtraction

All belong to the set Anot belonging to the set Bset of elements consisting of a set of known Awith respect to the set of Bdifference sets, denoted A-B.

Insert picture description here

In [11]: data - s                                                                                                   
Out[11]: {1, 3, 5}

In [12]: s - data                                                                                                   
Out[12]: {6}

6. Complement

Generally refers to complement absolute complement, if Ais Ba subset (subset of the concept of looking up ), Ball do not belong to Athe elements consisting of a collection called Athe Bcomplement of. It can be said that the complement set is a special case of the difference set.

Insert picture description here

AIt is Ba subset of Ba Asuperset.

In [13]: s1 = set({2, 4})                                                                                           

In [14]: data - s1                                                                                                  
Out[14]: {1, 3, 5}

7. Symmetric Difference

Set of Athe set Bare not all the A∩Belements of the collection is called symmetric difference, denoted A△B, namely: A△B=(A∪B)-(A∩B).

Insert picture description here

In [13]: data ^ s                                                                                                   
Out[13]: {1, 3, 5, 6}

Fourth, the algorithm implementation of the collection

The above code is used Pythonin data collection has been achieved good structure, then you are familiar with our language this set of data structures implemented algorithm.

This is really simple, I won't write it here (maybe I will write it when I'm not busy in the future). There are a few points to note, only using arrays as the basic data structure to achieve, the characteristics and types of the collection must all be satisfied, performance and efficiency do not need to be considered for the time being, after learning the string matching algorithm and sorting algorithm, etc., come back to optimize.

Guess you like

Origin blog.csdn.net/yilovexing/article/details/107335751
Recommended