Example #1
0
 /**
  * Returns the cut-of-the-phase. The cut-of-the-phase is a minimum s-t-cut
  * in the current graph, where {@code s} and {@code t} are the two vertices
  * added last in the phase. This algorithm is known in the literature as
  * <em>maximum adjacency search</em> or <em>maximum cardinality search</em>.
  * 
  * @param G the edge-weighted graph
  * @param marked the array of contracted vertices, where {@code marked[v]}
  *            is {@code true} if the vertex {@code v} was already
  *            contracted; or {@code false} otherwise
  * @param cp the previous cut-of-the-phase
  * @return the cut-of-the-phase
  */
 private CutPhase minCutPhase(EdgeWeightedGraph G, boolean[] marked, CutPhase cp) {
     IndexMaxPQ<Double> pq = new IndexMaxPQ<Double>(G.V());
     for (int v = 0; v < G.V(); v++) {
         if (v != cp.s && !marked[v]) pq.insert(v, 0.0);
     }
     pq.insert(cp.s, Double.POSITIVE_INFINITY);
     while (!pq.isEmpty()) {
         int v = pq.delMax();
         cp.s = cp.t;
         cp.t = v;
         for (Edge e : G.adj(v)) {
             int w = e.other(v);
             if (pq.contains(w)) pq.increaseKey(w, pq.keyOf(w) + e.weight());
         }
     }
     cp.weight = 0.0;
     for (Edge e : G.adj(cp.t)) {
         cp.weight += e.weight();
     }
     return cp;
 }
Example #2
0
    // Use this for initialization
    void Start()
    {
        // insert a bunch of strings
        string[] strings = { "it", "was", "the", "best", "of", "times", "it", "was", "the", "worst" };

        IndexMaxPQ <string> pq = new IndexMaxPQ <string>(strings.Length);

        for (int i = 0; i < strings.Length; i++)
        {
            pq.insert(i, strings[i]);
        }

        // print each key using the iterator
        foreach (string i in pq)
        {
            print(i + " " + strings[int.Parse(i)]);
        }


        // increase or decrease the key
        for (int i = 0; i < strings.Length; i++)
        {
            if (Random.Range(0.0f, 1.0f) < 0.5)  // Random.uniform() 返回一个随机的范围在[0,1)之间的double类型的数
            {
                pq.increaseKey(i, strings[i] + strings[i]);
            }
            else
            {
                pq.decreaseKey(i, strings[i].Substring(0, 1));
            }
        }

        // delete and print each key
        while (!pq.isEmpty())
        {
            string key = pq.maxKey();
            int    i   = pq.delMax();
            print(i + " " + key);
        }

        // reinsert the same strings
        for (int i = 0; i < strings.Length; i++)
        {
            pq.insert(i, strings[i]);
        }

        // delete them in random order
        int[] perm = new int[strings.Length];
        for (int i = 0; i < strings.Length; i++)
        {
            perm[i] = i;
        }

        UnSort(perm);  // StdRandom.shuffle() 随机打乱指定的object型数组
        for (int i = 0; i < perm.Length; i++)
        {
            string key = pq.keyOf(perm[i]);
            pq.delete(perm[i]);
            print(perm[i] + " " + key);
        }
    }