C++ C++;:使用一个条件从集合生成所有子集
我试图编写代码,用一个条件从集合中生成所有子集,如 如果阈值为2,且设置了三个:C++ C++;:使用一个条件从集合生成所有子集,c++,C++,我试图编写代码,用一个条件从集合中生成所有子集,如 如果阈值为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 第一次迭代时的生成集: 1 = number of frequen
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
第一次迭代时的生成集:
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
第四次迭代时的生成集
1,3,4= number of frequency = 2
1,3,5= number of frequency = 2
不再有超集,因为(4,5)<我们无法生成(1,3,4,5)的阈值
我写了这个程序,我已经生成了所有的子集,但在两件事上失败了:
- 我无法在map
中搜索以计算类似的集合(频率数)std::map 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;
}
int阈值=2;
std::矢量数据;
标准::map FISupp;
typedef std::pair组合;
地图目录;
std::列表FrqList;
/*
输入:阈值=2,数据=
1 2 3 4 5
1 3 4 5
1 2 3 5
1 3
PassOne函数后的第一次扫描:
菲苏普(1,4)
菲苏普(2,2)
菲苏普(3,4)
菲苏普(4,4)
菲苏普(5,3)
在Passk功能后的k扫描:
---
*/
int Lsize=2;//水平尺寸
无效Scanda()
{
如果输入;
in.open(“mydata.txt”);
/*mydata.txt
1 2 3 4 5
1 3 4 5
1 2 3 5
1 3
*/
std::字符串行;
int i=0;
while(std::getline(in,line))
{
标准::stringstream Sline1(线);
std::stringstream ss(线路);
std::列表内部;
国际信息;
而(ss>>信息)
内部。推回(信息);
数据。推回(内部);
}
}
/*第一次通过以生成第一个候选项*/
void PassOne()
{
for(无符号i=0;i秒<阈值)
{
ls.推回(当前->第一);
电流=f抑制擦除(电流);
}
其他的
++电流;
}
/*通过删除支持度<阈值的所有第一个候选项来更新原始数据*/
for(无符号i=0;i
好的,我会尝试回答。但首先是假设:
- 您使用的是有序集,即元素严格按升序排列
- 这两个假设可能很容易放松,但我将以此为基础
std::vector
或boost::dynamic_bitset
)。在这种位向量中,如果设置了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-模板
{ 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
-模具中的每一个,您计算发生次数并最终放弃(注意K
也可能是一个)。因此,您有三个任务,即
{1…10…0}
获得,其中K
元素为
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:
{}
{}
{}