繁体   English   中英

作业调度算法

[英]Job Scheduling Algorithm

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

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

样本文件:

5

1 <4

3 <2

4 <5

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

一种可能的顺序是: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());
    }   

正如几个用户(Gassa,shekhar suman,mhum和上校Panic)已经说过的那样,通过找到拓扑排序解决了该问题。 只要dag中的迭代器按该顺序返回元素,它都是正确的。 我不是DirectedAcyclicGraph类的来源,所以我无能为力。 否则,此方法将像您一样进行解析并使用简单的算法(实际上,第一个出现在我的脑海中)

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

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

在添加边缘时,在循环依赖的情况下,如果DAG引发的Exception消息不够清晰,则可能会使用户感到困惑。 再次,由于我不知道该类来自哪里,所以我也不知道。

暂无
暂无

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

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