Advertisement
gparlakov

C# part II - Arrays - 21.AllCombinations

Jan 19th, 2013
1,334
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.84 KB | None | 0 0
  1. //Write a program that reads two numbers N and K and generates all the
  2. //combinations of K distinct elements from the set [1..N]. Example:
  3. //    N = 5, K = 2  {1, 2}, {1, 3}, {1, 4}, {1, 5}, {2, 3}, {2, 4}, {2, 5}, {3, 4}, {3, 5}, {4, 5}
  4.  
  5. using System;
  6. using System.Collections.Generic;
  7.  
  8. namespace Combinatorics
  9. {
  10.     class AllCombinations
  11.     {
  12.  
  13.         static List<List<int>> Combinations(int[] array, int startingIndex = 0, int combinationLenght = 2)
  14.         {
  15.  
  16.             List<List<int>> combinations = new List<List<int>>();
  17.             if (combinationLenght == 2)
  18.             {
  19.  
  20.                 int combinationsListIndex = 0;
  21.                 for (int arrayIndex = startingIndex; arrayIndex < array.Length; arrayIndex++)
  22.                 {
  23.  
  24.                     for (int i = arrayIndex + 1; i < array.Length; i++)
  25.                     {
  26.  
  27.                         //add new List in the list to hold the new combination
  28.                         combinations.Add(new List<int>());
  29.  
  30.                         //add the starting index element from "array"
  31.                         combinations[combinationsListIndex].Add(array[arrayIndex]);
  32.                         while (combinations[combinationsListIndex].Count < combinationLenght)
  33.                         {
  34.  
  35.                             //add until we come to the length of the combination
  36.                             combinations[combinationsListIndex].Add(array[i]);
  37.                         }
  38.                         combinationsListIndex++;
  39.                     }
  40.  
  41.                 }
  42.  
  43.                 return combinations;
  44.             }
  45.  
  46.             List<List<int>> combinationsofMore = new List<List<int>>();
  47.             for (int i = startingIndex; i < array.Length - combinationLenght + 1; i++)
  48.             {
  49.                 //generate combinations of lenght-1(if lenght > 2 we enter into recursion)
  50.                 combinations = Combinations(array, i + 1, combinationLenght - 1);
  51.  
  52.                 //add the starting index Elemetn in the begginnig of each newly generated list
  53.                 for (int index = 0; index < combinations.Count; index++)
  54.                 {
  55.                     combinations[index].Insert(0, array[i]);
  56.                 }
  57.  
  58.                 for (int y = 0; y < combinations.Count; y++)
  59.                 {
  60.                     combinationsofMore.Add(combinations[y]);
  61.                 }
  62.             }
  63.  
  64.             return combinationsofMore;
  65.         }
  66.  
  67.         static void Main()
  68.         {
  69.             int n;
  70.             int k;
  71.             List<List<int>> combinationsOfMoreElements = new List<List<int>>();
  72.            
  73.            
  74.             Console.WriteLine("We'll generate all combinations Lenght of [K] in an array of [1,2,3,4....N]");            
  75.             do
  76.             {
  77.                 Console.Write("Lenght of array 1....N  N=");
  78.             } while (!int.TryParse(Console.ReadLine(),out n)||n<1);
  79.             int[] numbers = new int[n]; //{ 1, 2, 3, 4, 5, 6, 7, 8, 9, };//10, 11, 12, 13, 14, 15 };
  80.  
  81.             do
  82.             {
  83.                 Console.Write("Lenght of combination K=");
  84.             } while (!int.TryParse(Console.ReadLine(), out k) || k > n);
  85.            
  86.  
  87.             for (int i = 0; i < n; i++)
  88.             {
  89.                 numbers[i] = i + 1;
  90.             }
  91.            
  92.  
  93.             combinationsOfMoreElements = Combinations(numbers, 0, k);
  94.             Console.WriteLine("There are {0} combinations of {1} in that given array", combinationsOfMoreElements.Count, k);            
  95.             for (int i = 0; i < combinationsOfMoreElements.Count; i++)
  96.             {
  97.                 for (int y = 0; y < combinationsOfMoreElements[i].Count; y++)
  98.                 {
  99.                     Console.Write(combinationsOfMoreElements[i][y] + " ");
  100.                 }
  101.                 Console.WriteLine();
  102.             }
  103.  
  104.         }
  105.     }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement