【问题标题】:Where Can I Find Online Examples of Cormen's Assembly-Line Scheduling? [closed]在哪里可以找到 Cormen 流水线调度的在线示例? [关闭]
【发布时间】:2009-06-26 17:49:25
【问题描述】:

这是一个与学校有关的问题,虽然不完全是家庭作业。

我正在学习算法课程,目前正在学习Cormen's Introduction to Algorithms 书的第 15 章。我已经成功地找到了书中大部分算法的大量在线示例,而且我通常可以找到某种类型的 Java 小程序或其他程序,它们可以很好地可视化算法的工作原理。

第 15 章(动态编程)中的装配线调度是一个例外。

是否有人知道任何在线资源可以提供组装流水线调度算法的进一步示例或可视化?

【问题讨论】:

    标签: algorithm scheduling dynamic-programming


    【解决方案1】:

    我认为,如果您搜索该技术的示例/可视化而不是特定问题......即搜索动态编程,您会获得更好的运气。

    TopCoder "dynamic programming site:topcoder.com" 上可能有一些不错的教程。

    【讨论】:

      【解决方案2】:

      如果你想要解决方案,我写了一个来练习。这是动态规划的一个例子。这意味着您的解决方案避免重复计算。如果一个问题可以划分为多个子单元,并且这些子单元在某些场景中会重复出现,那么就没有必要多次解决它们。在这种情况下,您可以在第一次之后存储解决方案并在以后重复使用它。

      package com.zafar;
      
      import java.util.Scanner;
      
      public class AssemblyLineProblem {
          public static void main(String arg[]){
              Scanner scanner =new Scanner(System.in);
      
          int numberOfStations=scanner.nextInt();
          Station firstLine[]=new Station[numberOfStations];
          Station secondLine[]=new Station[numberOfStations];
      
          for(int i=0;i<numberOfStations;i++){
              firstLine[i]=new Station(i,scanner.nextInt());
              firstLine[i].setIndex(i);
              firstLine[i].setTransferCost(scanner.nextInt());
              firstLine[i].setNameOfLine("line 1");
          }
          for(int i=0;i<numberOfStations;i++){
              secondLine[i]=new Station(i,scanner.nextInt());
              secondLine[i].setIndex(i);
              secondLine[i].setTransferCost(scanner.nextInt());           
              secondLine[i].setNameOfLine("line 2");
          }
          int entryCostLine1=scanner.nextInt();
          int entryCostLine2=scanner.nextInt();
          Station beginStation=findOptimalRoute(numberOfStations, firstLine, secondLine, entryCostLine1, entryCostLine2);
          System.out.println("The optimal route is");
          print(beginStation);
          scanner.close();
      
      }
      public static void print(Station station){
          if(station==null)
              return;
          System.out.println(station.getNameOfLine()+", station "+station.getIndex()+", cost from here: "+station.getOptimalCostFromThisStation());       
          print(station.getNextOptimalStation());
      }
      
      public static Station findOptimalRoute(int numberOfStations,
              Station[] firstLine, Station[] secondLine, int entryCostLine1,
              int entryCostLine2) {
          for(int i=numberOfStations-1;i>=0;i--){
              if(i==numberOfStations-1)
              {
                  firstLine[i].setOptimalCostFromThisStation(firstLine[i].getTransferCost());
                  secondLine[i].setOptimalCostFromThisStation(secondLine[i].getTransferCost());
              }else{
                  calculateOptimalStation(firstLine, secondLine, i);
                  calculateOptimalStation(secondLine, firstLine, i);                          
              }               
          }
          if((entryCostLine1+firstLine[0].getCost()+firstLine[0].getOptimalCostFromThisStation())>= (entryCostLine2+secondLine[0].getCost()+secondLine[0].getOptimalCostFromThisStation())){
              return secondLine[0];
          }else
              return firstLine[0];
      
      }
      public static void calculateOptimalStation(Station[] currentLine, Station[] otherLine, int indexOfCurrentStation){
      
          int costOnContinuingOnSameLine= (currentLine[indexOfCurrentStation+1].getCost()+currentLine[indexOfCurrentStation+1].getOptimalCostFromThisStation());
      
          int costOnSwitchingLines=(currentLine[indexOfCurrentStation].getTransferCost()+otherLine[indexOfCurrentStation+1].getCost()+otherLine[indexOfCurrentStation+1].getOptimalCostFromThisStation()) ;
          if((costOnContinuingOnSameLine  <= costOnSwitchingLines)){
              currentLine[indexOfCurrentStation].setOptimalCostFromThisStation(costOnContinuingOnSameLine);
              currentLine[indexOfCurrentStation].setNextOptimalStation(currentLine[indexOfCurrentStation+1]);
          }
          else{
              currentLine[indexOfCurrentStation].setOptimalCostFromThisStation(costOnSwitchingLines);
              currentLine[indexOfCurrentStation].setNextOptimalStation(otherLine[indexOfCurrentStation+1]);
          }
      
      
      }
      
      }
      
      class Station{
          private String nameOfLine;
          private int index;
          private int cost;
          private int transferCost;
          private Station nextOptimalStation;
          private int optimatCostFromThisStation=0;
          public Station(int index, int cost){
              this.index=index;
              this.cost=cost;
          }
          public int getIndex() {
              return index;
          }
          public void setIndex(int index) {
              this.index = index;
          }
          public int getCost() {
              return cost;
          }
          public void setCost(int cost) {
              this.cost = cost;
          }
          public Station getNextOptimalStation() {
              return nextOptimalStation;
          }
          public void setNextOptimalStation(Station nextOptimalStation) {
              this.nextOptimalStation = nextOptimalStation;
          }
          public int getTransferCost() {
              return transferCost;
          }
          public void setTransferCost(int transferCost) {
              this.transferCost = transferCost;
          }
          public int getOptimalCostFromThisStation() {
              return optimatCostFromThisStation;
          }
          public void setOptimalCostFromThisStation(int optimatCostFromThisStation) {
              this.optimatCostFromThisStation = optimatCostFromThisStation;
          }
          public String getNameOfLine() {
              return nameOfLine;
          }
          public void setNameOfLine(String nameOfLine) {
              this.nameOfLine = nameOfLine;
          }
      
      }
      

      输入: 6

      7 2 9 3 3 1 4 3 8 4 4 3

      8 2 5 1 6 2 4 2 5 1 7 2

      2 4

      输出:

      最优路线是

      1号线,0号站,从这里的费用:29

      2号线,1号站,从这里的费用:22

      1号线,2号站,从这里的费用:18

      2号线,3号站,从这里的费用:13

      2号线,4号站,从这里的费用:8

      1号线,5号站,从这里的费用:3

      其他资源:https://parasol.tamu.edu/~welch/teaching/411.f14/dp.pdf

      【讨论】:

        猜你喜欢
        • 2010-10-01
        • 1970-01-01
        • 2010-09-07
        • 2011-01-17
        • 1970-01-01
        • 1970-01-01
        • 2010-09-23
        • 1970-01-01
        相关资源
        最近更新 更多