protected void BtnDijkstra_Click(object sender, EventArgs e) { string strPointAID, strPointBID; strPointAID = DdlPointA.SelectedValue; strPointBID = DdlPointB.SelectedValue; if (strPointAID.Equals(strPointBID)) { Response.Write("距离为: 0<br />路径: " + strPointAID); return; } try { // 调包, 运行Dijkstra算法 RoutePlanner planner = new RoutePlanner(); RoutePlanResult planResult = planner.Paln(arrayNodes, strPointAID, strPointBID); if (planResult.getWeight() < 100000) { Response.Write("距离为: " + planResult.getWeight().ToString()); PrintRouteResult(planResult); Response.Write(strPointBID + "<br />"); } else { Response.Write("无路径<br />"); } planner = null; } catch (Exception Ex) { Response.Write(Ex.Message); } }
protected void Page_Load(object sender, EventArgs e) { ArrayList nodeList = new ArrayList(); //Node aNode = new Node("北京"); //nodeList.Add(aNode); //aNode.EdgeList.Add(new Edge("北京", "上海", 20)); //aNode.EdgeList.Add(new Edge("北京", "武汉", 40)); Node bNode = new Node("上海"); nodeList.Add(bNode); bNode.EdgeList.Add(new Edge("上海", "北京", 20)); bNode.EdgeList.Add(new Edge("上海", "武汉", 70)); Node cNode = new Node("武汉"); nodeList.Add(cNode); cNode.EdgeList.Add(new Edge("武汉", "北京", 40)); cNode.EdgeList.Add(new Edge("武汉", "上海", 70)); RoutePlanner planner = new RoutePlanner(); RoutePlanResult result = null; result = planner.Paln(nodeList, "武汉", "上海"); Response.Write("距离为" + result.getWeight()); printRouteResult(result); Response.Write("上海"); Response.Write("<br />"); planner = null; }
protected void Page_Load(object sender, EventArgs e) { string pointA = "北京"; string pointB = "上海"; ArrayList nodeList = new ArrayList(); Node aNode = new Node("北京"); nodeList.Add(aNode); aNode.EdgeList.Add(new Edge("北京", "上海", 20)); aNode.EdgeList.Add(new Edge("北京", "武汉", 60)); Node dNode = new Node("武汉"); nodeList.Add(dNode); dNode.EdgeList.Add(new Edge("武汉", "北京", 40)); //string output = JsonConvert.SerializeObject(nodeList); //File.WriteAllText(@Server.MapPath("~/data/ExampleSQL.json"), output); RoutePlanner planner = new RoutePlanner(); RoutePlanResult result = null; result = planner.Paln(nodeList, pointA, pointB); Response.Write("距离为" + result.getWeight()); printRouteResult(result); Response.Write(pointB); Response.Write("</br>"); planner = null; }
protected void Page_Load(object sender, EventArgs e) { ArrayList nodeList = new ArrayList(); Node a = new Node("A"); nodeList.Add(a); a = new Node("A"); nodeList.Add(a); a.EdgeList.Add(new Edge("A", "B", 1)); a = new Node("B"); nodeList.Add(a); a.EdgeList.Add(new Edge("B", "C", 1)); a = new Node("A"); nodeList.Add(a); a.EdgeList.Add(new Edge("A", "B", 1)); a = new Node("C"); nodeList.Add(a); RoutePlanner planner = new RoutePlanner(); RoutePlanResult result = null; //Paln 函数是核心函数,首先将所有节点喂给模型,然后输入起点和终点,返回最优路径 result = planner.Paln(nodeList, "A", "C"); Response.Write("距离为" + result.getWeight()); printRouteResult(result); Response.Write("C"); //Response.Write("上海"); //Response.Write("<br>"); planner = null; //***************** A Node 北京 ******************* //Node aNode = new Node("北京"); //nodeList.Add(aNode); //aNode.EdgeList.Add(new Edge("北京", "上海", 20)); //aNode.EdgeList.Add(new Edge("北京", "武汉", 40)); }
protected void Page_Load(object sender, EventArgs e) { ArrayList nodeList = new ArrayList();//结点的序列 //***************** A Node 北京 ******************* Node aNode = new Node("北京"); nodeList.Add(aNode); aNode.EdgeList.Add(new Edge("北京", "上海", 20)); aNode.EdgeList.Add(new Edge("北京", "武汉", 40)); //***************** B Node 上海******************* Node bNode = new Node("上海"); nodeList.Add(bNode); bNode.EdgeList.Add(new Edge("上海", "武汉", 70)); bNode.EdgeList.Add(new Edge("上海", "北京", 20)); //***************** D Node ******************* Node dNode = new Node("武汉"); nodeList.Add(dNode); dNode.EdgeList.Add(new Edge("武汉", "上海", 70)); dNode.EdgeList.Add(new Edge("武汉", "北京", 40)); RoutePlanner planner = new RoutePlanner(); RoutePlanResult result = null; //Paln函数是核心函数,首先将所有节点喂给模型,然后输入起点和终点,返回最优路径 result = planner.Paln(nodeList, "武汉", "上海"); Response.Write("距离为" + result.getWeight()); printRouteResult(result); Response.Write("上海"); Response.Write("<br>"); planner = null; }
protected void btnStart_Click(object sender, EventArgs e) { if (ddlStart.SelectedValue == "0" || ddlEnd.SelectedValue == "0") { Response.Write("<script>alert('Didn't Choose!');</script>"); return; } if (ddlStart.SelectedValue == ddlEnd.SelectedValue) { lblResult.Text = "There is no need to walk!"; return; } SQLHelper sh = new SQLHelper(); DataSet dsNodeList = new DataSet(); string sqlNodeList = ("SELECT [Location1] AS [Location] FROM [dbo].[TblLocation] UNION SELECT [Location2] FROM [dbo].[TblLocation]"); sh.RunSQL(sqlNodeList, ref dsNodeList); int nodeNumber = dsNodeList.Tables[0].Rows.Count; Node[] node = new Node[nodeNumber]; ArrayList nodeList = new ArrayList(); for (int i = 0; i < nodeNumber; i++) { node[i] = new Node(dsNodeList.Tables[0].Rows[i][0].ToString()); nodeList.Add(node[i]); } DataSet dsPath = new DataSet(); for (int i = 0; i < nodeNumber; i++) { string sqlNodePath = string.Format("SELECT [Location2] AS Location,[distance] FROM [dbo].[TblLocation] WHERE [Location1] = '{0}' UNION SELECT [Location1] AS Location,[distance] FROM [dbo].[TblLocation] WHERE [Location2] = '{0}' ", node[i].ID.ToString()); sh.RunSQL(sqlNodePath, ref dsPath); for (int j = 0; j < dsPath.Tables[0].Rows.Count; j++) { node[i].EdgeList.Add(new Edge(node[i].ID.ToString(), dsPath.Tables[0].Rows[j][0].ToString(), double.Parse(dsPath.Tables[0].Rows[j][1].ToString()))); } dsPath = null; } sh.Close(); sh = null; RoutePlanner planner = new RoutePlanner(); RoutePlanResult result = planner.Paln(nodeList, ddlStart.SelectedValue, ddlEnd.SelectedValue); lblResult.Text = "Distance:" + result.getWeight().ToString() + " Route:" + DetailedRoute(result); planner = null; }
protected void Button1_Click(object sender, EventArgs e) { RoutePlanner planner = new RoutePlanner(); RoutePlanResult result = null; result = planner.Paln(nodeList, ddlStart.SelectedValue, ddlEnd.SelectedValue); //当路径的权值返回是double的最大值,就认为,两点间的距离是正无穷,也就是不通的 double aim = Math.Abs(result.getWeight() - double.MaxValue); string finalpath = string.Format("{0}到{1}的路径是:", ddlStart.SelectedValue, ddlEnd.SelectedValue); Response.Write(finalpath); if (aim <= 0) { Response.Write("距离为正无穷"); } else { Response.Write("距离为" + result.getWeight()); //输出路径 printRouteResult(result); } planner = null; }
protected void Button1_Click(object sender, EventArgs e) { string pointA = ddlPointA.SelectedItem.Text; string pointB = ddlPointB.SelectedItem.Text; if (pointA == pointB) { Response.Write("不能起始地为同一处!"); } else { //生成图 ArrayList nodeList = new ArrayList(); string sql = "select PointA from tblDistance2020 group by PointA"; SQLHelper sh1 = new SQLHelper(); DataTable dt = new DataTable(); DataSet ds = new DataSet(); try { sh1.RunSQL(sql, ref ds); if (ds.Tables[0] != null) { dt = ds.Tables[0]; if (dt.Rows.Count > 0) { foreach (DataRow dr in dt.Rows) { Node node = new Node(dr[0].ToString()); nodeList.Add(node); string sql2 = string.Format("select * from tblDistance2020 where PointA='{0}'", dr[0].ToString()); SQLHelper sh2 = new SQLHelper(); DataTable dt2 = new DataTable(); DataSet ds2 = new DataSet(); sh2.RunSQL(sql2, ref ds2); if (ds2.Tables[0] != null) { dt2 = ds2.Tables[0]; if (dt2.Rows.Count > 0) { foreach (DataRow dr2 in dt2.Rows) { node.EdgeList.Add(new Edge(dr2[1].ToString(), dr2[2].ToString(), double.Parse(dr2[3].ToString()))); } } } sh2.Close(); } } } } catch (Exception ex) { Response.Write(ex.Message); } finally { sh1.Close(); } RoutePlanner planner = new RoutePlanner(); RoutePlanResult result = null; try { result = planner.Paln(nodeList, pointA, pointB); Response.Write("距离为" + result.getWeight()); printRouteResult(result); Response.Write(pointB); Response.Write("</br>"); planner = null; } catch { Response.Write("两地不能到达!"); } } }
protected void Button1_Click(object sender, EventArgs e) { //需要四个sql,sh,ds,dt,row string sqla1, sqla2, sqlb1, sqlb2; string ca = ""; string cb = ""; string start = ddl1.SelectedValue; string end = ddl2.SelectedValue; SQLHelper sha1 = new SQLHelper(); SQLHelper sha2 = new SQLHelper(); SQLHelper shb1 = new SQLHelper(); SQLHelper shb2 = new SQLHelper(); DataSet dsa1 = new DataSet(); DataSet dsb1 = new DataSet(); DataTable dta1, dta2, dtb1, dtb2; DataRow[] ra1, ra2, rb1, rb2; ArrayList nodeList = new ArrayList(); //思路:遍历A,对A的每一个城市,遍历所有路径;然后遍历B中不在A中的城市,同样遍历路径 //介于debug次数过多,把快捷的debug方法写在这里提醒自己:通过response.write监视nodelist的录入 sqla1 = "select PointA from students.dbo.tblDistance group by PointA"; sha1.RunSQL(sqla1, ref dsa1); dta1 = dsa1.Tables[0]; ra1 = dta1.Select("PointA is not null"); sha1.Close(); for (int i = 0; i <= dta1.Rows.Count - 1; i++) { ca = ra1[i]["PointA"].ToString(); Node a = new Node(ca); nodeList.Add(a); DataSet dsa2 = new DataSet(); //dsa2、dsb2一定要在for循环内反复声明,以清空set内存;否则,新ref到的数据会加入到Tables[0]中而非替换之 sqla2 = string.Format("select PointA,PointB,Distance from students.dbo.tblDistance where PointA='{0}' group by PointA,PointB,Distance", ca); sha2.RunSQL(sqla2, ref dsa2); dta2 = dsa2.Tables[0]; ra2 = dta2.Select("PointA is not null"); sha2.Close(); for (int j = 0; j <= dta2.Rows.Count - 1; j++) { cb = ra2[j]["PointB"].ToString(); a.EdgeList.Add(new Edge(ca, cb, double.Parse(ra2[j]["Distance"].ToString()))); } } sqlb1 = "select PointB from (select PointB from students.dbo.tblDistance group by PointB)B left join (select PointA from students.dbo.tblDistance group by PointA)A on A.PointA=B.PointB where PointA is null"; shb1.RunSQL(sqlb1, ref dsb1); dtb1 = dsb1.Tables[0]; rb1 = dtb1.Select("PointB is not null"); shb1.Close(); for (int i = 0; i <= dtb1.Rows.Count - 1; i++) { cb = rb1[i]["PointB"].ToString(); DataSet dsb2 = new DataSet(); sqlb2 = string.Format("select PointA,PointB,Distance from students.dbo.tblDistance where PointB='{0}' group by PointA,PointB,Distance", cb); shb2.RunSQL(sqlb2, ref dsb2); dtb2 = dsb2.Tables[0]; rb2 = dtb2.Select("PointB is not null"); shb2.Close(); Node b = new Node(cb); nodeList.Add(b); for (int j = 0; j <= dtb2.Rows.Count - 1; j++) { ca = rb2[j]["PointA"].ToString(); b.EdgeList.Add(new Edge(cb, ca, double.Parse(rb2[j]["Distance"].ToString()))); } } RoutePlanner planner = new RoutePlanner(); RoutePlanResult result = null; if (start == end) { Response.Write("距离为0"); } else { result = planner.Paln(nodeList, start, end); if (result.getWeight() > 100000000) { Response.Write("两地无通路!"); } else { Response.Write("距离为" + result.getWeight()); printRouteResult(result); Response.Write(end + "</br>"); planner = null; } } }