Beispiel #1
0
        public static void Maina()
        {
            // Construct the Capacity Graph
            int s = 0;
            int t = 5;

            int[,] edges =
            {             { 0, 1, 16 }, { 0, 2, 13 }, { 1, 3, 12 }, { 2, 1, 4 }, { 2, 4, 14 }, { 3, 2, 9 },
                          { 3, 5, 20 }, { 4, 3,  7 }, { 4, 5,  4 } };
            //{{0,1,5},{0,2,7},{1,2,3},{2,3,8},{3,1,2},{1,4,6},{4,3,3},{3,5,4},{4,5,7}};

            Graph graph = new Graph(edges);

            graph.Display();

            // construct the initial network.
            FlowGraph network = new FlowGraph(graph, s, t);

            // Apply Edmonds Karp Algorithm.
            EdmondsKarp optimizer = new EdmondsKarp();

            optimizer.InteractiveOptimumFlow(network);

            // Display the optimum flow
            System.Console.WriteLine("Flow is " + network.ComputeFlow());
        }
Beispiel #2
0
        // Computes a bipartite matching in the given graph with the given partition of nodes.
        // The partition is given in form of a boolean array as returned by the Partition
        // function.
        public static List <Edge> Matching(UndirectedGraph.Graph graph, bool[] partition)
        {
            List <Edge> matching = new List <Edge>();

            //TODO: Implement

            DirectedWeightedGraph.Graph g = new DirectedWeightedGraph.Graph(graph.Nodes() + 2);

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

            for (int i = 0; i < graph.Nodes(); i++)
            {
                List <int> nodes = graph.AllNodes(i);
                for (int j = 0; j < nodes.Count; j++)
                {
                    g.AddEdge(i + 1, nodes[j], 1);

                    if (partition[i])
                    {
                        g.AddEdge(i + 1, t, 1);
                    }
                    else
                    {
                        g.AddEdge(s, i + 1, 1);
                    }
                }
            }


            FlowGraph     network   = new FlowGraph(g, s, t);
            FlowOptimizer optimizer = new EdmondsKarp.EdmondsKarp();

            //optimizer.InteractiveOptimumFlow(network);

            optimizer.OptimumFlow(network);
            network.ComputeFlow();

            for (int v = 1; v < network.Nodes() - 1; v++)
            {
                foreach (Edge edge in network.Edges(v))
                {
                    if (edge.To() != s && edge.From() != t && edge.Weight() > 0)
                    {
                        matching.Add(edge);
                        break;
                    }
                }
            }

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

            return(matching);
        }
Beispiel #3
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());
            }
        }