当前位置:编程学习 > C#/ASP.NET >>

C#实现路径规划(最短路径)算法

以前空闲的时候用C#实现的路径规划算法,今日贴它出来,看大家有没有更好的实现方案。关于路径规划(最短路径)算法的背景知识,大家可以参考《C++算法--图算法》一书。
    该图算法描述的是这样的场景:图由节点和带有方向的边构成,每条边都有相应的权值,路径规划(最短路径)算法就是要找出从节点A到节点B的累积权值最小的路径。
    首先,我们可以将“有向边”抽象为Edge类:

    public class Edge
    {
        public string StartNodeID ;
        public string EndNodeID   ;
        public double Weight      ; //权值,代价        
    }

    节点则抽象成Node类,一个节点上挂着以此节点作为起点的“出边”表。

    public class Node
    {
        private string iD ;
        private ArrayList edgeList ;//Edge的集合--出边表

        public Node(string id )
     {
            this.iD = id ;
            this.edgeList = new ArrayList() ;
        }

        property#region property
        public string ID
     {
            get
          {
                return this.iD ;
            }
        }

        public ArrayList EdgeList
      {
            get
          {
                return this.edgeList ;
            }
        }
        #endregion
    }
 


    在计算的过程中,我们需要记录到达每一个节点权值最小的路径,这个抽象可以用PassedPath类来表示:
    /// <summary>
    /// PassedPath 用于缓存计算过程中的到达某个节点的权值最小的路径
    /// </summary>
    public class PassedPath
    {
        private string     curNodeID ;
        private bool     beProcessed ;   //是否已被处理
        private double     weight ;        //累积的权值
        private ArrayList passedIDList ; //路径

        public PassedPath(string ID)
        {
            this.curNodeID = ID ;
            this.weight    =
补充:软件开发 , C# ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,