拓扑排序的实现java_java - 如何使用Java进行拓扑排序(依赖关系解析) - SO中文参考 - www.soinside.com...

说明

问题的目的是实现一个接口,该接口将根据有关任务依赖性的信息对任务列表进行排序。例如,如果任务A依赖于任务B和C,则这意味着要开始处理任务A,必须首先完成任务B和C。我认为它应该像有向图结构。import java.util.ArrayList;

import java.util.Arrays;

import java.util.List;

/**

* The task class represents a certain activities that must be done as the part of the project planning

*/

class Task {

/**

* Unique name of the activity

*/

private String name;

/**

* A list of names of the activities that must be completed in order to be able to start the current activity

*/

private List predecessors;

public Task(String name, List predecessors) {

this.name = name;

this.predecessors = predecessors;

}

public String getName() {

return name;

}

public List getPredecessors() {

return predecessors;

}

}

接口应将任务列表(以任何顺序定义)作为输入参数,并输出按执行顺序排序的任务列表。/**

* A scheduler interface is intended to process a random list of tasks with the information of their predecessors

* and return a list of the same tasks but in order they may be executed according to their dependencies

*/

interface IScheduler {

public List schedule(List tasks);

}

以下代码提供了如何使用该接口的示例。public class Main {

public static void main(String[] args) {

/**

* The following is the example of how the scheduler may be used

*/

List tasks = Arrays.asList(

new Task("E", Arrays.asList("B")),

new Task("D", Arrays.asList("A", "B")),

new Task("A", Arrays.asList()),

new Task("B", Arrays.asList("A")),

new Task("C", Arrays.asList("D", "B")),

new Task("F", Arrays.asList("E"))

);

IScheduler scheduler = /* implementation here*/;

List sortedTasks = scheduler.schedule(tasks);

for (Task t: sortedTasks) {

System.out.println(t.getName());

}

}

}

问题

我如何实现对任务进行排序的界面?我需要使用JGraphT或Guava Graph之类的东西,还是有一些简单的方法?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值