Esempio n. 1
0
    void DigraphTest()
    {
        Digraph r = new Digraph(7);

        r.AddEdge(0, 1);
        r.AddEdge(1, 2);
        r.AddEdge(3, 1);
        r.AddEdge(1, 4);
        r.AddEdge(4, 5);
        r.AddEdge(4, 6);
        r.AddEdge(6, 0);

        //r.Display();
        Digraph reverse = r.Reverse();
        //reverse.Display();

        DirectedDFS d = new DirectedDFS(r, 1);
        //d.Display();

        DirectedCycle c = new DirectedCycle(r);
        //Debug.Log(c.HasCycle());
        //c.Cycle();

        //DepthFirstOrder o = new DepthFirstOrder(r);
        //o.Pre();

        KosarajuSCC scc = new KosarajuSCC(r);

        Debug.Log(scc.Connected(0, 4));
    }
        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);
        }
Esempio n. 3
0
        static void Main(string[] args)
        {
            Graph <string> graph = new Graph <string>();

            graph.AddEdge("A", "B");
            graph.AddEdge("B", "C");
            graph.AddEdge("C", "A");
            graph.AddEdge("B", "D");
            graph.AddEdge("D", "E");
            graph.AddEdge("E", "F");
            graph.AddEdge("F", "D");
            graph.AddEdge("G", "F");
            graph.AddEdge("G", "H");
            graph.AddEdge("H", "I");
            graph.AddEdge("I", "J");
            graph.AddEdge("J", "G");
            graph.AddEdge("G", "H");
            graph.AddEdge("J", "K");

            Console.WriteLine("******Depth First Search*****");

            graph.DFS();

            Console.WriteLine();

            Console.WriteLine("*****Breadth First Search*****");

            graph.BFS();

            Console.WriteLine();

            Console.WriteLine("*****Toplological Sort*****");

            graph.TopologicalSort();

            Console.WriteLine("*****Kosaraju SCC*****");

            ISCC <string>         kosaraju = new KosarajuSCC <string>();
            List <List <string> > result   = kosaraju.FindSCG(graph);

            foreach (List <string> vertices in result)
            {
                Console.WriteLine(string.Join(",", vertices));
            }

            Console.ReadLine();
        }