GO-Ve01
ServicesDevelopersBooksDictionaryPapersWikidataEncyclopediaSearchBabelCareersDeck

Set (abstract data type)

In computer science, a set is an abstract data type that can store distinct values, without any particular order. It is a computer implementation of the mathematical concept of a finite set. Unlike most other collection types, rather than retrieving a specific element from a set, one typically tests a value for membership in a set. Some set data structures are designed for static or frozen sets that do not change after they are constructed. Static sets allow only query operations on their elements — such as checking whether a given value is in the set, or enumerating the values in some arbitrary order. Other variants, called dynamic or mutable sets, allow also the insertion and deletion of... Source: e01 Encyclopedia. Licence: CC BY-SA 4.0.

In computer science, a set is an abstract data type that can store distinct values, without any particular order. It is a computer implementation of the mathematical concept of a finite set. Unlike most other collection types, rather than retrieving a specific element from a set, one typically tests a value for membership in a set. Some set data structures are designed for static or frozen sets that do not change after they are constructed. Static sets allow only query operations on their elements — such as checking whether a given value is in the set, or enumerating the values in some arbitrary order. Other variants, called dynamic or mutable sets, allow also the insertion and deletion of elements from the set. A multiset is a special kind of set in which an element can appear multiple times in the set. ==Type theory== In type theory, sets are generally identified with their indicator function (characteristic function): accordingly, a set of values of type A may be denoted by 2^{A} or \mathcal{P}(A) . (Subtypes and subsets may be modeled by refinement types, and quotient sets may be replaced by setoids.) The characteristic function F of a set S is defined as: F(x) = \begin{cases} 1, & \mbox{if } x \in S \\ 0, & \mbox{if } x \not \in S \end{cases} In theory, many other abstract data structures can be viewed as set structures with additional operations and/or additional axioms imposed on the standard operations. For example, an abstract heap can be viewed as a set structure with a min(S) operation that returns the element of smallest value. ==Operations== fold(A 0 ,F,S) : returns the value A |S| after applying A i+1 := F(A i , e) for each element e of S, for some binary operation F. F must be associative and commutative for this to be well-defined. clear(S) : delete all elements of S. equal(S 1 ', S 2 ') : checks whether the two given sets are equal (i.e. contain all and only the same elements). hash(S) : returns a hash value for the static set S such that if equal(S 1 , S 2 ) then hash(S 1 ) = hash(S 2 ) Other operations can be defined for sets with elements of a special type: sum(S) : returns the sum of all elements of S for some definition of "sum". For example, over integers or reals, it may be defined as fold(0, add, S) . collapse(S) : given a set of sets, return the union. For example, collapse({{1}, {2, 3}}) == {1, 2, 3} . May be considered a kind of sum . flatten(S) : given a set consisting of sets and atomic elements (elements that are not sets), returns a set whose elements are the atomic elements of the original top-level set or elements of the sets it contains. In other words, remove a level of nesting – like collapse, but allow atoms. This can be done a single time, or recursively flattening to obtain a set of only atomic elements. For example, flatten({1, {2, 3}}) == {1, 2, 3} . nearest(S,x) : returns the element of S that is closest in value to x (by some metric). min(S) , max(S) : returns the minimum/maximum element of S. ==Implementations== Sets can be implemented using various data structures, which provide different time and space trade-offs for various operations. Some implementations are designed to improve the efficiency of very specialized operations, such as nearest or union . Implementations described as "general use" typically strive to optimize the element_of , add , and delete operations. A simple implementation is to use a list, ignoring the order of the elements and taking care to avoid repeated values. This is simple but inefficient, as operations like set membership or element deletion are O(n), as they require scanning the entire list. Sets are often instead implemented using more efficient data structures, particularly various flavors of trees, tries, or hash tables. As sets can be interpreted as a kind of map (by the indicator function), sets are commonly implemented in the same way as (partial) maps (associative arrays) – in this case in which the value of each key-value pair has the unit type or a sentinel value (like 1) – namely, a self-balancing binary search tree for sorted sets (which has O(log n) for most operations), or a hash table for unsorted sets (which has O(1) average-case, but O(n) worst-case, for most operations). A sorted linear hash table may be used to provide deterministically ordered sets. Further, in languages that support maps but not sets, sets can be implemented in terms of maps. For example, a common programming idiom in Perl that converts an array to a hash whose values are the sentinel value 1, for use as a set, is: my %elements = map { $_ => 1 } @elements; Other popular methods include arrays. In particular a subset of the integers 1..n can be implemented efficiently as an n-bit bit array, which also support very efficient union and intersection operations. A Bloom map implements a set probabilistically, using a very compact representation but risking a small chance of false positives on queries. The Boolean set operations can be implemented in terms of more elementary operations ( pop , clear , and add ), but specialized algorithms may yield lower asymptotic time bounds. If sets are implemented as sorted lists, for example, the naive algorithm for union(S,T) will take time proportional to the length m of S times the length n of T; whereas a variant of the list merging algorithm will do the job in time proportional to m+n. Moreover, there are specialized set data structures (such as the union-find data structure) that are optimized for one or more of these operations, at the expense of others. ==Multiset== A generalization of the notion of a set is that of a multiset or bag, which is similar to a set but allows repeated ("equal") values (duplicates). This is used in two distinct senses: either equal values are considered identical, and are simply counted, or equal values are considered equivalent, and are stored as distinct items. For example, given a list of people (by name) and ages (in years), one could construct a multiset of ages, which simply counts the number of people of a given age. Alternatively, one can construct a multiset of people, where two people are considered equivalent if their ages are the same (but may be different people and have different names), in which case each pair (name, age) must be stored, and selecting on a given age gives all the people of a given age. Formally, it is possible for objects in computer science to be considered "equal" under some equivalence relation but still distinct under another relation. Some types of multiset implementations will store distinct equal objects as separate items in the data structure; while others will collapse it down to one version (the first one encountered) and keep a positive integer count of the multiplicity of the element. As with sets, multisets can naturally be implemented using hash table or trees, which yield different performance characteristics. The set of all bags over type T is given by the expression bag T. If by multiset one considers equal items identical and simply counts them, then a multiset can be interpreted as a function from the input domain to the non-negative integers (natural numbers), generalizing the identification of a set with its indicator function. In some cases a multiset in this counting sense may be generalized to allow negative values, as in Python. C++'s Standard Template Library implements both sorted and unsorted multisets. It provides the multiset class for the sorted multiset, as a kind of associative container, which implements this multiset using a self-balancing binary search tree. It provides the unordered_multiset class for the unsorted multiset, as a kind of unordered associative container, which implements this multiset using a hash table. The unsorted multiset is standard as of C++11; previously SGI's STL provides the hash_multiset class, which was copied and eventually standardized. For Java, third-party libraries provide multiset functionality: Apache Commons Collections provides the Bag and SortedBag interfaces, with implementing classes like HashBag and TreeBag . Google Guava provides the Multiset interface, with implementing classes like HashMultiset and TreeMultiset . Apple provides the NSCountedSet class as part of Cocoa, and the CFBag and CFMutableBag types as part of CoreFoundation. Python's standard library includes collections.Counter , which is similar to a multiset. Smalltalk includes the Bag class, which can be instantiated to use either identity or equality as predicate for inclusion test. Where a multiset data structure is not available, a workaround is to use a regular set, but override the equality predicate of its items to always return "not equal" on distinct objects (however, such will still not be able to store multiple occurrences of the same object) or use an associative array mapping the values to their integer multiplicities (this will not be able to distinguish between equal elements at all). Typical operations on bags: contains(B, x) : checks whether the element x is present (at least once) in the bag B is_sub_bag(B 1 , B 2 ) : checks whether each element in the bag B 1 occurs in B 1 no more often than it occurs in the bag B 2 ; sometimes denoted as B 1 ⊑ B 2 . count(B, x) : returns the number of times that the element x occurs in the bag B; sometimes denoted as B # x. scaled_by(B, n) : given a natural number n, returns a bag which contains the same elements as the bag B, except that every element that occurs m times in B occurs n * m times in the resulting bag; sometimes denoted as n ⊗ B. union(B 1 , B 2 ) : returns a bag containing just those values that occur in either the bag B 1 or the bag B 2 , except that the number of times a value x occurs in the resulting bag is equal to (B 1 # x) + (B 2 # x); sometimes denoted as B 1 ⊎ B 2 . ===Multisets in SQL=== In relational databases, a table can be a (mathematical) set or a multiset, depending on the presence of unicity constraints on some columns (which turns it into a candidate key). SQL allows the selection of rows from a relational table: this operation will in general yield a multiset, unless the keyword DISTINCT is used to force the rows to be all different, or the selection includes the primary (or a candidate) key. In ANSI SQL the MULTISET keyword can be used to transform a subquery into a collection expression: SELECT expression1, expression2... FROM table_name... is a general select that can be used as subquery expression of another more general query, while MULTISET(SELECT expression1, expression2... FROM table_name...) transforms the subquery into a collection expression that can be used in another query, or in assignment to a column of appropriate collection type. ==See also== Bloom filter Disjoint set Set (mathematics) ==Notes== ==References==

source: e01 Encyclopedia · licence: CC BY-SA 4.0 · page · source revision

Cite this

"Set (abstract data type)". 1769627106. e01 Encyclopedia. CC BY-SA 4.0. https://en.wikipedia.org/w/index.php?oldid=1335354914

JSON record · API collection · BibTeX · RIS · CSL-JSON · citation policy

Save to pass ·