我使用队列实现了拓扑排序,但是,Vertex类中存在一个逻辑错误,导致此代码无法正常工作。
public class TopologicalSort {
public void TopSort(ArrayList<Vertex> vertices) throws CycleFoundException
{
int i;
int j;
for (i = 0; i < vertices.size(); i++)
{
for(j = 0; j < vertices.get(i).getAdjacentVertices(vertices.get(i)).size(); j++)
{
vertices.get(i).AdjacentVertices.get(j).indegree++;
}
}
Queue<Vertex> q = new LinkedList<Vertex>();
int counter = 0;
for(i = 0; i < vertices.size(); i++)
{
if(vertices.get(i).indegree == 0)
q.add(vertices.get(i));
}
while(!q.isEmpty())
{
Vertex v = q.remove();
v.topNum = counter++;
for (i = 0; i < vertices.size(); i++)
{
for(j = 0; j < vertices.get(i).getAdjacentVertices(vertices.get(i)).size(); j++)
{ System.out.println(vertices.get(i).AdjacentVertices.get(j).indegree);
if(--(vertices.get(i).AdjacentVertices.get(j).indegree) == 0)
{
q.add(vertices.get(i).AdjacentVertices.get(j));
System.out.println(q.toArray()[i]);
}
}
}
}
if(counter != vertices.size())
throw new CycleFoundException();
}
}
public class Vertex {
String name;
int indegree;
ArrayList<Vertex> AdjacentVertices;
public int topNum;
public Vertex(String name, int indegree, ArrayList<Vertex> AdjacentVertices)
{
this.name = name;
this.indegree = indegree;
this.AdjacentVertices = new ArrayList<Vertex>();
}
public ArrayList<Vertex> getAdjacentVertices(Vertex a) {
while (AdjacentVertices.size() != 0)
{
for (int i = 0; i < AdjacentVertices.size(); i++)
AdjacentVertices.remove(i);
}
switch (a.name)
{
case "S":
{
Vertex A = new Vertex("A", 0, AdjacentVertices);
AdjacentVertices.add(A);
Vertex D = new Vertex("D", 0, AdjacentVertices);
AdjacentVertices.add(D);
Vertex G = new Vertex("G", 0, AdjacentVertices);
AdjacentVertices.add(G);
break;
}
case "A":
{
Vertex B = new Vertex("B", 0, AdjacentVertices);
AdjacentVertices.add(B);
Vertex E = new Vertex("E", 0, AdjacentVertices);
AdjacentVertices.add(E);
break;
}
case "B":
{
Vertex C = new Vertex("C", 0, AdjacentVertices);
AdjacentVertices.add(C);
break;
}
case "C":
{
Vertex D = new Vertex("D", 0, AdjacentVertices);
AdjacentVertices.add(D);
break;
}
case "D":
{
Vertex E = new Vertex("E", 0, AdjacentVertices);
AdjacentVertices.add(E);
break;
}
case "E":
{
Vertex C = new Vertex("C", 0, AdjacentVertices);
AdjacentVertices.add(C);
Vertex F = new Vertex("F", 0, AdjacentVertices);
AdjacentVertices.add(F);
Vertex I = new Vertex("I", 0, AdjacentVertices);
AdjacentVertices.add(I);
break;
}
case "F":
{
Vertex C = new Vertex("C", 0, AdjacentVertices);
AdjacentVertices.add(C);
Vertex T = new Vertex("T", 0, AdjacentVertices);
AdjacentVertices.add(T);
break;
}
case "G":
{
Vertex D = new Vertex("D", 0, AdjacentVertices);
AdjacentVertices.add(D);
Vertex E = new Vertex("E", 0, AdjacentVertices);
AdjacentVertices.add(E);
Vertex H = new Vertex("H", 0, AdjacentVertices);
AdjacentVertices.add(H);
break;
}
case "H":
{
Vertex E = new Vertex("E", 0, AdjacentVertices);
AdjacentVertices.add(E);
Vertex I = new Vertex("I", 0, AdjacentVertices);
AdjacentVertices.add(I);
break;
}
case "I":
{
Vertex F = new Vertex("F", 0, AdjacentVertices);
AdjacentVertices.add(F);
Vertex T = new Vertex("T", 0, AdjacentVertices);
AdjacentVertices.add(T);
break;
}
case "T": break;
}
return AdjacentVertices;
}
@Override
public String toString() {
return name;
}
}
在Vertex类中,在getAdjacentVertices方法中,第一个while循环导致每次对此方法进行新调用时列表变为空,但是如果没有此循环,则arraylist的大小将每次乘以2调用此函数。我似乎无法想到修改getAdjacentVertices以正常工作。有没有办法修改getAdjacentVertices方法,以便我可以删除while循环,并且每次调用此方法时arraylist都不会乘以2?提前感谢您的帮助。