473,402 Members | 2,053 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

k-element subsets

Hi,
The aim of this C++ code is to print all the k-element subsets of a set of size n. But somewhere it goes wrong - it doesn't print the answer properly. I tried a lot to find the mistake, but in vain. Please help me in debugging it and thanks in advance.

Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. #include<vector>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7.     printf( "Enter the number of elements of the set:\n" );
  8.     int n;
  9.     scanf( "%d", &n );
  10.     printf( "Enter the elements of the set:\n" );
  11.     vector<int> v( n );
  12.     for( int i = 0; i < n; i++ )
  13.     scanf( "%d", &v[i] );
  14.     printf( "Enter the length K which is the length of the subsets to be printed: \n" );
  15.     int k;
  16.     scanf( "%d", &k );
  17.     int s = ( 1 << k ) - 1;
  18.     while( !( s & ( 1 << n ) ) )
  19.     {
  20.     printf( "\n" );
  21.     for( int i = v.size() - 1; i >= 0; i-- ) 
  22.     {
  23.         if( (s & (  1 << i )) != 0 )
  24.         {
  25.         printf( "%d ", v[i] );
  26.         }
  27.         int lo = s & ~( s - 1 ); // last one bit
  28.         int lz = ( s + lo ) & ~s; //last zero bit above lo
  29.         s |= lz; // setting the bit
  30.         s &= ~( lz - 1 ); // clearing bits below lz
  31.         s |= ( lz >> ffs(lo) ) - 1; // adding proper number of bits at the end
  32.     }
  33.     }
  34.     printf( "\n" );
  35.     return 0;
  36. }
  37.  
  38.  
Dec 23 '08 #1
1 3808
JosAH
11,448 Expert 8TB
You can do much better than that; basically you want C(n, k) n >= k; the numbers make up the index values of the elements of your set. The following function computes the next combination (lexicographically) given a current sequence of k numbers out of a set of n. If no such combination can be found anymore it'll return false otherwise true is returned. Have a look:

Expand|Select|Wrap|Line Numbers
  1. int combine(int* c, int k, int n) {
  2.  
  3.     for (int i= k; --i >= 0;) {
  4.         if (++c[i] <= n-(k-i)) {
  5.             while (++i < k)
  6.                 c[i]= c[i-1]+1;
  7.             return 1;
  8.         }
  9.     }
  10.     return 0;
  11. }
  12.  
kind regards,

Jos
Dec 23 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Simon | last post by:
Hi, I'm hoping you could show me examples of how a functional/declarative language could be used to consicely describe resticted subsets of elements. I'm looking for a 'specification' style...
4
by: Brett Calcott | last post by:
I've found some python solutions to find the set of subsets for a given set, but how do you find the set of the set of subsets whose union is the given set and whose intersections is the empty set....
3
by: will | last post by:
HOpe you can help with this... I have the following xml document... <?xml version="1.0" ?> <file> <header>some data</header> <detail> some more data</detail> <footer> gdshada </footer>
2
by: cdiggins | last post by:
I am familiar with TinyXML and "Simple XML Subset Parser" from GLib, but I was wondering if there exist any specifications subsets of XML without attributes? I proposed one called XML-- (XML minus...
7
by: Gaijinco | last post by:
I been thinking about this topic for a long time. The best I have done is the following code: #include <iostream> using namespace std; #include <cmath> int main(){ const int SIZE=3;
25
by: Jessica Weiner | last post by:
I have an array of n integers and I want a function that returns a list of arrays of all possible subsets. Can someone provide me with the code? Thanks. Jess
4
by: LurfysMa | last post by:
I could use some help with a table design problem. I have an electronic flashcard program. Actually, several of them. They each rely on a utility program to keep track of the usage statistics....
3
by: Lord0 | last post by:
I *think* I need to be able to validate subsets of an XML document using different schema. The functionality I'm trying to implement is this. a) External suppliers produce an XML document...
4
by: Patrick | last post by:
Hi, I want to write a programs that checks if a set of numbers in a list obey a condition, the problem is that i have say "n" numbers and i need to check all subsets of the n numbers for the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.