// Dijkstra's shortest paths algorithm, implemented
        // with priority queue. Running time: O(M * log M)
        // Learn more at: https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm#Using_a_priority_queue
        public static void DijkstraAlgorithm(
            Dictionary<Node, List<Edge>> graph,
            Node sourceNode)
        {
            var queue = new PriorityQueue<Node>();

            foreach (var node in graph)
            {
                node.Key.Distance = double.PositiveInfinity;
            }
            sourceNode.Distance = 0.0d;
            queue.Enqueue(sourceNode);

            while (queue.Count != 0)
            {
                var currentNode = queue.Dequeue();

                if (double.IsPositiveInfinity(currentNode.Distance))
                {
                    // All nodes processed --> algorithm finished
                    break;
                }

                foreach (var childEdge in graph[currentNode])
                {
                    var newDistance = currentNode.Distance + childEdge.Distance;
                    if (newDistance < childEdge.Node.Distance)
                    {
                        childEdge.Node.Distance = newDistance;
                        childEdge.Node.PreviousNode = currentNode;
                        queue.Enqueue(childEdge.Node);
                    }
                }
            }
        }
        public static void DijkstraAlgorithm(Dictionary<Node, List<Connection>> graph, Node source)
        {
            var queue = new PriorityQueue<Node>();

            foreach (var node in graph)
            {
                node.Key.DijkstraDistance = double.PositiveInfinity;
            }

            source.DijkstraDistance = 0.0d;
            queue.Enqueue(source);

            while (queue.Count != 0)
            {
                var currentNode = queue.Dequeue();

                if (double.IsPositiveInfinity(currentNode.DijkstraDistance))
                {
                    break;
                }

                foreach (var neighbor in graph[currentNode])
                {
                    var potDistance = currentNode.DijkstraDistance + neighbor.Distance;
                    if (potDistance < neighbor.Node.DijkstraDistance)
                    {
                        neighbor.Node.DijkstraDistance = potDistance;
                        queue.Enqueue(neighbor.Node);
                    }
                }
            }
        }
Exemplo n.º 3
0
        static void Main()
        {
            Node node1 = new Node(1);
            Node node2 = new Node(2);
            Node node3 = new Node(3);
            Node node4 = new Node(4);
            Node node5 = new Node(5);
            Node node6 = new Node(6);
            Node node7 = new Node(7);
            Node node8 = new Node(8);
            Node node9 = new Node(9);
            Node node10 = new Node(10);

            List<Node> nodes = new List<Node>();

            nodes.Add(node1);
            nodes.Add(node2);
            nodes.Add(node3);
            nodes.Add(node4);
            nodes.Add(node5);
            nodes.Add(node6);
            nodes.Add(node7);
            nodes.Add(node8);
            nodes.Add(node9);
            nodes.Add(node10);

            Dictionary<Node, List<Connection>> graph = new Dictionary<Node, List<Connection>>()
            {
                {node1, new List<Connection> {new Connection(node2, 23), new Connection(node8, 8)} },
                {node2, new List<Connection> {new Connection(node1, 23), new Connection(node4, 3), new Connection(node7, 34)} },
                {node3, new List<Connection> {new Connection(node4, 6), new Connection(node8, 25), new Connection(node10, 7)} },
                {node4, new List<Connection> {new Connection(node2, 3), new Connection(node3, 6)} },
                {node5, new List<Connection> {new Connection(node6, 10)} },
                {node6, new List<Connection> {new Connection(node5, 10)} },
                {node7, new List<Connection> {new Connection(node2, 34)} },
                {node8, new List<Connection> {new Connection(node1, 8), new Connection(node3, 25), new Connection(node10, 30)} },
                {node9, new List<Connection> {} },
                {node10, new List<Connection> {new Connection(node3, 7), new Connection(node8, 30)} },
            };

            Node source = node1;

            DijkstraAlgorithm(graph, source);

            for (int i = 0; i < nodes.Count; i++)
            {
                Console.Write("Distance from {0} to {1} ", source.ID, i);
                Console.WriteLine(nodes[i].DijkstraDistance);
            }
        }
Exemplo n.º 4
0
        static void DijkstraAlgorithm(Dictionary<Node, List<Connection>> graph, Node source)
        {
            PriorityQueue<Node> queue = new PriorityQueue<Node>();

            foreach (var node in graph)
            {
                if (source.ID != node.Key.ID)
                {
                    node.Key.DijkstraDistance = double.PositiveInfinity;
                    queue.Enqueue(node.Key);
                }
            }

            source.DijkstraDistance = 0.0d;
            queue.Enqueue(source);

            while (queue.Count != 0)
            {
                Node currentNode = queue.Peek();

                if (currentNode.DijkstraDistance == double.PositiveInfinity)
                {
                    break;
                }

                foreach (var neighbour in graph[currentNode])
                {
                    double potDistance = currentNode.DijkstraDistance + neighbour.Distance;

                    if (potDistance < neighbour.Node.DijkstraDistance)
                    {
                        neighbour.Node.DijkstraDistance = potDistance;
                    }
                }

                queue.Dequeue();
            }
        }
