Ejemplo n.º 1
0
        public static void Main(string[] args)
        {
            string path = (args.Length > 0) ? args [0] : "blatt8_aufg2.txt";

            // Construct the graph
            List <int[]> edges = TupleReader.ReadPairs(path);

            UndirectedGraph.Graph graph = new UndirectedGraph.Graph(edges);
            // display the graph
            graph.Display();

            // identify the bipartition
            bool[] partition = Bipartition(graph);
            if (partition == null)
            {
                System.Console.WriteLine("The graph is not bipartite!");
                return;
            }

            // show the bipartition
            DisplayPartition(partition);

            // Find the Bipartite Matching
            List <DirectedWeightedGraph.Edge> matching = Matching(graph, partition);

            DisplayMatching(matching);
        }
Ejemplo n.º 2
0
        public static void Main()
        {
            String pairPath = "./blatt04_aufg1_a.txt";

            Console.WriteLine("processing " + pairPath);
            List <int[]> edges = TupleReader.ReadTriples(pairPath);

            Graph graph = new Graph(edges);

            // Display the graph
            graph.Display();

            int vStart = 0;             //compute shortest paths from this node

            double[] pi;                //shortest known path lengths
            int[]    pred;              //predeceesor nodes for these paths

            // Find the shortest path using the Dijkstra algorithm.
            ShortestPathsHeap(graph, vStart, out pi, out pred);

            // Output shortest paths
            DisplayShortestPaths(vStart, pi, pred);

            double[] pi2;               //shortest known path lengths
            int[]    pred2;             //predeceesor nodes for these paths

//			// Check Again with Array implementation
            //ShortestPathsArray(graph, vStart, out pi2, out pred2);

//			// Output shortest paths
            //DisplayShortestPaths(vStart, pi2, pred2);
        }
Ejemplo n.º 3
0
        public static void Maina()
        {
            String pairPath = "./blatt04_aufg1_b.txt";

            Console.WriteLine("processing " + pairPath);
            List <int[]> edges = TupleReader.ReadTriples(pairPath);

            Graph graph = new Graph(edges);

            // Display the graph
            graph.Display();

            int vStart = 0;                     //compute shortest paths from this node

            // Apply Bellman-Ford algorithm
            double[] pi;                        //shortest known path lengths
            int[]    pred;                      //predeceesor nodes for these paths

            if (!ShortestPaths(graph, vStart, out pi, out pred))
            {
                System.Console.WriteLine("There is a negative cycle!");
            }
            else
            {
                // Output shortest paths
                DisplayShortestPaths(vStart, pi, pred);
            }
        }
Ejemplo n.º 4
0
        public static void Main()
        {
            // Set Graph
            //int[,] edges =
            //{{0,1},{0,2},{0,3},{0,5},{1,2},{1,4},{1,7},{3,5},{4,7},
            //	{5,6},{5,7},{6,8},{6,9},{6,10},{7,8},{8,9},{8,10}};
//			{{0,1},{0,2},{0,3},{0,4},{1,2},{1,5},{1,6},{3,4},{3,6},
//				{3,7},{5,6},{6,8},{7,8},{7,9},{7,10},{8,9},{8,10}};

            String pairPath = "./blatt2_aufgabe1_b_graph.txt";

            Console.WriteLine("processing " + pairPath);
            List <int[]> edges = TupleReader.ReadPairs(pairPath);


            Graph graph = new Graph(edges);

            Console.WriteLine($"Size: {graph.Size()}");

            foreach (var component in graph.Components())
            {
                var size = graph.DFS_Stack(component).Count;

                Console.WriteLine(size);
            }

            // Display the graph
            graph.Display();

            pairPath = "./blatt2_aufgabe2_graph.txt";

            Console.WriteLine("processing " + pairPath);
            edges = TupleReader.ReadPairs(pairPath);
            graph = new Graph(edges);

            // Compute Eulerian cycle
            //List<int> cycle = EulerianCycle(graph);

            // Compute Eulerian path
            List <int> cycle = EulerianPath(graph);

            if (cycle == null)
            {
                return;
            }

//			// Output tour
            System.Console.Write("Eulertour: ");
            foreach (int v in cycle)
            {
                System.Console.Write(v + ", ");
            }
            System.Console.WriteLine();
        }
