home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / theory / 2836 < prev    next >
Encoding:
Internet Message Format  |  1993-01-08  |  4.1 KB

  1. Xref: sparky comp.theory:2836 sci.crypt:6551 sci.math:17858 rec.puzzles:8292
  2. Newsgroups: comp.theory,sci.crypt,sci.math,rec.puzzles
  3. Path: sparky!uunet!stanford.edu!CSD-NewsHost.Stanford.EDU!Sunburn.Stanford.EDU!pratt
  4. From: pratt@Sunburn.Stanford.EDU (Vaughan R. Pratt)
  5. Subject: Re: Looking for random permutation generation algorithms
  6. Message-ID: <1993Jan8.210013.24614@CSD-NewsHost.Stanford.EDU>
  7. Sender: news@CSD-NewsHost.Stanford.EDU
  8. Organization: Computer Science Department,  Stanford University.
  9. References: <1993Jan6.014749.15323@ee.ubc.ca>
  10. Date: Fri, 8 Jan 1993 21:00:13 GMT
  11. Lines: 67
  12.  
  13. In article <1993Jan6.014749.15323@ee.ubc.ca> rayw@ee.ubc.ca (raymond w m woo) writes:
  14. >Hi, does anyone know, or can provide any pointer in the literature to, any 
  15. >random permutation generation algorithm that can be easily implemented as a 
  16. >function in a computer program?  
  17.  
  18. The more general problems of efficiently generating random combinations
  19. (sampling M out of N items without replacement, order immaterial) and
  20. permutations (ditto except that order now matters) are treated in Jon
  21. Bentley's "Programming Pearls" column in CACM 30:9 (Sept.87), 754-757.
  22. Bentley gives Bob Floyd's solutions to these two problems, and poses 8
  23. problems about Floyd's method whose solutions appear in the following
  24. December issue starting on p.999.
  25.  
  26. Floyd's method is so nice that it bears repeating here.
  27.  
  28. Floyd's method for sampling combinations, i.e. outputting a subset of M
  29. integers from among the first N positive integers, is as follows.   For
  30. each integer J from N-M+1 to N, choose an integer at random from the
  31. interval 1..J.  (So altogether we choose M integers from an interval
  32. starting at 1 that increases in length by 1 after each choice, with the
  33. final choice being made from the whole interval 1..N.)  For each such
  34. choice K, if K has not already been chosen previously then output K,
  35. call this a *virgin* output, otherwise output the current value of J
  36. instead of K, call this a *default* output.  The *set* (not list) of M
  37. integers so output then constitutes the random sample.
  38.  
  39. Floyd's method for sampling permutations, where the order matters, is
  40. the same algorithm but with the output now treated as a list instead of
  41. a set.  Virgin outputs go on the end of the list, default outputs are
  42. inserted on the list just in front of the occurrence of K responsible
  43. for making this a default output.
  44.  
  45. It is easily seen that the output of the permutation algorithm is one
  46. of the N!/(N-M)! possible permutations of M integers out of N.  What
  47. remains to show for this algorithm is not only that every permutation
  48. can occur, but that they do so with equal probability.  Doug McIlroy's
  49. nice proof of this runs as follows.
  50.  
  51. The output list determines the sequence of random numbers produced by
  52. the generator (nice exercise, Bentley's Problem 4).  But there are
  53. N!/(N-M)! possible sequences produced by the random number generator,
  54. whence all N!/(N-M)!  permutations must be producible.  Assuming the
  55. generator is truly random, these sequences are equiprobable, whence so
  56. are the corresponding permutations.
  57.  
  58. The correctness of Floyd's method for sampling combinations now follows
  59. immediately from the observation that each combination is represented
  60. by M!  permutations, whence the N choose M combinations are also
  61. equiprobable.
  62.  
  63. As noted in Bentley's December 1987 column, an alternative to Floyd's
  64. algorithm is to initialize an array a[1..N] to a[i]=i and then scan
  65. along from a[1] to a[M], exchanging each such a[i] with a randomly
  66. selected a[j] for j >= i.
  67.  
  68. For N = M (Woo's question) this method is the one Jim Roth gave.  It is
  69. preferable to Floyd's method because it does not entail testing for
  70. previous occurrences.
  71.  
  72. But for N >> M it has two drawbacks: the time to initialize the array,
  73. and the space required for it, both being O(N).  In contrast Floyd's
  74. method requires space O(M) and time somewhere between O(M) and O(M^2)
  75. depending on the method used to test for repetitions.  (For N some
  76. modest multiple of M one might instead keep track of repetitions using
  77. an array of N bits, i.e. N/8 bytes, initialized to 0.)
  78. -- 
  79. Vaughan Pratt                There's safety in certain numbers.
  80.