以C#实现最短路径算法

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

//边的定义如下:

public class Edge

{

public string StartNodeID;

public string EndNodeID;

public double Weight; //权值,代价

}

//节点的定义:

public class Node

{

private string iD ;

private List edgeList ;//Edge的集合--出边表

public Node(string id )

{

this.iD = id ;

this.edgeList = new List() ;

}

#region property

public string ID

{

get

{

return this.iD ;

}

}

public List EdgeList

{

get

{

return this.edgeList ;

}

}

#endregion

}

public class Graph

{

public List m_nodeList = null;

public Graph()

{

m_nodeList = new List();

}

///

///获取图的节点集合

///

public List NodeList

{

get { return this.m_nodeList; }

}

///

///初始化拓扑图

///

public void Init()

{

//***************** A Node ******************* Node aNode = new Node("A");

m_nodeList.Add(aNode);

//A -> B

Edge aEdge1 = new Edge();

aEdge1.StartNodeID = aNode.ID;

aEdge1.EndNodeID = "B";

aEdge1.Weight = 10;

aNode.EdgeList.Add(aEdge1);

//A -> C

Edge aEdge2 = new Edge();

aEdge2.StartNodeID = aNode.ID;

aEdge2.EndNodeID = "C";

aEdge2.Weight = 20;

aNode.EdgeList.Add(aEdge2);

//A -> E

Edge aEdge3 = new Edge();

aEdge3.StartNodeID = aNode.ID;

aEdge3.EndNodeID = "E";

aEdge3.Weight = 30;

aNode.EdgeList.Add(aEdge3);

//***************** B Node ******************* Node bNode = new Node("B");

m_nodeList.Add(bNode);

//B -> C

Edge bEdge1 = new Edge();

bEdge1.StartNodeID = bNode.ID;

bEdge1.EndNodeID = "C";

bEdge1.Weight = 5;

bNode.EdgeList.Add(bEdge1);

//B -> E

Edge bEdge2 = new Edge();

bEdge2.StartNodeID = bNode.ID;

bEdge2.EndNodeID = "E";

bEdge2.Weight = 10;

bNode.EdgeList.Add(bEdge2);

//***************** C Node ******************* Node cNode = new Node("C");

m_nodeList.Add(cNode);

//C -> D

Edge cEdge1 = new Edge();

cEdge1.StartNodeID = cNode.ID;

cEdge1.EndNodeID = "D";

cEdge1.Weight = 30;

cNode.EdgeList.Add(cEdge1);

//***************** D Node *******************

Node dNode = new Node("D");

m_nodeList.Add(dNode);

//***************** E Node *******************

Node eNode = new Node("E");

m_nodeList.Add(eNode);

//E -> D

Edge eEdge1 = new Edge();

eEdge1.StartNodeID = eNode.ID;

eEdge1.EndNodeID = "D";

eEdge1.Weight = 20;

eNode.EdgeList.Add(eEdge1);

}

}

///

/// PlanCourse 缓存从源节点到其它任一节点的最小权值路径(路径表)///

public class PlanCourse

{

private Hashtable htPassedPath;

#region ctor

public PlanCourse(List nodeList, string originID) {

this.htPassedPath = new Hashtable();

Node originNode = null;

foreach (Node node in nodeList)

{

if (node.ID == originID)

{

originNode = node;

}

else

{

PassedPath pPath = new PassedPath(node.ID);

this.htPassedPath.Add(node.ID, pPath);

}

}

if (originNode == null)

相关文档
最新文档