BinomialHeap *BinomialHeap::merge(BinomialHeap& b1,BinomialHeap& b2)
{
    NodoB* a;
    NodoB* b;
    NodoB* c;
     // punteros al nodo mas izquierdo del heap
    if(b1.heap.empty())
        a=NULL;
    else
        a = *b1.heap.begin();
    if (b2.heap.empty())
        b = NULL;
    else
        b = *b2.heap.begin();

    if(b1.heap.empty())
        b1.heap.push_back(minDegree(*a,*b));
    else
        *b1.heap.begin()=minDegree(*a,*b);

    if (*b1.heap.begin() == NULL) return &b1;
    if (*b1.heap.begin() == b ) b=a;

    a = *b1.heap.begin();

    while (b != NULL){

        if(a->siblingDer == NULL){

            a->siblingDer = b;
            b->siblingIzq=a;
            *b2.heap.begin()=NULL; // liberando el primero de la lista de heaps de b2
            delete &b2;
            //b1.heap.push_back(b);

            return &b1;

        }
        else
            if(a->siblingDer->degree < b->degree){
                a = a->siblingDer; //recorre a
            } else{
                c = b->siblingDer;
                b->siblingDer = a->siblingDer;
                a->siblingDer->siblingIzq = b; //el hermano izq del hermano derecho de a es b;
                a->siblingDer = b; //b apunta al hermano de a
                b->siblingIzq = a;
                a=a->siblingDer;
                b=c;
            }
    }
    return &b1;
}
Ejemplo n.º 2
0
 long numCol(const Graph & g)
 {
     long n = order(g);
     if (n == 1 || minDegree(g) == n-1)
     {
         return 1;
     }
     typedef std::pair<vertex_iter, vertex_iter> p_vertex_iter;
     vertex u,v;
     bool nonEdgeFound = false;
     for (p_vertex_iter vp = vertices(g); vp.first != vp.second && !nonEdgeFound; ++vp.first)
     {
         for (vertex_iter w = vp.first+1; w != vp.second && !nonEdgeFound; ++w)
         {
             if (!edge(*vp.first, *w, g).second)
             {
                 u = *vp.first;
                 v = *w;
                 nonEdgeFound = true;
             }
         }
     }
     Graph g1 = contract(u, v, g);
     long num = numCol(g1);
     Graph g2(g);
     add_edge(u,v,g2);
     num += numCol(g2);
     return num;
 }