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

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

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

  1. public class Main {
  2. public static void main(String[] args) {
  3. HashSet<Task> allTasks = new HashSet<Task>();
  4. Task end = new Task("End", 0);
  5. Task F = new Task("F", 2, end);
  6. Task A = new Task("A", 3, end);
  7. Task X = new Task("X", 4, F, A);
  8. Task Q = new Task("Q", 2, A, X);
  9. Task start = new Task("Start", 0, Q);
  10. allTasks.add(end);
  11. allTasks.add(F);
  12. allTasks.add(A);
  13. allTasks.add(X);
  14. allTasks.add(Q);
  15. allTasks.add(start);
  16. System.out.println("Critical Path: "+Arrays.toString(criticalPath(allTasks)));
  17. }
  18. //A wrapper class to hold the tasks during the calculation
  19. public static class Task{
  20. //the actual cost of the task
  21. public int cost;
  22. //the cost of the task along the critical path
  23. public int criticalCost;
  24. //a name for the task for printing
  25. public String name;
  26. //the tasks on which this task is dependant
  27. public HashSet<Task> dependencies = new HashSet<Task>();
  28. public Task(String name, int cost, Task... dependencies) {
  29. this.name = name;
  30. this.cost = cost;
  31. for(Task t : dependencies){
  32. this.dependencies.add(t);
  33. }
  34. }
  35. @Override
  36. public String toString() {
  37. return name+": "+criticalCost;
  38. }
  39. public boolean isDependent(Task t){
  40. //is t a direct dependency?
  41. if(dependencies.contains(t)){
  42. return true;
  43. }
  44. //is t an indirect dependency
  45. for(Task dep : dependencies){
  46. if(dep.isDependent(t)){
  47. return true;
  48. }
  49. }
  50. return false;
  51. }
  52. }
  53. public static Task[] criticalPath(Set<Task> tasks){
  54. //tasks whose critical cost has been calculated
  55. HashSet<Task> completed = new HashSet<Task>();
  56. //tasks whose ciritcal cost needs to be calculated
  57. HashSet<Task> remaining = new HashSet<Task>(tasks);
  58. //Backflow algorithm
  59. //while there are tasks whose critical cost isn't calculated.
  60. while(!remaining.isEmpty()){
  61. boolean progress = false;
  62. //find a new task to calculate
  63. for(Iterator<Task> it = remaining.iterator(); it.hasNext();){
  64. Task task = it.next();
  65. if(completed.containsAll(task.dependencies)){
  66. //all dependencies calculated, critical cost is max dependency
  67. //critical cost, plus our cost
  68. int critical = 0;
  69. for(Task t : task.dependencies){
  70. if(t.criticalCost > critical){
  71. critical = t.criticalCost;
  72. }
  73. }
  74. task.criticalCost = critical+task.cost;
  75. //set task as calculated an remove
  76. completed.add(task);
  77. it.remove();
  78. //note we are making progress
  79. progress = true;
  80. }
  81. }
  82. //If we haven't made any progress then a cycle must exist in
  83. //the graph and we wont be able to calculate the critical path
  84. if(!progress) throw new RuntimeException("Cyclic dependency, algorithm stopped!");
  85. }
  86. //get the tasks
  87. Task[] ret = completed.toArray(new Task[0]);
  88. //create a priority list
  89. Arrays.sort(ret, new Comparator<Task>() {
  90. @Override
  91. public int compare(Task o1, Task o2) {
  92. //sort by cost
  93. int i= o2.criticalCost-o1.criticalCost;
  94. if(i != 0)return i;
  95. //using dependency as a tie breaker
  96. //note if a is dependent on b then
  97. //critical cost a must be >= critical cost of b
  98. if(o1.isDependent(o2))return -1;
  99. if(o2.isDependent(o1))return 1;
  100. return 0;
  101. }
  102. });
  103. return ret;
  104. }

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

暂无答案!

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

相关问题