// wyznaczenie kolejnosci topologicznej static public int[] topOrder(CGraph G) { int[] LP = new int[G.n + 1]; int[] ORD = new int[G.n + 1]; for (int i = 1; i <= G.n; i++) { LP[i] = G.pred(i).Count; } Queue <int> Q = new Queue <int>(); for (int i = 1; i <= G.n; i++) { if (LP[i] == 0) { Q.Enqueue(i); } } int l = 0; while (Q.Count > 0) { int w = Q.Dequeue(); ORD[++l] = w; foreach (int ns in G.succ(w)) { if (--LP[ns] == 0) { Q.Enqueue(ns); } } } if (l < G.n) { ORD[0] = -1; } else { ORD[0] = 0; } return(ORD); }
//////////// ver 2 //////////// // added methods static public int[] topOrder2(CGraph G, SetPer pi) { int[] LP = new int[G.n + 1]; int[] ORD = new int[G.n + 1]; for (int i = 1; i <= G.n; i++) { LP[i] = G.pred(i).Count; } // added for (int i = 1; i <= pi.size; i++) { if (pi.pi[i - 1] != 0) { LP[pi.pi[i]]++; } } // added Queue <int> Q = new Queue <int>(); for (int i = 1; i <= G.n; i++) { if (LP[i] == 0) { Q.Enqueue(i); } } int l = 0; while (Q.Count > 0) { int w = Q.Dequeue(); ORD[++l] = w; foreach (int ns in G.succ(w)) { if (--LP[ns] == 0) { Q.Enqueue(ns); } } // added int nsp = pi.pi[pi.ps[w] + 1]; if (nsp != 0) { if (--LP[nsp] == 0) { Q.Enqueue(nsp); } } // added } if (l < G.n) { ORD[0] = -1; } else { ORD[0] = 0; } return(ORD); }