您的位置:首页 > 编程语言 > Java开发

java拓扑排序实现源码

2014-12-12 16:21 330 查看
java之拓扑排序实现
前言:因为我毕业设计做的是关键路径之前导图的实现,发现要做这个第一步是做拓扑排序,所以有了这篇文章。
用到了一些面向对象思想。

这个是节点的bean,其中一些属性暂时没有用到,是为后面做关键路径用的
package main;

import java.util.List;
/**
* @Title:java拓扑排序
* @Author:Ted
* @Date:2014-12-12 下午4:18:49
*/
public class node {

private Integer inDegree;//入度
private Integer outDegree;//出度
private Integer du;//历时
private Integer ls;//最晚开始时间
private List<String>  inName;//指向我的节点(如,该节点是B,且ACD都指向B,那么存ACD)
private String  name="";//本节点名字
private String outName="";//指向哪些节点

public node(String name,List<String> list,Integer du){
this.name=name;
this.inName=list;
this.du=du;
}
node(){}

public Integer getInDegree() {
return this.inName.size();
}
public void setInDegree(Integer inDegree) {
this.inDegree = inDegree;
}
public Integer getOutDegree() {
return outDegree;
}
public void setOutDegree(Integer outDegree) {
this.outDegree = outDegree;
}
public Integer getDu() {
return du;
}
public void setDu(Integer du) {
this.du = du;
}
public List<String> getInName() {
return inName;
}
public void setInName(List<String> inName) {
this.inName = inName;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getOutName() {
return outName;
}
public void setOutName(String outName) {
this.outName = outName;
}

@Override
public String toString() {
return "node [name=" + name + ", inName=" + inName + ", inDegree="
+ getInDegree() + "]";
}

}


这个就是具体的实现了,去掉注释和输出什么的,大概100行吧
package main;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
/**
* @Title:java拓扑排序
* @Author:Ted
* @Date:2014-12-12 下午4:18:49
*/
public class Toplogical {
private static Queue<node> queue = (Queue<node>) new LinkedList<node>();
private static int count =0;

//手动创建多个拓扑节点
public static List<node> createNode() {
String[] inName0 = {};
String[] inName1 = {};
String[] inName2 = { "b" };
String[] inName3 = { "a", "b" };
String[] inName4 = { "d" };
String[] inName5 = { "c", "d" };
String[] inName6 = { "e", "f" };
node[] n = new node[7];
n[0] = new node("a", Arrays.asList(inName0), 5);
n[1] = new node("b", Arrays.asList(inName1), 7);
n[2] = new node("c", Arrays.asList(inName2), 2);
n[3] = new node("d", Arrays.asList(inName3), 6);
n[4] = new node("e", Arrays.asList(inName4), 9);
n[5] = new node("f", Arrays.asList(inName5), 5);
n[6] = new node("g", Arrays.asList(inName6), 3);
List<node> nodeList = new LinkedList<node>();
System.out.println("初始nodeList为");
for (int i = 0; i < n.length; i++) {
nodeList.add(i, n[i]);
System.out.println(nodeList.get(i));
}
return nodeList;
}

//对传进来的LinkedList进行冒泡排序
public static List<node> sort(List<node> nodeList){
count++;
System.out.println("-----------第"+count+"轮排序----------");
node temp = new node();
for (int i = 0; i < nodeList.size(); i++) {
for (int j = i; j < nodeList.size(); j++) {
if (nodeList.get(i).getInDegree() > nodeList.get(j).getInDegree()) {
temp = nodeList.get(i);
nodeList.set(i, nodeList.get(j));
nodeList.set(j, temp);
}
}
}
for(int i=0;i<nodeList.size();i++){
System.out.println(nodeList.get(i));
}

return nodeList;
}

//删除节点及边
public static List<node> removeNode(List<node> nodeList,String Name){
for(int i=0;i<nodeList.size();i++){
//如果是最后一个节点,那么直接返回这个节点
if(nodeList.size()==1){
return nodeList;
}
//删除全部节点中某个节点
if(nodeList.get(i).getName().equals(Name)){
nodeList.remove(i);
}
//删除包含某个节点的边
//假如B节点中inName包含A,C。而要删除的是A,那么把C复制出来并回填给B
if(nodeList.get(i).getInName().contains(Name)){
List<String> strList =new LinkedList<String>();
for(int j=0;j<nodeList.get(i).getInName().size();j++){
if(!nodeList.get(i).getInName().get(j).equals(Name)){
strList.add(nodeList.get(i).getInName().get(j));
}
}
node n=new node(nodeList.get(i).getName(),strList,nodeList.get(i).getDu());
nodeList.set(i, n);
}
}
return nodeList;
}

public static void main(String[] args){
List<node> nodeList = createNode();
int nodeCount = nodeList.size();
//	System.out.println("nodeCount:"+nodeCount);
nodeList = sort(nodeList);
for(int i=0;i<nodeCount;i++){
//把排序后的节点的第一个压入队列
queue.offer(nodeList.get(0));
//把排序后的节点第一个删除
nodeList = removeNode(nodeList, nodeList.get(0).getName());
//最后一轮不必排序,为了提升效率所以省掉最后一次排序
if(i==nodeCount-1)break;
//对nodeList按照入度从小到大排序
nodeList = sort(nodeList);
}
System.out.println("\n"+"最终拓扑排序为");//输出并移除队列头
for (int i = 0; i < nodeCount; i++) {
System.out.println(i+" "+queue.remove());//输出并移除队列头
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息