views:

331

answers:

4

This could be language agnostic/helpful answers could just be in pseudo-code.

I have a program that I would like to test under a range of inputs. This program takes a set of files, one of which is designated as the root. I want to run the program with all possible subsets of files. (Two subsets, containing the same files, but with different roots, are considered to be different.)

Here's a same example. Say I have files A, B, and C. I would want to test with:

{A}, root = A
{B}, root = B
{C}, root = C
{A B}, root = A
{A B}, root = B
{B C}, root = B
{B C}, root = C
{A C}, root = A
{A C}, root = C
{A B C}, root = A
{A B C}, root = B
{A B C}, root = C

and so on. I believe this would be the powerset.

What is the best way to generate this set in Java, given a directory full of files?

A: 

Is this what you are after (psuedocode)?

set = new List()
foreach (file in dir) {
    set.add(file)
    foreach (entry in set) {
        do-test(set, entry)
    }
}

This would build a set, then pass the set and each entry in the set to a do-test method.

jheddings
+2  A: 

You said Java, but please take a look on this: Permutations, Combinations, and Variations using C# Generics.

Rubens Farias
+1  A: 

Here's pseudocode for a recursive approach to doing tests on all possible mixes.largest-subsets-first:

allofthem = set(listallfiles(thedir))

function trythemall(someset):
  if someset is empty: return
  for entry in someset:
    dotest(someset, root=entry)
  for entry in someset:
    trythemall(someset - set([entry]))

trythemall(allofthem)

Not hard to reorg if you want smallest subsets first, of course.

Alex Martelli
this would work, but I actually realized my specification of the problem was incorrect. See above.
Rosarch
OK, I've seen your edit, it seems to me that the difference in what I'm proposing is that in my pseudocode some subsets are presented more than once -- if that's your issue please clarify and I'll propose ways to address it, tx!
Alex Martelli
the problem, I believe, is that there are subsets that will not be generated by your solution.
Rosarch
@Rosarch, I think you're wrong there -- doing the obvious/trivial translation to Python I can confirm that all subsets do get generated (though some of the short ones surface more than once) as logic confirms (every subset can be generated by taking some items off the universal set, and this code will exhaustively take away each and every combination -- in fact subsets that take away 2 or more items are generated repeatedly because the items are taken away repeatedly in every possible _order_). Have any counterxample...?
Alex Martelli
A: 

How would I mark this post as a duplicate? This other one came in just 4 hours prior:

http://stackoverflow.com/questions/1670862/obtaining-powerset-of-a-set-in-java

Kevin Bourrillion
it's nearly a duplicate, but mine includes the changing root requirement.
Rosarch
(Without taking stance on whether this q is a dupe or not...) Kevin, in general, you'd add a comment to the question pointing that out (rather than an answer, unless you also answer the actual question). People with 3k+ reputation can vote to close as duplicate if they agree.
Jonik