演算法過程中需記錄每個節點目前已知的最小權值路徑 (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] 快速存取節點路徑資訊。