【问题标题】:Topological Sorting for given workflow给定工作流的拓扑排序
【发布时间】:2018-06-23 19:39:08
【问题描述】:

我正在尝试解决一个问题以找到拓扑排序。问题来了:

我们想要编写一个函数来将给定工作流的步骤分成多个阶段,这样每个阶段中的所有步骤都可以同时运行。该函数应返回一个列表列表,其中每个列表代表一个阶段。每个步骤都应在尽可能早的阶段运行。

input =  [
   ["clean", "build"],
   ["metadata", "binary"],
   ["build", "link"],
   ["link", "binary"],
   ["clean", "metadata"],
   ["build", "resources"]
   ]
output = [
   ["clean"],
   ["build", "metadata"],
   ["resources", "link"],
   ["binary"]
   ]

这是我写的代码:

public class Solution {

public static List<List<String>> createWorkflowStages(List<List<String>> precursorSteps){

    //main logic
    return new ArrayList<>();
}

static List<Test> tests = Arrays.asList(
new Test(
"build stages",
Arrays.asList(
    Arrays.asList("clean", "build"),
    Arrays.asList("metadata", "binary"),
    Arrays.asList("build", "link"),
    Arrays.asList("link", "binary"),
    Arrays.asList("build", "resources")
),
Arrays.asList(
    Arrays.asList("clean"),
    Arrays.asList("build", "metadata"),
    Arrays.asList("resources", "link"),
    Arrays.asList("binary")
    )),
new Test(
"making dinner",

Arrays.asList(
    Arrays.asList("boil", "serve"),
    Arrays.asList("chop", "boil"),
    Arrays.asList("stir", "boil"),
    Arrays.asList("set table", "serve")
    ),
Arrays.asList(
    Arrays.asList("chop", "stir", "set table"),
    Arrays.asList("boil"),
    Arrays.asList("serve")
)
)
);

public static class Test{
    public String name;
    public List<List<String>> input;
    public List<List<String>> expectedOutput;

    public Test(String name, List<List<String>> input, List<List<String>> expectedOutput){
        this.name = name;
        this.input = input;
        this.expectedOutput = expectedOutput;
    }
}

private static boolean equalOutputs(List<List<String>> a, List<List<String>> b){
    if(a == null || b == null || a.size() != b.size()){
        return false;
    }
    for(int i = 0; i<a.size(); i++){
        List<String> a1 = new ArrayList<>(a.get(i));
        List<String> b1 = new ArrayList<>(b.get(i));
        a1.sort(null);
        b1.sort(null);
        if(!a1.equals(b1)){
            return false;
        }
    }
    return true;
}

public static void main(String[] args) {
    int passed = 0;
    for(Test test : tests){
        System.out.printf("==> Testing %s...\n", test.name);
        try{
            List<List<String>> actualOutput = createWorkflowStages(test.input);
            if(equalOutputs(actualOutput, test.expectedOutput)){
                System.out.println("PASS");
                passed++;
            }else{
                System.out.println("FAIL");
                System.out.printf("Input: %s\n", test.input);
                System.out.printf("Expected Output: %s\n", test.expectedOutput);
                System.out.printf("Actual Output: %s\n", actualOutput);
            }
        }catch(Exception e){
            System.out.println("FAIL");
            System.out.println(e);
        }
    }
    System.out.printf("==> Passed %d of %d tests\n", passed, tests.size());
}

}

我被困在createWorkflowStages 方法中。解决这个问题的有效方法应该是什么?谢谢

【问题讨论】:

  • 问题是?
  • 编辑了问题。 @ceving
  • 当您说“每个单独阶段中的所有步骤都可以同时运行”时,您将这些约束存储在输入中的什么位置?这对我来说不是很清楚。
  • 例如:“clean”、“build”、“metadata”是每个阶段的步骤,现在每个步骤都存储在一个列表中。现在,我们要根据拓扑顺序对这些步骤进行排序。我希望这是有道理的。

标签: java arrays algorithm arraylist topological-sort


【解决方案1】:

您正在查看项目管理问题的一个特殊情况,Critical Path Problem,但在您的情况下,所有任务的持续时间都相同。你可以在网上找到很多实现,例如this one,其中有一个方法CalculateEarliestStartDate

请注意,为了获得有效的算法,您必须拥有比您建议的列表更适应的图形结构。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多