1. 程式人生 > >路徑規劃(最短路徑)演算法C#實現

路徑規劃(最短路徑)演算法C#實現

    ///<summary>/// RoutePlanner 提供圖演算法中常用的路徑規劃功能。
    
/// 2005.09.06
    
///</summary>publicclass RoutePlanner
    {
        
public RoutePlanner()
        {            
        }

        
#region Paln//獲取權值最小的路徑public RoutePlanResult Paln(ArrayList nodeList ,string originID ,string destID)
        {
            PlanCourse planCourse 
=new PlanCourse(nodeList ,originID) ;

            Node curNode 
=this.GetMinWeightRudeNode(planCourse ,nodeList ,originID) ;

            
#region 計算過程while(curNode !=null)
            {
                PassedPath curPath 
= planCourse[curNode.ID] ;
                
foreach(Edge edge in curNode.EdgeList)
                {
                    PassedPath targetPath 
= planCourse[edge.EndNodeID] ;
                    
double tempWeight = curPath.Weight + edge.Weight ;

                    
if(tempWeight < targetPath.Weight)
                    {
                        targetPath.Weight 
= tempWeight ;
                        targetPath.PassedIDList.Clear() ;

                        
for(int i=0 ;i<curPath.PassedIDList.Count ;i++)
                        {
                            targetPath.PassedIDList.Add(curPath.PassedIDList[i].ToString()) ;
                        }

                        targetPath.PassedIDList.Add(curNode.ID) ;
                    }
                }

                
//標誌為已處理                planCourse[curNode.ID].BeProcessed =true ;
                
//獲取下一個未處理節點                curNode =this.GetMinWeightRudeNode(planCourse ,nodeList ,originID) ;
            }
            
#endregion//表示規劃結束returnthis.GetResult(planCourse ,destID) ;                
        }
        
#endregion#region private method#region GetResult//從PlanCourse表中取出目標節點的PassedPath,這個PassedPath即是規劃結果private RoutePlanResult GetResult(PlanCourse planCourse ,string destID)
        {
            PassedPath pPath 
= planCourse[destID]  ;            

            
if(pPath.Weight ==int.MaxValue)
            {
                RoutePlanResult result1 
=new RoutePlanResult(null ,int.MaxValue) ;
                
return result1 ;
            }
            
            
string[] passedNodeIDs =newstring[pPath.PassedIDList.Count] ;
            
for(int i=0 ;i<passedNodeIDs.Length ;i++)
            {
                passedNodeIDs[i] 
= pPath.PassedIDList[i].ToString() ;
            }
            RoutePlanResult result 
=new RoutePlanResult(passedNodeIDs ,pPath.Weight) ;

            
return result ;            
        }
        
#endregion#region GetMinWeightRudeNode//從PlanCourse取出一個當前累積權值最小,並且沒有被處理過的節點private Node GetMinWeightRudeNode(PlanCourse planCourse ,ArrayList nodeList ,string originID)
        {
            
double weight =double.MaxValue ;
            Node destNode 
=null ;

            
foreach(Node node in nodeList)
            {
                
if(node.ID == originID)
                {
                    
continue ;
                }

                PassedPath pPath 
= planCourse[node.ID] ;
                
if(pPath.BeProcessed)
                {
                    
continue ;
                }

                
if(pPath.Weight < weight)
                {
                    weight 
= pPath.Weight ;
                    destNode 
= node ;
                }
            }

            
return destNode ;
        }
        
#endregion#endregion
    }