Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 116435
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T03:10:39+00:00 2026-05-11T03:10:39+00:00

Given this input: [1,2,3,4] I’d like to generate the set of spanning sets: [1]

  • 0

Given this input: [1,2,3,4]

I’d like to generate the set of spanning sets:

[1] [2] [3] [4] [1] [2] [3,4] [1] [2,3] [4] [1] [3] [2,4] [1,2] [3] [4] [1,3] [2] [4] [1,4] [2] [3] [1,2] [3,4] [1,3] [2,4] [1,4] [2,3] [1,2,3] [4] [1,2,4] [3] [1,3,4] [2] [2,3,4] [1] [1,2,3,4] 

Every set has all the elements of the original set, permuted to appear in unique subsets. What is the algorithm that produces these sets? I’ve tried Python generator functions using choose, permutation, combination, power set, and so on, but can’t get the right combination.

20 Jan 2009

This is not a homework question. This is an improved answer I was working on for http://www.projecteuler.net problem # 118. I already had a slow solution but came up with a better way — except I could not figure out how to do the spanning set.

I’ll post my code when I get back from an Inauguration Party.

21 Jan 2009

This is the eventual algorithm I used:

def spanningsets(items):     if len(items) == 1:         yield [items]     else:         left_set, last = items[:-1], [items[-1]]         for cc in spanningsets(left_set):             yield cc + [last]             for i,elem in enumerate(cc):                 yield cc[:i] + [elem + last] + cc[i+1:] 

@Yuval F: I know how to do a powerset. Here’s a straightforward implementation:

def powerset(s) :     length = len(s)     for i in xrange(0, 2**length) :         yield [c for j, c in enumerate(s) if (1 << j) & i]     return 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T03:10:39+00:00Added an answer on May 11, 2026 at 3:10 am

    This should work, though I haven’t tested it enough.

    def spanningsets(items):     if not items: return     if len(items) == 1:         yield [[items[-1]]]     else:         for cc in spanningsets(items[:-1]):             yield cc + [[items[-1]]]             for i in range(len(cc)):                 yield cc[:i] + [cc[i] + [items[-1]]] + cc[i+1:]  for sset in spanningsets([1, 2, 3, 4]):     print ' '.join(map(str, sset)) 

    Output:

    [1] [2] [3] [4] [1, 4] [2] [3] [1] [2, 4] [3] [1] [2] [3, 4] [1, 3] [2] [4] [1, 3, 4] [2] [1, 3] [2, 4] [1] [2, 3] [4] [1, 4] [2, 3] [1] [2, 3, 4] [1, 2] [3] [4] [1, 2, 4] [3] [1, 2] [3, 4] [1, 2, 3] [4] [1, 2, 3, 4] 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This follows on from this question: Algorithm to generate spanning set Given this input:
Follows on from this question Algorithm to generate (not quite) spanning set in Python
How can I do something like this: Given: <input type=text name=quantity[1] /> if ($(#item_kit_items).find('input[name=quantity[1]').length
By which I mean this: Given the input set of numbers: 1,2,3,4,5 becomes 1-5.
How to dynamically generate a fileset based on user input? Given this directories :
Given this set and an input GET parameter that indicates a chosen fruit fruit
So given this input string: =?ISO-8859-1?Q?TEST=2C_This_Is_A_Test_of_Some_Encoding=AE?= And this function: private string DecodeSubject(string input) {
Given this HTML: <form id=formx> <input type=text value= /> </form> And this JavaScript: $(function()
Given this HTML: <div id=foo> <input type=button class=foo abtn_1> <input type=button class=joe bbtn_2> <input
Given this HTML: <div id=foo> <input type=button class=foo abtn_1> <input type=button class=joe bbtn_2> <input

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.