Sets and MultiSets (Bags)

From

(Difference between revisions)
Jump to: navigation, search
m (Protected "Sets and MultiSets (Bags)" ([edit=autoconfirmed] (indefinite) [move=autoconfirmed] (indefinite)))
m
 
(5 intermediate revisions not shown)
Line 1: Line 1:
-
In [[computer science]], a '''set''' is a collection ([[container (data structure)|container]]) of certain values, without any particular [[Canonical order|order]], and no repeated values. It corresponds with a [[finite set]] in mathematics.  Disregarding [[sequence]], and the fact that there are no repeated values, it is the same as a [[list (computer science)|list]]. A set can be seen as an [[associative array]] (partial mapping) in which the value of each key-value pair is ignored.
+
<noinclude>
 +
{{CS2:BookNav|base=/|up=Contents: CS2|prev=Unordered Collection ADTs|next=Maps and Dictionaries}}
 +
</noinclude>
 +
<br/>
 +
In computer science, a '''set''' is a collection (container) of certain values, without any particular order, and no repeated values. It corresponds with a finite set in mathematics.  Disregarding sequence, and the fact that there are no repeated values, it is the same as a list. A set can be seen as an associative array (partial mapping) in which the value of each key-value pair is ignored.
<br/><br/>
<br/><br/>
== Implementations ==
== Implementations ==
-
Sets can be implemented using various [[data structure]]s. Ideal set data structures make it efficient to check if an object is in the set, as well as enabling other useful operations such as iterating through all the objects in the set, performing a union or intersection of two sets, or taking the complement of a set in some limited domain. Any [[associative array]] data structure can be used to implement a set by letting the set of keys be the elements of the set and ignoring the values. Because of the similarity to associative arrays, sets are commonly implemented in the same ways, 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<ref> {{Citation | title=Sorted Linear Hash Table | first1=Thomas | last1=Wang |url=http://www.concentric.net/~Ttwang/tech/sorthash.htm | year=1997 }} </ref> may be used to provide deterministically ordered sets.
+
Sets can be implemented using various data structures. Ideal set data structures make it efficient to check if an object is in the set, as well as enabling other useful operations such as iterating through all the objects in the set, performing a union or intersection of two sets, or taking the complement of a set in some limited domain. Any associative array data structure can be used to implement a set by letting the set of keys be the elements of the set and ignoring the values. Because of the similarity to associative arrays, sets are commonly implemented in the same ways, 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<ref> {{Citation | title=Sorted Linear Hash Table | first1=Thomas | last1=Wang |url=http://www.concentric.net/~Ttwang/tech/sorthash.htm | year=1997 }} </ref> may be used to provide deterministically ordered sets.
<br/><br/>
<br/><br/>
-
Other popular methods include [[array]]s. 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.
+
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.
<br/><br/>
<br/><br/>
-
However, very few of these data structures support [[set operations (mathematics)|set operations]] such as union or intersection efficiently. For these operations, more specialized set data structures exist.
+
However, very few of these data structures support set operations such as union or intersection efficiently. For these operations, more specialized set data structures exist.
<br/><br/>
<br/><br/>
==Multiset (Bag)==
==Multiset (Bag)==
-
A variation of the set is the '''[[multiset]]''' or '''bag''', which is the same as a set data structures, but allows repeated values.  Formally, a multiset can be thought of as an associative array that maps unique elements to [[positive integer]]s, indicating the mulplicity of the element, although actual implementation may vary.  C++'s Standard Template Library provides the "<code>multiset</code>" class for the sorted multiset, and SGI's STL provides the "<code>hash_multiset</code>" class, which implements a set using a hash table. [[Apache Commons]] Collections provides [http://commons.apache.org/collections/api-release/org/apache/commons/collections/Bag.html Bag] and SortedBag interface for [[Java (programming language)|Java]]; along with implementing classes like HashBag and TreeBag, analogous to similarly-named Set implementations.
+
A variation of the set is the '''multiset''' or '''bag''', which is the same as a set data structures, but allows repeated values.  Formally, a multiset can be thought of as an associative array that maps unique elements to positive integers, indicating the mulplicity of the element, although actual implementation may vary.  C++'s Standard Template Library provides the "<code>multiset</code>" class for the sorted multiset, and SGI's STL provides the "<code>hash_multiset</code>" class, which implements a set using a hash table. Apache Commons Collections provides [http://commons.apache.org/collections/api-release/org/apache/commons/collections/Bag.html Bag] and SortedBag interface for Java; along with implementing classes like HashBag and TreeBag, analogous to similarly-named Set implementations.
<br/><br/>
<br/><br/>
 +
----
 +
{{CS2/ChapNav}}
 +
----
 +
[[Category:CS2|{{SUBPAGENAME}}]]

Current revision as of 16:54, 2 June 2010

← Unordered Collection ADTs ↑ Contents: CS2 Maps and Dictionaries →


In computer science, a set is a collection (container) of certain values, without any particular order, and no repeated values. It corresponds with a finite set in mathematics. Disregarding sequence, and the fact that there are no repeated values, it is the same as a list. A set can be seen as an associative array (partial mapping) in which the value of each key-value pair is ignored.

Implementations

Sets can be implemented using various data structures. Ideal set data structures make it efficient to check if an object is in the set, as well as enabling other useful operations such as iterating through all the objects in the set, performing a union or intersection of two sets, or taking the complement of a set in some limited domain. Any associative array data structure can be used to implement a set by letting the set of keys be the elements of the set and ignoring the values. Because of the similarity to associative arrays, sets are commonly implemented in the same ways, 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[1] may be used to provide deterministically ordered sets.

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.

However, very few of these data structures support set operations such as union or intersection efficiently. For these operations, more specialized set data structures exist.

Multiset (Bag)

A variation of the set is the multiset or bag, which is the same as a set data structures, but allows repeated values. Formally, a multiset can be thought of as an associative array that maps unique elements to positive integers, indicating the mulplicity of the element, although actual implementation may vary. C++'s Standard Template Library provides the "multiset" class for the sorted multiset, and SGI's STL provides the "hash_multiset" class, which implements a set using a hash table. Apache Commons Collections provides Bag and SortedBag interface for Java; along with implementing classes like HashBag and TreeBag, analogous to similarly-named Set implementations.


CS2: Data Structures
Theory of Computation - ADT Preliminaries
Linear ADTs - Tree ADTs - Graph ADTs - Unordered Collection ADTs


Personal tools
MediaWiki Appliance - Powered by TurnKey Linux