动态迭代编程生成组合

2024-01-25

用我自己的程序版本更新:

我正在尝试进行迭代动态编程来生成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 : Table1

我们可以找N choose k通过以下方式使用动态程序进行组合:

方法如下:

  1. Block[0,1] 和 Block[0,2] 始终返回 [ ]。 {[ ] 表示空值,因为那里没有值}。
  2. Block[1,1] 接收 [ ],计算 {v1} + [ ] (即 v1 本身),并将其保存到 Block [1,1]。
  3. Block[1,2] 接收 [ ],执行 {v1} + [ ] & {v2} + [ ],将其保存到 Block [1,2]。
  4. Block[1,3] 接收 [ ],执行 {v1} + [ ]、{v2} + [ ] + {v3} U [ ]、Block [1,3]。
  5. 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 时就会有效。我知道算法(我的程序)的实现效率非常低。有什么办法可以改善吗?我认为相同的算法可以以更优雅的方式实现。任何帮助深表感谢。谢谢。


对于之前误解您的答案,我深表歉意,我真的不明白您在帖子中想要做什么,我以为您只是在寻找计算 nCk 的非递归方式:P

我创建了一个类,CombinationGenerator产生向量的组合,我相信这就是你想要的。它的工作原理是生成一个整数向量,表示要聚合的元素的索引(我已经包含了main下面的函数应该有助于以编程方式解释它)。

这是头文件:http://pastebin.com/F5x4WKD9 http://pastebin.com/F5x4WKD9

以及源文件:http://pastebin.com/CTV1PLRb http://pastebin.com/CTV1PLRb

这是一个示例主函数:

typedef std::vector<int> vecInt;

int main() {

    // We have a deque containing 3 elements (try using experimenting with data
    // types to test space complexity, std::set or std::unordered_set might be an option)
    vecInt vec1;
    for( int i = 0; i < 3; i++ )
    {
        vec1.push_back(1);
    }
    vecInt vec2;
    for( int i = 0; i < 3; i++ )
    {
        vec2.push_back(2);
    }
    vecInt vec3;
    for( int i = 0; i < 3; i++ )
    {
        vec3.push_back(3);
    }
    vecInt vec4;
    for( int i = 0; i < 3; i++ )
    {
        vec4.push_back(4);
    }
    vecInt vec5;
    for( int i = 0; i < 3; i++ )
    {
        vec5.push_back(5);
    }

    std::deque<std::vector<int>> dequeVecs;
    dequeVecs.push_back( vec1 );
    dequeVecs.push_back( vec2 );
    dequeVecs.push_back( vec3 );
    dequeVecs.push_back( vec4 );
    dequeVecs.push_back( vec5 );

    // Create our CombinationGenerator:
    CombinationGenerator* gen = new CombinationGenerator();

    g_pCombinationGen = gen;

    gen = NULL;

    unsigned long long size = g_pCombinationGen->ComputeBinomialCoefficient( dequeVecs.size(), 2 );

    std::vector<int> currCombination;

    g_pCombinationGen->Initialize( dequeVecs.size(), 2, size );

    while( !g_pCombinationGen->IsFinished() )
    {
        currCombination = g_pCombinationGen->NextCombination();

        std::vector<int> result;
        for( int i = 0; i < dequeVecs[0].size(); i++ )
        {
            result.push_back( dequeVecs[currCombination[0]][i] + dequeVecs[currCombination[1]][i] );
        }

        std::cout << "(";
        for( int i = 0; i < result.size(); i++ )
        {
            std::cout << result[i];
        }
        std::cout << ")" << std::endl;

    }

    return 0;

}

虽然这可能看起来相当大,但如果您分析它的空间使用情况(假设您使用 n = 50,000 和 k = 1000:

有 50,000 个向量,每个向量包含 3 个整数(让我们假设每个 32 字节向量的开销相当严重,在大多数实现中通常约为 20):因此,(50,000 * 3 * 4) + (50,000 * 32) = 2,200,000 Bytes

然后将其包含在双端队列中,我们还假设它的开销为 32 字节:2,200,000 + 32 = 2,200,032 Bytes

我们还有一个运行组合生成器的实例,它有 5 个成员变量、两个 int、两个 long long 和一个包含 k 个 int(在本例中为 1000)的向量,因此:2,200,032 + (2*4) + (2*8) + (1000*4) + 32 = 2,204,056 Bytes

我们还有包含 k 个整数的每次迭代结果的向量:2,204,056 + (1000*4) + 32 = 2,208,088 Bytes

正如您所看到的,这远远低于您的 4GB 内存。注意:无论您使用什么实现,都不可能将这些向量中的每一个存储在内存中,因为将会超过9.94 x 10^2126包含结果的向量。即使您选择较小的 k 值(例如 10),您仍然会得到超过2.69 x 10^40.

我希望这次我明白你的要求是什么!如果没有,我会尝试再次理解您想要实现的目标。 :)

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

