【问题标题】:C++ : generate all subsets from set with one conditionC ++:从具有一个条件的集合中生成所有子集
【发布时间】:2014-05-28 05:02:59
【问题描述】:

我正在尝试编写代码来生成集合中的所有子集,其中包含一个条件,例如 如果我有阈值=2,并且三个设置:

1, 2, 3, 4, 5
1,3,5
1,3,4

然后程序会输出:

第一次迭代时的生成集:

1 = number of frequency = 3
2 = number of frequency = 1
3 = number of frequency = 3
4 = number of frequency = 2
5= number of frequency = 2

由于数字2的频率

第二次迭代时的生成集

1,3 = number of frequency = 3
1,4 = number of frequency = 2
1,5 = number of frequency = 2
3,4 = number of frequency = 2
3,5= number of frequency = 2
4,5= number of frequency = 1

由于数字 (4,5)

第三次迭代的生成集

1,3,4= number of frequency = 2
1,3,5= number of frequency = 2

第四次迭代的生成集

不再有超集,因为 (4,5)

我写了程序,我已经生成了所有的子集,但是在两件事上失败了:

  • 我无法在地图中搜索std::map <int,std::pair<list<int>, int>> CandList 来统计相似集(频率数)
  • 我不知道如何应用条件

感谢您的帮助。

这是我的代码:

int threshold = 2;
std::vector<std::list<int>> data;
std::map<int, int> FISupp;
typedef std::pair<list<int>, int> combo;
std::map <int,combo> CandList;
std::list<int> FrqList;



/*
input:Threshold =2, and data=
1 2 3 4 5
1 3 4 5
1 2 3 5
1 3

at first scan after PassOne function:
FISupp(1,4)
FISupp(2,2)
FISupp(3,4)
FISupp(4,4)
FISupp(5,3)

at k scan after Passk function:
---
*/
int Lsize = 2; // Level size

void ScanData()
{
    ifstream in;
    in.open("mydata.txt");
    /* mydata.txt
    1 2 3 4 5
    1 3 4 5
    1 2 3 5
    1 3
    */
    std::string line;
    int i = 0;

    while (std::getline(in, line))
    {
        std::stringstream Sline1(line);
        std::stringstream ss(line);
        std::list<int> inner;
        int info;

        while (ss >> info)
            inner.push_back(info);

        data.push_back(inner);
    }
}


/* first pass to generate first Candidates items */
void PassOne()
{
    for (unsigned i = 0; i < data.size(); ++i)
    {
        std::list<int>::iterator li;

        for (li = data[i].begin(); li != data[i].end(); ++li)
            FISupp[*li] += 1;
    }


    /*update the FFISupp by erasing all first Candidates items  with support < Threshold*/

    std::map<int, int> ::iterator current = FISupp.begin();

    std::list<int> ls; /* save Candidates itemes with support < Threshold*/
    while (current != FISupp.end())
    {
        if (current->second < threshold)
        {
            ls.push_back(current->first);
            current = FISupp.erase(current);
        }
        else
            ++current;
    }


    /*update the the orginal data by erasing all first Candidates items  with support < Threshold*/
    for (unsigned i = 0; i < data.size(); ++i)
    {
        std::list<int>::iterator li;
        std::list<int>::iterator item = ls.begin();

        while (item != ls.end())
        {
            for (li = data[i].begin(); li != data[i].end(); ++li)
            {
                if (*li == *item)
                {
                    li = data[i].erase(li);
                    break;
                }
            }
            ++item;
        }

    }


}


void FrequentItem(list<int> l,   int indx)
{
    int a = 0;
    for (list<int>::iterator it = l.begin(); it != l.end(); ++it)
    {
        //std::list <int> &m2 = CandList[indx].first;

        //auto itr = m2.find(*it);

        //auto itr = std::find(CandList.begin(), CandList.end(), *it);

        auto itr = CandList.find(*it);
        if (itr != CandList.end())
        {
            a += CandList[indx].second;
            CandList[indx].first.push_back(*it);
            CandList[indx].second = a;
        }

    }

}

