Exemplo n.º 1
0
    void Start()
    {
        Digraph           G   = new Digraph(txt);
        KosarajuSharirSCC scc = new KosarajuSharirSCC(G);

        // number of connected components
        int m = scc.Count();

        print(m + " strong components");

        // compute list of vertices in each strong component
        Queue <int>[] components = new Queue <int> [m];
        for (int i = 0; i < m; i++)
        {
            components[i] = new Queue <int>();
        }
        for (int v = 0; v < G.V(); v++)
        {
            components[scc.ID(v)].Enqueue(v);
        }

        // print results
        for (int i = 0; i < m; i++)
        {
            string str = null;
            foreach (int v in components[i])
            {
                str += (v + " ");
            }
            print(str);
        }
    }
        public void Test_Id()
        {
            KosarajuSharirSCC scc = this.CreateSCC();

            Assert.Equal(3, scc.Id(0));
            Assert.Equal(1, scc.Id(4));
        }
    /**/ public static void main(string[] strarr)
    {
        In                i                 = new In(strarr[0]);
        Digraph           digraph           = new Digraph(i);
        KosarajuSharirSCC kosarajuSharirSCC = new KosarajuSharirSCC(digraph);
        int               num               = kosarajuSharirSCC.count();

        StdOut.println(new StringBuilder().append(num).append(" components").toString());
        Queue[] array = (Queue[])new Queue[num];
        for (int j = 0; j < num; j++)
        {
            array[j] = new Queue();
        }
        for (int j = 0; j < digraph.V(); j++)
        {
            array[kosarajuSharirSCC.id(j)].enqueue(Integer.valueOf(j));
        }
        for (int j = 0; j < num; j++)
        {
            Iterator iterator = array[j].iterator();
            while (iterator.hasNext())
            {
                int i2 = ((Integer)iterator.next()).intValue();
                StdOut.print(new StringBuilder().append(i2).append(" ").toString());
            }
            StdOut.println();
        }
    }
        private KosarajuSharirSCC CreateSCC()
        {
            Digraph graph = new Digraph(7);

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

            return(KosarajuSharirSCC.Create(graph));
        }
        public void Test_Count()
        {
            KosarajuSharirSCC scc = this.CreateSCC();

            Assert.Equal(4, scc.Count);
        }
        public void Test_NotStronglyConnected()
        {
            KosarajuSharirSCC scc = this.CreateSCC();

            Assert.False(scc.IsStronglyConnected(0, 4));
        }
        public void Test_IsStronglyConnected()
        {
            KosarajuSharirSCC scc = this.CreateSCC();

            Assert.True(scc.IsStronglyConnected(0, 1));
        }
Exemplo n.º 8
0
        public void Run()
        {
            Console.WriteLine("Choose file:");     // Prompt
            Console.WriteLine("1 - tinyDG.txt");   // Prompt
            Console.WriteLine("2 - mediumDG.txt"); // Prompt
            Console.WriteLine("or quit");          // Prompt

            var    fileNumber = Console.ReadLine();
            string fileName;

            switch (fileNumber)
            {
            case "1":
                fileName = "tinyDG.txt";
                break;

            case "2":
                fileName = "mediumDG.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 scc = new KosarajuSharirSCC(digraph);

            // number of connected components
            var m = scc.Count();

            Console.WriteLine($"{m} components");

            // compute list of vertices in each strong component
            var components = new Core.Collections.Queue <Integer> [m];

            for (var i = 0; i < m; i++)
            {
                components[i] = new Core.Collections.Queue <Integer>();
            }
            for (var i = 0; i < digraph.V; i++)
            {
                components[scc.Id(i)].Enqueue(i);
            }

            // print results
            for (var i = 0; i < m; i++)
            {
                foreach (int j in components[i])
                {
                    Console.Write($"{j} ");
                }
                Console.WriteLine();
            }

            Console.ReadLine();
        }