|
ADS
Algorithms and Data Structures
|
The SET as an unsorted LIST of section 8.2. More...
Files | |
| file | ADS/set/ListSet.h |
| The SET as an unsorted LIST of section 8.2: contains(), insert() and remove() in \( O( n ) \), insertNew() in \( O( 1 ) \) when the item is known to be absent, the operations of two sets in \( O( n m ) \), which is optimal when the items can only be compared for equality (exercises 8.1 and 8.2). | |
Classes | |
| class | ADS::ListSet< T > |
| a set as an unsorted list More... | |
Functions | |
| template<class T> | |
| ListSet< T > | ADS::setUnion (const ListSet< T > &A, const ListSet< T > &B) |
| the union: \( C \) starts as \( B \) and gets the items of \( A \) not in \( B \), inserted without looking for them in \( C \), \( O( n m ) \) time (exercise 8.2) | |
| template<class T> | |
| ListSet< T > | ADS::setIntersection (const ListSet< T > &A, const ListSet< T > &B) |
| the intersection, \( O( n m ) \) time (exercise 8.1) | |
| template<class T> | |
| ListSet< T > | ADS::setDifference (const ListSet< T > &A, const ListSet< T > &B) |
| the difference, \( O( n m ) \) time (the procedure of section 8.2) | |
| template<class T> | |
| ListSet< T > | ADS::setSymmetricDifference (const ListSet< T > &A, const ListSet< T > &B) |
| the symmetric difference, \( O( n m ) \) time (exercise 8.5) | |
The SET as an unsorted LIST of section 8.2.
The definitions, the invariants and the costs of the area are in Theory of sets.
| ListSet< T > ADS::setDifference | ( | const ListSet< T > & | A, |
| const ListSet< T > & | B ) |
the difference, \( O( n m ) \) time (the procedure of section 8.2)
In pseudocode (section 8.2), each search a scan of the other list:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{setDifference}( A , B ) \cr 2 & \quad C \gets \emptyset \cr 3 & \quad \textbf{foreach}\ x \in A\ \textbf{do}\ \textbf{if} \ \textbf{not}\ B.\mathrm{contains}( x )\ \textbf{then} \ C.\mathrm{insertNew}( x ) \cr 4 & \quad \textbf{return}\ C \end{array}} \]
| ListSet< T > ADS::setIntersection | ( | const ListSet< T > & | A, |
| const ListSet< T > & | B ) |
the intersection, \( O( n m ) \) time (exercise 8.1)
In pseudocode (exercise 8.1), each search a scan of the other list:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{setIntersection}( A , B ) \cr 2 & \quad C \gets \emptyset \cr 3 & \quad \textbf{foreach}\ x \in A\ \textbf{do}\ \textbf{if} \ B.\mathrm{contains}( x )\ \textbf{then}\ C.\mathrm{insertNew}( x ) \cr 4 & \quad \textbf{return}\ C \end{array}} \]
| ListSet< T > ADS::setSymmetricDifference | ( | const ListSet< T > & | A, |
| const ListSet< T > & | B ) |
the symmetric difference, \( O( n m ) \) time (exercise 8.5)
In pseudocode (exercise 8.5), each search a scan of the other list:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{setSymmetricDifference}( A , B ) \cr 2 & \quad C \gets \mathrm{setDifference}( A , B )\quad \triangleright \ \text{then the items of } B \text{ alone} \cr 3 & \quad \textbf{foreach}\ x \in B\ \textbf{do}\ \textbf{if} \ \textbf{not}\ A.\mathrm{contains}( x )\ \textbf{then} \ C.\mathrm{insertNew}( x ) \cr 4 & \quad \textbf{return}\ C \end{array}} \]
the union: \( C \) starts as \( B \) and gets the items of \( A \) not in \( B \), inserted without looking for them in \( C \), \( O( n m ) \) time (exercise 8.2)
In pseudocode (exercise 8.2), each search in \( B \) a scan of \( O( m ) \):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{setUnion}( A , B ) \cr 2 & \quad C \gets B \cr 3 & \quad \textbf{foreach}\ x \in A\ \textbf{do}\ \textbf{if} \ \textbf{not}\ B.\mathrm{contains}( x )\ \textbf{then} \ C.\mathrm{insertNew}( x )\quad \triangleright\ \text{no test in } C \cr 4 & \quad \textbf{return}\ C \end{array}} \]