int ind = 0;
void Passk(int j, std::list<int>::iterator Itm , int q = 0)
{

    if (Lsize == q)
    {
        FrequentItem(FrqList, ind);
        ++ind;
        return;
    }

    else
    {

        for (std::list<int>::iterator Itm2 = Itm; Itm2 != data[j].end(); ++Itm2)
        {
                FrqList.push_back(*Itm2);
                Passk(j,  ++Itm2, q + 1);
                FrqList.pop_back();
                --Itm2;

        }

    }


}



void main(int argc, char *argv[])
{
    int temp = 0;
    int j = -1;

    ScanData();
    PassOne();

    while (Lsize <= data.size()) // How to stop the loop when there is no more candidate >= threshold???
    {
        for (unsigned i = 0; i < data.size(); ++i)
        {
            std::list<int>::iterator items = data[i].begin();
            Passk(++j, items);  
        }

        j = -1;
        ++ Lsize;

    }

    data.clear();
    system("PAUSE");
    return;
}

【问题讨论】:

  • 你的程序的目标和逻辑我都不清楚。
  • 检查 我理解您的规定:您想列出所有集合 S 使得 S 至少是您列表中集合数量 threshold 的子集;并且您想按大小对输出进行排序?
  • 考虑使用std::set 而不是std::list

标签: c++


【解决方案1】:

好的,我会尝试回答。但首先是假设:

  • 您正在使用 有序 集,即元素严格按升序排列。
  • 您考虑的是“正常”集合,即没有可能出现重复元素的多集合。
  • 这两个假设都可以轻松放宽,但我会以此为基础。

对于这种情况,通过位向量对集合进行编码可能更自然(例如使用std::vector&lt;bool&gt;boost::dynamic_bitset&lt;&gt;)。在这样的位向量中,如果设置了i-th 元素,则表示集合中存在数字i

比如你的三个集合就是用这个来表示的

1 1 1 1 1
1 0 1 0 1
1 0 1 1 0

迭代 1:在您的第一次迭代中,您只需对元素求和,这在此表示中相当容易。一个获得

    1 1 1 1 1
    1 0 1 0 1
    1 0 1 1 0
   -----------
    3 1 3 2 2

接下来,您丢弃低于阈值的所有元素,这相当于将第二行设置为零:

    1 0 1 1 1
    1 0 1 0 1
    1 0 1 1 0

迭代K:在这里,您计算所有K-子集的出现,如果它们的数量小于阈值则丢弃它们。也就是说,正式地,您生成 K-stencils

{ 1 1 0 0 0, 1 0 1 0 0, ... , 0 0 0 1 1}   (for K=2)
{ 1 1 1 0 0, 1 1 0 1 0, ... , 0 0 1 1 1}   (for K=3)

等等。对于这些K-stencil 中的每一个,您计算其出现并最终丢弃(注意K 也可能是一个)。所以,你有三个任务,即

  1. Generation:通过初始位向量{1 ... 1 0 ... 0}的置换得到,其中K元素向左排序。

  2. 计数:循环遍历集合中的向量,并按位使用and 检查当前向量是否包含模板。例如:1 0 1 1 1 &amp; 0 0 0 1 1 == 0 0 0 1 1?。

  3. 丢弃:通过按位and 应用反转模板(通过flip() 完成反转)。这将删除相关的子集。最后丢弃任何小于迭代次数的子集(例如,在迭代 3 中,删除大小为 2 的子集)。

这是一个主要使用boost::dynamic_bitset&lt;&gt; 的实现,但std::vector&lt;bool&gt; 用于排列(这是因为我不想自己编写排列,但这当然可以改进)。请注意,没有地图或其他更复杂的存储方案:

#include<vector>
#include<iostream>
#include<algorithm>
#include<string>
#include<sstream>
#include<boost/dynamic_bitset.hpp>

