0

我正在做一个项目,在该项目中,我使用拓扑排序根据先决条件查找逻辑课程序列。这是我的排序方法:

public void topo()  //topological sort
{
    int orig_nVerts = nVerts;  //remember how many verts

    while(nVerts > 0)
    {
        int currentVertex = noSuccessors();


        if(currentVertex == -1)
        {
            System.out.println("ERROR: Graph has cycles");
            return;
        }

        sortedDepArray[nVerts - 1] = vertexList[currentVertex].department;
        sortedCrsNumArray[nVerts - 1] = vertexList[currentVertex].courseNum;
        deleteVertex(currentVertex);        
    } //end while
    System.out.println("Topologically sorted order: ");
    for(int j = 0; j < orig_nVerts; j++)
        System.out.print(sortedDepArray[j] + sortedCrsNumArray[j] + " ");

}//end topo()

我需要能够做的是在特定通道中找到没有任何后继者的所有顶点,并按课程编号对它们进行排序(每个顶点包含一个部门和课程编号,例如 CS140)。以下是实际描述:

During the topological sort, when selecting a node with no successors: first, find all such vertices, then, select courses based upon the course number(integer part) that would tend to have a student take lower-level courses first. 也就是说,如果候选发生在我身上:M152、M131 和 CS140 你应该选择 M152 让它稍后出现。

任何意见是极大的赞赏!

编辑
这是我迄今为止尝试过的:

while(nVerts > 0)
    {
        int currentVertex = noSuccessors();

        if(currentVertex == -1)
        {
            System.out.println("ERROR: Graph has cycles");
            return;
        }

        Vertex currVert = vertexList[currentVertex];
        int currCourseNum = currVert.courseNum;
        if(sortedCrsNumArray.length != 0)
        {
            if(currCourseNum > sortedCrsNumArray[nVerts - 1])
            {
                sortedCrsNumArray[nVerts - 2] = sortedCrsNumArray[nVerts - 1];
                sortedDepArray[nVerts - 2] = sortedDepArray[nVerts - 1];
            }
        }

        sortedDepArray[nVerts - 1] = vertexList[currentVertex].department;
        sortedCrsNumArray[nVerts - 1] = vertexList[currentVertex].courseNum;
        deleteVertex(currentVertex);

    } //end while

但我在: 处得到一个 ArrayIndexOutOfBoundsException sortedCrsNumArray[nVerts - 2] = sortedCrsNumArray[nVerts - 1];

4

0 回答 0