[Algorithm] Sets - Cartesian Product

집합 카테시안 곱

Posted by lim.Chuck on September 21, 2024

카테시안 곱?

카테시안 곱은 두 집합 $ A $와 $ B $의 곱으로, 각각의 집합에서 선택한 원소를 조합하여 만든 모든 순서쌍으로 구성된 집합입니다. 이를 수식으로 나타내면 $ A \times B $로 표기하며, 결과는 각 집합 $ A $의 원소 $ a $와 집합 $ B $의 원소 $ b $로 구성된 순서쌍 $ (a, b) $들로 이루어져 있습니다.

예를 들어: $ A = {1, 2}, \quad B = {x, y} $ 카테시안 곱 $ A \times B $는 다음과 같습니다: $ A \times B = {(1, x), (1, y), (2, x), (2, y)} $

카테시안 곱의 사용 사례

  1. 조합론: 카테시안 곱은 두 개 이상의 집합에서 원소들의 모든 조합을 계산할 때 사용됩니다.
  2. 데이터베이스 쿼리: SQL에서 카테시안 곱은 조건 없이 두 테이블의 모든 행을 결합할 때 발생하는 결과입니다.
  3. 그래프 이론: 그래프를 생성할 때 사용되며, 특히 곱 그래프(product graphs)를 생성하는 데 유용합니다.
  4. 기계 학습: 여러 목록이나 배열의 값 조합이 필요한 경우 카테시안 곱이 유용하게 사용됩니다.
  5. 프로그래밍: 여러 리스트나 배열의 가능한 모든 순서쌍이나 튜플을 생성하는 데 자주 사용됩니다.

JavaScript에서의 카테시안 곱 예제

JavaScript에서 두 배열의 카테시안 곱을 계산하는 방법은 다음과 같습니다:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
function cartesianProduct(arr1, arr2) {
  const result = [];

  for (let i = 0; i < arr1.length; i++) {
    for (let j = 0; j < arr2.length; j++) {
      result.push([arr1[i], arr2[j]]);
    }
  }

  return result;
}

// 사용 예시
const A = [1, 2];
const B = ["x", "y"];
console.log(cartesianProduct(A, B));

출력:

1
2
3
4
5
6
[
  [1, 'x'],
  [1, 'y'],
  [2, 'x'],
  [2, 'y']
]

Rust에서의 카테시안 곱 예제

Rust에서는 Vec과 반복자를 사용하여 카테시안 곱을 구할 수 있습니다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
fn cartesian_product<T: Clone, U: Clone>(vec1: Vec<T>, vec2: Vec<U>) -> Vec<(T, U)> {
    let mut product = Vec::new();

    for item1 in &vec1 {
        for item2 in &vec2 {
            product.push((item1.clone(), item2.clone()));
        }
    }

    product
}

fn main() {
    let vec1 = vec![1, 2];
    let vec2 = vec!['x', 'y'];

    let product = cartesian_product(vec1, vec2);

    for pair in product {
        println!("{:?}", pair);
    }
}

출력:

1
2
3
4
(1, 'x')
(1, 'y')
(2, 'x')
(2, 'y')

요약:

카테시안 곱은 두 집합이나 배열에서 모든 가능한 순서쌍을 생성하는 수학적 연산입니다. 이는 조합론, 데이터베이스, 그래프 이론, 기계 학습, 프로그래밍 등 다양한 분야에서 사용됩니다. JavaScript와 Rust에서는 중첩된 반복문이나 반복자를 사용하여 카테시안 곱을 계산할 수 있습니다.

Cartesian Product?

The Cartesian Product of two sets $ A $ and $ B $, denoted as $ A \times B $, is the set of all ordered pairs $ (a, b) $, where $ a \in A $ and $ b \in B $. In simple terms, it is the combination of every element from set $ A $ with every element from set $ B $.

For example: $ A = {1, 2}, \quad B = {x, y} $ The Cartesian Product $ A \times B $ is: $ A \times B = {(1, x), (1, y), (2, x), (2, y)} $

Use Cases of Cartesian Product

  1. Combinatorics: The Cartesian Product is widely used in combinatorics to calculate all possible combinations of elements from two or more sets.
  2. Database Queries: In SQL, Cartesian Product is the result of joining every row from one table with every row from another table when no specific condition (like a key) is used.
  3. Graph Theory: Used in graph construction, particularly in generating product graphs.
  4. Machine Learning: Sometimes used in cross-product features, where combinations of feature values are needed.
  5. Programming: Useful in generating all possible tuples of values when working with multiple lists or arrays.

JavaScript Example for Cartesian Product

Here’s how to calculate the Cartesian Product of two arrays in JavaScript:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
function cartesianProduct(arr1, arr2) {
  const result = [];

  for (let i = 0; i < arr1.length; i++) {
    for (let j = 0; j < arr2.length; j++) {
      result.push([arr1[i], arr2[j]]);
    }
  }

  return result;
}

// Example usage
const A = [1, 2];
const B = ["x", "y"];
console.log(cartesianProduct(A, B));

Output:

1
2
3
4
5
6
[
  [1, 'x'],
  [1, 'y'],
  [2, 'x'],
  [2, 'y']
]

Rust Example for Cartesian Product

In Rust, we can achieve a similar result using Vec and iterators.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
fn cartesian_product<T: Clone, U: Clone>(vec1: Vec<T>, vec2: Vec<U>) -> Vec<(T, U)> {
    let mut product = Vec::new();

    for item1 in &vec1 {
        for item2 in &vec2 {
            product.push((item1.clone(), item2.clone()));
        }
    }

    product
}

fn main() {
    let vec1 = vec![1, 2];
    let vec2 = vec!['x', 'y'];

    let product = cartesian_product(vec1, vec2);

    for pair in product {
        println!("{:?}", pair);
    }
}

Output:

1
2
3
4
(1, 'x')
(1, 'y')
(2, 'x')
(2, 'y')

Summary:

The Cartesian Product is a mathematical operation that generates all possible ordered pairs from two sets or arrays. It has applications in combinatorics, databases, graph theory, machine learning, and programming. Both JavaScript and Rust can compute Cartesian Products using nested loops or iterators.