Home > Backend Development > PHP Tutorial > PHP implements Cartesian product algorithm

PHP implements Cartesian product algorithm

藏色散人
Release: 2023-04-08 06:34:01
forward
4864 people have browsed it

Concept

In mathematics, the Cartesian product (Cartesian product) of two sets X and Y, also known as the direct product, is expressed as X × Y. Suppose A and B are any two sets. Take any element x from set A and take any element y from set B to form an ordered pair (x, y). Take such an ordered pair as a new Elements, the set composed of all of them is called the direct product of set A and set B, denoted as A×B, that is, A×B={(x, y)|x∈A and y∈B}.

Assume that set A={a, b} and set B={0, 1, 2}, then the Cartesian product of the two sets is {(a, 0), (a, 1), ( a, 2), (b, 0), (b, 1), (b, 2)}.

Example

Given three domains:

D1 = { 张清玫,刘逸 }
D2 = {计算机专业,信息专业}
D3 = {李勇,刘晨,王敏}
Copy after login

Then the Cartesian product D of D1, D2, D3 = D1×D2×D3, Equal to:

{
    (张清玫, 计算机专业, 李勇),
    (张清玫, 计算机专业, 刘晨),
    (张清玫, 计算机专业, 王敏),
    (张清玫, 信息专业, 李勇),
    (张清玫, 信息专业, 刘晨),
    (张清玫, 信息专业, 王敏),
    (刘逸, 计算机专业, 李勇),
    (刘逸, 计算机专业, 刘晨),
    (刘逸, 计算机专业, 王敏),
    (刘逸, 信息专业, 李勇),
    (刘逸, 信息专业, 刘晨),
    (刘逸, 信息专业, 王敏)
}
Copy after login

In this way, each element in the three sets D1, D2, and D3 is combined correspondingly to form a huge set group. In this example, there will be 2X2X3=12 elements in D. If a set has 1000 elements and there are 3 such sets, the new set composed of their Cartesian product will reach one billion elements. If a set is infinite, then the new set will have infinite elements.

PHP code - Output array form

function Descartes()
{
    $t = func_get_args();                                    // 获取传入的参数
    if (func_num_args() == 1) {                               // 判断参数个数是否为1
        return call_user_func_array(__FUNCTION__, $t[0]);  // 回调当前函数,并把第一个数组作为参数传入
    }
    $a = array_shift($t);        // 将 $t 中的第一个元素移动到 $a 中,$t 中索引值重新排序
    if ( !is_array($a)) {
        $a = [$a];
    }
    $a = array_chunk($a, 1);     // 分割数组 $a ,为每个单元1个元素的新数组
    do {
        $r = [];
        $b = array_shift($t);
        if ( !is_array($b)) {
            $b = [$b];
        }
        foreach ($a as $p) {
            foreach (array_chunk($b, 1) as $q) {
                $r[] = array_merge($p, $q);
            }
        }
        $a = $r;
    } while ($t);
    return $r;
}
Copy after login

Usage:

$arr = [
    [
        '张清玫',
        '刘逸'
    ],
    [
        '计算机专业',
        '信息管理与信息系统专业',
        '电子商务专业'
    ],
    [
        '2018级',
        '2017级'
    ]
];
$r = Descartes($arr);
Copy after login

Effect:

array(12) {
  [0]=>
  array(3) {
    [0]=>
    string(9) "张清玫"
    [1]=>
    string(15) "计算机专业"
    [2]=>
    string(7) "2018级"
  }
  [1]=>
  array(3) {
    [0]=>
    string(9) "张清玫"
    [1]=>
    string(15) "计算机专业"
    [2]=>
    string(7) "2017级"
  }
  [2]=>
  array(3) {
    [0]=>
    string(9) "张清玫"
    [1]=>
    string(33) "信息管理与信息系统专业"
    [2]=>
    string(7) "2018级"
  }
  [3]=>
  array(3) {
    [0]=>
    string(9) "张清玫"
    [1]=>
    string(33) "信息管理与信息系统专业"
    [2]=>
    string(7) "2017级"
  }
  [4]=>
  array(3) {
    [0]=>
    string(9) "张清玫"
    [1]=>
    string(18) "电子商务专业"
    [2]=>
    string(7) "2018级"
  }
  [5]=>
  array(3) {
    [0]=>
    string(9) "张清玫"
    [1]=>
    string(18) "电子商务专业"
    [2]=>
    string(7) "2017级"
  }
  [6]=>
  array(3) {
    [0]=>
    string(6) "刘逸"
    [1]=>
    string(15) "计算机专业"
    [2]=>
    string(7) "2018级"
  }
  [7]=>
  array(3) {
    [0]=>
    string(6) "刘逸"
    [1]=>
    string(15) "计算机专业"
    [2]=>
    string(7) "2017级"
  }
  [8]=>
  array(3) {
    [0]=>
    string(6) "刘逸"
    [1]=>
    string(33) "信息管理与信息系统专业"
    [2]=>
    string(7) "2018级"
  }
  [9]=>
  array(3) {
    [0]=>
    string(6) "刘逸"
    [1]=>
    string(33) "信息管理与信息系统专业"
    [2]=>
    string(7) "2017级"
  }
  [10]=>
  array(3) {
    [0]=>
    string(6) "刘逸"
    [1]=>
    string(18) "电子商务专业"
    [2]=>
    string(7) "2018级"
  }
  [11]=>
  array(3) {
    [0]=>
    string(6) "刘逸"
    [1]=>
    string(18) "电子商务专业"
    [2]=>
    string(7) "2017级"
  }
}
Copy after login

The above is the detailed content of PHP implements Cartesian product algorithm. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:ruoxiaozh.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template