public static void initTraverseDijkstra(int sourceGraphIndex, int destinationGraphIndex) { List <int> visitedIndex; int[,] distanceTable; visitedIndex = new List <int>(); distanceTable = new int[size, 2];//the first column will have the distance of that node from the starting node, the second column is the index of the node that came before it. for (int i = 0; i < size; i++) { distanceTable[i, 0] = int.MaxValue; distanceTable[i, 1] = -1; } distanceTable[sourceGraphIndex, 0] = 0; //Distance from the starting station from the starting station is 0, thus setting it. int currentNodeIndex = sourceGraphIndex; //while (visitedIndex.Count < size) //{ // currentNodeIndex = TraverseDijkstra(currentNodeIndex); //} TraverseDijkstraRecursive(distanceTable, visitedIndex, currentNodeIndex); for (int i = 0; i < size; i++) { Console.WriteLine("{0} - Distance:{1} - Comes From:{2}", i, distanceTable[i, 0], distanceTable[i, 1]); } List <int> routeGraphIndex = new List <int>() { destinationGraphIndex }; int currentIndex = destinationGraphIndex; while (currentIndex != sourceGraphIndex) { currentIndex = distanceTable[currentIndex, 1]; routeGraphIndex.Add(currentIndex); } routeGraphIndex.Reverse(); List <Station> routeStation = new List <Station>(); foreach (int gI in routeGraphIndex) { routeStation.Add(SearchByStationGraphIndex(gI)); } //int startIndex = -1; //int endIndex = -1; //string lineCd = ""; //int lineCdIdx = -1; //int nextLineCdIdx = -1; //int nextStartIndex = -1; //for (int idx = 0; idx < routeStation.Count; idx++) //{ // bool matchFound = false; // if (idx == 0) // { // if (routeStation[idx].IsInterchange) // { // List<string> stationLineCd = new List<string>(); // foreach(string statCd in routeStation[idx].StationCode) // { // stationLineCd.Add(statCd.Substring(0, 2)); // } // foreach(string statCd in routeStation[1].StationCode) // { // if (stationLineCd.Contains(statCd.Substring(0, 2))) // { // lineCd = statCd.Substring(0, 2); // } // } // lineCdIdx = Guide.GetIndexOfLine(lineCd); // } // else // { // lineCdIdx = Guide.GetIndexOfLine(routeStation[idx].StationCode[0].Substring(0, 2)); // } // startIndex = Guide.GetStationIndexFromLine(lineCdIdx, routeStation[idx].StationName); // } // else if (idx == (routeStation.Count - 1)) // { // if (routeStation[idx].IsInterchange) // { // List<string> stationLineCd = new List<string>(); // foreach (string statCd in routeStation[idx].StationCode) // { // stationLineCd.Add(statCd.Substring(0, 2)); // } // foreach (string statCd in routeStation[routeStation.Count - 2].StationCode) // { // if (stationLineCd.Contains(statCd.Substring(0, 2))) // { // lineCd = statCd.Substring(0, 2); // } // } // lineCdIdx = Guide.GetIndexOfLine(lineCd); // } // else // { // lineCdIdx = Guide.GetIndexOfLine(routeStation[idx].StationCode[0].Substring(0, 2)); // } // endIndex = Guide.GetStationIndexFromLine(lineCdIdx, routeStation[idx].StationName); // } // else // { // if (routeStation[idx].IsInterchange) // { // List<string> statLineCd = new List<string>(); // foreach (string statCd in routeStation[idx-1].StationCode) // { // statLineCd.Add(statCd.Substring(0, 2)); // } // foreach (string statCd in routeStation[idx+1].StationCode) // { // bool contains = statLineCd.Contains(statCd.Substring(0, 2)); // if (contains) // { // matchFound = true; // } // } // if (!matchFound) // { // endIndex = Guide.GetStationIndexFromLine(lineCdIdx,routeStation[idx].StationName); // List<string> stationLineCd = new List<string>(); // foreach (string statCd in routeStation[idx].StationCode) // { // stationLineCd.Add(statCd.Substring(0, 2)); // } // foreach (string statCd in routeStation[idx+1].StationCode) // { // if (stationLineCd.Contains(statCd.Substring(0, 2))) // { // lineCd = statCd.Substring(0, 2); // } // } // nextLineCdIdx = Guide.GetIndexOfLine(lineCd); // nextStartIndex = Guide.GetStationIndexFromLine(nextLineCdIdx, routeStation[idx].StationName); // } // } // } // if (matchFound) // { // continue; // } // if ((startIndex != -1) &&(endIndex != -1)&&(lineCdIdx != -1)) // { // Guide.DisplayFindPath(lineCdIdx, startIndex, endIndex); // lineCdIdx = nextLineCdIdx; // startIndex = nextStartIndex; // endIndex = -1; // } //} List <string> routeStationCd = new List <string>(); for (int idx = 0; idx < routeStation.Count; idx++) { string prevStatLineCd = ""; string nextStatLineCd = ""; if ((idx == 0) && (routeStation[idx].IsInterchange)) { List <string> stationLineCd = new List <string>(); foreach (string statCd in routeStation[idx].StationCode) { stationLineCd.Add(statCd.Substring(0, 2)); } foreach (string statCd in routeStation[idx + 1].StationCode) { if (stationLineCd.Contains(statCd.Substring(0, 2))) { nextStatLineCd = statCd.Substring(0, 2); } } foreach (string statCd in routeStation[idx].StationCode) { if (statCd.Contains(nextStatLineCd)) { routeStationCd.Add(statCd); } } } else if ((idx == routeStation.Count - 1) && (routeStation[idx].IsInterchange)) { List <string> stationLineCd = new List <string>(); foreach (string statCd in routeStation[idx].StationCode) { stationLineCd.Add(statCd.Substring(0, 2)); } foreach (string statCd in routeStation[idx - 1].StationCode) { if (stationLineCd.Contains(statCd.Substring(0, 2))) { prevStatLineCd = statCd.Substring(0, 2); } } foreach (string statCd in routeStation[idx].StationCode) { if (statCd.Contains(prevStatLineCd)) { routeStationCd.Add(statCd); } } } else if (routeStation[idx].IsInterchange) { List <string> stationLineCd = new List <string>(); foreach (string statCd in routeStation[idx].StationCode) { stationLineCd.Add(statCd.Substring(0, 2)); } foreach (string statCd in routeStation[idx - 1].StationCode) { if (stationLineCd.Contains(statCd.Substring(0, 2))) { prevStatLineCd = statCd.Substring(0, 2); } } foreach (string statCd in routeStation[idx + 1].StationCode) { if (stationLineCd.Contains(statCd.Substring(0, 2))) { nextStatLineCd = statCd.Substring(0, 2); } } foreach (string statCd in routeStation[idx].StationCode) { if (statCd.Contains(prevStatLineCd)) { routeStationCd.Add(statCd); } } if (!prevStatLineCd.Equals(nextStatLineCd)) { foreach (string statCd in routeStation[idx].StationCode) { if (statCd.Contains(nextStatLineCd)) { routeStationCd.Add(statCd); } } } } else { routeStationCd.Add(routeStation[idx].StationCode[0]); } } string outputRoute = string.Format("Display Route from {0} to {1} - Taking {2} stations\r\n", routeStation[0].StationName, routeStation[routeStation.Count - 1].StationName, routeStation.Count); outputRoute += "Start of Route\r\n"; foreach (string statCd in routeStationCd) { outputRoute += string.Format("{0} - {1}\r\n", statCd, Guide.SearchByStationCd(statCd).StationName); } outputRoute += "End of Route"; Console.WriteLine(outputRoute); }
public static void TestGraphRoute() { bool repeat = true; while (repeat) { Console.Write("Please enter the boarding station code: "); string boardingStationCd = Console.ReadLine(); Console.Write("Please enter the alighting station code: "); string alightingStationCd = Console.ReadLine(); initTraverseDijkstra(Guide.SearchByStationCd(boardingStationCd).GraphIndex, Guide.SearchByStationCd(alightingStationCd).GraphIndex); Console.Write("New Route? (Y/N)"); char response = char.Parse(Console.ReadLine().ToUpper()); switch (response) { case 'Y': repeat = true; break; case 'N': repeat = false; break; } } }