Dijkstra算法的一部分没有意义

时间:2014-09-17 16:45:46

标签: c# unity3d dijkstra

这是一个奇怪的查询,但我根本不需要任何代码帮助,这实际上已经有效并且很棒。我只是想了解发生了什么,我得到了大部分内容,但现在有一部分让我感到难过。

完整算法

public class DijkstraAlgorithm 
{

    public static Stack<GameObject> Dijkstra(GameObject[] Graph, GameObject source, GameObject target)
    {

        Dictionary<GameObject, float> dist = new Dictionary<GameObject, float>();
        Dictionary<GameObject, GameObject> previous = new Dictionary<GameObject, GameObject>(); //previous NODE to the node that is currently on this script
        List<GameObject> Q = new List<GameObject>();

        foreach(GameObject v in Graph)
        {
            dist[v] = Mathf.Infinity;
            //for all gameobjects (nodes) it will insert it into our dictionary as a key (kind of like a vector in c++)
            previous[v] = null;
            Q.Add (v);

        }

        dist[source] = 0;


        //up until this point we have done what?

        // Grabbed all the nodes via Graph (Graph = all the GameObjects with tag "Node")
        // Grabbed the source which is the closest node to whatever GameObject you're on
        // Grabbed the players closest node via target
        //
        //initializing dist[v] (dist is a dictionary holding a GameObject and a float, we are 
        //accessing the GameObject key of it.) to be positive infinity
        //initializing previous 'key' = GameObject v to null
        //Adding each GameObject v to Q which is a List of GameObjects
        while(Q.Count > 0)
        {
            float shortestDistance = Mathf.Infinity; //init
            GameObject shortestDistanceNode = null; //init
            foreach(GameObject obj in Q)
            {
                if(dist[obj] < shortestDistance) //very complicated if statement that basically finds the shortest distance based on whichever gameobject distance is not infinity.
                {
                    shortestDistance = dist[obj];
                    shortestDistanceNode = obj;
                }
            }

            GameObject u = shortestDistanceNode;
            Q.Remove (u);
            //
            //Up Until Here We Have Done:
            //init shortest distance to positive infinity
            // init shortestdistancenode to null for now
            //looping through list of nodes
            //if the gameobject key's value in the dist dictionary is smaller

            //Check to see if we made it to target
            if(u == target)
            {
                Stack<GameObject> S = new Stack<GameObject>();
                while(previous[u] != null)
                {
                    S.Push (u);
                    u = previous[u];
                }
                return S;
            }

            if(dist[u] == Mathf.Infinity)
            {
                break;
            }

            foreach(GameObject v in u.GetComponent<Node>().neighbors)
            {
                float alt = dist[u] + (u.transform.position - v.transform.position).magnitude;

                if (alt < dist[v])
                {
                    dist[v] = alt;
                    previous[v] = u;
                }
            }
        }
        return null;
    }
}

我感到困惑的部分是这一行

if(dist[obj] < shortestDistance) //very complicated if statement that basically finds the shortest distance based on whichever gameobject distance is not infinity.

所以它引用的GameObject键在这里被设置为Mathf.Infinity

dist[v] = Mathf.Infinity;

我没有看到我们正在改变的其他任何地方,所以这就是我感到困惑的地方。如何将distObject字典中的GameObject键浮点值小于无穷大的shortestDistance。它必须改变吗?我觉得那里有一些我没有得到的东西,因为否则这一切都是有道理的。 shortestDistance是正无穷大所以某处(没有看到哪里)但是在我们初始化为正无穷大之后,dist字典中的GameObject键的值必须改变。

0 个答案:

没有答案