Esempio n. 1
0
 public void Run(string inputFileName, string outputFileName)
 {
     var lines = File.ReadAllLines(inputFileName);
     var graph = new Graph();
     graph.Parse(lines, true);
     var result = graph.Tarjan();
     File.WriteAllLines(outputFileName, result.Select(x => x.Label));
 }
        public void Parse_WithWeight_ShouldParse_Test()
        {
            // Assign
            var lines = new[] { "1 2 10", "2 3 15", "3 1 20" };
            var graph = new Graph();

            // Act
            graph.Parse(lines, true);

            // Assert
            Assert.AreEqual(3, graph.Vertices.Count());
            Assert.AreEqual(3, graph.Edges.Count());
        }
Esempio n. 3
0
        public long FindMinimumLatency(string[] lines)
        {
            var graph = new Graph();
            graph.Parse(lines.Skip(2).ToArray());
            var clientVerticesLabels = lines[1].Split(' ');
            var clientVertices = new HashSet<Vertex>(clientVerticesLabels.Select(vertexLabel => graph[vertexLabel]));
            long minimumLatency = long.MaxValue;

            foreach (var currentVertex in graph.Vertices.Where(vertex => !clientVertices.Contains(vertex)))
            {
                var roadmap = graph.Dijkstra(currentVertex);
                var currentMinimumLatency = clientVertices.Max(x => roadmap[x].Distance);
                if (currentMinimumLatency < minimumLatency)
                {
                    minimumLatency = currentMinimumLatency;
                }
            }

            return minimumLatency;
        }
        public void Tarjans_Case1()
        {
            // Assign
            var lines = new[]
            {
                "visa foreignpassport",
                "visa hotel",
                "visa bankstatement",
                "bankstatement nationalpassport",
                "hotel creditcard",
                "creditcard nationalpassport",
                "nationalpassport birthcertificate",
                "foreignpassport nationalpassport",
                "foreignpassport militarycertificate",
                "militarycertificate nationalpassport"
            };
            var graph = new Graph();
            var expectedStrings = new[]
            {
                "birthcertificate",
                "nationalpassport",
                "bankstatement",
                "creditcard",
                "hotel",
                "militarycertificate",
                "foreignpassport",
                "visa"
            };
            var expected = expectedStrings.Aggregate((x, y) => x + Environment.NewLine + y);

            // Act
            graph.Parse(lines, true);
            var result = graph.Tarjan();
            var actual = result.Select(x => x.Label).Aggregate((x, y) => x + Environment.NewLine + y);

            // Assert
            Assert.AreEqual(expected, actual);
        }
        public void Tarjans_Case2()
        {
            // Assign
            var lines = new[]
            {
                "visa foreignpassport"
            };
            var graph = new Graph();
            var expectedStrings = new[]
            {
                "foreignpassport",
                "visa"
            };
            var expected = expectedStrings.Aggregate((x, y) => x + Environment.NewLine + y);

            // Act
            graph.Parse(lines, true);
            var result = graph.Tarjan();
            var actual = result.Select(x => x.Label).Aggregate((x, y) => x + Environment.NewLine + y);

            // Assert
            Assert.AreEqual(expected, actual);
        }
        public void Tarjans_Case3()
        {
            // Assign
            var lines = new[]
            {
                "cer03 cer09", "cer03 cer08", "cer03 cer07",
                "cer02 cer06", "cer02 cer05", "cer02 cer04",
                "cer01 cer03", "cer01 cer02"
            };
            var graph = new Graph();
            var expectedStrings = new[]
            {
                "cer07", "cer08", "cer09", "cer03",
                "cer04", "cer05", "cer06", "cer02",
                "cer01"
            };
            var expected = expectedStrings.Aggregate((x, y) => x + Environment.NewLine + y);

            // Act
            graph.Parse(lines, true);
            var result = graph.Tarjan();
            var actual = result.Select(x => x.Label).Aggregate((x, y) => x + Environment.NewLine + y);

            // Assert
            Assert.AreEqual(expected, actual);
        }
        public void Tarjans_Case5()
        {
            // Assign
            var lines = new[]
            {
                "a b", "c d", "e f", "g h", "i j", "k l", "m n",
                "o p", "q r", "s t", "u v", "w x", "y z"
            };
            var graph = new Graph();
            var expectedStrings = new[]
            {
                "b", "a", "d", "c", "f", "e", "h", "g", "j", "i", "l", "k", "n",
                "m", "p", "o", "r", "q", "t", "s", "v", "u", "x", "w", "z", "y"
            };
            var expected = expectedStrings.Aggregate((x, y) => x + Environment.NewLine + y);

            // Act
            graph.Parse(lines, true);
            var result = graph.Tarjan();
            var actual = result.Select(x => x.Label).Aggregate((x, y) => x + Environment.NewLine + y);

            // Assert
            Assert.AreEqual(expected, actual);
        }
        public void Tarjans_Case4()
        {
            // Assign
            var lines = new[]
            {
                "3a 4a", "3a 4b", "3b 4c", "3b 4d", "3c 4e",
                "3c 4f", "3d 4g", "3d 4h", "2a 3a", "2a 3b",
                "2b 3c", "2b 3d", "1a 2a", "1a 2b", "y z", "x v"
            };
            var graph = new Graph();
            var expectedStrings = new[]
            {
                "4b", "4a", "3a", "4d", "4c", "3b",
                "4f", "4e", "3c", "4h", "4g", "3d",
                "2a", "2b", "1a", "z", "y", "v", "x"
            };
            var expected = expectedStrings.Aggregate((x, y) => x + Environment.NewLine + y);

            // Act
            graph.Parse(lines, true);
            var result = graph.Tarjan();
            var actual = result.Select(x => x.Label).Aggregate((x, y) => x + Environment.NewLine + y);

            // Assert
            Assert.AreEqual(expected, actual);
        }