Ejemplo n.º 1
0
        public DijkstraSP(EdgeWeightedDigrap graph, int s)
        {
            distTo = new double[graph.V];
            edgeTo = new DirectedEdge[graph.V];
            minPq = new IndexMinPq<double>(graph.V);

            for (int i = 0; i < graph.V; i++)
            {
                distTo[i] = Double.MaxValue;
            }

            distTo[s] = 0;

            minPq.Insert(0, 0);
            while (!minPq.IsEmpty())
            {
                Visit(graph, minPq.DelMin());
            }
        }
Ejemplo n.º 2
0
        private void Relax(DirectedEdge edge)
        {
            var v = edge.From;
            var w = edge.To;

            if (_distanceTo[w] > _distanceTo[v] + edge.Weight)
            {
                _distanceTo[w] = _distanceTo[v] + edge.Weight;
                _edgeTo[w]     = edge;

                if (_queue.Contains(w))
                {
                    _queue.DescreaseKey(w, _distanceTo[w]);
                }
                else
                {
                    _queue.Insert(w, _distanceTo[w]);
                }
            }
        }
Ejemplo n.º 3
0
        public PrimMST(EdgeWeightedGraph graph)
        {
            edgeTo = new Edge[graph.V];
            diestTo = new double[graph.V];
            marked = new bool[graph.V];

            for (int v = 0; v < graph.V; v++)
            {
                diestTo[v] = double.MaxValue;
            }

            pq = new IndexMinPq<double>(graph.V);

            diestTo[0] = 0;
            pq.Insert(0, 0);

            while (!pq.IsEmpty())
            {
                visit(graph, pq.DelMin());
            }
        }
Ejemplo n.º 4
0
        public DijkstraSp(EdgeWeightedDigraph graph, int source)
        {
            _edgeTo     = new Dictionary <int, DirectedEdge>();
            _distanceTo = new Dictionary <int, double>();
            _queue      = new IndexMinPq <double>(graph.Vertices().Length);

            foreach (var vertex in graph.Vertices())
            {
                _distanceTo.Add(vertex, double.PositiveInfinity);
            }

            _distanceTo.Add(source, 0.0);
            _queue.Insert(source, 0.0);

            while (!_queue.IsEmpty())
            {
                var vertex = _queue.DelMin();

                foreach (var edge in graph.Adjacent(vertex))
                {
                    Relax(edge);
                }
            }
        }