//only for nice output of a bitset
std::string screenOutput(const boost::dynamic_bitset<>& bits)
{
    int n=bits.size();
    std::string ret;
    for(int i=n-1;i>=0;--i)
    {
        if(bits[i])
        {
           std::stringstream out;
           out<<i+1<<" ";
           ret=out.str()+ret;
        }
    }
    return "{"+ret+"}";
}

//function implementing the actual logic
void removeSubsets(std::vector<boost::dynamic_bitset<> > &currentSet, size_t K, size_t thresh)
{
    size_t n=currentSet.front().size();

    //create initial stencil {1 ... 1 0 ... 0}
    std::vector<bool> stencil(n);
    for(size_t i=0;i<K;++i)
        stencil[i]=true;

    //apply permutations to initial stencil
    do
    {
         //generate dynamic_bitset from permuted vector<bool>
         boost::dynamic_bitset<> temp(n);
         for(size_t i=0;i<n;++i)
              temp[i]=stencil[i];

         //count the occurence of the stencil
         size_t count=0;
         for(size_t j=0;j<currentSet.size();++j)
         {
              if((currentSet[j] & temp) == temp)
                 ++count;
         }

         //remove if at least one and less than thresh is found
         if(count<thresh && count>0)
         {
              boost::dynamic_bitset<> tempFlip=temp;
              tempFlip.flip();
              for(size_t j=0;j<currentSet.size();++j)
              {
                    //remove stencil from all bitset which contain it
                    if((currentSet[j] & temp) == temp)
                      currentSet[j]= (currentSet[j] & tempFlip);
              }
         }
    }
    while(std::prev_permutation(stencil.begin(),stencil.end()));

    //further remove all supersets which contain less than K elements
    for(size_t j=0;j<currentSet.size();++j)
         if(currentSet[j].count()<K)
         {
               currentSet[j]=boost::dynamic_bitset<>(n,0);
         }
}

代码可以这样使用:

int main()
{
    //initialize set of three bit-vectors (all elements to true)
    std::vector<boost::dynamic_bitset<> > yourSet(3, boost::dynamic_bitset<>(5, (1<<5)-1) );

    //set corresponding elements to false
    yourSet[1][1]=false;
    yourSet[1][3]=false;
    yourSet[2][1]=false;
    yourSet[2][4]=false;

    std::cout<<"Original sets"<<std::endl;
    for(size_t i=0;i<3;++i)
        std::cout<<screenOutput(yourSet[i])<<std::endl;
    std::cout<<std::endl;

    removeSubsets(yourSet, 1, 2);
    std::cout<<"After iteration 1:"<<std::endl;
    for(size_t i=0;i<3;++i)
        std::cout<<screenOutput(yourSet[i])<<std::endl;
    std::cout<<std::endl;

    removeSubsets(yourSet, 2, 2);
    std::cout<<"After iteration 2:"<<std::endl;
    for(size_t i=0;i<3;++i)
        std::cout<<screenOutput(yourSet[i])<<std::endl;
    std::cout<<std::endl;

    removeSubsets(yourSet, 3, 2);
    std::cout<<"After iteration 3:"<<std::endl;
    for(size_t i=0;i<3;++i)
        std::cout<<screenOutput(yourSet[i])<<std::endl;
    std::cout<<std::endl;
}

它输出:

Original set:
{1 2 3 4 5}
{1 3 5}
{1 3 4}

After iteration 1:
{1 3 4 5}
{1 3 5}
{1 3 4}

After iteration 2:
{}
{1 3 5}
{1 3 4}

After iteration 3:
{}
{}
{}

伙计,我显然有太多时间了。


EDIT:更正了代码。你仍然需要检查它是否真的带来了你想要的。

【讨论】:

  • 非常感谢,我是编程初学者,以前没有使用甚至安装过boost库,但现在我会的。你写的代码很有帮助,它让我从另一个角度打开了思路,我相信我会在这里学到很多东西。感谢您花时间帮助我
  • 不客气。我希望算法(和代码)也是正确的——如果不是,请在此处说明。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-10
  • 1970-01-01
相关资源
最近更新 更多