コード例 #1
0
ファイル: Dijkstra.cs プロジェクト: FerasAhmed/CP-Library
        public void Run()
        {
            IndexedPriorityQueue <double, int> pQ = new IndexedPriorityQueue <double, int>();

            for (int i = 0; i < vertexCount; ++i)
            {
                pQ.Insert(i, double.MaxValue);
            }

            pQ.Decrease_Key(source, 0);
            while (pQ.Count > 0)
            {
                KeyValuePair <double, int> kv = pQ.Extract_Minimum();
                vertexCost[kv.Value] = kv.Key;
                List <WeightedArc> edges = adjList.getEdges(kv.Value);
                foreach (WeightedArc e in edges)
                {
                    if (vertexCost[e.Vertex] > vertexCost[kv.Value] + e.Cost)
                    {
                        parent[e.Vertex] = kv.Value;
                        pQ.Decrease_Key(e.Vertex, vertexCost[kv.Value] + e.Cost);
                    }
                }
            }
        }
コード例 #2
0
        public IList <DirectedEdge> [] PathTo(int v)
        {
            verticesToCover = new IndexedPriorityQueue <DirectedEdge>(graph.VerticesCount);
            verticesToCover.Add(v, new DirectedEdge(0, 0, 0.0));
            distTo[v] = 0;
            while (!verticesToCover.IsEmpty())
            {
                var v1 = verticesToCover.DeleteMin();
                var adjacentEdgeList = graph.Adj(v1.Key);
                foreach (var edge in adjacentEdgeList)
                {
                    //if(marked[edge.To]) continue;
                    if (distTo[edge.To] > (distTo[v1.Key] + edge.Weight))
                    {
                        distTo[edge.To]  = distTo[v1.Key] + edge.Weight;
                        marked[edge.To]  = true;
                        hasPath[edge.To] = true;
                        verticesToCover.Add(edge.To, edge);

                        foreach (var item in listOfEdges[v1.Key])
                        {
                            listOfEdges[edge.To].Add(item);
                        }
                        if (!listOfEdges[edge.To].Contains(edge))
                        {
                            listOfEdges[edge.To].Add(edge);
                        }
                    }
                }
            }
            return(listOfEdges);
        }
コード例 #3
0
ファイル: AStar.cs プロジェクト: Pokoi/SGDJamII
    // metodo al que llamaremos desde fuera para realizar el calculo
    // se encargara de empezar la logica y de llamar al metodo principal
    public bool Begin(int heu, int destX, int destZ, int origX, int origZ, box[,] table)
    {
        Setup();

        // inicializamos heuristica a utilizar
        HEURISTIC = heu;

        // inicializamos contenedores
        abiertos = new IndexedPriorityQueue <Node>(table.GetLength(0) * table.GetLength(1));
        cerrados = new List <Node>();

        // iniciamos y creamos el nodo origen (los nodos se van creando en ejecucion)
        Node origen = new Node();

        origen.setPosition(origX, origZ);

        // metemos el nodo origen a la cola de prioridad
        abiertos.Insert(origX + origZ * table.GetLength(0), origen);

        // actualizamos matriz de nodos
        nodeMatrix[origen.i_, origen.j_] = origen;

        // llamamos al algoritmo principal
        Algorithm(destX, destZ, origX, origZ, table);

        return(FOUND);
    }
コード例 #4
0
 public EagerPrimsAlgorithm(Graph graph)
 {
     this.graph    = graph;
     this.vertices = graph.Vertices;
     EdgeTo        = new int [vertices];
     distTo        = new double [vertices];
     isVisited     = new bool [vertices];
     for (int i = 0; i < distTo.Length; i++)
     {
         distTo[i] = Double.MaxValue;
     }
     distTo[0]   = 0;
     minHeap     = new IndexedPriorityQueue <Edge>(vertices);
     listOfEdges = new List <Edge>();
 }
コード例 #5
0
        public void Run()
        {
            IndexedPriorityQueue<double, int> pQ = new IndexedPriorityQueue<double, int>();
            for(int i = 0; i < vertexCount; ++i)
                pQ.Insert(i, double.MaxValue);

            pQ.Decrease_Key(source, 0);
            while (pQ.Count > 0)
            {
                KeyValuePair<double, int> kv = pQ.Extract_Minimum();
                vertexCost[kv.Value] = kv.Key;
                List<WeightedArc> edges = adjList.getEdges(kv.Value);
                foreach (WeightedArc e in edges)
                {
                    if (vertexCost[e.Vertex] > vertexCost[kv.Value] + e.Cost)
                    {
                        parent[e.Vertex] = kv.Value;
                        pQ.Decrease_Key(e.Vertex, vertexCost[kv.Value] + e.Cost);
                    }
                }
            }
        }
コード例 #6
0
ファイル: AStarSearch.cs プロジェクト: RainsSoft/UJad-AI-VFS
        /// <summary>
        /// Initializes a search
        /// </summary>
        /// <param name="source">Source node</param>
        /// <param name="target">Target node</param>
        /// <remarks>Calling this method is mandatory when using time-sliced searchs</remarks>
        public override void Initialize(int source, int target)
        {
            //Create the auxiliary lists
            fCosts           = new List <double>(graph.NodeCount);
            gCosts           = new List <double>(graph.NodeCount);
            shortestPathTree = new List <K>(graph.NodeCount);
            frontier         = new List <K>(graph.NodeCount);

            //Initialize the values
            for (int i = 0; i < graph.NodeCount; i++)
            {
                fCosts.Add(0.0);
                gCosts.Add(0.0);
                shortestPathTree.Add(default(K));
                frontier.Add(default(K));
            }

            //Initialize the indexed priority queue and enqueue the start node
            queue = new IndexedPriorityQueue <double>(fCosts);
            queue.Enqueue(source);

            //Initialize the base
            base.Initialize(source, target);
        }
コード例 #7
0
        /// <summary>
        /// Initializes a search
        /// </summary>
        /// <param name="source">Source node</param>
        /// <param name="target">Target node</param>
        /// <remarks>Calling this method is mandatory when using time-sliced searchs</remarks>
        public override void Initialize(int source, int target)
        {
            //Create the auxiliary lists
            costToNode       = new List <double>(graph.NodeCount);
            shortestPathTree = new List <K>(graph.NodeCount);
            frontier         = new List <K>(graph.NodeCount);

            //Initialize the values
            for (int i = 0; i < graph.NodeCount; i++)
            {
                costToNode.Add(Double.MaxValue);
                shortestPathTree.Add(default(K));
                frontier.Add(default(K));
            }

            costToNode[source] = 0;

            //Initialize the indexed priority queue and enqueue the first node
            queue = new IndexedPriorityQueue <double>(costToNode);
            queue.Enqueue(source);

            //Initialize the base
            base.Initialize(source, target);
        }