Пример #1
0
 /// <summary>
 /// Determines whether the edge-weighted digraph <tt>G</tt> has a topological
 /// order and, if so, finds such an order.
 /// </summary>
 /// <param name="g">g the edge-weighted digraph</param>
 public Topological(EdgeWeightedDigraph g)
 {
     var finder = new EdgeWeightedDirectedCycle(g);
     if (!finder.HasCycle())
     {
         var dfs = new DepthFirstOrder(g);
         _order = dfs.ReversePost();
     }
 }
Пример #2
0
        /// <summary>
        /// Determines whether the edge-weighted digraph <tt>G</tt> has a topological
        /// order and, if so, finds such an order.
        /// </summary>
        /// <param name="g">g the edge-weighted digraph</param>
        public Topological(EdgeWeightedDigraph g)
        {
            var finder = new EdgeWeightedDirectedCycle(g);

            if (!finder.HasCycle())
            {
                var dfs = new DepthFirstOrder(g);
                _order = dfs.ReversePost();
            }
        }
Пример #3
0
        private readonly int[] _rank; // rank[v] = position of vertex v in topological order

        #endregion Fields

        #region Constructors

        /// <summary>
        /// Determines whether the digraph <tt>G</tt> has a topological order and, if so,
        /// finds such a topological order.
        /// </summary>
        /// <param name="g">g the digraph</param>
        public Topological(Digraph g)
        {
            var finder = new DirectedCycle(g);
            if (!finder.HasCycle())
            {
                var dfs = new DepthFirstOrder(g);
                _order = dfs.ReversePost();
                _rank = new int[g.V];
                var i = 0;
                foreach (int v in _order)
                    _rank[v] = i++;
            }
        }
Пример #4
0
        private readonly int[] _rank;                  // rank[v] = position of vertex v in topological order

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

            if (!finder.HasCycle())
            {
                var dfs = new DepthFirstOrder(g);
                _order = dfs.ReversePost();
                _rank  = new int[g.V];
                var i = 0;
                foreach (int v in _order)
                {
                    _rank[v] = i++;
                }
            }
        }
        public void Run()
        {
            Console.WriteLine("Choose file:"); // Prompt
            Console.WriteLine("1 - tinyDAG.txt"); // Prompt
            Console.WriteLine("or quit"); // Prompt

            var fileNumber = Console.ReadLine();
            string fileName;
            switch (fileNumber)
            {
                case "1":
                    fileName = "tinyDAG.txt";
                    break;
                case "quit":
                    return;
                default:
                    return;
            }

            var @in = new In($"Files\\Graphs\\{fileName}");
            var lines = @in.ReadAllLines();

            var lineIterator = 0;
            var v = 0;
            var e = 0;
            var edges = new List<EdgeD>();
            foreach (var line in lines)
            {
                if (lineIterator == 0)
                {
                    v = Convert.ToInt32(line);
                }
                if (lineIterator == 1)
                {
                    e = Convert.ToInt32(line);
                }
                if (lineIterator > 1)
                {
                    var lineSplitted = line.Split(new[] {' '}, StringSplitOptions.RemoveEmptyEntries);
                    var ve = Convert.ToInt32(lineSplitted[0]);
                    var we = Convert.ToInt32(lineSplitted[1]);
                    var edge = new EdgeD(ve, we);
                    edges.Add(edge);
                }

                lineIterator++;
            }

            var digraph = new Digraph(v, e, edges);
            Console.WriteLine(digraph);

            var dfs = new DepthFirstOrder(digraph);
            Console.WriteLine("   v  pre post");
            Console.WriteLine("--------------");
            for (var vi = 0; vi < digraph.V; vi++)
            {
                Console.Write($"{vi} {dfs.Pre(vi)} {dfs.Post(vi)}{Environment.NewLine}");
            }

            Console.Write("Preorder:  ");
            foreach (int vi in dfs.Pre())
            {
                Console.Write($"{vi} ");
            }
            Console.WriteLine();

            Console.Write("Postorder: ");
            foreach (int vi in dfs.Post())
            {
                Console.Write($"{vi} ");
            }
            Console.WriteLine();

            Console.Write("Reverse postorder: ");
            foreach (int vi in dfs.ReversePost())
            {
                Console.Write($"{vi} ");
            }
            Console.ReadLine();
        }