php - 集合归约递归算法

标签 php algorithm

给定(A、B、C、D)

什么算法可以将其缩减为大小为 n 的唯一非重复集。

例如,如果 n 是 3。

[A,B,C]
[A,C,D]
[A,B,D]
[B,C,D]

你会注意到 A,A,A 不可能有效,A,A,B 和 [A,C,D] = [C,A,D] = [A,C,D] = [D ,C,A] = 等..

有没有办法不生成幂集并减少它,因为 7 个元素的幂集是 n^7,这很快就会穷尽。

最佳答案

看看Math_Combinatorics .

<?php
require 'Combinatorics.php';

$combinatorics = new Math_Combinatorics;
$result = $combinatorics->combinations( ['A','B','C','D'], 3 );
var_export($result);

打印

array (
  0 => 
  array (
    0 => 'A',
    1 => 'B',
    2 => 'C',
  ),
  1 => 
  array (
    0 => 'A',
    1 => 'B',
    3 => 'D',
  ),
  2 => 
  array (
    0 => 'A',
    2 => 'C',
    3 => 'D',
  ),
  3 => 
  array (
    1 => 'B',
    2 => 'C',
    3 => 'D',
  ),
)

它还有一个 permutations 方法(即 [A,B,C]!=[A,C,B] 因此两者都在结果集中)

关于php - 集合归约递归算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31310379/

相关文章:

php - Magento:构建库存中所有可配置产品的自定义产品集合

phpMyAdmin - 不将 MySQL 查询读取为数字

java - PHP 中的 256 位 AES 解密

c++ - 随机字符串数组的选择排序

algorithm - 找出三个数字中第 n 个最大的数字

php - 将密码的 md5 散列存储在数据库中并进行比较

php - 如何在sql中计算用户优惠券 - mysql

python - 什么是半径 x 的圆中整数坐标的更快解决方案

java - 我怎样才能修改这个视线算法来接受穿过角落的光线?

mysql - 如何计算在给定时间段内旅行过和前一年旅行过的用户?