private void solve(IDirectedGraph g) { result = new List <int>(); Degrees degrees_info = new Degrees(g); int[] degrees = new int[g.V]; for (int i = 0; i < g.V; ++i) { degrees[i] = degrees_info.InDegree(i); } Queue <int> starts = new Queue <int>(); foreach (Int32 s in degrees_info.Sources()) { starts.Enqueue(s); } //只要还有顶点 while (starts.Count > 0) { Int32 v = starts.Dequeue(); result.Add(v); foreach (var w in g.Adj(v)) { //删除这个顶点的指出的边,因为这个顶点已经被删除了 --degrees[w]; if (degrees[w] == 0) { //下一个拓扑排序的顺序 starts.Enqueue(w); } } } }