Backtracking: All Subsets

For each item, try including it, then try leaving it out. Undo each choice on the way back. The result is every subset.

Python · intermediate · 9 min · 42 steps · free

What you'll learn

backtracking.py

def subsets(items):
    result = []
    current = []

    def choose(i):
        if i == len(items):
            result.append(current[:])
            return
        current.append(items[i])
        choose(i + 1)
        current.pop()
        choose(i + 1)

    choose(0)
    return result

print(subsets(['a', 'b']))

Step by step

  1. Python defines the function subsets(). Its body does not run yet: it runs each time the function is called.
  2. Python calls subsets(), with items = ['a', 'b']. It gets its own names.
  3. result is created and points to a new list: [].
  4. current is created and points to a new list: [].
  5. Python defines the function choose(). Its body does not run yet: it runs each time the function is called.
  6. Python calls choose(), with i = 0, choose = function choose(), current = [], items = ['a', 'b'] and result = []. It gets its own names.
  7. i == len(items) is false, so the block under it is skipped.
  8. Choice 1: include "a".
  9. Python calls choose() #2, with i = 1, choose = function choose(), current = ['a'], items = ['a', 'b'] and result = []. It gets its own names.
  10. i == len(items) is false, so the block under it is skipped.
  11. 'b' is added to the end of the list that current points to.
  12. Python calls choose() #3, with i = 2, choose = function choose(), current = ['a', 'b'], items = ['a', 'b'] and result = []. It gets its own names.
  13. i == len(items) is true, so the block under it runs.
  14. All items decided: save a copy of current, ["a", "b"].
  15. The function returns.
  16. choose() #3 has finished, so its names are gone.
  17. "b" is removed again: un-choose, to try the other option.
  18. Now the path without "b".
  19. i == len(items) is true, so the block under it runs.
  20. a list is added to the end of the list that result points to.
  21. The function returns.
  22. choose() #3 has finished, so its names are gone.
  23. choose() #2 has finished, so its names are gone.
  24. 'a' is removed from the end of the list that current points to.
  25. Python calls choose() #2, with i = 1, choose = function choose(), current = [], items = ['a', 'b'] and result = [a list, a list]. It gets its own names.
  26. i == len(items) is false, so the block under it is skipped.
  27. 'b' is added to the end of the list that current points to.
  28. Python calls choose() #3, with i = 2, choose = function choose(), current = ['b'], items = ['a', 'b'] and result = [a list, a list]. It gets its own names.
  29. i == len(items) is true, so the block under it runs.
  30. a list is added to the end of the list that result points to.
  31. The function returns.
  32. choose() #3 has finished, so its names are gone.
  33. 'b' is removed from the end of the list that current points to.
  34. Python calls choose() #3, with i = 2, choose = function choose(), current = [], items = ['a', 'b'] and result = [a list, a list, a list]. It gets its own names.
  35. i == len(items) is true, so the block under it runs.
  36. a list is added to the end of the list that result points to.
  37. The function returns.
  38. choose() #3 has finished, so its names are gone.
  39. choose() #2 has finished, so its names are gone.
  40. choose() has finished, so its names are gone.
  41. All 4 subsets: [a, b], [a], [b], [].
  42. subsets() has finished, so its names are gone. It prints [['a', 'b'], ['a'], ['b'], []].

Common mistakes

Best practices

Key takeaways

Check yourself

Subsets of 10 items?

Answer: 1024. 2¹⁰.

Why current[:]?

Answer: To save a copy

·

More in Backtracking and Dynamic Programming