public Dijkstra(List <Route> _routes, List <RouteNode> _routenodes, RouteNode _from) { this.routes = _routes; this.routenodes = _routenodes; this.from = _from; this.from_ind = routenodes.FindIndex(s => s.id == from.id); this.CreateDistanceMatrix(); foreach (RouteNode rn in routenodes) { rn.label = 0; rn.labeltype = false; } }
// Kiválasztjuk a legkisebb címkével rendelkező csúcsot, és címkéjét állandóvá tesszük... private RouteNode GetNextVertex() { int min = int.MaxValue; RouteNode nextvertex = new RouteNode(); foreach (var rn in routenodes.Where(rn => rn.labeltype == false && rn.label != 0)) { if (rn.label <= min) { min = rn.label; nextvertex = rn; } } nextvertex.labeltype = true; return(nextvertex); }
public void calculateClosestNode() { if (route.direction == "uni") { closestNode = route.node_1; nextNode = route.node_2; } else if (route.direction == "1_2") { closestNode = route.node_1; nextNode = route.node_2; } else if (route.direction == "2_1") { closestNode = route.node_2; nextNode = route.node_1; } }
public int GetDistance(RouteNode to) { shortestPath = new List <RouteNode>(); List <Route> connectingRoutes = new List <Route>(); List <RouteNode> adjacentNodes = new List <RouteNode>(); int prevNodeIndex, currNodeIndex, pathLength; RouteNode final = to; pathLength = 0; List <NodeDistance> fromList = new List <NodeDistance>(); foreach (NodeDistance nd in distancematrix) { if ((nd.to == to.id || nd.from == to.id) && nd.length != 0 && nd.length != int.MaxValue) { fromList.Add(nd); } } while (from != to) { // Megkeressük annak a RouteNode-nak a szomszédos node-jait, amelyikben éppen tartózkodunk foreach (NodeDistance nd in distancematrix) { if (nd.to == to.id && nd.length != 0 && nd.length < int.MaxValue) // Csak a szomszédos nodeok között keresünk { prevNodeIndex = routenodes.FindIndex(s => s.id == nd.from); currNodeIndex = routenodes.FindIndex(s => s.id == to.id); if ((to.label - routenodes[prevNodeIndex].label) == distancematrix[prevNodeIndex, currNodeIndex].length) { shortestPath.Add(routenodes[prevNodeIndex]); pathLength += nd.length; to = routenodes[prevNodeIndex]; } } } } shortestPath.Reverse(); shortestPath.Add(final); return(pathLength); }
public void CalculateDijkstra() { Init(); while (routenodes.Count(s => s.labeltype == true) != routenodes.Count) { RouteNode u = GetNextVertex(); foreach (var v in routenodes.Where(rn => rn.labeltype == false)) { int u_ind = routenodes.FindIndex(s => s.id == u.id); int v_ind = routenodes.FindIndex(s => s.id == v.id); int u_v = distancematrix[u_ind, v_ind].length; //Megnézzük, hogy a kiválasztott következő node szomszédos-e a mostanival if (u_v > 0 && u_v < int.MaxValue) { if (v.label > u.label + u_v) { v.label = u.label + u_v; } } } } }
public void CalculateXY(RouteNode node_1, RouteNode node_2) { this.x_coord = ((int.Parse(node_1.x_coord) + int.Parse(node_2.x_coord)) / 2).ToString(); this.y_coord = ((int.Parse(node_1.y_coord) + int.Parse(node_2.y_coord)) / 2).ToString(); }