CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Feb 2009
    Posts
    17

    generate subsets

    I'd like to generate all subsets of a given set recursively.
    For example ,

    A = { 1, 2, 3 }

    The subsets will be {}, { 1 }, { 2 }, { 3 }, { 1, 2 }, { 1, 3 }, { 2, 3 }, { 1, 2, 3 }

    I'm having trouble coming up with a recursive way to do this.

    Could someone help me out in this please ?
    Thanks..

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: generate subsets

    I'm not sure how to do it recursively. The easy way to do it is to count from 0 to 2^3-1, and use the bits to indicate membership.

  3. #3
    Join Date
    Feb 2009
    Posts
    17

    Re: generate subsets

    I am aware of that technique. I heard that there was a recursive way of doing it and was wondering how to do it.

  4. #4
    Join Date
    Oct 2006
    Posts
    616

    Re: generate subsets

    Google is your friend.

    Tons of links with examples & implementations.

    Regards,
    Zachm

  5. #5
    Join Date
    Feb 2009
    Posts
    17

    Re: generate subsets

    Thanks...
    I did search on google but I guess I chose the wrong search terms.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured