home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / theory / 2833 < prev    next >
Encoding:
Text File  |  1993-01-08  |  3.8 KB  |  126 lines

  1. Path: sparky!uunet!cs.utexas.edu!zaphod.mps.ohio-state.edu!uwm.edu!ogicse!mimbres.cs.unm.edu!cs.sandia.gov!samitch
  2. From: samitch@cs.sandia.gov (Scott A. Mitchell)
  3. Newsgroups: comp.theory
  4. Subject: Re: Real Numbers vs. Rational Numbers?
  5. Message-ID: <1993Jan8.003049.7737@cs.sandia.gov>
  6. Date: 8 Jan 93 00:30:49 GMT
  7. Article-I.D.: cs.1993Jan8.003049.7737
  8. References: <1992Dec16.095412.19570@tom.rz.uni-passau.de> <1992Dec17.114502@di.epfl.ch> <1277@zogwarg.etl.army.mil>
  9. Sender: usenet@cs.sandia.gov (Another name for news)
  10. Organization: Sandia National Laboratories, Albuquerque, NM
  11. Lines: 113
  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. >Ideally, this function would be computationally effective, and the input 
  19. >that this function takes would be some kind of index/rank number of the
  20. >permution to be generated.  
  21.  
  22. then karr@cs.cornell.edu (David Karr) describes an alg
  23.  
  24. >Now for b = N down to 2, take i = k mod b.  Copy v[i] to w[p],
  25. >increment p, copy v[t] to v[i], decrement t.  Essentially this has
  26. >just removed the i-th element of v, adding it to the output and
  27. >shrinking v accordingly.
  28.  
  29. Additionally one should (integer) divide k by b in the loop, 
  30. (i.e. k = k div b) after the computation of i.
  31.  
  32. Follows is a quickly written C++ program for this, that has been tested
  33. to be correct (provide a unique permutation for each index)
  34. for all permutations up to and including rank 8:
  35.  
  36. ===============================================================================
  37.  
  38. //permute.C,  a C++ program for computing random permutations
  39.  
  40. #include <stream.h>
  41.  
  42. void permute( int rank, int index, int output[] ) {
  43. // returns a permutation of given rank.  
  44. // index is a number between 1 and rank!, corresponding to a unique
  45. // permutation to return
  46.  
  47.   int i;
  48.   int v[rank];
  49.   // set v = {1234 . . . rank}
  50.   for (i = 0; i<rank;) 
  51.     v[i] = ++i;
  52.  
  53.   int t = rank-1;
  54.   int p = 0;
  55.  
  56.   for (int b = rank; b>1; b--) {
  57.     i = index % b;
  58.       index /= b;
  59.     output[p++] = v[i]; //move v[i] to the output permutation
  60.     v[i] = v[t--];        //remove v[i] from list of remaining elements
  61.   }
  62.   output[rank-1] = v[0];
  63. }
  64.  
  65. int fact( int v ) {
  66.   if (v>1) {
  67.     int w = v--;
  68.     for (; v > 1; v--)
  69.     w *= v;
  70.     return w;
  71.   }
  72.   else 
  73.     return 1;
  74. }
  75.  
  76. void vecset( int dest[], int source[], int rank ) {
  77. //assign dest permutation vector to be equall to source
  78. //vectors are of length rank 
  79. //use memcpy for faster results if you like
  80.     for (int i =0; i<rank; i++) 
  81.       dest[i] = source[i];
  82. }
  83.  
  84. int is_same_vec( int v1[], int v2[], int rank ) {
  85. //return true if v1 and v2 are identical vectors of length rank
  86.     for (int i = 0; i<rank; i++)
  87.       if (v1[i] != v2[i]) return 0;
  88.     return 1;
  89. }
  90.  
  91. void display( int v1[], int rank ) {
  92. //display a permutation vector on standard output
  93.     for (int i = 0; i<rank; i++) 
  94.          cout << v1[i];
  95.       cout << "\n";
  96. }
  97.  
  98. main( int argc, char * argv[] ) {
  99. //generate and display permutation vectors, 
  100.  
  101.   //display all permutation vectors of rank 1..6
  102.   for (int rank = 1; rank < 7; rank++) {
  103.     cout << "\n";  //new line
  104.     int output [rank];  // a particular permutation
  105.     int savoutput[fact(rank)] [rank]; //save all permutation vectors for rank
  106.  
  107.     //generate and display all permutation vectors of given rank
  108.     for (int k = fact(rank); k>0; k--) {
  109.     permute( rank, k, output );
  110.     vecset(savoutput[k-1], output, rank);
  111.     display( output, rank );
  112.     }
  113.  
  114.     //test uniqueness of permutation vectors of given rank
  115.     for (int j = 0; j<fact(rank)-1; j++)
  116.       for (int n = j+1; n<fact(rank); n++)
  117.     if (is_same_vec(savoutput[j], savoutput [n], rank)) {
  118.         cout << "error " << n << " " << j 
  119.         << " rank= " << rank << "\n";
  120.         display( savoutput[j], rank );
  121.         display( savoutput[n], rank );
  122.     }
  123.  
  124.   }
  125. }
  126.