private DejkstraAlgorim startNewDejkster(int pointId) { Point[] points = new Point[Size * Size]; for (int i = 0; i < Size; i++) { for (int j = 0; j < Size; j++) { points[i * Size + j] = new Point(9999, false, "[" + i.ToString() + "," + j.ToString() + "]", i * Size + j); } } int halfEdgeSize = (Size - 1) * Size; Edge[] edges = new Edge[halfEdgeSize * 2]; Random rand = new Random(0); for (int i = 0; i < (Size - 1); i++) { for (int j = 0; j < Size; j++) { edges[i * (Size) + j] = new Edge(points[i * Size + j], points[(i + 1) * Size + j], rand.Next(20)); edges[halfEdgeSize + i * (Size) + j] = new Edge(points[j * (Size) + i], points[j * (Size) + i + 1], rand.Next(20)); } } DejkstraAlgorim da = new DejkstraAlgorim(points, edges); da.AlgoritmRun(points[pointId]); calculatedDejksters.Add(pointId, da); return(da); }
public Route getRoute(int p1, int p2) { int[] idxs; if (calculatedDejksters.ContainsKey(p1)) { DejkstraAlgorim alg = calculatedDejksters[p1]; idxs = pathToArray(alg.MinPath1(alg.points[p2])); idxs = idxs.Reverse().ToArray <int>(); } else if (calculatedDejksters.ContainsKey(p2)) { DejkstraAlgorim alg = calculatedDejksters[p2]; idxs = pathToArray(alg.MinPath1(alg.points[p1])); } else { DejkstraAlgorim alg = startNewDejkster(p1); idxs = pathToArray(alg.MinPath1(alg.points[p2])); idxs = idxs.Reverse().ToArray <int>(); } Route r = new Route(idxs); return(r); }
public static List <string> PrintAllPoints(DejkstraAlgorim da) { List <string> retListOfPoints = new List <string>(); foreach (Point p in da.points) { retListOfPoints.Add(string.Format("point name={0}, point value={1}, predok={2}", p.Name, p.ValueMetka, p.predPoint.Name ?? "нет предка")); } return(retListOfPoints); }
private void printDejkster(DejkstraAlgorim da) { foreach (string s in PrintGraph.PrintAllMinPaths(da)) { Console.WriteLine(s); } Console.WriteLine(); foreach (string s in PrintGraph.PrintAllPoints(da)) { Console.WriteLine(s); } }
public int getDist(int p1, int p2) { if (calculatedDejksters.ContainsKey(p1)) { DejkstraAlgorim alg = calculatedDejksters[p1]; return((int)alg.points[p2].ValueMetka); } else if (calculatedDejksters.ContainsKey(p2)) { DejkstraAlgorim alg = calculatedDejksters[p2]; return((int)alg.points[p1].ValueMetka); } else { DejkstraAlgorim alg = startNewDejkster(p1); return((int)alg.points[p2].ValueMetka); } }
public static List <string> PrintAllMinPaths(DejkstraAlgorim da) { List <string> retListOfPointsAndPaths = new List <string>(); foreach (Point p in da.points) { if (p != da.BeginPoint) { string s = string.Empty; foreach (Point p1 in da.MinPath1(p)) { s += string.Format("{0} ", p1.Name); } retListOfPointsAndPaths.Add(string.Format("Point ={0},MinPath from {1} = {2}", p.Name, da.BeginPoint.Name, s)); } } return(retListOfPointsAndPaths); }