Ejemplo n.º 1
0
        public static void MainTest(string[] args)
        {
            Console.WriteLine($"Running {nameof(PacketProcessing)}");
            int    size     = StdIn.ReadInt();
            Buffer buffer   = new Buffer(size);
            var    requests = ReadQueries();

            foreach (var response in ProcessRequests(buffer, requests))
            {
                Console.WriteLine(response.Dropped ? -1 : response.StartTime);
            }

            IEnumerable <(int TimeOfArrival, int ProcessingTime)> ReadQueries()
            {
                int n = StdIn.ReadInt();

                for (int i = 0; i < n; i++)
                {
                    yield return(StdIn.ReadInt(), StdIn.ReadInt());
                }
            }

            IEnumerable <(bool Dropped, int StartTime)> ProcessRequests(Buffer buffer,
                                                                        IEnumerable <(int TimeOfArrival, int ProcessingTime)> requests)
            {
                foreach (var request in requests)
                {
                    yield return(buffer.Process(request));
                }
            }
        }
Ejemplo n.º 2
0
        public static void Main(string[] args)
        {
            int N = StdIn.ReadInt();

            QuickFindUF uf = new QuickFindUF(N);

            // read in a sequence of pairs of integers (each in the range 0 to N-1),
            // calling find() for each pair: If the members of the pair are not already
            // call union() and print the pair.
            while (!StdIn.IsEmpty())
            {
                int p = StdIn.ReadInt();
                int q = StdIn.ReadInt();

                if (uf.Connected(p, q))
                {
                    continue;
                }
                uf.Union(p, q);

                Console.WriteLine(p + " " + q);
            }

            Console.Write(uf.Count + " components");
        }
Ejemplo n.º 3
0
        private static void ShortestPath()
        {
            string  filename       = "mediumEWD.txt";
            Scanner scanner        = new Scanner(new StreamReader(File.OpenRead(filename)));
            IEdgeWeightedDIgraph G = new EdgeWeightedDigraph(scanner);

            StdOut.Print("输入一个边:");
            int           s  = StdIn.ReadInt();
            IShortestPath sp = new DijkstraSP(G, s);

            for (int t = 0; t < G.V; t++)
            {
                StdOut.Print(s + " to " + t);
                StdOut.Printf(" ({0}): ", sp.DistTo(t));
                if (sp.HasPathTo(t))
                {
                    foreach (var e in sp.PathTo(t))
                    {
                        StdOut.Print(e + "  ");
                    }
                }
                StdOut.Println();
            }
            DijkstraAllPairsSP allPairsSP = new DijkstraAllPairsSP(G);

            StdOut.Println();
            StdOut.Println(allPairsSP.Dist(1, 28));

            //string filename2 = "tinyEWDAG.txt";
            //scanner = new Scanner(new StreamReader(File.OpenRead(filename2)));
            //G = new EdgeWeightedDigraph(scanner);
            //IShortestPath sp2 = new AcyclicSP(G, s);
            //for (int t = 0; t < G.V; t++)
            //{
            //    StdOut.Print(s + " to " + t);
            //    StdOut.Printf(" ({0}): ", sp2.DistTo(t));
            //    if (sp2.HasPathTo(t))
            //        foreach (var e in sp.PathTo(t))
            //        {
            //            StdOut.Print(e + "  ");
            //        }
            //    StdOut.Println();
            //}

            //filename = "tinyEWDnc.txt";
            //scanner = new Scanner(new StreamReader(File.OpenRead(filename)));
            //G = new EdgeWeightedDigraph(scanner);
            //sp = new BellmanFordSP(G, s);
            //for (int t = 0; t < G.V; t++)
            //{
            //    StdOut.Print(s + " to " + t);
            //    StdOut.Printf(" ({0}): ", sp.DistTo(t));
            //    if (sp.HasPathTo(t))
            //        foreach (var e in sp.PathTo(t))
            //        {
            //            StdOut.Print(e + "  ");
            //        }
            //    StdOut.Println();
            //}
        }