动态迭代编程生成组合 的相关文章

  • 编译时运算符

    有人可以列出 C 中可用的所有编译时运算符吗 C 中有两个运算符 无论操作数如何 它们的结果始终可以在编译时确定 它们是sizeof 1 and 2 当然 其他运算符的许多特殊用途可以在编译时解决 例如标准中列出的那些整数常量表达式 1 与
  • 用于检查类是否具有运算符/成员的 C++ 类型特征[重复]

    这个问题在这里已经有答案了 可能的重复 是否可以编写一个 C 模板来检查函数是否存在 https stackoverflow com questions 257288 is it possible to write a c template
  • 嵌套接口:将 IDictionary> 转换为 IDictionary>?

    我认为投射一个相当简单IDictionary
  • 使用实体框架模型输入安全密钥

    这是我今天的完美想法 Entity Framework 中的强类型 ID 动机 比较 ModelTypeA ID 和 ModelTypeB ID 总是 至少几乎 错误 为什么编译时不处理它 如果您使用每个请求示例 DbContext 那么很
  • BitTorrent 追踪器宣布问题

    我花了一点业余时间编写 BitTorrent 客户端 主要是出于好奇 但部分是出于提高我的 C 技能的愿望 我一直在使用理论维基 http wiki theory org BitTorrentSpecification作为我的向导 我已经建
  • 关于 C++ 转换:参数 1 从“[some_class]”到“[some_class]&”没有已知的转换

    我正在研究 C 并且遇到了一个错误 我不知道确切的原因 我已经找到了解决方案 但仍然想知道原因 class Base public void something Base b int main Base b b something Base
  • 在 ASP.NET 5 中使用 DI 调用构造函数时解决依赖关系

    Web 上似乎充斥着如何在 ASP NET 5 中使用 DI 的示例 但没有一个示例显示如何调用构造函数并解决依赖关系 以下只是众多案例之一 http social technet microsoft com wiki contents a
  • C# 中通过 Process.Kill() 终止的进程的退出代码

    如果在我的 C 应用程序中 我正在创建一个可以正常终止或开始行为异常的子进程 在这种情况下 我通过调用 Process Kill 来终止它 但是 我想知道该进程是否已退出通常情况下 我知道我可以获得终止进程的错误代码 但是正常的退出代码是什
  • 使用 WebClient 时出现 System.Net.WebException:无法创建 SSL/TLS 安全通道

    当我执行以下代码时 System Net ServicePointManager ServerCertificateValidationCallback sender certificate chain errors gt return t
  • C++ OpenSSL 导出私钥

    到目前为止 我成功地使用了 SSL 但遇到了令人困惑的障碍 我生成了 RSA 密钥对 之前使用 PEM write bio RSAPrivateKey 来导出它们 然而 手册页声称该格式已经过时 实际上它看起来与通常的 PEM 格式不同 相
  • 将多个表映射到实体框架中的单个实体类

    我正在开发一个旧数据库 该数据库有 2 个具有 1 1 关系的表 目前 我为每个定义的表定义了一种类型 1Test 1Result 我想将这些特定的表合并到一个类中 当前的类型如下所示 public class Result public
  • 重载<<的返回值

    include
  • while 循环中的 scanf

    在这段代码中 scanf只工作一次 我究竟做错了什么 include
  • 使用 x509 证书签署 json 文档或字符串

    如何使用 x509 证书签署 json 文档或字符串 public static void fund string filePath C Users VIKAS Desktop Data xml Read the file XmlDocum
  • 覆盖子类中的字段或属性

    我有一个抽象基类 我想声明一个字段或属性 该字段或属性在从该父类继承的每个类中具有不同的值 我想在基类中定义它 以便我可以在基类方法中引用它 例如覆盖 ToString 来表示 此对象的类型为 property field 我有三种方法可以
  • 链接器错误:已定义

    我尝试在 Microsoft Visual Studio 2012 中编译我的 Visual C 项目 使用 MFC 但出现以下错误 error LNK2005 void cdecl operator new unsigned int 2
  • 向现有 TCP 和 UDP 代码添加 SSL 支持?

    这是我的问题 现在我有一个 Linux 服务器应用程序 使用 C gcc 编写 它与 Windows C 客户端应用程序 Visual Studio 9 Qt 4 5 进行通信 是什么very在不完全破坏现有协议的情况下向双方添加 SSL
  • 如何在Xamarin中删除ViewTreeObserver?

    假设我需要获取并设置视图的高度 在 Android 中 众所周知 只有在绘制视图之后才能获取视图高度 如果您使用 Java 有很多答案 最著名的方法之一如下 取自这个答案 https stackoverflow com a 24035591
  • C# 成员变量继承

    我对 C 有点陌生 但我在编程方面有相当广泛的背景 我想做的事情 为游戏定义不同的 MapTiles 我已经像这样定义了 MapTile 基类 public class MapTile public Texture2D texture pu
  • 如何将服务器服务连接到 Dynamics Online

    我正在修改内部管理应用程序以连接到我们的在线托管 Dynamics 2016 实例 根据一些在线教程 我一直在使用OrganizationServiceProxy out of Microsoft Xrm Sdk Client来自 SDK

随机推荐