Ejemplo n.º 5
0
        public static void Main(string[] args)
        {
            string corrds_path = "cities_xy.txt";
            string names_path  = "city_names.txt";
            string start_city  = "Roswell, NM";

            // Einlesen der Städtedaten
            List <double[]> coords = TupleReader.ReadDoublesFromFile(corrds_path, 2, ";", "", "^", "$");
            List <string>   names  = TupleReader.ReadList(names_path, "", "^", "$");

            if (coords.Count != names.Count)
            {
                throw new InvalidOperationException("Number of city coordinates and city names does not match.");
            }

            int V   = coords.Count;
            int v_0 = names.IndexOf(start_city);

            if ((v_0 < 0) || (v_0 >= V))
            {
                throw new InvalidOperationException("Did not find start city.");
            }

            // erzeuge Distanzmatrix
            double[,] D = new double[V, V];
            for (int i = 0; i < V; ++i)
            {
                // Diagonale mit 0 initialisieren
                D[i, i] = 0;
                // Rest der symmetrischen Matrix füllen
                for (int j = i + 1; j < V; ++j)
                {
                    D[i, j] = D[j, i] = EuclideanDistance(coords[i], coords[j]);                     // euklidischer Abstand
                }
            }

            // berechne Greedy-TSP (Aufgabe 6.1)
            List <int> greedyTour = GreedyTour(D, v_0);

            Console.Write("===\nGreedy, ");
            DisplayPath(greedyTour, names, D);

            // berechne MST-Approximation
            List <int> mstTour = MSTApproximation(D, v_0);

            Console.Write("===\nSpannbaumheuristik, ");
            DisplayPath(mstTour, names, D);
        }
Ejemplo n.º 6
0
//*********************
//Main
//testGraphs:
//./blatt2_aufgabe1_a_graph.txt
//./blatt2_aufgabe1_b_graph.txt
//./blatt2_aufgabe1_c_graph.txt
//./blatt2_aufgabe2_a_graph.txt
//*********************

        public static void Main(string[] args)
        {
            Stack <string> stackPath = new Stack <string>();

            stackPath.Push("./blatt2_aufgabe1_c_graph.txt");
            stackPath.Push("./blatt2_aufgabe1_b_graph.txt");
            stackPath.Push("./blatt2_aufgabe1_a_graph.txt");
            while (stackPath.Count > 0)
            {
                String       pairPath = stackPath.Pop();
                List <int[]> edges    = TupleReader.ReadInBrackets(pairPath, 2);
                Graph        graph    = new Graph(edges);
                Console.WriteLine("Graph: " + pairPath.Substring(18) + ":");
                graph.Display();
                graph.MaxConnectedComponents();
                int size = graph.CountConnectedComponents();
                Console.WriteLine("");
                Console.WriteLine("Größe der Zusammenhangskomponente (Start bei 0): " + size);
                Console.WriteLine("");
            }
        }
Ejemplo n.º 7
0
        public static void Maina(string[] args)
        {
            string path = (args.Length > 0) ? args [0] : "blatt8_aufg1.txt";

            // Construct the Capacity Graph
            List <int[]> edges = TupleReader.ReadTriples(path);

            Graph graph = new Graph(edges);

            graph.Display();

            int s = 0;
            int t = graph.Nodes() - 1;

            // Apply Edmonds-Karp Algorithm.
            {
                System.Console.WriteLine("\nEdmonds-Karp:\n");
                FlowGraph     network   = new FlowGraph(graph, s, t);
                FlowOptimizer optimizer = new EdmondsKarp.EdmondsKarp();
                optimizer.InteractiveOptimumFlow(network);

                // Display the optimum flow
                System.Console.WriteLine("Flow is " + network.ComputeFlow());
            }

            // Apply Ford-Fulkerson Algorithm.
            {
                System.Console.WriteLine("\nFord-Fulkerson:\n");
                FlowGraph     network   = new FlowGraph(graph, s, t);
                FlowOptimizer optimizer = new FordFulkerson();
                optimizer.InteractiveOptimumFlow(network);

                // Display the optimum flow
                System.Console.WriteLine("Flow is " + network.ComputeFlow());
            }
        }