- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Collections;
- using System.Data;
- using System.Data.OleDb;
- namespace Dijkstra
- {
- class Program
- {
- public class initialization
- {
- public ArrayList initial(DataSet datas)
- {
- ArrayList al = new ArrayList();
- #region 初始化
- for (int i = 0; i < datas.Tables[0].Rows.Count; i++)
- {
- DataRow dr = datas.Tables[0].Rows[i];
- Node tempNode = new Node(dr["NodeName"].ToString());
- al.Add(tempNode);
- // DataTable dt = datas.Tables[1].Clone();
- string strExp = "FromNode = '" + tempNode.ID + "'";
- DataRow[] dr1 = datas.Tables[1].Select(strExp);
- if (dr1.Length > 0)
- for (int j = 0; j < dr1.Length; j++)
- {
- Edge tempEdge = new Edge();
- tempEdge.StartNodeID = tempNode.ID;
- tempEdge.EndNodeID = dr1[j]["ToNode"].ToString();
- tempEdge.Weight = (float)dr1[j]["Weight"];
- tempNode.EdgeList.Add(tempEdge);
- }
- else
- continue;
- }
- #endregion
- return al;
- }
- }
- static void Main(string[] args)
- {
- //初始化
- ArrayList nodeList = new ArrayList();
- //连接数据库
- string constring = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=F://working//NET.mdb";
- OleDbConnection con = new OleDbConnection(constring);
- OleDbDataAdapter adap = new OleDbDataAdapter();
- adap.SelectCommand = new OleDbCommand("SELECT * FROM NodeInfo", con);
- DataSet ds = new DataSet();
- adap.Fill(ds, "NodeInfo");
- adap.SelectCommand = new OleDbCommand("SELECT * FROM RoadInfo", con);
- adap.Fill(ds, "RoadInfo");
- //adap.SelectCommand = new OleDbCommand("SELECT * FROM BusLine", con);
- //adap.Fill(ds, "BusLine");
- initialization init = new initialization();
- nodeList = init.initial(ds);
- con.Close();
- Console.WriteLine("The total Node is A,B,C,D,E");
- Console.WriteLine("Please input original node");
- string origin, destination;
- origin = Console.ReadLine();
- int tag = 0;
- while (tag == 0)
- {
- for (int int1 = 0; int1 < ds.Tables[0].Rows.Count; int1++)
- {
- DataRow datarow1 = ds.Tables[0].Rows[int1];
- if (origin == datarow1["NodeName"].ToString())
- tag = 1;
- }
- if (tag == 0)
- {
- Console.WriteLine("This origin node is wrong, please input again!");
- origin = Console.ReadLine();
- }
- }
- Console.WriteLine("Please input destination node");
- destination = Console.ReadLine();
- int tag2 = 0;
- for (int int2 = 0; int2 < ds.Tables[0].Rows.Count; int2++)
- {
- DataRow datarow2 = ds.Tables[0].Rows[int2];
- if (origin == datarow2["NodeName"].ToString())
- tag2 = 1;
- }
- if (tag2 == 0)
- {
- Console.WriteLine("This destination node is wrong, please input again!");
- destination = Console.ReadLine();
- }
- RoutePlanner planner = new RoutePlanner();
- RoutePlanResult result = planner.Paln(nodeList, origin, destination);
- Console.WriteLine("最少公交站路线:");
- foreach (string node in result.passedNodeIDs)
- {
- Console.Write(node);
- Console.Write("->");
- }
- Console.WriteLine(destination);
- Console.WriteLine("经过的公交站数为: " + result.weight.ToString());
- planner = null;
- }
- public class Edge
- {
- public string StartNodeID;
- public string EndNodeID;
- public double Weight; //权值,代价
- }
- public class Node
- {
- private string iD;
- private ArrayList edgeList;//Edge的集合--出边表
- public Node(string id)
- {
- this.iD = id;
- this.edgeList = new ArrayList();
- }
- #region property
- public string ID
- {
- get
- {
- return this.iD;
- }
- }
- public ArrayList EdgeList
- {
- get
- {
- return this.edgeList;
- }
- }
- #endregion
- }
- /// <summary>
- /// PassedPath 用于暂存计算过程中的到达某个节点的权值最小的路径
- /// </summary>
- public class PassedPath
- {
- private string curNodeID;
- private bool beProcessed; //是否已被处理
- private double weight; //累积的权值
- private ArrayList passedIDList; //路径
- public PassedPath(string ID)
- {
- this.curNodeID = ID;
- this.weight = double.MaxValue;
- this.passedIDList = new ArrayList();
- this.beProcessed = false;
- }
- #region property
- public bool BeProcessed
- {
- get
- {
- return this.beProcessed;
- }
- set
- {
- this.beProcessed = value;
- }
- }
- public string CurNodeID
- {
- get
- {
- return this.curNodeID;
- }
- }
- public double Weight
- {
- get
- {
- return this.weight;
- }
- set
- {
- this.weight = value;
- }
- }
- public ArrayList PassedIDList
- {
- get
- {
- return this.passedIDList;
- }
- }
- #endregion
- }
- /// <summary>
- /// PlanCourse 暂存从起始点到其它任一节点的最小权值路径=》路径表
- /// </summary>
- public class PlanCourse
- {
- private Hashtable htPassedPath;
- #region ctor
- public PlanCourse(ArrayList 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)
- {
- throw new Exception("The origin node is not exist !");
- }
- this.InitializeWeight(originNode);
- }
- private void InitializeWeight(Node originNode)
- {
- if ((originNode.EdgeList == null) || (originNode.EdgeList.Count == 0))
- {
- return;
- }
- foreach (Edge edge in originNode.EdgeList)
- {
- PassedPath pPath = this[edge.EndNodeID];
- if (pPath == null)
- {
- continue;
- }
- pPath.PassedIDList.Add(originNode.ID);
- pPath.Weight = edge.Weight;
- }
- }
- #endregion
- public PassedPath this[string nodeID]
- {
- get
- {
- return (PassedPath)this.htPassedPath[nodeID];
- }
- }
- }
- /// <summary>
- /// RoutePlanResult
- /// </summary>
- public class RoutePlanResult
- {
- public string[] passedNodeIDs;
- public double weight;
- public RoutePlanResult(string[] strings, double d)
- {
- this.passedNodeIDs = strings;
- this.weight = d;
- }
- public string[] getPassedNodeIDs()
- {
- return passedNodeIDs;
- }
- public void setPassedNodeIDs(string[] passedNodeIDs)
- {
- this.passedNodeIDs = passedNodeIDs;
- }
- public double getWeight()
- {
- return weight;
- }
- public void setWeight(double weight)
- {
- this.weight = weight;
- }
- }
- /// <summary>
- /// RoutePlanner 提供图算法中常用的路径规划功能。
- /// 2008.10.15
- /// </summary>
- public class 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)
- {
- if (edge.EndNodeID == originID)
- continue;
- 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
- //从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 = 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
- //从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
- }
- }
- }
Dijkstra算法的最短路径实现
最新推荐文章于 2021-10-10 17:29:13 发布