コード例 #1
0
        /// <summary>
        /// Lädt einen Graphen aus der gegebenen Datei und führt auf diesem den Prim-Algorithmus aus.
        /// </summary>
        /// <param name="fileName">Der Name der Datei, in dem der zu prüfende Graph enthalten ist.</param>
        /// <param name="vStart">Optional kann ein Knoten angegeben werden, von dem aus der Minimale Spannbaum aufgezogen werden soll.</param>
        public static void CallPrim(string fileName, string vStart = null)
        {
            // Den Graphen aus der Datei holen.
            Graph <string> graph = ApplicationHelper.LoadGraph(fileName);

            // Falls vorhanden den Start-Vertex setzen.
            Vertex <string> start = (vStart != null) ? new Vertex <string>(vStart) : null;

            // Prim ausführen.
            Graph <string> result = (start != null) ? graph.Prim(start) : graph.Prim();

            // Die Kosten des Baumens berechnen
            int costs = result.Edges.Sum((e) => e.Weight.Value);

            // Das Ergebnis in eine Datei schreiben.

            string baseFileName = fileName.Split('.')[0];

            ApplicationHelper.WriteResult(baseFileName, "primResult_", "Tree costs: " + costs, result.Edges.Select((edge) => edge.ToString()));
        }
コード例 #2
0
        static void Main(string[] args)
        {
            // remove parameter '-'
            foreach (string arg in args)
            {
                if (arg[0].Equals('-'))
                {
                    args[0] = arg.Remove(0, 1);
                }
            }

            // Die Input Parameter prüfen.
            switch (args.Count())
            {
            case 0:
                Program.WriteUsageInfo();
                break;

            case 1:
                if (args[0].ToLower().Equals("help") || args[0].ToLower().Equals("h"))
                {
                    Program.WriteUsageInfo();
                }
                else if (args[0].ToLower().Equals("info"))
                {
                    WriteInfo();
                }
                else
                {
                    Console.WriteLine("");
                    Console.WriteLine("Unknown Paramter");
                    Console.WriteLine("");
                    Program.WriteUsageInfo();
                }
                break;

            //throw new ArgumentException("Not enough Arguments given.");
            case 2:
                string filePath = Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), args[0]);
                if (!File.Exists(filePath))
                {
                    Console.WriteLine("File with given filename could not be found in application folder.");
                    //throw new ArgumentException("File doesn't exists.");
                }
                break;
            }

            // Je nach angabe des zweiten Parameters die entsprechende Funktion aufrufen.
            switch (args[1].ToLower())
            {
            case "dijkstra":
                if (args.Count() != 4)
                {
                    Console.WriteLine("");
                    Console.WriteLine("Number of Arguments doesn't match the function requirements");
                    Program.WriteUsageInfo();
                    //throw new ArgumentException("Not enough Arguments for this function.");
                }

                ApplicationHelper.CallDijkstra(args[0], args[2], args[3]);
                break;

            case "prim":
                if (args.Count() > 3)
                {
                    Console.WriteLine("");
                    Console.WriteLine("Number of Arguments doesn't match the function requirements");
                    Program.WriteUsageInfo();
                    //throw new ArgumentException("To much parameters for this function");
                }

                if (args.Count() == 2)
                {
                    ApplicationHelper.CallPrim(args[0]);
                }
                else
                {
                    ApplicationHelper.CallPrim(args[0], args[2]);
                }

                break;

            case "kruskal":
                if (args.Count() > 2)
                {
                    Console.WriteLine("");
                    Console.WriteLine("Number of Arguments doesn't match the function requirements");
                    Program.WriteUsageInfo();
                    //throw new ArgumentException("To much parameters for this function");
                }

                ApplicationHelper.CallKruskal(args[0]);
                break;

            case "ford-fulkerson":
                if (args.Count() > 2)
                {
                    Console.WriteLine("");
                    Console.WriteLine("Number of Arguments doesn't match the function requirements");
                    Program.WriteUsageInfo();
                }

                ApplicationHelper.CallFordFulkerson(args[0]);
                break;

            case "strong-connect":
                if (args.Count() > 2)
                {
                    Console.WriteLine("");
                    Console.WriteLine("Number of Arguments doesn't match the function requirements");
                    Program.WriteUsageInfo();
                }

                ApplicationHelper.CallStrongConnect(args[0]);
                break;

            case "tsort":
                if (args.Count() > 2)
                {
                    Console.WriteLine("");
                    Console.WriteLine("Number of Arguments doesn't match the function requirements");
                    Program.WriteUsageInfo();
                }

                ApplicationHelper.CallTopSort(args[0]);
                break;

            case "allpairs-shortestpath":
                if (args.Count() > 2)
                {
                    Console.WriteLine("");
                    Console.WriteLine("Number of Arguments doesn't match the function requirements");
                    Program.WriteUsageInfo();
                }

                ApplicationHelper.CallAllPairsShortestPath(args[0]);
                break;

            case "dfs":
            case "depth-first-search":
                if (args.Count() != 4)
                {
                    Console.WriteLine("");
                    Console.WriteLine("Number of Arguments doesn't match the function requirements");
                    Program.WriteUsageInfo();
                }

                ApplicationHelper.CallDepthFirstSearch(args[0], args[2], args[3]);
                break;

            default:
                Console.WriteLine("");
                Console.WriteLine($"No matching funciton found for '{args[1]}'");
                break;
                //throw new ArgumentException($"No matching function found for '{args[1]}'");
            }

            //Console.WriteLine(args.Select((arg) => arg.ToString()).Aggregate((a, b) => a + ", " + b));
        }