公式排序算法实际运用

        试想下这个么个场景:用户可以自己配置多个公式,公式与公式之间又有依赖关系。比如A=B+C ,B=C+D。需要做个算法来排序这些公式。实际我们可以分为两个步骤来看这个问题。    

         1,配置的公式之间不能死循环依赖。比如A=B+C ,B=A+C。这种A依赖于B,那就的先算B。但是B又依赖于A,的先算A。这就成了死循环,这种必须检测出来,提示用户配置错误。

         2,依赖的公式之间必须要排序,这样才能保证计算结果正确。比如:A=B+C ,B=C+D。那么公式排序下来B=C+D就要先算,A=B+C就的后算。

我们定义公式为:

class Formula{
    //公式左边属性
    private key;
    //公式右边属性列表
    private Set<String> attributes;
    //公式深度
    private int depth;
}

我们先看死锁检测:


    public void check(List<Formula> formulas){

        LinkedList<Formula> formulaLists = new LinkedList<>(formulas);
        Formula formula;
        while(Objects.nonNull(formula = formulaLists.poll())){

            TreeSet<String> feeSets = new TreeSet<>();
            relyOn(formula,feeSets,formulas);
        }
    }


    public void relyOn(Formula formula,Set<String> formulaKeyList,List<Formula> formulas){
        //将自己加进去
        formulaKeyList.add(formula.getKey());
        
        //遍历属性
        for(String attr:formula.getAttributeList()){
            if(formulaKeyList.contains(attr)){
               //在公式列表属性里又找到了自己
            }
            
            //继续找到依赖的公式列表(从formulas里找)
            List<Formula> feeRelationList = null;
      
            //遍历feeRelationList将formual、sets、formulas再次调用dependsOn
            //注意sets需要新定义并将formulaKeyList放进去
         
        }
    }

死锁完了再看依赖排序:

public List<Formula> pinfoSort(List<Formula> formulas) {

        LinkedList<Formula> formulaLinkedList = new LinkedList<>(formulas);
        //定义公式列表A
        Formula formula;
        while (Objects.nonNull(formula = formulaLinkedList.poll())) {

            int depth = getDepth(formula.getAttributes(), formulas);
            formula.setDepth(depth);
            //往公式列表A加入公式formula
        }

        //再按深度排序
        return 公式列表A;

    }


 public int getDepth(Set<String> attributeList, List<Formula> formulas) {
        int depth = 0;
        if (Objects.isNull(attributeList) || attributeList.size() == 0) {
            return depth;
        }
        int max = 0;
        for (String attr : attributeList) {
            List<Formula> feeRelationList = 继续查找公式依赖列表

            for (Formula formula : feeRelationList) {
                
                depth = getDepth(formula.getAttributeList(), formulas);
                max = Math.max(depth, max);

            }
        }
        return max + 1;
    }

相关推荐

  1. 公式排序算法实际运用

    2024-03-12 07:00:04       45 阅读
  2. 排序算法及其实现

    2024-03-12 07:00:04       54 阅读
  3. C++实现排序算法

    2024-03-12 07:00:04       22 阅读
  4. python实现计数排序、桶排序和基数排序算法

    2024-03-12 07:00:04       18 阅读
  5. 冒泡排序算法实现步骤

    2024-03-12 07:00:04       33 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-12 07:00:04       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-12 07:00:04       100 阅读
  3. 在Django里面运行非项目文件

    2024-03-12 07:00:04       82 阅读
  4. Python语言-面向对象

    2024-03-12 07:00:04       91 阅读

热门阅读

  1. 每天学习一个Linux命令之less

    2024-03-12 07:00:04       42 阅读
  2. 二、UML 类图与面向对象设计原则 之 UML概述

    2024-03-12 07:00:04       50 阅读
  3. excel 将缺失的单元个填充为NA

    2024-03-12 07:00:04       59 阅读
  4. [2023年]-hadoop面试真题(三)

    2024-03-12 07:00:04       40 阅读
  5. Unity 3D常用的数据结构

    2024-03-12 07:00:04       46 阅读
  6. 大语言模型提示工程简介

    2024-03-12 07:00:04       35 阅读
  7. 二维的旋转平移矩阵

    2024-03-12 07:00:04       38 阅读
  8. 矩阵最大权值

    2024-03-12 07:00:04       43 阅读
  9. 华为HCIE实验题库哪里有?Cloud相关证书咋样?

    2024-03-12 07:00:04       45 阅读
  10. 【DevOps基础篇】Dockerfile快速掌握

    2024-03-12 07:00:04       42 阅读