如何找到一组特定大小的元素集合,其中包括集合中可能的最大集合?

如何解决如何找到一组特定大小的元素集合,其中包括集合中可能的最大集合?

给定一个基数在1到6之间的有限集族,它们本身是可能元素的更大有限集的子集,我的目标是创建包含性最强的元素集,即所创建元素的集合。集合可用于从集合系列中创建最可能的集合。我不确定我是否正确地陈述了这个问题,因为自从我在学校学习这类问题以来已经有很长时间了,但是让我尝试通过一个例子来澄清一下。

以下是一组集合的示例,最大元素数为6:

{1029}
{1029}
{1049}
{1029,1049}
{1029,1049,1118,1125}
{1029,1112,1125,1505}
{1029,1094,1505,1525}
{1029,1138,1525}
{1049,1182,1525,1531}

这些集合是由更大的有限元素集创建的:

{1029,1531}

目标是创建不超过6个元素的集合,这些元素可用于重新创建族中的最大集合数。这是一个答案集的示例:

Created Set 1: {1029,1505}

创建的集合的元素可用于重新创建系列中的6个集合。

{1029}
{1029}
{1049}
{1029,1505}

希望这是有道理的。显然,集合的族和可能元素的集合要大得多。我还需要重复创建集合的过程,直到可以重新创建系列中的所有集合。

我真的在寻找一种算法解决方案,用任何编程语言或伪代码编写都是特别有用的,但是即使是我可以用来转换为代码的数学公式也很有帮助。如果我能弄清楚这种类型的问题的名字,我什至会很高兴,这样我自己就可以对它进行更有效的研究。谢谢!

解决方法

如我所见,这很简单。从术语上讲,我将给定的集合称为原始集合,然后将这些集合称为制造的集合。此问题与布景保护问题之间的主要区别在于,您不需要从原始的布景中选取所选择的布景。

当且仅当原始集合是所选集合的子集时,才可以从所选集合中“创建”原始集合。类似地,如果原始集合是所选集合并集的子集,则可以从所选集合的集合中创建原始集合。因此,如果原始集的并集是所选集的并集的子集,则可以创建所有原始集。因此,您可以通过采用原始集合的并集,然后将该联盟(按您希望的任何方式)划分为不超过6个元素的集合的集合来制造所选集合。

,

好的,所以我想出了一个答案,尽管它可能不是有史以来最优化的解决方案。如果有人可以帮助我更快地运行它,或者可以帮助我以能够找到最佳前三名的方式编写它,我将不胜感激。这是我的代码,请注意,我正在使用数据结构“ Set”,可从https://github.com/php-ds

获得
class ProductionOptimizer{
    private int $CARDINALITY=6;
    private CombinationsIterator $combinationsIterator;
    private array $tickets;
    private Set $threads;

    public function __construct(array $array){
        $this->tickets=$array;
        $start = microtime(TRUE);
        $this->threads=new Set();
        foreach ($array as $ticketThreads){
            foreach ($ticketThreads as $thread)
                $this->threads->add($thread);
        }
        $end = microtime(TRUE);
        // print_r($this->threads);
        echo PHP_EOL."Creating the Set took " . ($end - $start) . " seconds to complete.";
        $this->combinationsIterator=new CombinationsIterator($this->getThreadsAsArray(),6);
    }

    public function outputThreads() : void {
        print_r($this->threads);
    }

    public function getThreadsAsArray() : array {
        return $this->threads->toArray();
    }

    public function getCombinationsIterator() : CombinationsIterator{
        return $this->combinationsIterator;
    }

    public function createNewCombinationsIterator(array $array,int $subsetCardinality=null) : CombinationsIterator {
        if(is_null($subsetCardinality)) $subsetCardinality=$this->CARDINALITY;
        $this->combinationsIterator=new CombinationsIterator($array,$subsetCardinality);
        return $this->combinationsIterator;
    }

    public function removeFromSet(array $array){
        // var_dump($this->threads);
        echo "Removing Threads(before count :".$this->threads->count().")...".PHP_EOL;
        $this->threads->remove(...$array);
        // var_dump($this->threads);
        echo "Removing Threads(after count: ".$this->threads->count().")...".PHP_EOL;
        $this->combinationsIterator=new CombinationsIterator($this->getThreadsAsArray(),6);
    }

    public function getSet() : Set {
        return $this->threads;
    }
    public function getThreads() : Set {
        return $this->threads;
    }

    /* not need if using Set class */
    /*
        Set:
            The code took 4.5061111450195E-5 seconds to complete.
        This Function:
            The code took 0.0054061412811279 seconds to complete.
    */
    public function deduplicateArray($array){
        array_walk_recursive($array,function($v) use (&$r){$r[]=$v;});
        return array_values(array_unique($r));
    }

}

