首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> 關於C語言

C# 實現路徑規劃 (最短路徑) 算法 (3) — PlanCourse 中間結果表

     編輯:關於C語言

PlanCourse:管理從源節點到各節點的最小權值路徑

演算法過程中需記錄每個節點目前已知的最小權值路徑 (PassedPath),PlanCourse 封裝此邏輯:


public class PlanCourse
{
    private Hashtable _htPassedPath;

    public PlanCourse(ArrayList nodeList, string originID)
    {
        _htPassedPath = new Hashtable();
        Node originNode = null;

        foreach (Node node in nodeList)
        {
            if (node.ID == originID)
                originNode = node;
            else
            {
                var pPath = new PassedPath(node.ID);
                _htPassedPath.Add(node.ID, pPath);
            }
        }

        if (originNode == null)
            throw new Exception("起點節點不存在!");

        InitializeWeight(originNode);
    }

    private void InitializeWeight(Node originNode)
    {
        if (originNode.EdgeList == null || originNode.EdgeList.Count == 0) return;

        foreach (Edge edge in originNode.EdgeList)
        {
            var pPath = this[edge.EndNodeID];
            if (pPath == null) continue;
            pPath.PassedIDList.Add(originNode.ID);
            pPath.Weight = edge.Weight; // 直達邊的權值作為初始值
        }
    }

    public PassedPath this[string nodeID]
    {
        get { return (PassedPath)_htPassedPath[nodeID]; }
    }
}

職責

  • 建構時為所有非起點節點建立 PassedPath,初始權值 double.MaxValue。
  • 以起點直達邊初始化鄰居節點的權值與路徑。
  • 提供索引器 this[string nodeID] 快速存取節點路徑資訊。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved