Saturday, March 19, 2011

Powerset - copied and pasted :)

import java.util.*;


public class PowerSet {
        public static void main(String[] args) {
                String st[] = { "x", "y", "z" };
                
                LinkedHashSet hashSet = new LinkedHashSet();
                int len = st.length;
                int elements = (int) Math.pow(2, len);
                for (int i = 0; i < elements; i++) {
                        String str = Integer.toBinaryString(i);
                        System.out.println("" + i + " = " + str + " binary");
                        int value = str.length();
                        String pset = str;
                        for (int k = value; k < len; k++) {
                                pset = "0" + pset;
                        }
                        LinkedHashSet set = new LinkedHashSet();
                        for (int j = 0; j < pset.length(); j++) {
                                if (pset.charAt(j) == '1')
                                        set.add(st[j]);
                        }
                        hashSet.add(set);
                }
                System.out.println(hashSet.toString().replace("[", "{").replace("]","}"));
        }
}

Thursday, March 17, 2011

Problem with Values and Weights

You are given two numbers V and W
V represents a total value
W represents a total weight. 
You are also given 9 items (each one of them has a value and a weight). These are given as nine value,weight pairs. The format is as shown below:
V W
v1 w1
v2 w2
v3 w3
v4 w4
v5 w5
v6 w6
v7 w7
v8 w8
v9 w9


Your task is the following:
Are there three items i,j,k with respective values vi,vj,vk and respective weights wi,wj,wk such that:
                              vi  + 2·vj + 3·v V
and
                                         wk  + 2·wj + 3·w W


Print any one of such such triplet if there are any, in the right order i,j,k
else print NO.


Example
input:
14 16
1 5
2 8
3 4
8 3
5 1
5 6
3 10
9 5
1 10


output: 5,3,1
Precisely:

                              v5  + 2·v3 + 3·v V
                                         w1  + 2·w3 + 3·w W



as:

                              5  + 2·(3) + 3·(1)  14
and
                                            5  + 2·(4) + 3·(1)  16

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);
 }
}

Permutations of M elements out of N elements checking for repetition

This function finds permutations of M elements out of N elements, checking not to output the same permutation twice in case that there are repetitions.

-----------------------------------------

