Home Backend Development PHP Tutorial PHP implements Cartesian product algorithm

PHP implements Cartesian product algorithm

Dec 21, 2019 pm 02:59 PM
php

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

See all articles