Exemplo n.º 1
0
        /// <summary>
        /// Computes a shortest paths tree from the specified sourceVertex to every other vertex in the edge-weighted directed graph
        /// </summary>
        /// <param name="graph">The edge-weighted directed graph</param>
        /// <param name="sourceVertex">The source vertex to compute the shortest paths tree from</param>
        /// <exception cref="ArgumentOutOfRangeException">Throws an ArgumentOutOfRangeException if an edge weight is negative</exception>
        /// <exception cref="ArgumentNullException">Thrown if EdgeWeightedDigraph is null</exception>
        public DijkstraShortestPath(EdgeWeightedDigraph graph, int sourceVertex)
        {
            if (graph == null)
            {
                throw new ArgumentNullException("graph", "EdgeWeightedDigraph cannot be null");
            }

            foreach (DirectedEdge edge in graph.Edges())
            {
                if (edge.Weight < 0)
                {
                    throw new ArgumentOutOfRangeException($"Edge: '{edge}' has negative weight");
                }
            }

            _distanceTo = new double[graph.NumberOfVertices];
            _edgeTo     = new DirectedEdge[graph.NumberOfVertices];
            for (int v = 0; v < graph.NumberOfVertices; v++)
            {
                _distanceTo[v] = double.PositiveInfinity;
            }
            _distanceTo[sourceVertex] = 0.0;

            _priorityQueue = new IndexMinPriorityQueue <double>(graph.NumberOfVertices);
            _priorityQueue.Insert(sourceVertex, _distanceTo[sourceVertex]);
            while (!_priorityQueue.IsEmpty())
            {
                int v = _priorityQueue.DeleteMin();
                foreach (DirectedEdge edge in graph.Adjacent(v))
                {
                    Relax(edge);
                }
            }
        }
        /// <summary>
        /// Computes a shortest paths tree from the specified sourceVertex to every other vertex in the edge-weighted directed graph
        /// </summary>
        /// <param name="graph">The edge-weighted directed graph</param>
        /// <param name="sourceVertex">The source vertex to compute the shortest paths tree from</param>
        /// <exception cref="ArgumentOutOfRangeException">Throws an ArgumentOutOfRangeException if an edge weight is negative</exception>
        /// <exception cref="ArgumentNullException">Thrown if EdgeWeightedDigraph is null</exception>
        public DijkstraShortestPath( EdgeWeightedDigraph graph, int sourceVertex )
        {
            if ( graph == null )
             {
            throw new ArgumentNullException( "graph", "EdgeWeightedDigraph cannot be null" );
             }

             foreach ( DirectedEdge edge in graph.Edges() )
             {
            if ( edge.Weight < 0 )
            {
               throw new ArgumentOutOfRangeException( string.Format( "Edge: '{0}' has negative weight", edge ) );
            }
             }

             _distanceTo = new double[graph.NumberOfVertices];
             _edgeTo = new DirectedEdge[graph.NumberOfVertices];
             for ( int v = 0; v < graph.NumberOfVertices; v++ )
             {
            _distanceTo[v] = Double.PositiveInfinity;
             }
             _distanceTo[sourceVertex] = 0.0;

             _priorityQueue = new IndexMinPriorityQueue<double>( graph.NumberOfVertices );
             _priorityQueue.Insert( sourceVertex, _distanceTo[sourceVertex] );
             while ( !_priorityQueue.IsEmpty() )
             {
            int v = _priorityQueue.DeleteMin();
            foreach ( DirectedEdge edge in graph.Adjacent( v ) )
            {
               Relax( edge );
            }
             }
        }
Exemplo n.º 3
0
 private void Prim(EdgeWeightedGraph G, int s)
 {
     _distTo[s] = 0.0;
     _pq.Insert(s, _distTo[s]);
     while (!_pq.IsEmpty())
     {
         int v = _pq.DeleteMin();
         Scan(G, v);
     }
 }
Exemplo n.º 4
0
    List <List <int> > GetMST()
    {
        var size    = nodes.NodeList.Count;
        var parents = new int[size];
        var weights = new float[size];
        var marked  = new bool[size];
        var pq      = new IndexMinPriorityQueue <float>(size);

        for (var x = 1; x < size; x++)
        {
            weights[x] = float.MaxValue;
        }
        weights[0] = 0;

        pq.Insert(0, 0);
        while (!pq.IsEmpty())
        {
            int v = pq.DeleteMin();
            marked[v] = true;
            for (var x = 0; x < size; x++)
            {
                if (x != v && !marked[x])
                {
                    var distance = Vector2.Distance(nodes.NodeList[v], nodes.NodeList[x]);
                    if (distance < weights[x])
                    {
                        weights[x] = distance;
                        parents[x] = v;
                        if (pq.Contains(x))
                        {
                            pq.ChangeKey(x, distance);
                        }
                        else
                        {
                            pq.Insert(x, distance);
                        }
                    }
                }
            }
        }

        var result = GetAdjacencyLists(size);

        for (var x = 1; x < parents.Length; x++)
        {
            var target = parents[x];
            result[x].Add(target);
            result[target].Add(x);
        }
        return(result);
    }
Exemplo n.º 5
0
 public PrimMinimumSpanTree(EdgeWeightedGraph g)
 {
     edgeTo = new Edge[g.Vcount];
     distTo = new double[g.Vcount];
     marked = new bool[g.Vcount];
     for (int i = 0; i < g.Vcount; i++)
     {
         distTo[i] = double.PositiveInfinity;
     }
     pq        = new IndexMinPriorityQueue <double>(g.Vcount);
     distTo[0] = 0.0;
     pq.Insert(0, 0.0);
     while (!pq.IsEmpty())
     {
         Visit(g, pq.DelMin());
     }
 }
Exemplo n.º 6
0
        public DijkstraShortestPath(EdgeWeightedDigraph g, int s)
        {
            edgeTo = new DirectedEdge[g.Vcount];
            distTo = new double[g.Vcount];
            pq     = new IndexMinPriorityQueue <double>(g.Vcount);

            for (int i = 0; i < g.Vcount; i++)
            {
                distTo[i] = double.PositiveInfinity;
            }
            distTo[0] = 0.0;
            pq.Insert(s, 0.0);
            while (!pq.IsEmpty())
            {
                Relax(g, pq.DelMin());
            }
        }
Exemplo n.º 7
0
        public void IsEmpty_AfterConstructingNewQueue_WillBeTrue()
        {
            IndexMinPriorityQueue <double> queue = new IndexMinPriorityQueue <double>(10);

            Assert.IsTrue(queue.IsEmpty());
        }