[c++] ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ฐœ๋…๊ณต๋ถ€ :: ์ˆœ์—ด๊ณผ ์กฐํ•ฉ (์ฝ”๋“œ, next_permutation ์ด์šฉ, ์ง์ ‘ ๊ตฌํ˜„)

์ˆœ์—ด๊ณผ ์กฐํ•ฉ

์ˆœ์—ด

์ •์˜

์ˆœ์—ด์€ ์ค‘๋ณต์—†์ด n๊ฐœ ์ค‘ r๊ฐœ๋ฅผ ๋ฝ‘์•„ ์ˆœ์„œ๋ฅผ ์ •ํ•ด ๋‚˜์—ดํ•˜๋Š” ๊ฒฝ์šฐ์ด๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์ฒ˜๋Ÿผ A, B, C ์„ธ๊ฐ€์ง€ ์›์†Œ๊ฐ€ ์žˆ์„ ๋•Œ, 2๊ฐœ๋ฅผ ๋ฝ‘์•„ ์ˆœ์„œ๋ฅผ ์ •ํ•˜๋Š” ๊ฒƒ์„ 3P2๋ผ๊ณ  ํ•œ๋‹ค. ์•„๋ž˜์™€ ๊ฐ™์€ ๊ฒฝ์šฐ์˜ ์ˆ˜๊ฐ€ ๋‚˜์˜ค๋ฏ€๋กœ ์ด 6๊ฐ€์ง€ ์ด๋‹ค.

์ˆœ์—ด์€ P(Permutation)๋กœ ํ‘œํ˜„ํ•˜๋ฉฐ ์‹์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.
$$
{}n\mathrm{P}{r} = \frac{n!}{(n-r)!}
$$

์ค‘๋ณต ์ˆœ์—ด์ด๋ผ๋Š” ํŠน๋ณ„ํ•œ ๊ฒฝ์šฐ๋Š” ์ˆœ์—ด ์ค‘ ๊ฐ™์€ ์ข…๋ฅ˜์˜ ๊ฒƒ์„ ๋‹ค์‹œ ๋ฝ‘์„ ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ๋ฅผ ๋งํ•œ๋‹ค.

์ค‘๋ณต ์ˆœ์—ด์€ Π๋กœ ํ‘œํ˜„ํ•˜๋ฉฐ ์‹์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.
$$
{n}\mathrm{\pi}{r} = n^r
$$

 

๊ตฌํ˜„

์ˆœ์—ด

// ์ง์ ‘ ๊ตฌํ˜„
void swap(vector<int>& set, int a, int b){
    int tmp = set[a];
    set[a] = set[b];
    set[b] = tmp;
}

void permutation(vector<int>& set, int size, int n, int r){
    if(size==r){
        for(int i=0; i<size; i++)
            cout << set[i] << " ";
        cout << endl;
        return;
    }
    for(int i=size; i<n; i++){
        swap(set, i, size);
        permutation(set, size+1, n, r);
        swap(set, i, size);
    }
}

int main()
{
    vector<int> set;
    set.push_back(1);
    set.push_back(2);
    set.push_back(3);

    combination(set, 0, 3, 2);

    return 0;
}
// next_permutation ์ด์šฉ
void permutation(vector<int>& set, int n, int r){
    do{
        for(int i=0; i<r; i++)
            cout << set[i] << " ";
        cout << endl;
    }while(next_permutation(set.begin(), set.end()));
}

int main()
{
    vector<int> set;
    set.push_back(1);
    set.push_back(2);
    set.push_back(3);

    combination(set, 3, 2);

    return 0;
}

 

์ค‘๋ณต ์ˆœ์—ด

void repeatedPermutation(vector<int>& set, vector<int>&tmp, int size, int n, int r){
    if(size==r){
        for(int i=0; i<size; i++)
            cout<< tmp[i] << " ";
        cout << endl;
        return;
    }
    for(int i=0; i<n; i++){
        tmp[size] = set[i];
        permutation(set, tmp, size+1, n, r);
        tmp[size] = 0;
    }
}

int main()
{
    vector<int> set;
    set.push_back(1);
    set.push_back(2);
    set.push_back(3);
    vector<int>tmp(3);

    repeatedPermutation(set,tmp,0, 3, 2);

    return 0;
}

์ค‘๋ณต ์ˆœ์—ด์„ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด์„œ tmp๋ผ๋Š” vector๋ฅผ ํ•˜๋‚˜ ๋” ์‚ฌ์šฉํ•˜์˜€๋‹ค. ๋”ฐ๋กœ vector๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ ๋„ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ์„๊นŒ?

 

์กฐํ•ฉ

์ •์˜

