private void ShowRouteGreedy(int from, int to) { Thailand.Search.RouteInfo route = Thailand.Search.Greedy.FindRoute(from, to); Length = Convert.ToString(route.Length); Thread thread = new Thread((start) => ShowRoute(route)); thread.Start(); }
public static RouteInfo FindRoute(int start, int end) { //list of visited vertexes ArrayList closed = new ArrayList(); //priority queue of not visited vertexes PQueue open = new PQueue(); //resulted route RouteInfo route = new RouteInfo(); route.Towns = new System.Collections.Generic.List <int>(); route.Towns.Add(start); //put all start vertex neighbours to the open collection //put the start to the closed closed.Add(start); for (int i = 0; i < RoutesManager.Length; i++) { if (RoutesManager.AdjacencyMatrix[start, i] != 0) { VertexInfo toPush = new VertexInfo(); toPush.Distance = RoutesManager.AdjacencyMatrix[start, i]; toPush.Priority = RoutesManager.EvristicMatrix[i, end] + toPush.Distance; toPush.Town = i; //push to the queue open.Push(toPush); } } while (open.Capacity > 0) { //get the lowest evristic value var current = open.Pop(); //the route has already found if (current.Town == end) { //add the last node route.Towns.Add(current.Town); break; } //add town to the closed list closed.Add(current.Town); //new vertex to add to the open list VertexInfo toPush = new VertexInfo(); //discovet the neighbours //that is not in closed list for (int i = 0; i < RoutesManager.Length; i++) { if (RoutesManager.AdjacencyMatrix[current.Town, i] != 0 && !closed.Contains(i)) { int tentative = RoutesManager.AdjacencyMatrix[current.Town, i] + current.Distance; if (tentative > RoutesManager.AdjacencyMatrix[start, i]) { //add distances to the current node and from current to i toPush.Distance += RoutesManager.AdjacencyMatrix[current.Town, i]; toPush.Distance += current.Distance; toPush.Priority = RoutesManager.EvristicMatrix[i, end]; toPush.Town = i; //push to the queue open.Push(toPush); } } } route.Towns.Add(current.Town); } //calculate length for (int i = 0; i < route.Towns.Count - 1; i++) { route.Length += RoutesManager.AdjacencyMatrix[route.Towns[i], route.Towns[i + 1]]; } return(route); }