Beispiel #1
0
        public static void MainTest(string[] args)
        {
            // read in digraph from command-line argument
            if (args.Length < 1)
            {
                throw new ArgumentException("Expecting input file");
            }
            TextInput input = new TextInput(args[0]);
            Digraph   G     = new Digraph(input);

            DirectedCycle finder = new DirectedCycle(G);

            if (finder.HasCycle)
            {
                Console.Write("Directed cycle: ");
                foreach (int v in finder.GetCycle())
                {
                    Console.Write(v + " ");
                }
                Console.WriteLine();
            }
            else
            {
                Console.WriteLine("No directed cycle");
            }
            Console.WriteLine();
        }
Beispiel #2
0
        private int[] rank;              // rank[v] = position of vertex v in topological order

        /// <summary>
        /// Determines whether the digraph <c>G</c> has a topological order and, if so,
        /// finds such a topological order.</summary>
        /// <param name="G">the digraph</param>
        ///
        public Topological(Digraph G)
        {
            DirectedCycle finder = new DirectedCycle(G);

            if (!finder.HasCycle)
            {
                DepthFirstOrder dfs = new DepthFirstOrder(G);
                order = dfs.ReversePost();
                rank  = new int[G.V];
                int i = 0;
                foreach (int v in order)
                {
                    rank[v] = i++;
                }
            }
        }