Wednesday, March 16, 2011
Permutations and Combinations - Java Class
import java.util.Scanner;
import java.util.ArrayList;
public class Permutations_and_Combinations {
public static ArrayList<ArrayList<Integer>> P_n_of_n(ArrayList<Integer> items, ArrayList<Integer> prev)
{
ArrayList<ArrayList<Integer>> permutations = new ArrayList<ArrayList<Integer>>();
if(items.size() == 0)
{
ArrayList<Integer> copy = new ArrayList<Integer>();
for(int i = 0; i < prev.size(); i++)
{
copy.add(prev.get(i));
}
permutations.add(copy);
}
for (int i = 0; i < items.size(); i++)
{
int first = items.get(0);
int ith = items.get(i);
items.set(i, first);
items.remove(0);
ArrayList<Integer> copy = new ArrayList<Integer>();
for(int j = 0; j < items.size(); j++)
{
copy.add(items.get(j));
}
prev.add(ith);
ArrayList<ArrayList<Integer>> thisPermutations = P_n_of_n(copy, prev);
prev.remove(prev.size() - 1);
permutations.addAll(thisPermutations);
items.add(0, ith);
}
return permutations;
}
public static ArrayList<ArrayList<Integer>> P_n_of_n_with_copies_present(ArrayList<Integer> items, ArrayList<Integer> prev)
{
ArrayList<ArrayList<Integer>> permutations = new ArrayList<ArrayList<Integer>>();
if(items.size() == 0)
{
ArrayList<Integer> copy = new ArrayList<Integer>();
for(int i = 0; i < prev.size(); i++)
{
copy.add(prev.get(i));
}
permutations.add(copy);
}
for (int i = 0; i < items.size(); i++)
{
int first = items.get(0);
int ith = items.get(i);
if(first == ith && (i > 0))
{
continue;
}
items.set(i, first);
items.remove(0);
ArrayList<Integer> copy = new ArrayList<Integer>();
for(int j = 0; j < items.size(); j++)
{
copy.add(items.get(j));
}
prev.add(ith);
ArrayList<ArrayList<Integer>> thisPermutations = P_n_of_n_with_copies_present(copy, prev);
prev.remove(prev.size() - 1);
permutations.addAll(thisPermutations);
items.add(0, ith);
}
return permutations;
}
public static ArrayList<ArrayList<Integer>> P_m_of_n_with_copies_present(ArrayList<Integer> items, int m, ArrayList<Integer> prev)
{
ArrayList<ArrayList<Integer>> permutations = new ArrayList<ArrayList<Integer>>();
if(prev.size() == m)
{
ArrayList<Integer> copy = new ArrayList<Integer>();
for(int i = 0; i < prev.size(); i++)
{
copy.add(prev.get(i));
}
permutations.add(copy);
}
for (int i = 0; i < items.size(); i++)
{
int first = items.get(0);
int ith = items.get(i);
if(first == ith && (i > 0))
{
continue;
}
items.set(i, first);
items.remove(0);
ArrayList<Integer> copy = new ArrayList<Integer>();
for(int j = 0; j < items.size(); j++)
{
copy.add(items.get(j));
}
prev.add(ith);
ArrayList<ArrayList<Integer>> thisPermutations = P_m_of_n_with_copies_present(copy, m, prev);
prev.remove(prev.size() - 1);
permutations.addAll(thisPermutations);
items.add(0, ith);
}
return permutations;
}
public static ArrayList<ArrayList<Integer>> C_m_of_n(ArrayList<Integer> items, int start, int m, ArrayList<Integer> prev)
{
int n = items.size();
if (m > n)
return null;
ArrayList<ArrayList<Integer>> combinations = new ArrayList<ArrayList<Integer>>();
if (m == 0)
{
ArrayList<Integer> copy = new ArrayList<Integer>();
for(int i = 0; i < prev.size(); i++)
{
copy.add(prev.get(i));
}
combinations.add(copy);
return combinations;
}
for(int i = start; i < n-m+1; i++)
{
prev.add(items.get(i));
ArrayList<ArrayList<Integer>> thisCombinations = C_m_of_n(items, i+1, m-1, prev);
prev.remove(prev.size() - 1);
combinations.addAll(thisCombinations);
}
return combinations;
}
public static void Print(ArrayList<ArrayList<Integer>> array)
{
for(int i = 0; i < array.size(); i++)
{
for(int j = 0; j < array.get(i).size(); j++)
{
String word = array.get(i).get(j).toString();
System.out.print(word + ",");
}
System.out.println();
}
}
public static void Example1(Scanner sc)
{
int n;
System.out.println("Enter n: (suggestion of n < 10)");
n = sc.nextInt();
ArrayList<Integer> words = new ArrayList<Integer>();
for (int i = 1; i <= n; i++)
{
words.add(i);
}
ArrayList<Integer> prev = new ArrayList<Integer>();
ArrayList<ArrayList<Integer>> allPermutations = P_n_of_n(words, prev);
Print(allPermutations);
}
public static void Example2(Scanner sc)
{
int n;
System.out.println("Enter number of unique items: (suggestion of n < 6)");
n = sc.nextInt();
ArrayList<Integer> words = new ArrayList<Integer>();
for (int i = 1; i <= n; i++)
{
words.add(i);
words.add(i + 1);
}
System.out.print("Generated: ");
for(int i = 0; i < words.size(); i++)
{
String word = words.get(i).toString();
System.out.print(word + ",");
}
System.out.println();
System.out.print("Ready for permutations?");
sc.next();
ArrayList<Integer> prev = new ArrayList<Integer>();
ArrayList<ArrayList<Integer>> allPermutations = P_n_of_n_with_copies_present(words, prev);
Print(allPermutations);
}
public static void Example3(Scanner sc)
{
int n, m;
System.out.println("Enter n: (suggestion of n < 10)");
n = sc.nextInt();
System.out.println("Enter m: ");
m = sc.nextInt();
ArrayList<Integer> words = new ArrayList<Integer>();
for (int i = 1; i <= n; i++)
{
words.add(i);
}
ArrayList<Integer> prev = new ArrayList<Integer>();
ArrayList<ArrayList<Integer>> combinations = P_m_of_n_with_copies_present(words, m, prev);
Print(combinations);
}
public static void Example4(Scanner sc)
{
int n, m;
System.out.println("Enter n: (suggestion of n < 7)");
n = sc.nextInt();
System.out.println("Enter m: ");
m = sc.nextInt();
ArrayList<Integer> words = new ArrayList<Integer>();
for (int i = 1; i <= n; i++)
{
words.add(i);
words.add(i+1);
}
System.out.print("Generated: ");
for(int i = 0; i < words.size(); i++)
{
String word = words.get(i).toString();
System.out.print(word + ",");
}
System.out.println();
System.out.print("Ready for permutations?");
sc.next();
ArrayList<Integer> prev = new ArrayList<Integer>();
ArrayList<ArrayList<Integer>> combinations = P_m_of_n_with_copies_present(words, m, prev);
Print(combinations);
}
public static void Example5(Scanner sc)
{
int n, m;
System.out.println("Enter n: ");
n = sc.nextInt();
System.out.println("Enter m: ");
m = sc.nextInt();
ArrayList<Integer> words = new ArrayList<Integer>();
for (int i = 1; i <= n; i++)
{
words.add(i);
}
ArrayList<Integer> prev = new ArrayList<Integer>();
ArrayList<ArrayList<Integer>> combinations = C_m_of_n(words, 0, m, prev);
Print(combinations);
}
public static void main(String [] args)
{
Scanner sc = new Scanner(System.in);
Example1(sc);
Example2(sc);
Example3(sc);
Example4(sc);
Example5(sc);
}
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment