Ejemplo n.º 1
0
        private PrimMSTAlgorithm(EdgeWeightedGraph graph)
        {
            this._edgeTo  = new Edge[graph.VerticesCount];
            this._distTo  = new Double[graph.VerticesCount];
            this._visited = new Boolean[graph.VerticesCount];
            this._crossingEdgesByWeight = new IndexedMinPQ <Double>(graph.VerticesCount);

            for (int i = 0; i < graph.VerticesCount; i++)
            {
                this._distTo[i] = Double.PositiveInfinity;
            }

            for (int i = 0; i < graph.VerticesCount; i++)
            {
                if (!this._visited[i])
                {
                    this.Prim(graph, i);
                }
            }

            this._lazyWeight = new Lazy <Double>(() =>
            {
                Double weight = 0;

                foreach (Edge edge in this.GetEdges())
                {
                    weight += edge.Weight;
                }

                return(weight);
            });
        }
        public void Test_Empty_OnCreation()
        {
            IndexedMinPQ <Int32> pq = new IndexedMinPQ <Int32>(1);

            Assert.True(pq.IsEmpty);
            Assert.Equal(0, pq.Count);
        }
Ejemplo n.º 3
0
        HashSet <int> _processed;                      //All Processed nodes.

        public Dijkstra(List <SharedClasses.IEdgeWithWeight> edges, int source)
        {
            _edges            = edges;
            _source           = source;
            _pQ               = new IndexedMinPQ();
            _adj              = Helper.BuildAdjListWithWeights(_edges);
            _parents          = new Dictionary <int, int>();
            _currMinDistances = new Dictionary <int, int>();
            _processed        = new HashSet <int>();
        }
        public void Test_ItemAt()
        {
            IndexedMinPQ <Int32> pq = new IndexedMinPQ <Int32>(10);

            for (int i = 1; i < 11; i++)
            {
                pq.Add(i, i);
            }

            Assert.Equal(2, pq.ItemAt(2));
        }
        public void Test_Add_MinIndex()
        {
            IndexedMinPQ <Int32> pq = new IndexedMinPQ <Int32>(10);

            for (int i = 1; i < 11; i++)
            {
                pq.Add(i, i);
            }

            Assert.Equal(1, pq.MinIndex());
        }
        public void Test_NotEmpty_WhenAdding()
        {
            IndexedMinPQ <Int32> pq = new IndexedMinPQ <Int32>(2)
            {
                { 0, 18 },
                { 1, 2 }
            };

            Assert.False(pq.IsEmpty);
            Assert.Equal(2, pq.Count);
        }
        public void Test_Contains()
        {
            IndexedMinPQ <Int32> pq = new IndexedMinPQ <Int32>(5);

            for (int i = 1; i < 6; i++)
            {
                pq.Add(i, i);
            }

            Assert.True(pq.Contains(4));
        }
        public void Test_Empty_WhenCleaning()
        {
            IndexedMinPQ <Int32> pq = new IndexedMinPQ <Int32>(1)
            {
                { 0, 2 }
            };

            pq.DeleteMin();

            Assert.True(pq.IsEmpty);
            Assert.Equal(0, pq.Count);
        }
        public void Test_DecreaseItem()
        {
            IndexedMinPQ <Int32> pq = new IndexedMinPQ <Int32>(5);

            for (int i = 1; i < 6; i++)
            {
                pq.Add(i, i);
            }

            pq.DecreaseItem(1, -1);

            Assert.Equal(-1, pq.ItemAt(1));
        }
        public void Test_Delete()
        {
            IndexedMinPQ <Int32> pq = new IndexedMinPQ <Int32>(5);

            for (int i = 1; i < 6; i++)
            {
                pq.Add(i, i);
            }

            pq.Delete(4);

            Assert.False(pq.Contains(4));
        }
        public void Test_AddDeleteMin_Order()
        {
            IndexedMinPQ <Int32> pq = new IndexedMinPQ <Int32>(10);

            for (int i = 1; i < 11; i++)
            {
                pq.Add(i, i);
            }

            for (int i = 1; i < 11; i++)
            {
                Assert.Equal(i, pq.DeleteMin());
            }
        }
Ejemplo n.º 12
0
        public DijkstraShortestPathAlgorithm(EdgeWeightedDigraph graph, Int32 vertice)
        {
            foreach (Edge edge in graph.GetEdges())
            {
                if (edge.Weight < 0)
                {
                    throw new InvalidOperationException("Edge" + edge + " has negative weight.");
                }
            }

            this._distTo = new Double[graph.VerticesCount];
            this._edgeTo = new Edge[graph.VerticesCount];

            for (int i = 0; i < graph.VerticesCount; i++)
            {
                this._distTo[i] = Double.PositiveInfinity;
            }

            this._distTo[vertice] = 0.0;

            // Relax vertices in order of distance from vertice.
            this._crossingEdgesByWeight = new IndexedMinPQ <Double>(graph.VerticesCount)
            {
                { vertice, this._distTo[vertice] }
            };

            while (!this._crossingEdgesByWeight.IsEmpty)
            {
                Int32 v = this._crossingEdgesByWeight.DeleteMin();

                foreach (Edge edge in graph.GetAdjacentVertices(v))
                {
                    this.Relax(edge);
                }
            }
        }