Esempio n. 1
0
        static ICollection<Connection> PrimAlgorithm(Node startNode)
        {
            var mst = new List<Connection>();
            PriorityQueue queue = new PriorityQueue();
            startNode.Used = true;
            Connection first = new Connection(startNode, 0);

            first.FromNode = startNode;

            mst.Add(first);

            foreach (var connection in neighbourhood[startNode])
            {
                queue.Enqueue(connection);
                connection.FromNode = startNode;
            }

            while (mst.Count != houses.Count)
            {
                var current = queue.Dequeue();

                current.ToNode.Used = true;
                mst.Add(current);

                foreach (var connection in neighbourhood[current.ToNode])
                {
                    if (!connection.ToNode.Used)
                    {
                        connection.FromNode = current.ToNode;
                        queue.Enqueue(connection);
                    }
                }
            }

            return mst;
        }
 public void Enqueue(Connection connection)
 {
     this.elements.Add(connection);
 }