public void TryGetMinGraph3Test()
        {
            var nodes = new Dictionary <int, List <int> >();

            nodes.Add(1, new List <int>()
            {
                2, 4
            });
            nodes.Add(2, new List <int>()
            {
                3, 1
            });
            nodes.Add(3, new List <int>()
            {
                4, 2
            });
            nodes.Add(4, new List <int>()
            {
                1, 3
            });

            int actual = new KargerMinCut(nodes).TryGetMin(100);

            Assert.That(actual, Is.EqualTo(2));
        }
Example #2
0
        private static void Main(string[] args)
        {
            if (args.Count() == 1)
            {
                string filePath = args[0];
                var vertices = new List<Vertex>();
                using (var reader = new StreamReader(filePath))
                {
                    while (!reader.EndOfStream)
                    {
                        var verticeArray = reader.ReadLine().Split(new[] {' ', '\t'}, StringSplitOptions.RemoveEmptyEntries);
                        vertices.Add(new Vertex(int.Parse(verticeArray[0]),
                                                 verticeArray.Skip(1).Select(int.Parse).ToArray()));
                    }
                }
                vertices.Sort();
                var stopWatch = new Stopwatch();
                //var repeatCount = (int)(Math.Pow(vertices.Count, 2)*Math.Log(vertices.Count, Math.E));
                var repeatCount = (int)(Math.Pow(vertices.Count, 2));
                stopWatch.Start();
                var kargerAlg = new KargerMinCut(vertices.ToArray());
                var minCut = Enumerable.Range(1, repeatCount)
                              .Select(i => kargerAlg.CalculateMinCut())
                              .Min();

                stopWatch.Stop();
                Console.WriteLine("min cut={0}, repeatCount = {1}, running time = {2} ms", minCut, repeatCount, stopWatch.ElapsedMilliseconds);
                Console.WriteLine("press any key");
                Console.ReadLine();
            }
        }
 public void FourVertices()
 {
     var vertices = new Vertex[]
         {
             new Vertex(1, new [] {2, 4, 3}),
             new Vertex(2, new [] {1, 3}),
             new Vertex(3, new [] {1, 2}),
             new Vertex(4, new [] {1})
         };
     var alg = new KargerMinCut(vertices);
     var minCut = Enumerable.Range(1, 32).Select(i => alg.CalculateMinCut()).Min();
     Assert.AreEqual(1, minCut);
 }