Exemplo n.º 5
0
        public static void DijkstraAlgorithm(Dictionary<Node, List<Edge>> graph, Node source)
        {
            var pQueue = new PriorityQueue<Node>();
            source.MinDistance = 0.0;
            pQueue.Enqueue(source);

            while (pQueue.Count != 0)
            {
                Console.WriteLine(string.Join<Node>(", ", pQueue.heap));
                Node currentNode = pQueue.Dequeue();

                foreach (var neighbour in graph[currentNode])
                {
                    double newDist = currentNode.MinDistance + neighbour.Distance;

                    if (newDist < neighbour.Node.MinDistance)
                    {
                        neighbour.Node.MinDistance = newDist;
                        pQueue.Enqueue(neighbour.Node);
                    }
                }
            }
        }
        public static void Main()
        {
            int n = 12;
            var nodes = new Node[n];
            for (int i = 0; i < n; i++)
            {
                nodes[i] = new Node(i);
            }

            var graph = new Dictionary<Node, List<Edge>>
                {
                    {
                        nodes[0], new List<Edge>
                            {
                                new Edge(nodes[6], 10),
                                new Edge(nodes[8], 12),
                            }
                    },
                    {
                        nodes[1], new List<Edge>
                            {
                                new Edge(nodes[4], 20),
                                new Edge(nodes[7], 26),
                                new Edge(nodes[9], 5),
                                new Edge(nodes[11], 6),
                            }
                    },
                    {
                        nodes[2], new List<Edge>
                            {
                                new Edge(nodes[7], 15),
                                new Edge(nodes[8], 14),
                                new Edge(nodes[11], 9),
                            }
                    },
                    {
                        nodes[3], new List<Edge>
                            {
                                new Edge(nodes[10], 7),
                            }
                    },
                    {
                        nodes[4], new List<Edge>
                            {
                                new Edge(nodes[1], 20),
                                new Edge(nodes[5], 5),
                                new Edge(nodes[6], 17),
                                new Edge(nodes[11], 11),
                            }
                    },
                    {
                        nodes[5], new List<Edge>
                            {
                                new Edge(nodes[4], 5),
                                new Edge(nodes[6], 6),
                                new Edge(nodes[8], 3),
                                new Edge(nodes[11], 33),
                            }
                    },
                    {
                        nodes[6], new List<Edge>
                            {
                                new Edge(nodes[0], 10),
                                new Edge(nodes[4], 17),
                                new Edge(nodes[5], 6),
                            }
                    },
                    {
                        nodes[7], new List<Edge>
                            {
                                new Edge(nodes[1], 26),
                                new Edge(nodes[2], 15),
                                new Edge(nodes[9], 3),
                                new Edge(nodes[11], 20),
                            }
                    },
                    {
                        nodes[8], new List<Edge>
                            {
                                new Edge(nodes[0], 12),
                                new Edge(nodes[2], 14),
                                new Edge(nodes[5], 3),
                            }
                    },
                    {
                        nodes[9], new List<Edge>
                            {
                                new Edge(nodes[1], 5),
                                new Edge(nodes[7], 3),
                            }
                    },
                    {
                        nodes[10], new List<Edge>
                            {
                                new Edge(nodes[3], 7),
                            }
                    },
                    {
                        nodes[11], new List<Edge>
                            {
                                new Edge(nodes[1], 6),
                                new Edge(nodes[2], 9),
                                new Edge(nodes[4], 11),
                                new Edge(nodes[5], 33),
                                new Edge(nodes[7], 20),
                            }
                    }
                };

            Node source = nodes[0];

            DijkstraAlgorithm(graph, source);

            for (int i = 0; i < nodes.Length; i++)
            {
                Console.Write("Shortest distance [{0} -> {1}]: ", source.Id, i);
                double distance = nodes[i].Distance;
                if (double.IsPositiveInfinity(distance))
                {
                    Console.WriteLine("no path");
                }
                else
                {
                    var path = FindPath(graph, nodes[i]);
                    var pathStr = string.Join("->", path.Select(node => node.Id));
                    Console.WriteLine("{0} (Path = {1})", distance, pathStr);
                }
            }
        }
 private static List<Node> FindPath(
     Dictionary<Node, List<Edge>> graph,
     Node node)
 {
     var path = new List<Node>();
     while (node != null)
     {
         path.Add(node);
         node = node.PreviousNode;
     }
     path.Reverse();
     return path;
 }
Exemplo n.º 8
0
 public Connection(Node node, double distance)
 {
     this.Node = node;
     this.Distance = distance;
 }