sqlite数据库和关键路径算法?

q1qsirdb  于 2021-07-11  发布在  Java
关注(0)|答案(0)|浏览(265)

所以我有一个有效的关键路径算法,它是如何工作的;它将计算每个元素的成本,然后创建一个序列,从开始到开始打印出最快的路径。它与djikstra算法非常相似。现在,我已经成功地使用并实现了我自己的元素,但是我希望这个算法能够从sqlite数据库中检索数据,我不确定如何进行这项工作,因为我对在程序中使用数据库是新的,我可以从数据库中以数组的形式返回某些列,但这样做并不能让我真正遍历每一行进行计算。我只需要一个指针,我该怎么办?如何通过此关键路径算法检索和放置数据?我的代码如下,不使用数据库:

public class Main {

    public static void main(String[] args) {

        HashSet<Task> allTasks = new HashSet<Task>();
        Task end = new Task("End", 0);
        Task F = new Task("F", 2, end);
        Task A = new Task("A", 3, end);
        Task X = new Task("X", 4, F, A);
        Task Q = new Task("Q", 2, A, X);
        Task start = new Task("Start", 0, Q);

        allTasks.add(end);
        allTasks.add(F);
        allTasks.add(A);
        allTasks.add(X);
        allTasks.add(Q);
        allTasks.add(start);
        System.out.println("Critical Path: "+Arrays.toString(criticalPath(allTasks)));
    }

    //A wrapper class to hold the tasks during the calculation
    public static class Task{
        //the actual cost of the task
        public int cost;
        //the cost of the task along the critical path
        public int criticalCost;
        //a name for the task for printing
        public String name;
        //the tasks on which this task is dependant
        public HashSet<Task> dependencies = new HashSet<Task>();
        public Task(String name, int cost, Task... dependencies) {
            this.name = name;
            this.cost = cost;
            for(Task t : dependencies){
                this.dependencies.add(t);
            }
        }
        @Override
        public String toString() {
            return name+": "+criticalCost;
        }
        public boolean isDependent(Task t){
            //is t a direct dependency?
            if(dependencies.contains(t)){
                return true;
            }
            //is t an indirect dependency
            for(Task dep : dependencies){
                if(dep.isDependent(t)){
                    return true;
                }
            }
            return false;
        }
    }

    public static Task[] criticalPath(Set<Task> tasks){
        //tasks whose critical cost has been calculated
        HashSet<Task> completed = new HashSet<Task>();
        //tasks whose ciritcal cost needs to be calculated
        HashSet<Task> remaining = new HashSet<Task>(tasks);

        //Backflow algorithm
        //while there are tasks whose critical cost isn't calculated.
        while(!remaining.isEmpty()){
            boolean progress = false;

            //find a new task to calculate
            for(Iterator<Task> it = remaining.iterator(); it.hasNext();){
                Task task = it.next();
                if(completed.containsAll(task.dependencies)){
                    //all dependencies calculated, critical cost is max dependency
                    //critical cost, plus our cost
                    int critical = 0;
                    for(Task t : task.dependencies){
                        if(t.criticalCost > critical){
                            critical = t.criticalCost;
                        }
                    }
                    task.criticalCost = critical+task.cost;
                    //set task as calculated an remove
                    completed.add(task);
                    it.remove();
                    //note we are making progress
                    progress = true;
                }
            }
            //If we haven't made any progress then a cycle must exist in
            //the graph and we wont be able to calculate the critical path
            if(!progress) throw new RuntimeException("Cyclic dependency, algorithm stopped!");
        }

        //get the tasks
        Task[] ret = completed.toArray(new Task[0]);
        //create a priority list
        Arrays.sort(ret, new Comparator<Task>() {

            @Override
            public int compare(Task o1, Task o2) {
                //sort by cost
                int i= o2.criticalCost-o1.criticalCost;
                if(i != 0)return i;

                //using dependency as a tie breaker
                //note if a is dependent on b then
                //critical cost a must be >= critical cost of b
                if(o1.isDependent(o2))return -1;
                if(o2.isDependent(o1))return 1;
                return 0;
            }
        });

        return ret;
    }

我还将提供一个秘密要点,说明我是如何尝试在算法中实现数据库的,但最终失败了:https://gist.github.com/jabz259/2be17a89042ae012a083d04591799df9

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题