簡體   English   中英

如何編寫C ++版本的Python Powerset函數

[英]How to write C++ version of Python powerset function

我想編寫與鄰接矩陣一起使用的最大集團算法。 我正在觀看一個視頻 ,該視頻解釋了如何使用Python編碼和實現算法。 我目前正在嘗試在視頻播放2分鍾時對powerset函數進行編碼。

def powerSet(elts):
    if len(elts) == 0:
        return [[]]
    else:
        smaller = powerSet(elts[1:])
        elt = [elts[0]]
        withElt = []
        for s in smaller:
            withElt.append(s + elt)
        allofthem = smaller + withElt
    return allofthem

print powerSet([1, 2, 3, 4, 5])

我想用C ++重寫它。 我不確定是否應該使用數組。 到目前為止,我已經對下面的代碼進行了編碼,但是我不知道如何在空數組內返回空數組(當elts list的大小為0時)。

由於無法像在Python中那樣使用len(elts) ,因此為數組編寫了isEmpty函數。 我的方法可能不是最好的方法,因此我願意接受任何建議。

更新:

array powerSet(int elts[])
{
     if (isEmpty(elts)==1)
     {
          return {{}};
     }

}

在我的int main中,我有:

list<int> elts;

list<int>::iterator i;

for (i=elts.begin(); i != elts.end(); ++i)
      cout << *i << " ";
      cout << endl;

powerSet(elts);    

我不知道從這里做什么。

代碼應使用我們稱為“ elts”(元素的縮寫)的array / list / vector 然后,首先,它應該添加一個空列表[] ,然后添加其余的功率集(全部顯示在視頻中)。

因此,例如,在elts = [1,2,3,4]的情況下,我的代碼應返回:

`[ [],[4],[3],[4,3],[2],[4,2],[3,2],[4,3,2],[1],[4,1],[3,1],[4,3,1],[2,1],[4,2,1],[‌​3,2,1],[4,3,2,1] ]  `  

我不知道如何使用array / list / vector來完成上述操作。

這是Python代碼的文字轉換

#include <vector>
using std::vector;
#include <iostream>
using std::cout;

//def powerSet(elts):
vector<vector<int>> powerSet(const vector<int>& elts)
{
//  if len(elts) == 0:
    if (elts.empty()) {
//      return [[]]
        return vector<vector<int>>(
            1, // vector contains 1 element which is...
            vector<int>()); // ...empty vector of ints
    }
//  else:
    else {
//      smaller = powerSet(elts[1:])
        vector<vector<int>> smaller = powerSet(
            vector<int>(elts.begin() +1, elts.end()));
//      elt = [elts[0]]
        int elt = elts[0]; // in Python elt is a list (of int)
//      withElt = []
        vector<vector<int>> withElt;
//      for s in smaller:
        for (const vector<int>& s: smaller) {
//          withElt.append(s + elt)
            withElt.push_back(s);
            withElt.back().push_back(elt);
        }
//      allofthem = smaller + withElt
        vector<vector<int>> allofthem(smaller);
        allofthem.insert(allofthem.end(), withElt.begin(), withElt.end());
//      return allofthem
        return allofthem;
    }
}

這將vector<int>用於整數集。 然后它的vector ,即vector<vector<int>>是整數集的列表。 你專門問了一件事

我不知道如何在一個空數組內返回一個空數組(當elts list的大小為0時)。

第一個return語句使用vector構造函數返回一個包含一個元素(空集)的列表,其構造函數的第一個參數nvector中元素的數量,第二個參數是重復n次的元素。 一個等效但更復雜的選擇是

    vector<vector<int>> powerSetOfEmptySet;
    vector<int> emptySet;
    powerSetOfEmptySet.push_back(emptySet);
    return powerSetOfEmptySet;

我試圖使代碼保持簡單,並避免使用太多深奧的C ++功能。 希望您可以在良好參考的幫助下完成它。 已經使用一個C ++成語是附加一個矢量到另一個在allofthem.insert(allofthem.end(), withElt.begin(), withElt.end());

同樣在C ++中,它比Python“更接近金屬”,您可能只使用一個vector來代替三個smaller的向量,分別是withEltallofthem 這導致下面的代碼更短,更優化。

//def powerSet(elts):
vector<vector<int>> powerSet(const vector<int>& elts)
{
//  if len(elts) == 0:
    if (elts.empty()) {
//      return [[]]
        return vector<vector<int>>(1, vector<int>());
    }
//  else:
    else {
//      smaller = powerSet(elts[1:])
        vector<vector<int>> allofthem = powerSet(
            vector<int>(elts.begin() +1, elts.end()));
//      elt = [elts[0]]
        int elt = elts[0]; // in Python elt is a list (of int)
//      withElt = []
//      for s in smaller:
//          withElt.append(s + elt)
//      allofthem = smaller + withElt
        const int n = allofthem.size();
        for (int i=0; i<n; ++i) {
            const vector<int>& s = allofthem[i];
            allofthem.push_back(s);
            allofthem.back().push_back(elt);
        }
//      return allofthem
        return allofthem;
    }
}

下面的測試代碼

int main()
{
    const int N = 5;
    vector<int> input;
    for(int i=1; i<=N; ++i) {
        input.push_back(i);
    }
    vector<vector<int>> ps = powerSet(input);
    for(const vector<int>& set:ps) {
        cout << "[ ";
        for(int elt: set) {
            cout << elt << " ";
        }
        cout << "]\n";
    }
    return 0;
}

作為一個腳注,我很驚訝地發現從Python轉換為C ++如此簡單。 我已經讀過,使用Python編寫(或原型)算法,然后根據需要使用諸如C ++的語言進行重寫可能很有意義,但是,正如諺語所說,眼見為實。


這是與C ++ 98一起使用的程序的版本。 我恢復,我用(基本的C ++ 11特征>>在模板聲明和基於范圍for )。

#include <vector>
using std::vector;
#include <iostream>
using std::cout;

vector<vector<int> > powerSet(const vector<int>& elts)
{
    if (elts.empty()) {
        return vector<vector<int> >(1, vector<int>());
    }
    else {
        vector<vector<int> > allofthem = powerSet(
            vector<int>(elts.begin() +1, elts.end()));
        int elt = elts[0];
        const int n = allofthem.size();
        for (int i=0; i<n; ++i) {
            const vector<int>& s = allofthem[i];
            allofthem.push_back(s);
            allofthem.back().push_back(elt);
        }
        return allofthem;
    }
}

int main()
{
    const int N = 5;
    vector<int> input;
    for(int i=1; i<=N; ++i) {
        input.push_back(i);
    }
    vector<vector<int> > ps = powerSet(input);
    for(vector<vector<int> >::const_iterator i=ps.begin(); i!=ps.end(); ++i) {
        const vector<int>& set = *i;
        cout << "[ ";
        for(vector<int>::const_iterator j=set.begin(); j!=set.end(); ++j) {
            int elt = *j;
            cout << elt << " ";
        }
        cout << "]\n";
    }
    return 0;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM