Skip to content
StackDecode
BacktrackingAnimated

Subset Generation

For each item choose include or exclude.

Time

O(2ⁿ · n)

Extra space

O(n)

Use it for

Power set enumeration.

How it works

  1. For every element there are two choices: include it or leave it out.
  2. Recurse down the list making one choice per element.
  3. When every element has been decided, the choices made so far form one subset. Record it.
  4. Then backtrack: undo the last choice and try the other branch. 2ⁿ subsets in total.

Watch it run

Loading animation…