Exemple #1
0
        public Dictionary <Router, int> SPDijkstra(Router startRouter, Network network)
        {
            shortestPaths = new Dictionary <Router, int>();
            paths         = new List <Network.Path>();
            paths         = network.GetPaths();
            routers       = new List <Router>();
            routers       = network.GetRouters();
            routersPath   = SetDoubleDimensionRouterToNull(routers.Count, routers.Count);
            finalRoute    = new List <Router>();
            visited       = new bool[routers.Count];
            bool ending = false;
            int  lengthToCurrentRouter;
            int  lengthCurrent;
            int  lengthToNext;
            int  memberNumber = 0;

            for (int i = 0; i < routers.Count; i++)
            {
                if (routers[i] == startRouter)
                {
                    shortestPaths.Add(routers[i], 0);
                }
                else
                {
                    shortestPaths.Add(routers[i], int.MaxValue);
                }
                visited[i] = false;
            }
            shortestPaths = SortDictionary(shortestPaths);
            while (!ending)
            {
                int CurrentRoutePathMember = 0;

                Router currentRouter = shortestPaths.Keys.ToArray <Router>()[memberNumber];
                for (int i = 0; i < paths.Count; i++)
                {
                    Router firstRouter  = paths[i].GetFirstRouter();
                    Router secondRouter = paths[i].GetSecondRouter();
                    if (paths[i].EqualsFirstRouter(currentRouter))
                    {
                        if (!HasBeenVisited(secondRouter))
                        {
                            shortestPaths.TryGetValue(secondRouter, out lengthCurrent);
                            shortestPaths.TryGetValue(currentRouter, out lengthToCurrentRouter);
                            lengthToNext = paths[i].getCost();
                            int cost = lengthToNext + lengthToCurrentRouter;
                            if (cost < lengthCurrent)
                            {
                                shortestPaths.Remove(secondRouter);
                                shortestPaths.Add(secondRouter, lengthToCurrentRouter + lengthToNext);
                                shortestPaths = SortDictionary(shortestPaths);
                                routersPath[memberNumber, CurrentRoutePathMember] = secondRouter;
                                CurrentRoutePathMember++;
                            }
                        }
                    }
                    else if (paths[i].EqualsSecondRouter(currentRouter))
                    {
                        if (!HasBeenVisited(firstRouter))
                        {
                            shortestPaths.TryGetValue(firstRouter, out lengthCurrent);
                            shortestPaths.TryGetValue(currentRouter, out lengthToCurrentRouter);
                            lengthToNext = paths[i].getCost();
                            int cost = lengthToNext + lengthToCurrentRouter;
                            if (cost < lengthCurrent)
                            {
                                shortestPaths.Remove(firstRouter);
                                shortestPaths.Add(firstRouter, lengthToCurrentRouter + lengthToNext);
                                shortestPaths = SortDictionary(shortestPaths);
                                routersPath[memberNumber, CurrentRoutePathMember] = firstRouter;
                                CurrentRoutePathMember++;
                            }
                        }
                    }
                }
                CheckVisited(currentRouter);
                if (memberNumber + 1 == shortestPaths.Count)
                {
                    ending = true;
                }
                shortestPaths = SortDictionary(shortestPaths);
                memberNumber  = memberNumber + 1;
            }
            return(shortestPaths);
        }