/// <summary>
 /// Updates all Dijkstra Routes
 /// </summary>
 public void UpdateAllStarts()
 {
     startingPoints = new List <DijkstraStart>();
     foreach (Location l in reachableLocations)
     {
         DijkstraStart temp = createDijkstraStart(l, false);
         startingPoints.Add(temp);
     }
 }
        /// <summary>
        /// Update all Dijkstra routes that have startLocation as starting point
        /// </summary>
        /// <param name="startLocation"></param>
        public void UpdateSingleStart(Location startLocation)
        {
            DijkstraStart result = null;

            if (reachableLocations.Contains(startLocation))
            {
                foreach (DijkstraStart ds in startingPoints)
                {
                    if (ds.StartPoint == startLocation)
                    {
                        result = ds;
                    }
                }
                if (result != null)
                {
                    startingPoints.Remove(result);
                }
                DijkstraStart temp = createDijkstraStart(startLocation, false);
                startingPoints.Add(temp);
            }
        }
        private DijkstraStart createDijkstraStart(Location startLocation, bool animate) //main methods for creating shortest routes accros the map
        {
            if (!reachableLocations.Contains(startLocation))
            {
                throw new Exception("Unknown location detected");
            }
            List <Road>   currentRoads        = new List <Road>(); //considered roads at the moment
            List <int>    currentRoadsLenghts = new List <int>();  //road lenghts
            DijkstraStart dijkstraStart       = new DijkstraStart(startLocation);

            currentRoads = getRoadsConnectedToLocation(startLocation);
            foreach (Road r in currentRoads)
            {
                currentRoadsLenghts.Add(r.InitialCost);
            }

            int      lowestIndex;
            Road     currentRoad;
            int      currentRoadLenght;
            Location prevLocation = null;
            Location newLocation  = null;

            //ANIMATION
            if (animate)
            {
                // resets all roads to black color
                foreach (Road r in allRoads)
                {
                    r.ResetDrawFields();
                }
            }

            while (currentRoads.Count != 0)
            {
                //get next shortest route
                lowestIndex       = findLowestIndex(currentRoadsLenghts);
                currentRoad       = currentRoads[lowestIndex];
                currentRoadLenght = currentRoadsLenghts[lowestIndex];

                //ANIMATION
                if (animate)
                {
                    // colors the potential roads with yellow
                    foreach (Road r in currentRoads)
                    {
                        r.LineColor = Color.Yellow;
                    }
                    Map.RedrawMapNow();
                    // makes the system wait 2 seconds
                    System.Threading.Thread.Sleep(1000);
                    // after it colors all potential yellow it colors the best green.
                    currentRoad.LineColor = Color.Green;
                }

                //claim new shortest route
                if (dijkstraStart.isConnectedToLocation(currentRoad.Vertex1))
                {
                    prevLocation = currentRoad.Vertex1;
                }
                else
                {
                    newLocation = currentRoad.Vertex1;
                }
                if (dijkstraStart.isConnectedToLocation(currentRoad.Vertex2))
                {
                    prevLocation = currentRoad.Vertex2;
                }
                else
                {
                    newLocation = currentRoad.Vertex2;
                }
                if (prevLocation == null)
                {
                    throw new NullReferenceException();
                }
                if (newLocation == null)
                {
                    throw new NullReferenceException();
                }
                DijkstraRoute previousDijkstraRoute = dijkstraStart.GetRouteTo(prevLocation);
                List <Road>   newRoute;

                //makes a copy of the current route and adds the current road
                if (previousDijkstraRoute != null)
                {
                    newRoute = previousDijkstraRoute.CopyRoute();
                    newRoute.Add(currentRoad);
                }
                else
                {
                    // makes a new route and adds the road to it
                    newRoute = new List <Road>();
                    newRoute.Add(currentRoad);
                }
                DijkstraRoute newDijkstraRoute = new DijkstraRoute(newRoute, newLocation);
                dijkstraStart.AddNewRoute(newDijkstraRoute);

                //Determine new roads and remove currentRoad/exsiting roads
                currentRoads.RemoveAt(lowestIndex);
                currentRoadsLenghts.RemoveAt(lowestIndex);

                List <Road> newAddingRoads = getRoadsConnectedToLocation(newLocation);
                newAddingRoads.Remove(currentRoad);

                //filter out already existing roads in currentRoads
                for (int i = 0; i < newAddingRoads.Count; i++)
                {
                    if (dijkstraStart.isConnectedToLocation(newAddingRoads[i].Vertex1) &&
                        dijkstraStart.isConnectedToLocation(newAddingRoads[i].Vertex2))
                    {
                        if (currentRoads.Contains(newAddingRoads[i]))
                        {
                            int index = currentRoads.IndexOf(newAddingRoads[i]);
                            currentRoads.RemoveAt(index);
                            currentRoadsLenghts.RemoveAt(index);
                        }
                        newAddingRoads.RemoveAt(i);
                        i--;
                    }
                }

                //Adding new roads and calculating new roads lenghts
                foreach (Road r in newAddingRoads)
                {
                    int roadLenght = r.InitialCost + currentRoadLenght;
                    currentRoads.Add(r);
                    currentRoadsLenghts.Add(roadLenght);
                }

                //reseting values
                currentRoad       = null;
                prevLocation      = null;
                newLocation       = null;
                currentRoadLenght = -1;
                lowestIndex       = -1;
            }
            if (!animate)
            {
                return(dijkstraStart);
            }
            else
            {
                Map.RedrawMap();
            }
            return(null);
        }