Skip to content

Commit

Permalink
guava library updated to 27.0.1
Browse files Browse the repository at this point in the history
upgraded guava library to 27.0.1
added javadoc
  • Loading branch information
chandra1123 committed Mar 3, 2019
1 parent 7b3da02 commit b796bb0
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 14 deletions.
2 changes: 1 addition & 1 deletion algorithms-miscellaneous-1/pom.xml
Expand Up @@ -82,7 +82,7 @@
<commons-math3.version>3.6.1</commons-math3.version>
<org.assertj.core.version>3.9.0</org.assertj.core.version>
<commons-codec.version>1.11</commons-codec.version>
<guava.version>25.1-jre</guava.version>
<guava.version>27.0.1-jre</guava.version>
</properties>

</project>
Expand Up @@ -7,11 +7,23 @@

public class ApacheCommonsCombinationGenerator {

public static void main(String[] args) {
Iterator<int[]> iterator = CombinatoricsUtils.combinationsIterator(5, 3);
private static final int N = 6;
private static final int R = 3;

/**
* Print all combinations of r elements from a set
* @param n - number of elements in set
* @param r - number of elements in selection
*/
public static void generate(int n, int r) {
Iterator<int[]> iterator = CombinatoricsUtils.combinationsIterator(n, r);
while (iterator.hasNext()) {
final int[] combination = iterator.next();
System.out.println(Arrays.toString(combination));
}
}

public static void main(String[] args) {
generate(N, R);
}
}
Expand Up @@ -5,21 +5,29 @@
import java.util.List;

public class IterativeCombinationGenerator {

private static final int N = 5;
private static final int R = 2;

/**
* Generate all combinations of r elements from a set
* @param n the number of elements in input set
* @param r the number of elements in a combination
* @return the list containing all combinations
*/
public List<int[]> generate(int n, int r) {
List<int[]> combinations = new ArrayList<>();
int[] combination = new int[r];

// initialize with lowest lexicographic combination
for (int i = 0; i < r; i++) {
combination[i] = i;
}

while (combination[r - 1] < n) {
combinations.add(combination.clone());

// generate next combination in lexicographic order
int t = r - 1;
while (t != 0 && combination[t] == n - r + t) {
t--;
Expand All @@ -41,5 +49,4 @@ public static void main(String[] args) {
System.out.println(Arrays.toString(combination));
}
}

}
Expand Up @@ -9,12 +9,26 @@ public class SelectionRecursiveCombinationGenerator {
private static final int N = 6;
private static final int R = 3;

/**
* Generate all combinations of r elements from a set
* @param n - number of elements in input set
* @param r - number of elements to be chosen
* @return the list containing all combinations
*/
public List<int[]> generate(int n, int r) {
List<int[]> combinations = new ArrayList<>();
helper(combinations, new int[r], 0, n - 1, 0);
return combinations;
}

/**
* Choose elements from set by recursing over elements selected
* @param combinations - List to store generated combinations
* @param data - current combination
* @param start - starting element of remaining set
* @param end - last element of remaining set
* @param index - number of elements chosen so far.
*/
private void helper(List<int[]> combinations, int data[], int start, int end, int index) {
if (index == data.length) {
int[] combination = data.clone();
Expand All @@ -31,9 +45,9 @@ private void helper(List<int[]> combinations, int data[], int start, int end, in
public static void main(String[] args) {
SelectionRecursiveCombinationGenerator generator = new SelectionRecursiveCombinationGenerator();
List<int[]> combinations = generator.generate(N, R);
System.out.println(combinations.size());
for (int[] combination : combinations) {
System.out.println(Arrays.toString(combination));
}
System.out.printf("generated %d combinations of %d items from %d ", combinations.size(), R, N);
}
}
Expand Up @@ -6,33 +6,45 @@

public class SetRecursiveCombinationGenerator {

private static final int N = 6;
private static final int R = 3;
private static final int N = 5;
private static final int R = 2;

/**
* Generate all combinations of r elements from a set
* @param n - number of elements in set
* @param r - number of elements in selection
* @return the list containing all combinations
*/
public List<int[]> generate(int n, int r) {
List<int[]> combinations = new ArrayList<>();
helper(combinations, new int[r], 0, n - 1, 0, r);
helper(combinations, new int[r], 0, n-1, 0);
return combinations;
}

private void helper(List<int[]> combinations, int data[], int start, int end, int index, int r) {
/**
* @param combinations - List to contain the generated combinations
* @param data - List of elements in the selection
* @param start - index of the starting element in the remaining set
* @param end - index of the last element in the set
* @param index - number of elements selected so far
*/
private void helper(List<int[]> combinations, int data[], int start, int end, int index) {
if (index == data.length) {
int[] combination = data.clone();
combinations.add(combination);

} else if (start <= end) {
data[index] = start;
helper(combinations, data, start + 1, end, index + 1, r);
helper(combinations, data, start + 1, end, index, r);
helper(combinations, data, start + 1, end, index + 1);
helper(combinations, data, start + 1, end, index);
}
}

public static void main(String[] args) {
SetRecursiveCombinationGenerator generator = new SetRecursiveCombinationGenerator();
List<int[]> combinations = generator.generate(N, R);
System.out.println(combinations.size());
for (int[] combination : combinations) {
System.out.println(Arrays.toString(combination));
}
System.out.printf("generated %d combinations of %d items from %d ", combinations.size(), R, N);
}
}

0 comments on commit b796bb0

Please sign in to comment.