Exemple #1
0
        public Digraph reverse()
        {
            Digraph R = new Digraph(V);
            for (int v = 0; v < V; v++)
                foreach (var w in getAdj(v))
                    R.addEdge(w, v);

            return R;
        }
        private char[] find(String[] orderStrings)
        {
            Digraph g = new Digraph(26);

            int j = 0;
            for (int i = 0; i < orderStrings.Length - 1; i++)
            {
                string high = orderStrings[i + 1];
                string low = orderStrings[i];
                int minLength = Math.Min(low.Length, high.Length);

                for (j = 0; j < minLength; j++)
                {
                    if (low[j] == high[j])
                        continue;

                    if (low[j] != high[j])
                        g.addEdge(get(low[j]), get(high[j]));
                }
            }

            DirectedCycle cycle = new DirectedCycle(g);
            if (cycle.hasCycle()) {
                foreach (var c in cycle.getCycle())
                    Console.Write(c + " -> ");

                Console.WriteLine();
                return null;
            }

            List<char> orders = new List<char>();
            DFSDiagraph dfs = new DFSDiagraph(g);
            foreach (var v in dfs.sort()) {
                orders.Add(get(v));
            }

            return orders.ToArray();
        }