Exemplo n.º 1
0
        public void PriorityQueueFindPathBetween0And2()
        {
            var path   = DijkstraWithPriorityQueue.DijkstraAlgorithm(Graph, nodes[0], nodes[2]);
            var length = nodes[2].DistanceFromStart;

            var expectedPath   = new[] { 0, 8, 2 };
            var expectedLength = 26;

            Assert.AreEqual(expectedLength, length);
            CollectionAssert.AreEqual(path, expectedPath);
        }
Exemplo n.º 2
0
        public static int FindMinPath(Dictionary <Node, Dictionary <Node, int> > graph, Dictionary <int, Node> nodes, int sourceNode, int destinationNode)
        {
            var path = DijkstraWithPriorityQueue.DijkstraAlgorithm(graph, nodes[sourceNode], nodes[destinationNode]);

            if (path == null)
            {
                return(0);
            }
            else
            {
                return((int)nodes[destinationNode].DistanceFromStart);
            }
        }
Exemplo n.º 3
0
        public static void Main()
        {
            Dictionary <Node, Dictionary <Node, int> > graph = ReadInput();

            Node start = graph.Keys.FirstOrDefault(x => x.Id == pathStart);
            Node end   = graph.Keys.FirstOrDefault(x => x.Id == pathEnd);

            var result = DijkstraWithPriorityQueue.DijkstraAlgorithm(graph, start, end);

            double percentageResult = graph.Keys.FirstOrDefault(x => x.Id == result.Last()).DistanceFromStart;

            Console.WriteLine($"Most reliable path reliability: {percentageResult:F2}%");

            Console.WriteLine(string.Join(" -> ", result));
        }
Exemplo n.º 4
0
    public static void PrintPath(Dictionary <Node, Dictionary <Node, int> > graph, Dictionary <int, Node> nodes, int sourceNode, int destinationNode)
    {
        Console.Write(
            "Shortest path [{0} -> {1}]: ",
            sourceNode,
            destinationNode);

        var path = DijkstraWithPriorityQueue.DijkstraAlgorithm(graph, nodes[sourceNode], nodes[destinationNode]);

        if (path == null)
        {
            Console.WriteLine("no path");
        }
        else
        {
            var formattedPath = string.Join("->", path);
            Console.WriteLine("{0} (length {1})", formattedPath, nodes[destinationNode].DistanceFromStart);
        }
    }
Exemplo n.º 5
0
        public void PriorityQueueFindPathBetween0And10()
        {
            var path = DijkstraWithPriorityQueue.DijkstraAlgorithm(Graph, nodes[0], nodes[10]);

            Assert.IsNull(path);
        }
Exemplo n.º 6
0
        //100/100
        public static void Main()
        {
            int[] numbers = Console.ReadLine().Split(' ').Select(y => int.Parse(y)).ToArray();

            int n = numbers[0];
            int m = numbers[1];
            int h = numbers[2];

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

            for (int i = 0; i < n; i++)
            {
                nodes.Add(i + 1, new Node(i + 1));
            }

            int[] hospitals = Console.ReadLine().Split(' ').Select(x => int.Parse(x)).ToArray();

            for (int i = 0; i < hospitals.Length; i++)
            {
                nodes[hospitals[i]].IsHospital = true;
            }

            var graph = new Dictionary <Node, Dictionary <Node, int> >();

            for (int i = 0; i < n; i++)
            {
                graph.Add(nodes[i + 1], new Dictionary <Node, int>());
            }

            for (int i = 0; i < m; i++)
            {
                int[] inputEdge = Console.ReadLine().Split(' ').Select(x => int.Parse(x)).ToArray();

                graph[nodes[inputEdge[0]]].Add(nodes[inputEdge[1]], inputEdge[2]);
                graph[nodes[inputEdge[1]]].Add(nodes[inputEdge[0]], inputEdge[2]);
            }

            var min = int.MaxValue;

            var hospitalsSet     = new HashSet <int>(hospitals);
            var firstNotHospital = nodes.FirstOrDefault(x => !x.Value.IsHospital).Key;

            for (int i = 0; i < hospitals.Length; i++)
            {
                var currentHospital = hospitals[i];
                var allPaths        = DijkstraWithPriorityQueue.DijkstraAlgorithm(graph, nodes[currentHospital], nodes[firstNotHospital]);
                var currentPath     = 0;

                for (int j = 0; j < allPaths.Length; j++)
                {
                    if (!nodes[j + 1].IsHospital)
                    {
                        currentPath += allPaths[j];
                    }
                }

                for (int k = 0; k < n; k++)
                {
                    nodes[k + 1].DistanceFromStart = double.PositiveInfinity;
                }

                if (currentPath < min && currentPath != 0)
                {
                    min = currentPath;
                }
            }

            Console.WriteLine(min);
        }