作业调度算法

Joh*_*rts 8 java algorithm

在面试中得到了这个问题.想知道是否有更好的解决方案:

给定N个任务以及它们之间的依赖关系,请提供执行序列,以确保在不违反依赖关系的情况下执行作业.

示例文件:

1 <4

3 <2

4 <5

第一行是总任务的数量.1 <4表示任务1必须在任务4之前执行.

一个可能的顺序是:1 4 5 3 2

我的解决方案使用DAG存储所有数字,然后进行拓扑排序.是否有一种不那么严厉的方法来解决这个问题?:

    DirectedAcyclicGraph<Integer, DefaultEdge> dag = new DirectedAcyclicGraph<Integer, DefaultEdge>(DefaultEdge.class); 
    Integer [] hm = new Integer[6];
    //Add integer objects to storage array for later edge creation and add vertices to DAG
    for(int x = 1; x <= numVertices; x++){
        Integer newInteger = new Integer(x);
        hm[x] = newInteger;
        dag.addVertex(newInteger);
    }
    for(int x = 1; x < lines.size()-1; x++){
        //Add edges between vertices
        String[] parts = lines.get(x).split("<");
        String firstVertex = parts[0];
        String secondVertex = parts[1];
        dag.addDagEdge(hm[Integer.valueOf(firstVertex)], hm[Integer.valueOf(secondVertex)]);
    }
    //Topological sort
    Iterator<Integer> itr = dag.iterator();
    while(itr.hasNext()){
        System.out.println(itr.next());
    }   
Run Code Online (Sandbox Code Playgroud)

dto*_*ola 3

正如几位用户(Gassa、shekhar suman、mhum 和 Colonel Panic)所说,该问题是通过找到拓扑排序来解决的。只要 dag 中的迭代器按该顺序返回元素,它就是正确的。我不知道 DirectedAcyclingGraph 类来自哪里,所以我无能为力。否则,这个方法会像你一样进行解析并使用一个简单的算法(实际上,我想到的第一个算法)

public static int[] orderTasks (String[] lines){
    // parse
    int numTasks = Integer.parseInt(lines[0]);
    List<int[]> restrictions = new ArrayList<int[]>(lines.length-1);
    for (int i = 1; i < lines.length; i++){
        String[] strings = lines[i].split("<");
        restrictions.add(new int[]{Integer.parseInt(strings[0]), Integer.parseInt(strings[1])});
    }

    // ordered
    int[] tasks = new int[numTasks];
    int current = 0;

    Set<Integer> left = new HashSet<Integer>(numTasks);
    for (int i = 1; i <= numTasks; i++){
        left.add(i);
    }
    while (current < tasks.length){
        // these numbers can't be written yet
        Set<Integer> currentIteration = new HashSet<Integer>(left);
        for (int[] restriction : restrictions){
            // the second element has at least the first one as precondition
            currentIteration.remove(restriction[1]);
        }
        if (currentIteration.isEmpty()){
            // control for circular dependencies
            throw new IllegalArgumentException("There's circular dependencies");
        }
        for (Integer i : currentIteration){
            tasks[current++]=i;
        }
        // update tasks left
        left.removeAll(currentIteration);
        // update restrictions
        Iterator<int[]> iterator = restrictions.iterator();
        while (iterator.hasNext()){
            if (currentIteration.contains(iterator.next()[0])){
                iterator.remove();
            }
        }
    }
    return tasks;
}
Run Code Online (Sandbox Code Playgroud)

顺便说一句,在 hm 数组初始化中,您定义它有 6 个元素。它将 0 位置保留为空(这不是问题,因为您无论如何都不调用它),但在一般情况下,任务数可能大于 5,然后您将遇到 IndexOutOfBoundsException

另外需要注意的是,在添加边时,如果存在循环依赖,如果 DAG 引发的异常消息不够清晰,用户可能会感到困惑。同样,由于我不知道该类来自哪里,所以我不知道。