Ejemplo n.º 4
0
        private static void DiGraph()
        {
            IDiGraph  G       = new DiGraph.DiGraph(new Scanner(new StreamReader(File.OpenRead("tinyDG.txt"))));
            Bag <int> sources = new Bag <int>();

            StdOut.Println("搜索几个结点:");
            int i = StdIn.ReadInt();

            while (i-- > 0)
            {
                sources.Add(StdIn.ReadInt());
            }
            DirectedDFS reachable = new DirectedDFS(G, sources);

            for (int v = 0; v < G.V; v++)
            {
                if (reachable.Marked(v))
                {
                    StdOut.Print(v + " ");
                }
            }

            //string filename = "jobs.txt"; //文件有问题
            //string seperator = "/";
            //SymbolDiGraph sg = new SymbolDiGraph(filename, seperator);
            //Topological topological = new Topological(sg.G);
            //foreach (int v in topological.Order)
            //    StdOut.Println(sg.Name(v));

            StdOut.Println();
            StdOut.Println("强连通分量的数量");
            ISCC scc = new KosarajuSCC(G);

            StdOut.Println(scc.Count);
        }
Ejemplo n.º 5
0
 private static void TestRank()
 {
     int[] ints = StdIn.ReadAllInts();
     Array.Sort(ints);
     while (!StdIn.IsEmpty())
     {
         int key = StdIn.ReadInt();
         if (Rank(key, ints) < 0)
         {
             StdOut.Println(key); //如果不在名单里 则打印
         }
     }
 }
        public static void Main(string[] args)
        {
            int[] whitelist = new In(args[0]).ReadAllInts();

            Array.Sort(whitelist);

            // read key; print if not in whitelist
            while (!StdIn.IsEmpty())
            {
                int key = StdIn.ReadInt();

                if (Rank(key, whitelist) == -1)
                {
                    Console.WriteLine(key);
                }
            }
        }
Ejemplo n.º 7
0
        private static void MST()
        {
            int i = 0;

            StdOut.Println("输入选择:0,1");
            i = StdIn.ReadInt();
            string filename = i switch
            {
                0 => "tinyEWG.txt",
                1 => "mediumEWG.txt",
                _ => throw new Exception("no file name is assigned"),
            };
            IEdgeWeightGraph G    = new EdgeWeightGraph(new Scanner(new StreamReader(File.OpenRead(filename))));
            IMST             mst  = new LazyPrimMST(G);
            IMST             mst2 = new PrimeMST(G);
            IMST             mst3 = new KruskalMST(G);

            ShowMSTResult(G, mst);
            ShowMSTResult(G, mst2);
            ShowMSTResult(G, mst3);
        }
Ejemplo n.º 8
0
        private static void Graph()
        {
            Graph graph  = new Graph(new Scanner(new StreamReader(File.OpenRead("tinyG.txt"))));
            Graph graph2 = new Graph(new Scanner(new StreamReader(File.OpenRead("tinyCG.txt"))));

            StdOut.Println("输入一个数字: ");
            int     s      = StdIn.ReadInt();
            ISearch search = new DepthFirstSearch(graph, s);

            for (int v = 0; v < graph.V; v++)
            {
                if (search.Marked(v))
                {
                    StdOut.Print(v + " ");
                }
            }
            StdOut.Println();
            StdOut.Println();
            StdOut.Println();

            ICC cc = new DepthFirstSearch(graph, s).InitCC();
            int M  = cc.CountCC();

            StdOut.Println(M + " components");
            Bag <int>[] components = new Bag <int> [M];
            for (int i = 0; i < M; i++)
            {
                components[i] = new Bag <int>();
            }
            for (int v = 0; v < graph.V; v++)
            {
                components[cc.Id(v)].Add(v);
            }
            for (int i = 0; i < M; i++)
            {
                foreach (int v in components[i])
                {
                    StdOut.Print(v + " ");
                }
                StdOut.Println();
            }
            StdOut.Println(cc.HasCycle);

            if (search.Count() != graph.V)
            {
                StdOut.Print("Not ");
            }
            StdOut.Println("connected");

            IPath path = new DepthFirstSearch(graph2, s);

            for (int v = 0; v < graph2.V; v++)
            {
                StdOut.Print(s + " to " + v + ": ");
                if (path.HasPathTo(v))
                {
                    foreach (int x in path.PathTo(v))
                    {
                        if (x == s)
                        {
                            StdOut.Print(x);
                        }
                        else
                        {
                            StdOut.Print("-" + x);
                        }
                    }
                }
                StdOut.Println();
            }


            //符号图
            string       filename  = "routes.txt";
            string       filename2 = "movies.txt";
            string       delim     = "/";
            ISymbolGraph sg        = new SymbolGraph(filename2, delim);
            IGraph       g         = sg.G;

            while (StdIn.HasNextLine())
            {
                StdOut.Println("输入查找的字符串");
                string source = StdIn.ReadLine();
                foreach (int w in g.Adj(sg.Index(source)))
                {
                    StdOut.Println("   " + sg.Name(w));
                }
            }
        }