Exemple #1
0
        private Packet GeneratePacket(Router router)
        {
            NetworkGraph secondNetwork = router.GetNetwork();

            foreach (string edge in secondNetwork.getEdges())
            {
                network.addEdge(edge);
                foreach (string neighbor in secondNetwork.getNeighbors(edge))
                {
                    network.addEdge(neighbor);
                    network.setLink(edge, neighbor, secondNetwork.getWeight(edge, neighbor));
                }
            }

            return(new Packet(Packet.GetCounter(), id, network));
        }
Exemple #2
0
        // Dijkstra's algorithm to find shortest path from s to all other nodes
        public static Dictionary <string, string> dijkstra(NetworkGraph G, string source)
        {
            string[] routers = G.getEdges();
            int[]    dist    = new int[routers.Length];
            string[] queue   = new string[routers.Length];
            Dictionary <string, string> pred = new Dictionary <string, string>();

            bool[] visited = new bool[routers.Length]; // all false initially

            for (int i = 0; i < dist.Length; i++)
            {
                dist[i]  = int.MaxValue;
                queue[i] = routers[i];
                if (routers[i].Equals(source))
                {
                    dist[i] = 0;
                }
            }

            for (int i = 0; i < dist.Length; i++)
            {
                int next = minVertex(dist, visited);
                if (next != -1)
                {
                    visited[next] = true;
                    string[] neighbors = G.getNeighbors(routers[next]);
                    for (int j = 0; j < neighbors.Length; j++)
                    {
                        int nr = 0;
                        for (int h = 0; h < routers.Length; h++)
                        {
                            if (routers[h].Equals(neighbors[j]))
                            {
                                nr = h;
                            }
                        }
                        int d = dist[next] + G.getWeight(routers[next], routers[nr]);
                        if (dist[nr] > d)
                        {
                            dist[nr]  = d;
                            queue[nr] = queue[next] + seperator + queue[nr];
                        }
                    }
                }
            }

            foreach (string str in queue)
            {
                string[] parts = str.Split(seperator);
                if (parts.Length != 1)
                {
                    pred[parts[parts.Length - 1]] = parts[1];
                }
                else
                {
                    pred[parts[0]] = parts[0];
                }
            }

            return(pred);
        }