Пример #1
0
        private Packet GeneratePacket(Node node)
        {
            NetworkGraph tempNet = node.GetNetwork();

            foreach (var edge in tempNet.GetEdges())
            {
                netGraph.AddEdge(edge);

                foreach (var neighbor in tempNet.GetNeighbors(edge))
                {
                    netGraph.AddEdge(neighbor);
                    netGraph.SetLink(edge, neighbor, tempNet.GetWeight(edge, neighbor));
                }
            }

            return(new Packet(Packet.GetCounter(), id, netGraph));
        }
Пример #2
0
        public static Dictionary <string, string> DijkstraAlgorithm(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];

            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);
        }