Permutations and Combinations

advertisement
Permutations and
Combinations
David Cline
Subsets (recursive)
• Print all subsets of a set of strings
Initial call: printSubsetsRec(S, 0, "");
(Q) Trace the call above if S = {"One ", "Two "}
Subsets (Binary counter)
(Q) Trace the call above if A = {"X", "Y"}
Combinations from multiple sets
• Print all combinations of one string from each list
• (Q) How many combinations are there?
Combinations from multiple sets
• Print all combinations of one string from each list
Combinations (loopy)
(Q) What happens if the we don't know how many sets?
Combinations (Recursive)
Initial call: printCombinationsRec(someData, 0, “”);
The recursion "rolls up" the nesting into a recursive call
Combinations (Odometer)
GetNextCombination routine
Permutations
This runs in O(n*n!) time
(Q) Fix the routine it so that it's O(n!)?
Hint: Reorder the A list as you go
Download