예제 #1
0
        private DirectedEdge[] edgeTo; // edgeTo[v] = last edge on longest s->v path

        /// <summary>
        /// Computes a longest paths tree from <c>s</c> to every other vertex in
        /// the directed acyclic graph <c>G</c>.</summary>
        /// <param name="G">the acyclic digraph</param>
        /// <param name="s">the source vertex</param>
        /// <exception cref="ArgumentException">if the digraph is not acyclic</exception>
        /// <exception cref="ArgumentException">unless 0 &lt;= <c>s</c> &lt;= <c>V</c> - 1</exception>
        ///
        public AcyclicLP(EdgeWeightedDigraph G, int s)
        {
            distTo = new double[G.V];
            edgeTo = new DirectedEdge[G.V];
            for (int v = 0; v < G.V; v++)
            {
                distTo[v] = double.NegativeInfinity;
            }
            distTo[s] = 0.0;

            // relax vertices in toplogical order
            Topological topological = new Topological(G);

            if (!topological.HasOrder)
            {
                throw new ArgumentException("Digraph is not acyclic.");
            }
            foreach (int v in topological.Order())
            {
                foreach (DirectedEdge e in G.Adj(v))
                {
                    relax(e);
                }
            }
        }
예제 #2
0
        public static void MainTest(string[] args)
        {
            string  filename = args[0];
            Digraph g;

            if (args.Length >= 2)
            {
                string        delimiter = args[1];
                SymbolDigraph sg        = new SymbolDigraph(filename, delimiter);
                g = sg.G;

                Topological topological = new Topological(g);
                foreach (int v in topological.Order())
                {
                    Console.WriteLine(sg.Name(v));
                }
            }
            else
            {
                g = new Digraph(new TextInput(filename));

                Topological topological = new Topological(g);
                foreach (int v in topological.Order())
                {
                    Console.WriteLine(v);
                }
            }
        }