Exemplo n.º 1
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();
            //}
        }
Exemplo n.º 2
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);
        }
 public static void Show <T>(T[] a) where T : IComparable <T>
 {
     foreach (var item in a)
     {
         StdOut.Print(item + " ");
     }
     StdOut.Println();
 }
Exemplo n.º 4
0
        static void Main(string[] args)
        {
            Stack <string> stack = new Stack <string>();
            Queue <string> q     = new Queue <string>();
            Bag <string>   b     = new Bag <string>();
            Deque <string> deque = new Deque <string>();

            while (!StdIn.IsEmpty())
            {
                string item = StdIn.ReadString();
                b.Add(item);
                stack.Push(item);
                deque.PushLeft(item);
                if (!item.Equals("-"))
                {
                    q.Enqueue(item);
                }
                //else if (!q.IsEmpty)
                //StdOut.Print(q.Dequeue() + " ");
            }

            stack.Reverse();
            Queue <string> q2 = new Queue <string>(q);

            q.Reverse();
            while (!q2.IsEmpty)
            {
                StdOut.Print(q2.Dequeue() + " ");
            }
            while (!q.IsEmpty)
            {
                StdOut.Print(q.Dequeue() + " ");
            }

            Stack <string> stack2 = new Stack <string>(stack);

            while (!stack.IsEmpty)
            {
                StdOut.Print(stack.Pop() + " ");
            }
            while (!stack2.IsEmpty)
            {
                StdOut.Print(stack2.Pop() + " ");
            }

            StdOut.Println();

            while (!deque.IsEmpty)
            {
                StdOut.Print(deque.PopRight() + " ");
            }
            StdOut.Println();
            foreach (var item in b)
            {
                StdOut.Print(item + " ");
            }
        }
Exemplo n.º 5
0
        private static void DoSearch(IStringSearch stringSearch, string pat, string text)
        {
            StdOut.Println("text:    " + text);
            int offset = stringSearch.Search(text);

            StdOut.Print("pattern: ");
            for (int i = 0; i < offset; i++)
            {
                StdOut.Print(" ");
            }
            StdOut.Println(pat);
        }
        public static double TimeRandomInput(AlgorithmOptions options, int N, int T, bool showResult = false)
        {
            StdOut.Print("Algorithm: " + options.ToString() + " " + "Sorting " + N + " number for " + T + " times ");
            double total = 0.0;

            double[] a = new double[N];
            for (int t = 0; t < T; ++t)
            {
                //进行一次测试
                for (int i = 0; i < N; ++i)
                {
                    a[i] = StdRandom.Uniform();
                }
                total += Time(options, ref a, showResult);
            }
            return(total);
        }
Exemplo n.º 7
0
        static void Main(string[] args)
        {
            //LSDSort();
            //MSDSort();
            //Q3String();
            //StringSearch();
            //Regax();

            for (; !BinaryStdIn.IsEmpty();)
            {
                if (BinaryStdIn.ReadBoolean())
                {
                    StdOut.Print("1");
                }
                else
                {
                    StdOut.Print("0");
                }
            }
            StdOut.Println();
        }
Exemplo 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));
                }
            }
        }