Class Combinations
java.lang.Object
topics.dynamic.Combinations
DYNAMIC PROGRAMMING PROBLEM: MATHEMATICAL COMBINATIONS n x k
The recursive algorithm that calculates Combinations has
exponential complexity and repeats calculations
already performed. However, it is possible to design an
algorithm with an quadratic execution time of O(n*k) based
on the idea of the Pascal Triangle. This requires the creation
of a two-dimensional table wherein storing the intermediate
values we are obtaining
- Author:
- vicegd
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionlongcombinations(long[][] table, int n, int k) Calculates the combinations of n elements taken k by klongcombinationsDivideAndConquer(int n, int k) Divide and conquer recursive version.voidwriteSolution(long[][] table, int n, int k) Shows the result combinations of n elements taken k by k
-
Constructor Details
-
Combinations
public Combinations()
-
-
Method Details
-
combinations
public long combinations(long[][] table, int n, int k) Calculates the combinations of n elements taken k by k- Parameters:
table- Table to calculate the combinationsn- n elementsk- Taken k by k- Returns:
- Number of combinations
-
writeSolution
public void writeSolution(long[][] table, int n, int k) Shows the result combinations of n elements taken k by k- Parameters:
table- Table to calculate the combinationsn- n elementsk- taken k by k
-
combinationsDivideAndConquer
public long combinationsDivideAndConquer(int n, int k) Divide and conquer recursive version. It solves repeatedly the same subproblems. It has no polynomial complexity (so it is untreatable), and the worst case occurs with calls of the type combiDyV(n, n/2)- Parameters:
n- n elementsk- Taken k by k- Returns:
- Number of combinations
-