Beispiel #1
0
        public static void Test()
        {
            Graph g = new Graph();
            StreamReader fin = new StreamReader("maxflow.in");
            string str = fin.ReadLine();
            string[] ss = str.Split();
            int s = Int32.Parse(ss[0]);
            int t = Int32.Parse(ss[1]);
            int i, j, c;
            while(true)
            {
                str = fin.ReadLine();
                if(str == null)
                    break;
                if(str.Length < 4)
                    break;

                ss = str.Split();
                i = Int32.Parse(ss[0]);
                j = Int32.Parse(ss[1]);
                c = Int32.Parse(ss[2]);
                g.AddEdge(i, j, c);
            }
            g.Source = g.GetVertex(s);
            g.Sink = g.GetVertex(t);
            Console.WriteLine(g.FordFulkerson());

            int n = 10000;
            g = new Graph();
            Random r = new Random();
            g.Source = g.GetVertex(0);
            for(i = 0; i < n; i++)
                for(j = 0; j < n; j++)
                    if(i != j)
                        if(r.Next(100) < 1)
                            g.AddEdge(i, j, r.Next(2) + 1);
            g.Sink = g.GetVertex(n - 1);
            Console.WriteLine(g.FordFulkerson());
        }
        /// <summary>
        /// Adds weights into flowgraph to favour connections that are in a larger 
        /// group of connections disabled by the same barrier
        /// </summary>
        /// <param name="g"></param>
        public static void AddWeight(Graph g)
        {
            IEnumerator<Vertex> vertexIter = g.vertices.GetEnumerator();
            Hashtable weights = new Hashtable();
            int max_weight = 0;
            int min_weight = 0xFFFFFFF;
            while (vertexIter.MoveNext())
            {
                Vertex v = vertexIter.Current;
                for (int i = 0; i < v.forward.Count; i++)
                {
                    Edge e = (Edge)v.forward[i];
                    if (e.ExtraInfo != null)
                    {
                        CILInstruction ins = ((DelayedAction)e.ExtraInfo).SourceInstruction;
                        if (weights[ins.ID] == null)
                            weights.Add(ins.ID, 1);
                        else
                        {
                            weights[ins.ID] = (int)weights[ins.ID] + 1;
                            if (max_weight < (int)weights[ins.ID])
                                max_weight = (int)weights[ins.ID];
                        }
                    }
                }
            }

            // print out the list of instruction usage with counters
            // first copy them to array to sort
            if (weights.Count == 0)
                return;
            IntPair[] arr = new IntPair[weights.Count];
            IDictionaryEnumerator iter = weights.GetEnumerator();
            int ix = 0;
            while (iter.MoveNext())
            {
                arr[ix++] = new IntPair((int)iter.Key, (int)iter.Value);
                if (min_weight > (int)iter.Value)
                    min_weight = (int)iter.Value;
            }
            IntPair.QSortIntPair(arr, 0, weights.Count - 1);

            StreamWriter fout = new StreamWriter("weightlog.tmp");
            for (int i = 0; i < arr.Length; i++)
                fout.WriteLine("{0} {1}", arr[i].First, arr[i].Second);

            int MAX_CAPACITY = 50;
            double scale;
            if (max_weight != min_weight)
                scale = (min_weight * MAX_CAPACITY) / (max_weight - min_weight);
            else
                scale = 0;
            // assigns the weights
            vertexIter = g.vertices.GetEnumerator();
            while (vertexIter.MoveNext())
            {
                Vertex v = vertexIter.Current;
                for (int i = 0; i < v.forward.Count; i++)
                {
                    Edge e = (Edge)v.forward[i];
                    if (e.ExtraInfo != null)
                    {
                        CILInstruction ins = ((DelayedAction)e.ExtraInfo).SourceInstruction;
                        e.capacity = (int)((1.0 * max_weight / (int)weights[ins.ID] - 1) * scale) + 1;
                        fout.Write("{0} ", e.capacity);
                    }
                }
            }
            fout.Close();
        }
Beispiel #3
0
 public static void FindBarrierByMaxflow(State st0)
 {
     SimpleFlowGraphBuilder builder = new SimpleFlowGraphBuilder();
     mfGraph = builder.BuildGraph(st0);
     Console.WriteLine("Done, running maxflow algorithm with {0} vertices {1} edges", mfGraph.CountVertex(), mfGraph.CountEdge());
     Console.WriteLine("Ford-Fulkerson: {0}", mfGraph.FordFulkerson());
     DateTime maxflowStartTime = DateTime.Now;
     Edge[] edges = mfGraph.MinCut;
     maxflowTime = DateTime.Now.Subtract(maxflowStartTime);
     ArrayList positions = new ArrayList();
     foreach(Edge e in edges)
     {
         CILInstruction inst = ((DelayedAction)e.ExtraInfo).SourceInstruction;
         bool found = false;
         foreach(CILInstruction ii in positions)
             if(ii == inst)
             {
                 found = true;
                 break;
             }
         if(found == false)
             positions.Add(inst);
     }
     foreach(CILInstruction inst in positions)
         Console.WriteLine(inst.ParentMethod + "::" + inst.ToString());
 }