Backtracking: The Power Set: William Fiset
Backtracking: The Power Set: William Fiset
Subsets of size 0: {}
A B C
Let’s see how we can use backtracking to
generate all subsets of a set. The key
realization is to notice that any subset can
be represented as a bit string of length N.
A B C
0 0 0
Let’s see how we can use backtracking to
generate all subsets of a set. The key
realization is to notice that any subset can
be represented as a bit string of length N.
A B C
1 0 1
Let’s see how we can use backtracking to
generate all subsets of a set. The key
realization is to notice that any subset can
be represented as a bit string of length N.
A B C
0 1 1
Suppose s = { , , }, what is P(s)?
Suppose s = { , , }, what is P(s)?
0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
1 1 1
Suppose s = { , , }, what is P(s)?
0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
1 1 1
Suppose s = { , , }, what is P(s)?
0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
1 1 1
Suppose s = { , , }, what is P(s)?
0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
1 1 1
Suppose s = { , , }, what is P(s)?
0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
1 1 1
Suppose s = { , , }, what is P(s)?
0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
1 1 1
Suppose s = { , , }, what is P(s)?
0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
1 1 1
Suppose s = { , , }, what is P(s)?
0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
1 1 1
Suppose s = { , , }, what is P(s)?
0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
1 1 1
function powerSet(set):
N = set.length
B = [0,0,…,0] # B should be of length N
bitstrings = []
generateBitstrings(0, B, bitstrings)
1 0
i = 1
i = 0
1 0
i = 1
11 10 01 00
i = 2
i = 0
1 0
i = 1
11 10 01 00
i = 2
github.com/williamfiset/algorithms