拓扑排序中的空列表?

时间:2016-02-22 15:32:37

标签: java topological-sort

我的Java类基本上是一个完成数组拓扑排序的类。但是,当我运行测试类时,返回一个空数组。我对此感到困惑,因为主RefCountTopologicalSort类中的代码看起来都是正确的,包括方法。

  public class RefCountTopologicalSort<T> extends Graph<T> implements TopologicalSort<T>{

private HashMap<T,Integer> noOfRefCounts;
private List<T> sort = new ArrayList<T>();

public RefCountTopologicalSort(){
    noOfRefCounts = new HashMap<T, Integer>();
    sort = new ArrayList<T>();
}

@Override
public List<T> getSort() throws GraphError {
    setUpReferenceCount();
    doSort();
    return sort;
}

public T setUpReferenceCount() throws GraphError{
    for(T node: getNodes()){
    for(T neighbour: neighbours(node)){
        increaseReferenceCount(neighbour);
    }
  }
    return null;
}

public void increaseReferenceCount(T node){
    Integer count = noOfRefCounts.get(node);
    if (count == null){
        count = 1;
    }else{
        count++;
    }
    noOfRefCounts.put(node, count);
}

public void reduceReferenceCount(T node){
    Integer count = noOfRefCounts.get(node);
    if(count != null){
        noOfRefCounts.put(node,count--);
    }
}


public T nextReferenceZeroNode(){
    return null;

}

private T visitNode(T node) throws GraphError{
    sort.add(node);
    for(T neighbour: neighbours(node)){
        reduceReferenceCount(neighbour);
    }
    return node;
}

private void doSort() throws GraphError{
    T node;
    while ((node = nextReferenceZeroNode()) != null){
        visitNode(node);
    }
 }
}

  public class RefCountTest {

@Test
public void test() throws GraphError {
    RefCountTopologicalSort<Integer> graph =new RefCountTopologicalSort<Integer>();
    //Create graph
    Integer node1 = new Integer(1);
    Integer node2 = new Integer(2);
    Integer node3 = new Integer(3);
    Integer node4 = new Integer(4);
    Integer node5 = new Integer(5);
    Integer node6 = new Integer(6);
    graph.add(node1);
    graph.add(node2);
    graph.add(node3);
    graph.add(node4);
    graph.add(node5);
    graph.add(node6);
    graph.add(1, 3);
    graph.add(1, 2);
    graph.add(3, 4);
    graph.add(5, 6);
    graph.add(6, 3);

    graph.getSort();
    System.out.println(graph.getSort());
 }
}

测试类中的某些内容是错误的吗?

0 个答案:

没有答案