home *** CD-ROM | disk | FTP | other *** search
/ Math Solutions 1995 October / Math_Solutions_CD-ROM_Walnut_Creek_October_1995.iso / pc / mac / discrete / doc / set.tex < prev    next >
Encoding:
Text File  |  1993-05-05  |  16.3 KB  |  383 lines

  1. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2. %%
  3. %A  set.tex                     GAP documentation            Martin Schoenert
  4. %%
  5. %A  @(#)$Id: set.tex,v 3.6 1993/02/19 10:48:42 gap Exp $
  6. %%
  7. %Y  Copyright 1990-1992,  Lehrstuhl D fuer Mathematik,  RWTH Aachen,  Germany
  8. %%
  9. %%  This file describes those functions that mainly deal  with  proper  sets.
  10. %%
  11. %H  $Log: set.tex,v $
  12. %H  Revision 3.6  1993/02/19  10:48:42  gap
  13. %H  adjustments in line length and spelling
  14. %H
  15. %H  Revision 3.5  1993/02/04  16:03:43  felsch
  16. %H  examples fixed
  17. %H
  18. %H  Revision 3.4  1992/04/02  21:06:23  martin
  19. %H  changed *domain functions* to *set theoretic functions*
  20. %H
  21. %H  Revision 3.3  1991/12/27  16:07:04  martin
  22. %H  revised everything for GAP 3.1 manual
  23. %H
  24. %H  Revision 3.2  1991/07/26  09:01:07  martin
  25. %H  changed |\GAP\ | to |{\GAP}|
  26. %H
  27. %H  Revision 3.1  1991/07/25  16:16:59  martin
  28. %H  fixed some minor typos
  29. %H
  30. %H  Revision 3.0  1991/04/11  11:33:34  martin
  31. %H  Initial revision under RCS
  32. %H
  33. %%
  34. \Chapter{Sets}%
  35. \index{multisets}
  36.  
  37. A  very important mathematical concept,  maybe the most important of all,
  38. are sets.  Mathematically  a *set* is  an abstract object such that  each
  39. object  is either an element  of the  set or it  is not.   So  a set is a
  40. collection like  a list, and in fact {\GAP} uses lists to represent sets.
  41. Note that this of course implies that {\GAP} only deals with finite sets.
  42.  
  43. Unlike a list a set must not contain an element several times.  It simply
  44. makes no sense to say   that an object is   twice  an element of a   set,
  45. because an object is either an element of a set, or it is not.  Therefore
  46. the list that is used  to represent a set has  no duplicates, that is, no
  47. two elements of such a list are equal.
  48.  
  49. Also unlike a  list a set does not  impose any ordering  on the elements.
  50. Again it  simply makes no  sense to say  that an object  is  the first or
  51. second etc.  element of  a set, because,  again, an  object is  either an
  52. element of a set, or it is not.  Since ordering is  not defined for a set
  53. we can put the elements in any order into the  list used to represent the
  54. set.  We put the elements sorted into the  list, because this ordering is
  55. very practical.  For example if we convert a  list into a  set we have to
  56. remove  duplicates, which is  very  easy to do  after  we have sorted the
  57. list, since then equal elements will be next to each other.
  58.  
  59. In   short   sets are represented  by   sorted  lists without  holes  and
  60. duplicates in {\GAP}.  Such  a list is  in this document called a  proper
  61. set.  Note that we guarantee this representation, so  you may make use of
  62. the fact that a set is represented by a sorted list in your functions.
  63.  
  64. In some contexts (for example see "Combinatorics"),  we also want to talk
  65. about multisets.  A *multiset* is like a set,  except that an element may
  66. appear several  times  in a multiset.   Such multisets are represented by
  67. sorted lists with holes that may have duplicates.
  68.  
  69. The first section in this chapter  describes the  functions to test if an
  70. object is a set and to convert objects to sets (see "IsSet" and "Set").
  71.  
  72. The next section describes the function that tests if two sets  are equal
  73. (see "IsEqualSet").
  74.  
  75. The next sections  describe  the destructive  functions that  compute the
  76. standard   set   operations   for   sets    (see   "AddSet", "RemoveSet",
  77. "UniteSet", "IntersectSet", and "SubtractSet").
  78.  
  79. The last   section  tells  you more   about   sets  and   their  internal
  80. representation (see "More about Sets").
  81.  
  82. All set theoretic functions, especially  'Intersection' and 'Union', also
  83. accept  sets  as  arguments.  Thus  all  functions  described in  chapter
  84. "Domains" are applicable to sets (see "Set Functions for Sets").
  85.  
  86. Since sets are just  a  special case of lists,   all the  operations  and
  87. functions for lists, especially  the membership test  (see "In"),  can be
  88. used for sets just as well (see "Lists").
  89.  
  90. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  91. \Section{IsSet}%
  92. \index{test!for a set}
  93.  
  94. 'IsSet( <obj> )'
  95.  
  96. 'IsSet'  returns   'true' if the   object   <obj> is  a set  and  'false'
  97. otherwise.  An object is a  set if it is a  sorted lists without holes or
  98. duplicates.  Will cause an  error if evaluation of   <obj> is an  unbound
  99. variable.
  100.  
  101. |    gap> IsSet( [] );
  102.     true
  103.     gap> IsSet( [ 2, 3, 5, 7, 11 ] );
  104.     true
  105.     gap> IsSet( [, 2, 3,, 5,, 7,,,, 11 ] );
  106.     false        # this list contains holes
  107.     gap> IsSet( [ 11, 7, 5, 3, 2 ] );
  108.     false        # this list is not sorted
  109.     gap> IsSet( [ 2, 2, 3, 5, 5, 7, 11, 11 ] );
  110.     false        # this list contains duplicates
  111.     gap> IsSet( 235711 );
  112.     false        # this argument is not even a list |
  113.  
  114. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  115. \Section{Set}%
  116. \index{convert!to a set}
  117.  
  118. 'Set( <list> )'
  119.  
  120. 'Set' returns a  new proper  set, which  is represented as  a sorted list
  121. without holes or duplicates, containing the elements of the list <list>.
  122.  
  123. 'Set' returns a new list even if the list <list> is already a proper set,
  124. in this  case  it is   equivalent to  'ShallowCopy' (see  "ShallowCopy").
  125. Thus the result is  a new list  that is not  identical to any other list.
  126. The elements of  the result are however identical  to elements of <list>.
  127. If <list> contains equal elements, it is  not specified to which of those
  128. the element of the result is identical (see "Identical Lists").
  129.  
  130. |    gap> Set( [3,2,11,7,2,,5] );
  131.     [ 2, 3, 5, 7, 11 ]
  132.     gap> Set( [] );
  133.     [  ] |
  134.  
  135. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  136. \Section{IsEqualSet}%
  137. \index{test!for set equality}
  138.  
  139. 'IsEqualSet( <list1>, <list2> )'
  140.  
  141. 'IsEqualSet' returns  'true' if the   two lists <list1>  and <list2>  are
  142. equal *when viewed as sets*, and  'false' otherwise.  <list1> and <list2>
  143. are equal if  every element of <list1> is  also an element of <list2> and
  144. if every element of <list2> is also an element of <list1>.
  145.  
  146. If both lists are proper sets then they are of course  equal if  and only
  147. if they are also equal as  lists.  Thus  'IsEqualSet( <list1>, <list2> )'
  148. is equivalent to 'Set( <list1>  ) = Set( <list2> )'  (see "Set"), but the
  149. former is more efficient.
  150.  
  151. |    gap> IsEqualSet( [2,3,5,7,11], [11,7,5,3,2] );
  152.     true
  153.     gap> IsEqualSet( [2,3,5,7,11], [2,3,5,7,11,13] );
  154.     false |
  155.  
  156. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  157. \Section{AddSet}%
  158. \index{add!an element to a set}
  159.  
  160. 'AddSet( <set>, <elm> )'
  161.  
  162. 'AddSet' adds <elm>, which may be an elment of an  arbitrary type, to the
  163. set   <set>, which must  be  a proper  set,  otherwise an  error  will be
  164. signalled.  If <elm> is already an element of the  set <set>, the  set is
  165. not  changed.  Otherwise <elm> is inserted  at the  correct position such
  166. that <set> is again a set afterwards.
  167.  
  168. |    gap> s := [2,3,7,11];;
  169.     gap> AddSet( s, 5 );  s;
  170.     [ 2, 3, 5, 7, 11 ]
  171.     gap> AddSet( s, 13 );  s;
  172.     [ 2, 3, 5, 7, 11, 13 ]
  173.     gap> AddSet( s, 3 );  s;
  174.     [ 2, 3, 5, 7, 11, 13 ] |
  175.  
  176. 'RemoveSet' (see "RemoveSet") is the counterpart of 'AddSet'.
  177.  
  178. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  179. \Section{RemoveSet}%
  180. \index{remove!an element from a set}
  181.  
  182. 'RemoveSet( <set>, <elm> )'
  183.  
  184. 'RemoveSet' removes   the  element  <elm>,  which  may be   an  object of
  185. arbitrary  type, from the set <set>,  which must be  a  set, otherwise an
  186. error will be signalled.  If  <elm>  is  not an  element of <set> nothing
  187. happens.  If <elm>  is  an element it is removed   and  all the following
  188. elements in the list are moved one position forward.
  189.  
  190. |    gap> s := [ 2, 3, 4, 5, 6, 7 ];;
  191.     gap> RemoveSet( s, 6 );
  192.     gap> s;
  193.     [ 2, 3, 4, 5, 7 ]
  194.     gap> RemoveSet( s, 10 );
  195.     gap> s;
  196.     [ 2, 3, 4, 5, 7 ] |
  197.  
  198. 'AddSet' (see "AddSet") is the counterpart of 'RemoveSet'.
  199.  
  200. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  201. \Section{UniteSet}%
  202. \index{union!of sets}
  203.  
  204. 'UniteSet( <set1>, <set2> )'
  205.  
  206. 'UniteSet' unites the set <set1> with the set <set2>.  This is equivalent
  207. to adding all the elements  in <set2>  to <set1> (see "AddSet").   <set1>
  208. must be a proper set, otherwise an  error is  signalled.  <set2> may also
  209. be  list that  is  not a  proper set,  in  which case 'UniteSet' silently
  210. applies 'Set' to it first (see "Set").  'UniteSet' returns nothing, it is
  211. only called to change <set1>.
  212.  
  213. |    gap> set := [ 2, 3, 5, 7, 11 ];;
  214.     gap> UniteSet( set, [ 4, 8, 9 ] );  set;
  215.     [ 2, 3, 4, 5, 7, 8, 9, 11 ]
  216.     gap> UniteSet( set, [ 16, 9, 25, 13, 16 ] );  set;
  217.     [ 2, 3, 4, 5, 7, 8, 9, 11, 13, 16, 25 ] |
  218.  
  219. The  function  'UnionSet'   (see  "Set  Functions   for  Sets")  is   the
  220. nondestructive counterpart to the destructive procedure 'UniteSet'.
  221.  
  222. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  223. \Section{IntersectSet}%
  224. \index{intersection!of sets}
  225.  
  226. 'IntersectSet( <set1>, <set2> )'
  227.  
  228. 'IntersectSet' intersects  the set <set1> with the  set <set2>.  This  is
  229. equivalent  to removing all  the elements  that are not  in  <set2>  from
  230. <set1> (see  "RemoveSet").  <set1> must be a  set, otherwise  an error is
  231. signalled.  <set2> may be a list that is not a proper  set, in which case
  232. 'IntersectSet'   silently  applies  'Set' to      it first  (see  "Set").
  233. 'IntersectSet' returns nothing, it is only called to change <set1>.
  234.  
  235. |    gap> set := [ 2, 3, 4, 5, 7, 8, 9, 11, 13, 16 ];;
  236.     gap> IntersectSet( set, [ 3, 5, 7, 9, 11, 13, 15, 17 ] );  set;
  237.     [ 3, 5, 7, 9, 11, 13 ]
  238.     gap> IntersectSet( set, [ 9, 4, 6, 8 ] );  set;
  239.     [ 9 ] |
  240.  
  241. The function 'IntersectionSet'  (see  "Set Functions  for Sets")  is  the
  242. nondestructive counterpart to the destructive procedure 'IntersectSet'.
  243.  
  244. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  245. \Section{SubtractSet}%
  246. \index{subtract!a set from another}
  247.  
  248. 'SubtractSet( <set1>, <set2> )'
  249.  
  250. 'SubtractSet'  subtracts  the set  <set2>  from the set  <set1>.  This is
  251. equivalent to  removing  all the elements in   <set2>  from  <set1>  (see
  252. "RemoveSet").   <set1> must  be  a  proper  set, otherwise   an  error is
  253. signalled.  <set2> may be a list that is not a proper  set, in which case
  254. 'SubtractSet' applies  'Set'   to it   first  (see "Set").  'SubtractSet'
  255. returns nothing, it is only called to change <set1>.
  256.  
  257. |    gap> set := [ 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 ];;
  258.     gap> SubtractSet( set, [ 6, 10 ] );  set;
  259.     [ 2, 3, 4, 5, 7, 8, 9, 11 ]
  260.     gap> SubtractSet( set, [ 9, 4, 6, 8 ] );  set;
  261.     [ 2, 3, 5, 7, 11 ] |
  262.  
  263. The function 'Difference'   (see  "Difference")  is   the  nondestructive
  264. counterpart to destructive the procedure 'SubtractSet'.
  265.  
  266. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  267. \Section{Set Functions for Sets}%
  268. \index{test!for subsets}
  269.  
  270. As was already mentioned  in  the introduction to this chapter all domain
  271. functions also accept sets as arguments.  Thus all functions described in
  272. the chapter "Domains"  are applicable to sets.   This  section  describes
  273. those functions where  it might be helpful to know  the implementation of
  274. those functions for sets.
  275.  
  276. 'IsSubset( <set1>, <set2> )'
  277.  
  278. This is implemented by 'IsSubsetSet', which you can call directly to save
  279. a little  bit of  time.  Either argument to  'IsSubsetSet' may also  be a
  280. list that is not a proper set, in which  case 'IsSubset' silently applies
  281. 'Set' (see "Set") to it first.
  282.  
  283. 'Union( <set1>, <set2> )'
  284.  
  285. This is implemented by 'UnionSet', which you can call  directly to save a
  286. little bit of time.  Note that  'UnionSet' only accepts two  sets, unlike
  287. 'Union',  which accepts several sets or  a list of  sets.  The  result of
  288. 'UnionSet' is a new set,  represented as  a sorted list  without holes or
  289. duplicates.  Each argument to 'UnionSet' may also be a list that is not a
  290. proper set, in which case 'UnionSet'  silently  applies 'Set' (see "Set")
  291. to this argument.  'UnionSet' is implemented in terms of its  destructive
  292. counterpart 'UniteSet' (see "UniteSet").
  293.  
  294. 'Intersection( <set1>, <set2> )'
  295.  
  296. This is implemented by 'IntersectionSet', which you can call  directly to
  297. save a little bit of time.  Note that 'IntersectionSet'  only accepts two
  298. sets, unlike 'Intersection',  which  accepts several sets  or  a list  of
  299. sets.  The  result of 'IntersectionSet' is  a new  set,  represented as a
  300. sorted  list     without  holes   or  duplicates.   Each   argument    to
  301. 'IntersectionSet' may also be a list  that is not  a proper set, in which
  302. case  'IntersectionSet'  silently  applies  'Set' (see  "Set")    to this
  303. argument.  'IntersectionSet' is implemented in  terms of its  destructive
  304. counterpart 'IntersectSet' (see "IntersectSet").
  305.  
  306. The result of 'IntersectionSet' and 'UnionSet' is always a new list, that
  307. is not  identical to any other list.   The elements of that  list however
  308. are identical to the corresponding elements of <set1>.   If <set1> is not
  309. a proper list it is not specified to which of a number  of equal elements
  310. in <set1> the element in the result is identical (see "Identical Lists").
  311.  
  312. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  313. \Section{More about Sets}
  314.  
  315. In the previous section we defined a proper  set as a sorted list without
  316. holes or duplicates.  This representation is not  only nice to use, it is
  317. also a good internal representation supporting efficient algorithms.  For
  318. example the 'in' operator can use binary instead of a linear search since
  319. a set is sorted.  For another example 'Union' only has to merge the sets.
  320.  
  321. However, all  those set functions  also allow lists that are  not  proper
  322. sets,  silently making  a copy  of it and  converting this copy to a set.
  323. Suppose all the functions would have to test  their arguments every time,
  324. comparing  each element  with its  successor, to see if  they  are proper
  325. sets.  This would chew up most  of  the performance advantage again.  For
  326. example suppose 'in' would have to run  over the whole list, to see if it
  327. is  a  proper set, so  it could  use the  binary search.   That  would be
  328. ridiculous.
  329.  
  330. To avoid this a  list that is  a proper set  may, but need  not, have  an
  331. internal flag set that tells  those functions that  this list is indeed a
  332. proper set.  Those functions do not have to check this argument then, and
  333. can use the more  efficient algorithms.  This  section tells  you  when a
  334. proper set obtains this flag,  so you can write your  functions in such a
  335. way that you make best use of the algorithms.
  336.  
  337. The results of 'Set', 'Difference', 'Intersection'  and 'Union' are known
  338. to be sets by construction, and thus have the flag set upon creation.
  339.  
  340. If an argument to 'IsSet', 'IsEqualSet', 'IsSubset', 'Set', 'Difference',
  341. 'Intersection' or  'Union' is a proper  set, that does  not  yet have the
  342. flag set, those functions will notice that and set the flag for this set.
  343. Note that 'in' will use linear search if the  right operand does not have
  344. the flag set, will therefore not detect  if it is  a proper set and will,
  345. unlike the functions above, never set the flag.
  346.  
  347. If you change a proper set, that does have this  flag set, by assignment,
  348. 'Add'   or 'Append' the  set  will generally lose  it  flag,  even if the
  349. change is such that the resulting list is still a proper set.  However if
  350. the set has more than 100 elements and the value assigned or added is not
  351. a list and not a record and the resulting list is still a proper set than
  352. it will keep  the flag.  Note that  changing a list  that is not a proper
  353. set will never set the flag, even if the resulting list  is a proper set.
  354. Such a set will obtain the flag only if it is passed to a set function.
  355.  
  356. Suppose you have built a proper set  in such a way that  it does not have
  357. the flag set, and that you now want  to perform lots of membership tests.
  358. Then you  should call 'IsSet'  with that set   as an argument.   If it is
  359. indeed  a proper set  'IsSet' will set the flag,  and the subsequent 'in'
  360. operations will use  the more efficient binary  search.  You can think of
  361. the call to 'IsSet' as a hint to {\GAP} that this list is a proper set.
  362.  
  363. There is no way you can set the flag for an ordinary  list  without going
  364. through the checking in 'IsSet'.  The  internal  functions depend so much
  365. on the fact that a list with  this flag set  is indeed sorted and without
  366. holes and duplicates that the risk would be too high to allow setting the
  367. flag without such a check.
  368.  
  369. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  370. %%
  371. %E  Emacs . . . . . . . . . . . . . . . . . . . . . local Emacs variables
  372. %%
  373. %%  Local Variables:
  374. %%  mode:               outline
  375. %%  outline-regexp:     "\\\\Chapter\\|\\\\Section"
  376. %%  fill-column:        73
  377. %%  eval:               (hide-body)
  378. %%  End:
  379. %%
  380.  
  381.  
  382.  
  383.