# Algorithm for subsets

**URL:** <https://boards.straightdope.com/t/algorithm-for-subsets/36348>\
**Category:** Factual Questions\
**Created:** [October 10, 2000, 1:48am UTC](https://boards.straightdope.com/t/algorithm-for-subsets/36348 "2000-10-10T01:48:26Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fleejagoob](https://avatars.discourse-cdn.com/v4/letter/f/848f3c/32.png) [@Fleejagoob](https://boards.straightdope.com/u/Fleejagoob)\
**Post date:** [October 10, 2000, 1:48am UTC](https://boards.straightdope.com/t/algorithm-for-subsets/36348/1 "2000-10-10T01:48:26Z")

</div>

Ok, this seems easy, but I’ve been doing HW for too long and my brain needs a jump start. What better place to get an intellectual boost than the SDMB? Anyway, does anyone happen to know (or know of a link to a site that knows) an algorithm for generating all of the subsets of a given finite set? Thanks…

---

<div class="post-metadata">

**Author:** ![Cabbage](https://avatars.discourse-cdn.com/v4/letter/c/f07891/32.png) [@Cabbage](https://boards.straightdope.com/u/Cabbage)\
**Post date:** [October 10, 2000, 2:06am UTC](https://boards.straightdope.com/t/algorithm-for-subsets/36348/2 "2000-10-10T02:06:34Z")

</div>

You could try it this way: If there are n elements in the set, there are going to be 2[sup]n[/sup] subsets and each subset is going to correspond to a binary number. So put the elements in some order and all of the binary numbers from 0 to 2[sup]n[/sup]-1 underneath them in columns. Each binary number corresponds to a subset, where a “1” means include the element, “0” means exclude it. For example, with the set (a,b,c}:

a b c

0 0 0 empty set  
0 0 1 c  
0 1 0 b  
0 1 1 b,c  
1 0 0 a  
1 0 1 a,c  
1 1 0 a,b  
1 1 1 a,b,c
