用我自己的程序版本更新:
我正在尝试进行迭代动态编程来生成n choose k
组合。
假设我有 4 个值向量
v1 : 1 1 1
v2 : 2 2 2
v3 : 3 3 3
v4 : 4 4 4
现在我使用加法作为我的聚合函数,我想生成4 choose 2
向量组合如下:
v1v2 : 3 3 3
v1v3 : 4 4 4
v1v4 : 5 5 5
v2v3 : 5 5 5
v2v4 : 6 6 6
v3v4 : 7 7 7
一个简单的方法是遍历每一对并找到结果。如果N
and k
非常大,这是非常非常低效的。因此,另一种方法是递归/迭代动态编程。非常大的 N 和 k 的递归会占用大量内存,因此理想的方法是迭代动态编程,可以按如下方式完成:
Consider the following table row header is N
and the column header is k
and our objective is to find N choose k
:
我们可以找N choose k
通过以下方式使用动态程序进行组合:
方法如下:
- Block[0,1] 和 Block[0,2] 始终返回 [ ]。 {[ ] 表示空值,因为那里没有值}。
- Block[1,1] 接收 [ ],计算 {v1} + [ ] (即 v1 本身),并将其保存到 Block [1,1]。
- Block[1,2] 接收 [ ],执行 {v1} + [ ] & {v2} + [ ],将其保存到 Block [1,2]。
- Block[1,3] 接收 [ ],执行 {v1} + [ ]、{v2} + [ ] + {v3} U [ ]、Block [1,3]。
- Block[2,4] receives :
- [{v1}] 来自 [1,1] 并计算 {v1} + {v2},
- [{v1}{v2}] 来自 [1,2] 并计算 {v1} + {v3} 和 {v2} + {v3}
- [{v1}, {v2}, {v3}] 从 [1,3] 计算 {v4} + {v1}, {v4} + {v2} 和 {v4} + {v3},将其保存到 Block [ 2,4]。
现在我们在块 [2,4] 中拥有了所需的所有值。我如何用 C++ 有效地编写这个概念?
任何帮助是极大的赞赏。谢谢。
这是我的想法:
我不知道这是否正确。对不起
=================================================== =======
//Block [0...k][0...n];
//Block[i][j] contains i-set groups (for eg :if i = 2 it will have v1v2, v1v3, etc..)
//initially, Block[0][i] = [ ] for 0 <= i <= n and all other Block [i][j] = [ $ ]
// "$" is just a symbol to indicate that no computation is done on that block
algorithm(int k, int n) //k-point skyline groups from n number of points.
{
if( Block[k][n] != [ $ ] ) return memory[k][n];
Group = [ ]; //G indicate a collection of vectors
for( int i = k; i <= n; i++ )
{
Group` = algorithm(k-1, i-1);
for( each v` in Group` )
{
Group = Group + (group` + v_i);
}
}
memory[k][n] = Group;
return Group;
}
=================================================== =======
这是我针对上述算法的程序:
#include <iostream>
#include <iterator>
#include <set>
#include <vector>
#include <map>
#define DIMENSIONS 5 // No. of elements in the vector. eg. v1: 1 1 1 1 1
using namespace std;
typedef std::set < int > my_set; // To hold vector id's
typedef std::vector < int > my_vector; // To hold values of the vector's
typedef std::vector < std::pair < my_set, my_vector > > my_vector_pair;
typedef std::map < my_set, my_vector > my_map;
typedef std::vector < vector < std::pair < int,my_map > > > my_pair;
typedef my_map::iterator m_it;
my_vector_pair bases; // To hold all the initial <id,vector_values> pair
my_map data, G;
my_pair memory;
void print(my_map& data)
{
for( m_it it(data.begin()) ; it!=data.end(); ++it)
{
cout << "Id : ";
copy(it->first.begin(), it->first.end(), ostream_iterator<int>(cout, " "));
cout << " => value : ";
copy (it->second.begin(),it->second.end(),ostream_iterator<int>(cout," "));
cout << endl;
}
cout << "---------------------------------------------------------------\n";
}
my_map union_(my_map& G, int p)
{
static my_map result;
my_set id;
my_vector scores;
result.clear();
for (m_it it(G.begin()); it != G.end(); ++it)
{
id = it->first;
scores = it->second;
id.insert( bases.at(p-1).first.begin(),bases.at(p-1).first.end() );
for (int j = 0; j < DIMENSIONS; j++)
{
scores.at(j) += bases.at(p - 1).second.at(j);
}
result.insert(make_pair(id, scores));
}
return result;
}
my_map algorithm_(int k, int n) {
unsigned long size = memory.at(n).size();
for (unsigned long i = 0; i < size; i++) {
if (memory.at(n).at(i).first == k) {
return memory.at(n).at(i).second; //if exists in hash table then no need to calculate again
}
}
my_map G_k_1;
if (k != n)
{
G_k_1 = algorithm_(k, n - 1);
if(G_k_1.size() == 0)
{
return G_k_1;
}
}
G_k_1 = algorithm_(k - 1, n - 1);
if(G_k_1.size() == 0)
{
return G_k_1;
}
G_k_1 = union_(G_k_1, n);
if (k != n) {
for (unsigned long i = 0; i < memory.at(n - 1).size(); i++) {
if (memory.at(n - 1).at(i).first == k) {
G_k_1.insert(memory.at(n - 1).at(i).second.begin(), memory.at(n - 1).at(i).second.end());
memory.at(n - 1).at(i).second.clear();
break;
}
}
}
std::pair<int,my_map> temp;
temp.first = k ;
temp.second = G_k_1;
memory.at(n).push_back( temp ); //storing in hash table for further use
return memory.at(n).back().second;
}
int main()
{
my_vector v1,v2,v3,v4,v5;
my_set s1,s2,s3,s4,s5;
for(int i = 1; i<=5; ++i)
{
v1.push_back(1);
v2.push_back(2);
v3.push_back(3);
v4.push_back(4);
v5.push_back(5);
}
s1.insert(1);
s2.insert(2);
s3.insert(3);
s4.insert(4);
s5.insert(5);
bases.insert(bases.end(),make_pair(s1,v1));
bases.insert(bases.end(),make_pair(s2,v2));
bases.insert(bases.end(),make_pair(s3,v3));
bases.insert(bases.end(),make_pair(s4,v4));
bases.insert(bases.end(),make_pair(s5,v5));
my_set empty_set;
my_vector empty_group(DIMENSIONS);
G.insert(make_pair(empty_set,empty_group));
vector<std::pair<int,my_map> > empty_element;
empty_element.push_back(make_pair(0,G));
for (int i = 0; i <= 5; i++) { // 5 is the total number od vectors : v1,v2,v3,v4,v5
memory.push_back(empty_element);
}
data.insert(bases.begin(),bases.end());
cout << endl << "The intial set of vectors are : " << endl;
print ( data );
int k;
cout << "N = 5 " << endl << "Enter the value of k : ";
cin >> k;
cout << "The values for N choose k are : " << endl;
data = algorithm_(k,5);
print ( data );
}
如果您运行该程序,您就会知道我想要实现什么以及以何种方式实现。这个算法(不是程序)对于较少数量的向量可能效率不高,但当 N > 50k 和 k ~ 10 时就会有效。我知道算法(我的程序)的实现效率非常低。有什么办法可以改善吗?我认为相同的算法可以以更优雅的方式实现。任何帮助深表感谢。谢谢。