psotirov

Variations / multicombinations

Jan 24th, 2013
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.70 KB | None | 0 0
  1. using System;
  2.  
  3. class Variations
  4. {
  5.     static void Main()
  6.     {
  7.         Console.WriteLine("Task 20 - Prints all variations V of N elements chosen as subset of K of them\n\n");
  8.         Console.Write("Please enter number of elements N: ");
  9.         int inputN = 0;
  10.         if (!int.TryParse(Console.ReadLine(), out inputN) || inputN < 2)
  11.         {
  12.             Console.WriteLine("Wrong number for N");
  13.             return;
  14.         }
  15.  
  16.         Console.Write("\nPlease enter length of subsets K for variations: ");
  17.  
  18.         int inputK = 0;
  19.         if (!int.TryParse(Console.ReadLine(), out inputK) || inputK < 2 || inputK > inputN)
  20.         {
  21.             Console.WriteLine("Wrong number for K");
  22.             return;
  23.         }
  24.  
  25.         int[] variation = new int[inputK];
  26.  
  27.         // fills-in first variation {1, 1,...., 1}
  28.         for (int i = 0; i < inputK; i++)
  29.         {
  30.             variation[i] = 1;
  31.         }
  32.  
  33.         do
  34.         {
  35.             PrintVariation(variation, inputK); // prints last variation
  36.         } while (NextVariation(variation, inputK, inputN)); // goes to the next one, until finish all variations (method returns false)
  37.  
  38.         Console.Write("\nPress Enter to finish");
  39.         Console.ReadLine();
  40.     }
  41.  
  42.     static void PrintVariation(int[] variat, int k) // prints each variation on a separate line, divided by comma and closed in { }
  43.     {
  44.         Console.Write("\n{");
  45.         for (int i = 0; i < k; i++)
  46.         {
  47.             Console.Write(" " + variat[i] + ((i < k - 1) ? "," : " "));
  48.         }
  49.         Console.WriteLine("}");
  50.     }
  51.  
  52.     static bool NextVariation(int[] variat, int k, int n)
  53.     {
  54.         // Variation (multicombination, i.e. combination with repetition) V (n,k) = n*(n-1)*...*(n-k+1)
  55.         // Every variation is an array of k elements. Next, every digit in the set is between 1 and n.
  56.         // So, the algorithm is:
  57.         // 1. Start with (1, 1, ..., 1); this is the first variation.
  58.         // 2. Print it.
  59.         // 3. Given the variation (V0, V1, ..., Vk), start from the back and for Vi increment it, if it is larger than n
  60.         //    then set to 1 and go on to the next indice i.
  61.         // 4. if V0 > n, then this is not a valid variation so we stop (return false).
  62.         // 5. Go to point 2
  63.  
  64.         int i = k - 1; // index of last element
  65.         variat[i]++; // increases the last element
  66.         while (i > 0 && variat[i] > n) // checks the elements from right to left for overflow
  67.         {
  68.             variat[i] = 1;
  69.             i--; // if yes increases previous element
  70.             variat[i]++;
  71.         }
  72.         return !(variat[0] > n); // if first element oveflowed - end of the algorithm (false), otherwise returns true
  73.     }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment