Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 迪克斯特拉';给出错误结果的s算法实现_C#_Algorithm_Dijkstra - Fatal编程技术网

C# 迪克斯特拉';给出错误结果的s算法实现

C# 迪克斯特拉';给出错误结果的s算法实现,c#,algorithm,dijkstra,C#,Algorithm,Dijkstra,我需要一些帮助来实现Dijkstra的算法,希望有人能帮助我。我有它,所以它正在打印一些路线,但它没有捕获路径的正确成本 以下是我的节点结构: class Node { public enum Color {White, Gray, Black}; public string Name { get; set; } //city public List<NeighborNode> Neighbors { get; set;

我需要一些帮助来实现Dijkstra的算法,希望有人能帮助我。我有它,所以它正在打印一些路线,但它没有捕获路径的正确成本

以下是我的节点结构:

   class Node
    {
        public enum Color {White, Gray, Black};
        public string Name { get; set; } //city
        public List<NeighborNode> Neighbors { get; set; } //Connected Edges
        public Color nodeColor = Color.White;
        public int timeDiscover { get; set; }//discover time
        public int timeFinish { get; set; } // finish time

        public Node() 
        { 
            Neighbors = new List<NeighborNode>();
        }
        public Node(string n, int discover)
        {
            Neighbors = new List<NeighborNode>();
            this.Name = n;
            timeDiscover = discover;
        }


        public Node(string n, NeighborNode e, decimal m)
        {
            Neighbors = new List<NeighborNode>();
            this.Name = n;
            this.Neighbors.Add(e);
        }

    }

    class NeighborNode
    {
        public Node Name { get; set; }
        public decimal Miles { get; set; } //Track the miles on the neighbor node

        public NeighborNode() { }
        public NeighborNode(Node n, decimal m)
        {
            Name = n;
            Miles = m;
        }

    }
类节点
{
公共枚举颜色{白色、灰色、黑色};
公共字符串名称{get;set;}//city
公共列表邻居{get;set;}//连通边
公共颜色nodeColor=Color.White;
公共int-timeDiscover{get;set;}//发现时间
public int timeFinish{get;set;}//完成时间
公共节点()
{ 
邻居=新列表();
}
公共节点(字符串n,int discover)
{
邻居=新列表();
this.Name=n;
时间发现=发现;
}
公共节点(字符串n,相邻节点e,十进制m)
{
邻居=新列表();
this.Name=n;
这个。邻居。添加(e);
}
}
类邻接节点
{
公共节点名称{get;set;}
公共十进制英里数{get;set;}//跟踪邻居节点上的英里数
公共邻居节点(){}
公共邻居节点(节点n,十进制m)
{
Name=n;
英里=米;
}
}
这是我的算法:

   public void DijkstraAlgorithm(List<Node> graph)
    {

        List<DA> _algorithmList = new List<DA>(); //track the node cost/positioning
        Stack<Node> _allCities = new Stack<Node>(); // add all cities into this for examination
        Node _nodeToExamine = new Node(); //this is the node we're currently looking at.
        decimal _cost = 0;

        foreach (var city in graph) // putting these onto a stack for easy manipulation. Probably could have just made this a stack to start
        {
            _allCities.Push(city);
            _algorithmList.Add(new DA(city));
        }

        _nodeToExamine = _allCities.Pop(); //pop off the first node

        while (_allCities.Count != 0) // loop through each city
        {

            foreach (var neighbor in _nodeToExamine.Neighbors) //loop through each neighbor of the node
            {
                for (int i = 0; i < _algorithmList.Count; i++) //search the alorithm list for the current neighbor node
                {
                    if (_algorithmList[i].Name.Name == neighbor.Name.Name) //found it
                    {
                        for (int j = 0; j < _algorithmList.Count; j++) //check for the cost of the parent node
                        {
                            if (_algorithmList[j].Name.Name == _nodeToExamine.Name) //looping through
                            {
                                if (_algorithmList[j].Cost != 100000000) //not infinity
                                    _cost = _algorithmList[j].Cost; //set the cost to be the parent cost

                                break;
                            }
                        }
                        _cost = _cost + neighbor.Miles;

                        if (_algorithmList[i].Cost > _cost) // check to make sure the miles are less (better path)
                        {
                            _algorithmList[i].Parent = _nodeToExamine; //set the parent to be the top node
                            _algorithmList[i].Cost = _cost; // set the weight to be correct
                            break;
                        }
                    }
                }

            }
            _cost = 0;
            _nodeToExamine = _allCities.Pop();
        }
    }