class CombinationsIterator implements Iterator
{
    protected $c = null;
    protected $s = null;
    protected $n = 0;
    protected $k = 0;
    protected $pos = 0;

    function __construct($s,$k) {
        if(is_array($s)) {
            $this->s = array_values($s);
            $this->n = count($this->s);
        } else {
            $this->s = (string) $s;
            $this->n = strlen($this->s);
        }
        $this->k = $k;
        $this->rewind();
    }
    function key() {
        return $this->pos;
    }
    function current() {
        $r = array();
        for($i = 0; $i < $this->k; $i++)
            $r[] = $this->s[$this->c[$i]];
        return is_array($this->s) ? $r : implode('',$r);
    }
    function next() {
        if($this->_next())
            $this->pos++;
        else
            $this->pos = -1;
    }
    function rewind() {
        $this->c = range(0,$this->k);
        $this->pos = 0;
    }
    function valid() {
        return $this->pos >= 0;
    }

    protected function _next() {
        $i = $this->k - 1;
        while ($i >= 0 && $this->c[$i] == $this->n - $this->k + $i)
            $i--;
        if($i < 0)
            return false;
        $this->c[$i]++;
        while($i++ < $this->k - 1)
            $this->c[$i] = $this->c[$i - 1] + 1;
        return true;
    }

}

,这是一些要测试的示例数据。这将获得前3个有序序列,并在每次迭代时从集合的范围中删除匹配的集合:

function humanReadableMemory($size)
{
    $unit=array('b','kb','mb','gb','tb','pb');
    return @round($size/pow(1024,($i=floor(log($size,1024)))),2).' '.$unit[$i];
}
$tickets = [
    [1029],[1029],[1029,1049],1049,1080,1112,1125,1188],1125],1188,1278],1056,1138,1158,1182,1514,1531],1071,1076],1074,1075,1092,1093,1242,1523,1525],1094,1158],1221,1505],1278,1298],1298,1516],1138],1517],1317,1505,1242],1270,1524],1370,1317],[1038,1076,1177,1182],[1045,1048,1097,1100],[1046,[1049],[1049]
];
$po=new ProductionOptimizer($tickets);
$start = microtime(TRUE);
$end = microtime(TRUE);
echo "Current Memory Usage: ".humanReadableMemory(memory_get_usage()).PHP_EOL;
echo "Peak Memory Usage: ".humanReadableMemory(memory_get_peak_usage()).PHP_EOL;
echo "Starting Iterations and Combination Testing...".PHP_EOL;
$rankedOutput=[];
$start = microtime(TRUE);
for ($i=0;$i<3;$i++) {
    $matchCountArray=[];$bestChoice=0;$bestChoiceId=-1;
    foreach ($po->getCombinationsIterator() as $comboTest){
        $combinationString=implode(",",$comboTest);
        $matchCountArray[$combinationString]=0;
        $comboTestSet=new Set($comboTest);
        foreach ($tickets as $ticketIdx=>$ticketElements) {
            $ticketElementsSet = new Set($ticketElements);
            $testsArray[$combinationString]+=($comboTestSet->contains(...$ticketElements) ? 1 : 0);
        }
        if ($matchCountArray[$combinationString]>$bestChoice) {
            $bestChoice = $matchCountArray[$combinationString];
            $bestChoiceId=$combinationString;
        } else {
            //trying to save memory
            unset($matchCountArray[$combinationString]);
        }
    }
    $currentBestChoiceSet=new Set(explode(",$bestChoiceId));
    $currentUniverseSet=$po->getSet()->copy();
    for($tmpJ=0;$tmpJ<$currentUniverseSet->count();$tmpJ++){
        $testAgainstSet=$currentUniverseSet->get($tmpJ);
        if ($currentBestChoiceSet->contains(...$testAgainstSet->toArray())){
            $currentUniverseSet->remove($testAgainstSet);
        }
    }
    $tickets = [];
    foreach ($currentUniverseSet as $singleSet){
        $tickets[]=$singleSet->toArray();
    }
    $po=new ProductionOptimizer($tickets);
    $rankedOutput[$i]=["greatest_matching_sequence"=>$bestChoiceId,"coverage_count"=>$bestChoice];
}
echo "RankedOutput:".PHP_EOL;
var_dump($rankedOutput);
echo PHP_EOL;
$end = microtime(TRUE);
echo PHP_EOL."The code took " . ($end - $start) . " seconds to complete.".PHP_EOL;

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-