简体   繁体   English

给定工作流程的拓扑排序

[英]Topological Sorting for given workflow

I am trying to solve a question in order to find the topological sorting. 我正在尝试解决一个问题,以便找到拓扑排序。 Here is the question: 这是问题:

We want to write a function to separate a given workflow's steps into multiple stages in such a way that all the steps in each individual stage can run at the same time. 我们希望编写一个函数,以将给定工作流的步骤分为多个阶段,以使每个单独阶段中的所有步骤都可以同时运行。 The function should return a list of lists, in which each list represents one stage. 该函数应返回一个列表列表,其中每个列表代表一个阶段。 Each step should run in the earliest possible stage. 每个步骤应尽早进行。

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

Here is the code that I have written: 这是我编写的代码:

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());
}

}

I am stuck in createWorkflowStages method. 我被卡在createWorkflowStages方法中。 What should be the efficient way to solve this question? 解决这个问题的有效方法应该是什么? thanks 谢谢

You are looking at a special case of project management problem, the Critical Path Problem , but in your case all the tasks are of equal duration. 您正在查看项目管理问题的一种特殊情况,即关键路径问题 ,但是在您的情况下,所有任务的期限都是相等的。 You can find many implementations on the Web, for example this one , in which you have a method CalculateEarliestStartDate . 您可以在Web上找到许多实现,例如本实现 ,其中具有CalculateEarliestStartDate方法。

Note that you have to have a graph structure that is much more adapted that what you propose with your lists, in order to have an effective algorithm. 请注意,为了拥有有效的算法,必须具有比列表建议的图结构更适应的图结构。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM