cartesian product을 구하는 알고리즘

lacovnk의 이미지

난감합니다 -o-

A1, A2,...An의 카티션 곱을 구하는데,

각각의 도메인은 D1,D2,...Dn이라고 하고, 각각 값이 a1,a2..an이라고 합시다.

간단히 다음과 같이 모든 경우를 찾을 수 있을 것입니다.

for each A1
 for each A2
  ....
   for each An
    print a1,a2,...an

문제는, n이 변할수 있기 때문에, 이렇게 프로그램을 짤 수 없다는 점입니다! -o-

어떻게 구현하면 좋을까요? 음음..

재귀를 사용하려는데..

cartesian product(A,B) // A is relations & B is set of Domain
 if B is only one set
 -for each A
 --for each B
 ---add tuple (A,B) to result
 -return result
 else cartesian product(cartesian product(A,B[0]),left of B)

call cartesian product (A1,(A2...An))

어떤가요? 음음..

lacovnk의 이미지

혹시

Quote:
두 벡터의 내적을 구한다. inner_product

이것을 이용하여 구현할 수 있을까요?

c++ 구현 예제입니다; 잘 돌아가는 것 같군요;

#include <string>
#include <vector>
#include <iostream>

using namespace std;

typedef string domain;
typedef vector<domain> tuple;
typedef vector<tuple> table;

table
cp(table t,vector<table> vt)
{
        if(1 == vt.size())
        {
                table result;
                // for each tuple of t
                // original tuple : t[i]
                for(unsigned int i=0;i<t.size();i++)
                {
                        // for each tuple of vt : B
                        for(unsigned int j=0;j<vt[0].size();j++)
                        {
                                tuple temp_tuple(t[i]);
                                // for each column of B
                                for(unsigned int k=0;k<vt[0][j].size();k++)
                                {
                                        // vt[0][j] : table b's jth tuple
                                        temp_tuple.push_back(vt[0][j][k]);
                                }
                                result.push_back(temp_tuple);
                        }
                }
                return result;
        }
        else
        {
                vector<table> one;
                one.push_back(vt[0]);
                vector<table> left;
                for(unsigned int i=1;i<vt.size();i++){left.push_back(vt[i]);}
                return cp(cp(t,one),left);

        }
}

void
print_table(table t)
{
        // for each tuple
        for(unsigned int i=0;i < t.size();i++)
        {
                // for each column
                for(unsigned int j=0;j<t[i].size();j++)
                {
                        cout << "(" << t[i][j] << ") ";
                }
                cout << endl;
        }
}
int
main()
{
        table A;
        tuple a1;
        tuple a2;
        tuple a3;
        a1.push_back("a1");
        a2.push_back("a2");
        A.push_back(a1);
        A.push_back(a2);



        table B;
        tuple b1;
        tuple b2;
        tuple b3;
        b1.push_back("b1");
        b2.push_back("b2");
        b3.push_back("b3");
        B.push_back(b1);
        B.push_back(b2);
        B.push_back(b3);

        table C;
        tuple c1;
        tuple c2;
        tuple c3;
        c1.push_back("c1");
        c3.push_back("c3");
        C.push_back(c1);
        C.push_back(c3);

        vector<table> vt;
        vt.push_back(B);
        vt.push_back(C);

        table result;
        result = cp(A,vt);

        print_table(result);

        return 0;
}
lacovnk의 이미지

A1, A2,...An의 카티션 곱을 구하는데,

각각의 도메인은 D1,D2,...Dn이라고 하고, 각각 값이 a1,a2..an이라고 합시다.

size of result = sizeof(D1) * sizeof(D2)... * sizeof(Dn)
index // find index'th element
for(n to 1 as i)
  select  & push_back index % sizeof(Dn)
  index /= sizeof(Dn)

모든 경우를 구하려면, 1부터 size of result까지 값을 index로 돌리면 되겠군요~

....조합으로 간단히 되는군요;

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.