Example #1
0
        private static void KosarajuRun(string filePath)
        {
            var stopWatch = new Stopwatch();
            stopWatch.Start();

            var edgesEnumerable = File.ReadLines(filePath);
            var edges = edgesEnumerable.Select(edge => edge.Split(new[] {' ', '\t'}, StringSplitOptions.RemoveEmptyEntries))
                                       .Select(edgePair => new Tuple<int, int>(int.Parse(edgePair[1]), (int.Parse(edgePair[0]))))
                                       .ToArray();

            int verticesCount = Math.Max(edges.Max(e => e.Item1), edges.Max(e => e.Item2));
            var vertices = Enumerable.Range(1, verticesCount).Select(nodeId => new KosarajuVertex(nodeId)).ToArray();

            edges.ForEach(edge =>
                {
                    vertices[edge.Item1 - 1].AdjacentVertices.Add(edge.Item2);
                    vertices[edge.Item2 - 1].ReversedAdjacentVertices.Add(edge.Item1);
                });

            stopWatch.Stop();
            var initTime = stopWatch.ElapsedMilliseconds;
            Console.WriteLine("read and init time={0}", initTime);
            stopWatch.Restart();
            var alg = new KosarajuAlgorithm();
            var result = alg.CountStronglyConnectedComponentes(vertices);
            stopWatch.Stop();
            Console.WriteLine("running time = {0}, result={1}", stopWatch.ElapsedMilliseconds, string.Join(",", result.Take(10)));
            Console.WriteLine("press any key");
            Console.ReadLine();
        }
 public void ThreeNodeTest()
 {
     var vertices = new KosarajuVertex[]
         {
             new KosarajuVertex(1, new int[]{2}, new int[]{3} ),
             new KosarajuVertex(2, new int[]{3}, new int[]{1} ),
             new KosarajuVertex(3, new int[]{1}, new int[]{2} ),
         };
     var alg = new KosarajuAlgorithm();
     var result = alg.CountStronglyConnectedComponentes(vertices);
     Assert.AreEqual(1, result.Length);
     Assert.AreEqual(3, result[0]);
 }
 public void TestCaseFromLecture()
 {
     var vertices = new KosarajuVertex[]
         {
             new KosarajuVertex(1, new int[]{4}, new int[]{7} ),
             new KosarajuVertex(2, new int[]{8}, new int[]{5} ),
             new KosarajuVertex(3, new int[]{6}, new int[]{9} ),
             new KosarajuVertex(4, new int[]{7}, new int[]{1} ),
             new KosarajuVertex(5, new int[]{2}, new int[]{8} ),
             new KosarajuVertex(6, new int[]{9}, new int[]{3,8} ),
             new KosarajuVertex(7, new int[]{1}, new int[]{4,9} ),
             new KosarajuVertex(8, new int[]{5,6}, new int[]{2} ),
             new KosarajuVertex(9, new int[]{3,7}, new int[]{6} ),
         };
     var alg = new KosarajuAlgorithm();
     var result = alg.CountStronglyConnectedComponentes(vertices);
     Assert.AreEqual(3, result.Length);
     Assert.AreEqual(3, result[0]);
     Assert.AreEqual(3, result[1]);
     Assert.AreEqual(3, result[2]);
 }