์กฐํ•ฉ์€ ์ค‘๋ณต์—†์ด n๊ฐœ ์ค‘ r๊ฐœ๋ฅผ ์ˆœ์„œ์— ์ƒ๊ด€์—†์ด ๋ฝ‘๋Š” ๊ฒฝ์šฐ์ด๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์ฒ˜๋Ÿผ A, B, C ์„ธ๊ฐ€์ง€ ์›์†Œ๊ฐ€ ์žˆ์„ ๋•Œ, 2๊ฐœ๋ฅผ ์ˆœ์„œ์— ์ƒ๊ด€์—†์ด ๋ฝ‘๋Š” ๊ฒƒ์„3C2๋ผ๊ณ  ํ•œ๋‹ค. ์•„๋ž˜์™€ ๊ฐ™์€ ๊ฒฝ์šฐ์˜ ์ˆ˜๊ฐ€ ๋‚˜์˜ค๋ฏ€๋กœ ์ด 3๊ฐ€์ง€์ด๋‹ค.

์กฐํ•ฉ์€ C(Combination)๋กœ ํ‘œํ˜„ํ•˜๋ฉฐ ์‹์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.
$$
{n}\mathrm{C}{r} = \frac{{n}\mathrm{P}{r}}{r!} \ ({n}\mathrm{C}{0},\ {n}\mathrm{C}{n}์€\ 0)
$$

$$
{n}\mathrm{C}{r} = {n-1}\mathrm{C}{r-1}+{n-1}\mathrm{C}{r}
$$
์ค‘๋ณต ์กฐํ•ฉ์ด๋ผ๋Š” ํŠน๋ณ„ํ•œ ๊ฒฝ์šฐ๋Š” ์กฐํ•ฉ ์ค‘ ๊ฐ™์€ ์ข…๋ฅ˜์˜ ๊ฒƒ์„ ๋‹ค์‹œ ๋ฝ‘์„ ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ๋ฅผ ๋งํ•œ๋‹ค.

์ค‘๋ณต ์กฐํ•ฉ์€ H๋กœ ํ‘œํ˜„ํ•˜๋ฉฐ ์‹์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.
$$
{n}\mathrm{H}{r} = {n+r-1}\mathrm{C}{r}
$$

$$
{n}\mathrm{H}{r} = {n}\mathrm{H}{r-1} +{n-1}\mathrm{H}{r}
$$

 

๊ตฌํ˜„

์กฐํ•ฉ

// ์ง์ ‘ ๊ตฌํ˜„
void swap(vector<int>& set, int a, int b){
    int tmp = set[a];
    set[a] = set[b];
    set[b] = tmp;
}

void combination(vector<int>& set, int idx, int size, int n, int r){
    if(size==r){
        for(int i=0; i<size; i++)
            cout << set[i] << " ";
        cout << endl;
        return;
    }
    for(int i = idx; i<n; i++){
        swap(set, i, size);
        combination(set, i+1, size+1, n, r);
        swap(set, i, size);
    }
}

int main()
{
    vector<int> set;
    set.push_back(1);
    set.push_back(2);
    set.push_back(3);

    combination(set, 0, 0, 3, 2);

    return 0;
}
// next_permutation ์ด์šฉ
void combination(vector<int>& set, vector<int>& tmp, int n, int r){
    do{
        for(int i=0; i<n; i++){
            if(tmp[i]==1)
                cout << set[i] << " ";
        }
        cout << endl;
    }while(next_permutation(tmp.begin(), tmp.end()));
}

int main()
{
    vector<int> set;
    set.push_back(1);
    set.push_back(2);
    set.push_back(3);

    vector<int> tmp;
    tmp.push_back(0);
    tmp.push_back(1);
    tmp.push_back(1);
    // ์ฃผ์˜!! 0, 1, 1 ์˜ค๋ฆ„์ฐจ์ˆœ ์ˆœ์„œ๋กœ ์จ์•ผ next_permutation์ด ์ž‘์šฉํ•œ๋‹ค.
    // ์•„๋‹ˆ๋ฉด ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ ํ›„ prev_permutation๋„ ๊ฐ€๋Šฅ

    combination(set, tmp, 3, 2);

    return 0;
}

 

์ค‘๋ณต ์กฐํ•ฉ

void repeatedCombination(vector<int>& set, vector<int>& tmp, int size, int index, int n, int r){
    if(size==r){
        for(int i=0; i<size; i++)
            cout << tmp[i] << " ";
        cout << endl;
        return;
    }

    for(int i=index; i<n; i++){
        tmp[size] = set[i];
        repeatedCombination(set, tmp, size+1, i, n, r);
    }
}

int main()
{
    vector<int> set;
    set.push_back(1);
    set.push_back(2);
    set.push_back(3);

    vector<int> tmp(3);

    repeatedCombination(set, tmp, 0, 0, 3, 2);

    return 0;
}

 

๊ด€๋ จ ๋ฌธ์ œ

Gaaaaaaaaaarden

๋ฐ˜์‘ํ˜•