コード例 #1
0
ファイル: Program.cs プロジェクト: kaplunov93/Algorithms
        static void Main(string[] args)
        {
            // Print the top M lines in the input stream.
            Console.Write("Write Lenth : ");
            int M = int.Parse(Console.ReadLine());
            Console.Write("Write Del max(True) or min(False) : ");
            bool b = false;
            bool.TryParse(Console.ReadLine(),out b);

            PriorityQueue<int> pq = new PriorityQueue<int>(M,b);

            Console.WriteLine("Insert Queue : ");
            for(int i=0;i<M;i++)
                pq.Insert(int.Parse(Console.ReadLine()));

            Stack<int> stack = new Stack<int>();

            while (!pq.isEmpty())
                stack.push(pq.Del());

            Console.WriteLine("\n\t OutPut Stack");
            while(!stack.isEmpty())
                Console.WriteLine(stack.pull());

            Console.ReadKey();
        }
コード例 #2
0
ファイル: LazyPrimMST.cs プロジェクト: kaplunov93/Algorithms
        private PriorityQueue<Edge> pq; // crossing (and ineligible) edges

        #endregion Fields

        #region Constructors

        public LazyPrimMST(EdgeWeightedGraph G)
        {
            pq = new PriorityQueue<Edge>(G.V);
            marked = new bool[G.V];
            mst = new Queue<Edge>();
            visit(G, 0); // assumes G is connected (see Exercise 4.3.22)
            while (!pq.isEmpty())
            {
                Edge e = pq.Del(); // Get lowest-weight
                int v = e.either;
                int w = e.other(v); // edge from pq.
                if (marked[v] && marked[w])
                    continue; // Skip if ineligible.
                mst.Enqueue(e); // Add edge to tree.
                if (!marked[v])
                    visit(G, v); // Add vertex to tree
                if (!marked[w])
                    visit(G, w); // (either v or w).
            }
        }