public static ArrayList<ArrayList<String>> P_m_of_n_with_copies_present(ArrayList<String> items, int m, ArrayList<String> prev)
{
ArrayList<ArrayList<String>> permutations = new ArrayList<ArrayList<String>>();


if(prev.size() == m)
{
ArrayList<String> copy = new ArrayList<String>();
for(int i = 0; i < prev.size(); i++)
{
copy.add(prev.get(i));
}
permutations.add(copy);
}


for (int i = 0; i < items.size(); i++)
{
String first = items.get(0);
String ith = items.get(i);

if(first.equals(ith) && (i > 0))
{
continue;
}

items.set(i, first);
items.remove(0);

ArrayList<String> copy = new ArrayList<String>();
for(int j = 0; j < items.size(); j++)
{
copy.add(items.get(j));
}

prev.add(ith);
ArrayList<ArrayList<String>> 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;
}

-----------------------------------------

Permutations of all of N elements without allowing repetition - JAVA

This function generates the permutations of all of N elements given in an ArrayList of Strings. But if two strings are the same in the given array, it does not permute any pair of equal strings.
For example, if we have ["cat", "dog", "cat", "fox"]
we do not want to return ["cat", "cat", "dog", "fox"] twice for instance.
-------------------------------

public static ArrayList<ArrayList<String>> P_n_of_n_with_copies_present(ArrayList<String> items, ArrayList<String> prev)
{
ArrayList<ArrayList<String>> permutations = new ArrayList<ArrayList<String>>();


if(items.size() == 0)
{
ArrayList<String> copy = new ArrayList<String>();
for(int i = 0; i < prev.size(); i++)
{
copy.add(prev.get(i));
}
permutations.add(copy);
}


for (int i = 0; i < items.size(); i++)
{
String first = items.get(0);
String ith = items.get(i);

if(first.equals(ith) && (i > 0))
{
continue;
}

items.set(i, first);
items.remove(0);

ArrayList<String> copy = new ArrayList<String>();
for(int j = 0; j < items.size(); j++)
{
copy.add(items.get(j));
}

prev.add(ith);
ArrayList<ArrayList<String>> thisPermutations = P_n_of_n_with_copies_present(copy, prev);
prev.remove(prev.size() - 1);
permutations.addAll(thisPermutations);
items.add(0, ith);
}


return permutations;
}

-------------------------------

Permutations of all of N elements - JAVA

This code generates the permutations of all of N elements given in an ArrayList of Strings.

-----------------------

public static ArrayList<ArrayList<String>> P_n_of_n(ArrayList<String> items, ArrayList<String> prev)
{
ArrayList<ArrayList<String>> permutations = new ArrayList<ArrayList<String>>();


if(items.size() == 0)
{
ArrayList<String> copy = new ArrayList<String>();
for(int i = 0; i < prev.size(); i++)
{
copy.add(prev.get(i));
}
permutations.add(copy);
}


for (int i = 0; i < items.size(); i++)
{
String first = items.get(0);
String ith = items.get(i);
items.set(i, first);
items.remove(0);

ArrayList<String> copy = new ArrayList<String>();
for(int j = 0; j < items.size(); j++)
{
copy.add(items.get(j));
}

prev.add(ith);
ArrayList<ArrayList<String>> thisPermutations = P_n_of_n(copy, prev);
prev.remove(prev.size() - 1);
permutations.addAll(thisPermutations);
items.add(0, ith);
}


return permutations;
}

-----------------------


MArio

Monday, March 14, 2011

Get all permutations - C++ code

All right today I am presenting some C++ code that gets all the permutations of a list of items. The code is based in the following function:
----------------------------------

#include <vector>
using namespace std;


vector<vector<int>> GetAllPermutations(vector<int> indexes, vector<int> prev)
{
vector<vector<int>> permutations;


if(indexes.size() == 0)
{
permutations.push_back(prev);
}


for (int i = 0; i < indexes.size(); i++)
{
int first = indexes[0];
int ith = indexes[i];
indexes[i] = first;
indexes.erase(indexes.begin());


prev.push_back(ith);
vector<vector<int>> thisPermutations = GetAllPermutations(indexes, prev);
prev.pop_back();
permutations.insert(permutations.end(), thisPermutations.begin(), thisPermutations.end());
indexes.insert(indexes.begin(), ith);
}


return permutations;
}

----------------------------------


Below a couple of examples of functions using the method above:

----------------------------------
void Run1()
{
int n = 6;
vector<int> items;
for(int i = 0; i < n; i++)
{
items.push_back(i);
}
vector<int> prev;
vector<vector<int>> permut = GetAllPermutations(items,prev);
for(int i = 0; i < permut.size(); i++)
{
for(int j = 0; j < permut[i].size(); j++)
{
printf("%d,", permut[i][j]);
}
printf("\n");
}
}

----------------------------------

----------------------------------
void Run2()
{
vector<string> items;
items.push_back("ant");
items.push_back("bear");
items.push_back("cat");
items.push_back("deer");
items.push_back("elephant");
//items.push_back("fox");

vector<int> indexes;
for(int i = 0; i < items.size(); i++)
{
indexes.push_back(i);
}
vector<int> prev;
vector<vector<int>> permut = GetAllPermutations(indexes,prev);
for(int i = 0; i < permut.size(); i++)
{
for(int j = 0; j < permut[i].size(); j++)
{
int index = permut[i][j];
printf("%s,", items[index].c_str());
}
printf("\n");
}
}
----------------------------------
Run it and it will print all the permutations of the items in the list:
----------------------------------
int main()
{
//Run1();
Run2();
}
----------------------------------
and so on...

MArio