public void dijkstra算法(列表图)
{
List _algorithmList=new List();//跟踪节点成本/定位
Stack _allCities=new Stack();//将所有城市添加到此中进行检查
Node _nodetoexecast=new Node();//这是我们当前正在查看的节点。
十进制成本=0;
foreach(图中的var city)//将它们放在一个堆栈上以便于操作。可能刚开始就可以将其作为一个堆栈
{
_所有城市。推动(城市);
_添加(新DA(城市));
}
_NodeToInspect=\u allCities.Pop();//弹出第一个节点
while(_allCities.Count!=0)//遍历每个城市
{
foreach(var nextor in _nodetoexecast.nextries)//循环遍历节点的每个邻居
{
for(int i=0;i<\u algorithmList.Count;i++)//搜索当前邻居节点的算术列表
{
if(_algorithmList[i].Name.Name==neighbor.Name.Name)//找到了它
{
for(int j=0;j<\u algorithmList.Count;j++)//检查父节点的开销
{
if(_algorithmList[j].Name.Name==_nodetoexecast.Name)//循环通过
{
if(_algorithmList[j].Cost!=100000000)//不是无穷大
_成本=_algorithmList[j].cost;//将成本设置为父成本
打破
}
}
_成本=_成本+邻居。英里;
if(_algorithmList[i].Cost>_Cost)//检查以确保英里数更少(更好的路径)
{
_algorithmList[i].Parent=\u nodeToCheck;//将父节点设置为顶部节点
_algorithmList[i].Cost=\u Cost;//设置正确的权重
打破
}
}
}
}
_成本=0;
_nodeToInspect=_allCities.Pop();
}
}
这是图形的外观:

图形列表节点本质上是

节点——邻居节点

例如:

节点=奥林匹亚,相邻节点=莱西和塔科马


我认为问题在于

\u成本=\u算法列表[j]。成本//将成本设置为父成本

您直接分配成本,而不是添加新旧成本

还有,事实上你

if(_algorithmList[j].成本!=100000000)//非无穷大

在这之前,它意味着如果路径的成本是无穷大,那么你会做相反的事情——你将路径的成本加上零,使它成为成本最低的路径,而不是成本最高的路径


如果要正确地检查无穷大,在检查其成本时必须完全跳过该路径,而不仅仅是跳过计算成本。

我需要重写整个算法,因为它处理不正确:

    public void DijkstraAlgorithm(List<Node> graph)
    {

        List<DA> _algorithmList = new List<DA>(); //track the node cost/positioning
        DA _nodeToExamine = new DA(); //this is the node we're currently looking at.
        bool flag = true; //for exting the while loop later

        foreach (var node in graph)
        {
            _algorithmList.Add(new DA(node));
        }

        foreach (var children in _algorithmList[0].Name.Neighbors) //just starting at the first node
        {
            for (int i = 0; i < _algorithmList.Count; i++)
            {
                if (children.Name == _algorithmList[i].Name)
                {
                    _algorithmList[i].Parent = _algorithmList[0].Name;
                    _algorithmList[i].Cost = children.Miles;
                    _algorithmList[0].Complete = true;

                }
            }
        }

        while (flag) //loop through the rest to organize
        {
            _algorithmList = _algorithmList.OrderBy(x => x.Cost).ToList(); //sort by shortest path

            for (int i = 0; i < _algorithmList.Count; i++) //loop through each looking for a node that isn't complete
            {
                if (_algorithmList[i].Complete == false)
                {
                    _nodeToExamine = _algorithmList[i];
                    break;
                }
                if (i == 13) //if the counter reaches 13 then we have completed all nodes and should bail out of the loop
                    flag = false;
            }
            if (_nodeToExamine.Name.Neighbors.Count == 0) //set any nodes that do not have children to be complete
            {
                _nodeToExamine.Complete = true;
            }

            foreach (var children in _nodeToExamine.Name.Neighbors) //loop through the children/neighbors to see if there's one with a shorter path
            {
                for (int i = 0; i < _algorithmList.Count; i++) 
                {
                    if (children.Name == _algorithmList[i].Name)
                    {
                        if (_nodeToExamine.Cost + children.Miles < _algorithmList[i].Cost) //found a better path
                        {
                            _algorithmList[i].Parent = _nodeToExamine.Name;
                            _algorithmList[i].Cost = _nodeToExamine.Cost + children.Miles;
                        }
                    }
                }
                _nodeToExamine.Complete = true;
            }
        }

        PrintDijkstraAlgoirthm(_algorithmList);
    }


    public void PrintDijkstraAlgoirthm(List<DA> _finalList)
    {
        foreach (var item in _finalList)
        {
           if (item.Parent != null)
                Console.WriteLine("{0} ---> {1}: {2}", item.Parent.Name, item.Name.Name, item.Cost);
        }
    }
public void dijkstra算法(列表图)
{
List _algorithmList=new List();//跟踪节点成本/定位
DA _nodetoexecast=new DA();//这是我们当前正在查看的节点。
bool flag=true;//用于稍后取消while循环
foreach(图中的var节点)
{
_添加(新的DA(节点));
}
foreach(在_algorithmList[0].Name.neights中的var子节点)//仅从第一个节点开始
{
对于(int i=0;i<\u algorithmList.Count;i++)
{
if(children.Name==\u algorithmList[i].Name)
{
_algorithmList[i]。父项=_algorithmList[0]。名称;
_algorithmList[i].Cost=children.Miles;
_algorithmList[0]。Complete=true;
}
}
}
while(flag)//循环其余部分以进行组织
{
_algorithmList=_algorithmList.OrderBy(x=>x.Cost).ToList();//按最短路径排序
for(int i=0;i<\u algorithmList.Count;i++)//循环遍历每个节点,查找未完成的节点
{
if(_algorithmList[i].Complete==false)
{
_nodetoexecast=_算法列表[i];
打破
}