コード例 #1
0
        public static void Main(string[] args)
        {
            var filePath = @"D:\Github\Algorithms\AirlineNetworks\airlines\routes.txt";

            var(routes, routeIds) = CsvParser.FileToRoutes(filePath);
            Console.WriteLine(routeIds.Count);
            var ewg = new EdgeWeightedGraph(routeIds.Count);

            foreach (var route in routes)
            {
                routeIds.TryGetValue(route.Source, out var source);
                routeIds.TryGetValue(route.Destination, out var destination);
                ewg.AddEdge(new Edge(source - 1, destination - 1, route.Distance));
            }

            var adj = GetAdjacents("TUN");

            foreach (var route in adj)
            {
                var source = GetAirportCode(route.V);
                var dest   = GetAirportCode(route.W);
                Console.WriteLine($"{source};{dest} {route.Weight:N2}");
            }

            var mst = new KruskalMST(ewg);

            Console.WriteLine(mst);
            var Edges = mst.Edges();

            for (var i = 0; i < 200; i++)
            {
                Console.WriteLine(Edges.Dequeue());
            }
            IEnumerable <Edge> GetAdjacents(string key)
            {
                routeIds.TryGetValue(key, out int val);
                return(ewg.Adjacents(val - 1));
            }

            string GetAirportCode(int id)
            {
                return(routeIds.FirstOrDefault(x => x.Value == id + 1).Key);
            }
        }
コード例 #2
0
        public void Run()
        {
            Console.WriteLine("Choose file:");      // Prompt
            Console.WriteLine("1 - tinyEWG.txt");   // Prompt
            Console.WriteLine("2 - mediumEWG.txt"); // Prompt
            //Console.WriteLine("3 - mediumEWG.txt"); // Prompt

            Console.WriteLine("or quit"); // Prompt

            var    fileNumber = Console.ReadLine();
            string fileName;

            switch (fileNumber)
            {
            case "1":
                fileName = "tinyEWG.txt";
                break;

            case "2":
                fileName = "mediumEWG.txt";
                break;

            //case "3":
            //    fileName = "largeEWG.zip";
            //    break;
            case "quit":
                return;

            default:
                return;
            }


            var @in   = new In($"Files\\Graphs\\{fileName}");
            var lines = @in.ReadAllLines();

            var lineIterator = 0;
            var v            = 0;
            var e            = 0;
            var edges        = new List <EdgeW>();

            foreach (var line in lines)
            {
                if (lineIterator == 0)
                {
                    v = Convert.ToInt32(line);
                }
                if (lineIterator == 1)
                {
                    e = Convert.ToInt32(line);
                }
                if (lineIterator > 1)
                {
                    var lineSplitted = line.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    var ve           = Convert.ToInt32(lineSplitted[0]);
                    var we           = Convert.ToInt32(lineSplitted[1]);
                    var weight       = Convert.ToDouble(lineSplitted[2], CultureInfo.InvariantCulture);
                    var edge         = new EdgeW(ve, we, weight);
                    edges.Add(edge);
                }

                lineIterator++;
            }

            var edgeWeightedGraph = new EdgeWeightedGraph(v, e, edges);

            Console.WriteLine(edgeWeightedGraph);

            var mst = new KruskalMST(edgeWeightedGraph);

            foreach (var edge in mst.Edges())
            {
                Console.WriteLine(edge);
            }
            Console.Write($"{$"{mst.Weight():0.00000}"}{Environment.NewLine}");

            Console.ReadLine();
        }