public void Run()
        {
            Console.WriteLine("Choose file:"); // Prompt
            Console.WriteLine("1 - jobs.txt"); // Prompt
            Console.WriteLine("or quit"); // Prompt

            var fileNumber = Console.ReadLine();
            string fileName;
            char delimiter;
            switch (fileNumber)
            {
                case "1":
                    fileName = "jobs.txt";
                    delimiter = '/';
                    break;
                case "quit":
                    return;
                default:
                    return;
            }

            var @in = new In($"Files\\Graphs\\{fileName}");
            var lines = !fileName.EndsWith("zip") ? @in.ReadAllLines() : @in.ReadAllLinesFromZip();

            var sg = new SymbolDigraph(lines, delimiter);
            Console.WriteLine(sg.G);

            var topological = new Topological(sg.G);
            foreach (int v in topological.Order())
            {
                Console.WriteLine(sg.Name(v));
            }

            Console.ReadLine();
        }
Beispiel #2
0
        private readonly DirectedEdge[] _edgeTo;   // edgeTo[v] = last edge on shortest s->v path


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

            // visit vertices in toplogical order
            var topological = new Topological(g);

            if (!topological.HasOrder())
            {
                throw new ArgumentException("Digraph is not acyclic.");
            }
            foreach (int v in topological.Order())
            {
                foreach (var e in g.Adj(v))
                {
                    Relax(e);
                }
            }
        }
Beispiel #3
0
        private readonly DirectedEdge[] _edgeTo; // edgeTo[v] = last edge on longest s->v path

        #endregion Fields

        #region Constructors

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

            // relax vertices in toplogical order
            var topological = new Topological(g);
            if (!topological.HasOrder())
                throw new ArgumentException("Digraph is not acyclic.");
            foreach (int v in topological.Order())
            {
                foreach (var e in g.Adj(v))
                    Relax(e);
            }
        }