基本信息
源码名称:C# 最短路径算法 实例源码下载
源码大小:0.05M
文件格式:.rar
开发语言:C#
更新时间:2016-01-20
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace PathAna
{
/// <summary>
/// RoutePlanner 提供图算法中常用的路径规划功能。
/// 2005.09.06
/// </summary>
public class RoutePlanner
{
public RoutePlanner()
{
}
#region Paln
//获取权值最小的路径
public RoutePlanResult Paln(List<Node> 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
//表示规划结束
return this.GetResult(planCourse, destID);
}
#endregion
#region private method
#region GetResult
/// <summary>
/// 从PlanCourse表中取出目标节点的PassedPath,这个PassedPath即是规划结果
/// </summary>
/// <returns></returns>
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 = new string[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
/// <summary>
/// 从PlanCourse取出一个当前累积权值最小,并且没有被处理过的节点
/// </summary>
/// <returns></returns>
private Node GetMinWeightRudeNode(PlanCourse planCourse, List<Node> 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
}
}