Example #4
0
        public void CanFindMinCut_Graph3()
        {
            var input = new[]
            {
                new [] { 1, 19, 15, 36, 23, 18, 39, },
                new [] { 2, 36, 23, 4, 18, 26, 9 },
                new [] { 3, 35, 6, 16, 11 },
                new [] { 4, 23, 2, 18, 24 },
                new [] { 5, 14, 8, 29, 21 },
                new [] { 6, 34, 35, 3, 16 },
                new [] { 7, 30, 33, 38, 28 },
                new [] { 8, 12, 14, 5, 29, 31 },
                new [] { 9, 39, 13, 20, 10, 17, 2 },
                new [] { 10, 9, 20, 12, 14, 29 },
                new [] { 11, 3, 16, 30, 33, 26 },
                new [] { 12, 20, 10, 14, 8 },
                new [] { 13, 24, 39, 9, 20 },
                new [] { 14, 10, 12, 8, 5 },
                new [] { 15, 26, 19, 1, 36 },
                new [] { 16, 6, 3, 11, 30, 17, 35, 32 },
                new [] { 17, 38, 28, 32, 40, 9, 16 },
                new [] { 18, 2, 4, 24, 39, 1 },
                new [] { 19, 27, 26, 15, 1 },
                new [] { 20, 13, 9, 10, 12 },
                new [] { 21, 5, 29, 25, 37 },
                new [] { 22, 32, 40, 34, 35 },
                new [] { 23, 1, 36, 2, 4 },
                new [] { 24, 4, 18, 39, 13 },
                new [] { 25, 29, 21, 37, 31 },
                new [] { 26, 31, 27, 19, 15, 11, 2 },
                new [] { 27, 37, 31, 26, 19, 29 },
                new [] { 28, 7, 38, 17, 32 },
                new [] { 29, 8, 5, 21, 25, 10, 27 },
                new [] { 30, 16, 11, 33, 7, 37 },
                new [] { 31, 25, 37, 27, 26, 8 },
                new [] { 32, 28, 17, 40, 22, 16 },
                new [] { 33, 11, 30, 7, 38 },
                new [] { 34, 40, 22, 35, 6 },
                new [] { 35, 22, 34, 6, 3, 16 },
                new [] { 36, 15, 1, 23, 2 },
                new [] { 37, 21, 25, 31, 27, 30 },
                new [] { 38, 33, 7, 28, 17, 40 },
                new [] { 39, 18, 24, 13, 9, 1 },
                new [] { 40, 17, 32, 22, 34, 38 },
            }
            .Select(a => a.Select(i => i - 1).Skip(1).ToArray()).ToArray();

            var minCut = new KargerMinCut().CalcMinCut(input);

            Assert.AreEqual(3, minCut);
        }
        public void TryGetMinGraph5Test()
        {
            var array = File.ReadAllLines("kargerMinCut.txt")
                        .Select(s => Parse(s.Split("	"))).ToArray()
                        .ToList();

            var nodes = new Dictionary <int, List <int> >();

            foreach (var lst in array)
            {
                nodes.Add(lst.First(), lst.Skip(1).ToList());
            }

            int actual = new KargerMinCut(nodes).TryGetMin(100);

            Assert.That(actual, Is.EqualTo(17));
        }
        public void EigthVertices()
        {
            var vertices = new Vertex[]
            {
                    new Vertex(1, new [] {5, 2, 6}),
                    new Vertex(2, new [] {1, 5, 6, 3}),
                    new Vertex(3, new [] {2, 7, 4, 8}),
                    new Vertex(4, new [] {3, 7, 8}),
                    new Vertex(5, new [] {1, 2, 6}),
                    new Vertex(6, new [] {1, 2, 5, 7}),
                    new Vertex(7, new [] {6, 3, 4, 8}),
                    new Vertex(8, new [] {3, 4, 7}),
                };

            var alg = new KargerMinCut(vertices);
            var minCut = Enumerable.Range(1, 200).Select(i => alg.CalculateMinCut()).Min();
            Assert.AreEqual(2, minCut);
        }
Example #7
0
        public void CanFindMinCut_Graph2()
        {
            var input = new[]
            {
                new [] { 1, 2, 3, 4, 7 },
                new [] { 2, 1, 3, 4 },
                new [] { 3, 1, 2, 4 },
                new [] { 4, 1, 2, 3, 5 },
                new [] { 5, 4, 6, 7, 8 },
                new [] { 6, 5, 7, 8 },
                new [] { 7, 1, 5, 6, 8 },
                new [] { 8, 5, 6, 7 }
            }
            .Select(a => a.Select(i => i - 1).Skip(1).ToArray()).ToArray();

            var minCut = new KargerMinCut().CalcMinCut(input);

            Assert.AreEqual(2, minCut);
        }
        public void CalcualteMinCutTest()
        {
            //Arrange
            var fileLocation = @"..\..\TestData\SampleData_kargerMinCut.txt";

            if (!File.Exists(fileLocation))
            {
                Assert.Fail("File not found");
            }
            var lines    = File.ReadAllLines(fileLocation).Where(line => !string.IsNullOrEmpty(line));
            var vertices = new List <Vertex>();

            foreach (var line in lines)
            {
                var lineItems = line.Split(new[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
                vertices.Add(new Vertex(int.Parse(lineItems.First()), lineItems.Skip(1).Select(int.Parse).ToArray()));
            }
            vertices.Sort();
            var maxIteration = (int)Math.Pow(vertices.Count, 2);
            var kargerAlgo   = new KargerMinCut(vertices);
            var minCutCount  = Enumerable.Range(0, maxIteration).Select(index => kargerAlgo.GetCount()).ToList().Min();

            Assert.IsTrue(minCutCount == 17);
        }
Example #9
0
        public int FindMinCutByKargerStein()
        {
            var minCut = new KargerMinCut(_graphArray);

            return(minCut.FindMinCutKargerStein());
        }
Example #10
0
        public int FindMinCutByTrialsSlow()
        {
            var minCut = new KargerMinCut(_graphArray);

            return(minCut.FindMinCutAdjacencyList());
        }
Example #11
0
        public int FindMinCutByTrialsUnion()
        {
            var minCut = new KargerMinCut(_graphArray);

            return(minCut.FindMinCutUnion());
        }