Beispiel #1
0
        // points foundNode at the corresponding node

        public void insertItem(int newKey, double newStored)// insert a new key with stored value
        // first we check to see if newKey is already present in the tree; if so, we simply
        // set .stored += newStored; if not, we must find where to insert the key
        {
            element newNode, current;

            newNode = new element();

            current = findItem(newKey);                                         // find newKey in tree; return pointer to it O(log k)
            if (current != null)
            {
                current.stored += newStored;                                    // update its stored value
                heap.updateItemdub(current.heap_ptr, current.stored);
                // update corresponding element in heap + reheapify; O(log k)
            }
            else                                                                // didn't find it, so need to create it
            {
                tuple newitem = new tuple();                                    //
                newitem.m = newStored;                                          //
                newitem.i = -1;                                                 //
                newitem.j = newKey;                                             //

                //newNode			= new element;				// element for the vektor
                newNode.key      = newKey;                      //  store newKey
                newNode.stored   = newStored;                   //  store newStored
                newNode.color    = true;                        //  new nodes are always RED
                newNode.parent   = null;                        //  new node initially has no parent
                newNode.left     = leaf;                        //  left leaf
                newNode.right    = leaf;                        //  right leaf
                newNode.heap_ptr = heap.insertItem(newitem);    // add new item to the vektor heap
                support++;                                      // increment node count in vektor

                // must now search for where to insert newNode, i.e., find the correct parent and
                // set the parent and child to point to each other properly
                current = root;
                if (current.key == 0)                                                                           // insert as root
                                                                                                                //   delete old root
                {
                    root        = newNode;                                                                      //   set root to newNode
                    leaf.parent = newNode;                                                                      //   set leaf's parent
                    current     = leaf;                                                                         //   skip next loop
                }

                while (current != leaf)                                                                         // search for insertion point
                {
                    if (newKey < current.key)                                                                   // left-or-right?
                    {
                        if (current.left != leaf)
                        {
                            current = current.left;
                        }                                                               // try moving down-left
                        else                                                            // else found new parent
                        {
                            newNode.parent = current;                                   //    set parent
                            current.left   = newNode;                                   //    set child
                            current        = leaf;                                      //    exit search
                        }
                    }
                    else                                                                                                        //
                    {
                        if (current.right != leaf)
                        {
                            current = current.right;
                        }                                                                 // try moving down-right
                        else                                                              // else found new parent
                        {
                            newNode.parent = current;                                     //    set parent
                            current.right  = newNode;                                     //    set child
                            current        = leaf;                                        //    exit search
                        }
                    }
                }

                // now do the house-keeping necessary to preserve the red-black properties
                insertCleanup(newNode);                 // do house-keeping to maintain balance
            }
            return;
        }
Beispiel #2
0
        // ------------------------------------------------------------------------------------ //
        // ------------------------------------------------------------------------------------ //
        // ------------------------------------------------------------------------------------ //
        // FUNCTION DEFINITIONS --------------------------------------------------------------- //

        void buildDeltaQMatrix()
        {
            // Given that we've now populated a sparse (unordered) adjacency matrix e (e),
            // we now need to construct the intial dQ matrix according to the definition of dQ
            // which may be derived from the definition of modularity Q:
            //    Q(t) = \sum_{i} (e_{ii} - a_{i}^2) = Tr(e) - ||e^2||
            // thus dQ is
            //    dQ_{i,j} = 2* ( e_{i,j} - a_{i}a_{j} )
            //    where a_{i} = \sum_{j} e_{i,j} (i.e., the sum over the ith row)
            // To create dQ, we must insert each value of dQ_{i,j} into a binary search tree,
            // for the jth column. That is, dQ is simply an array of such binary search trees,
            // each of which represents the dQ_{x,j} adjacency vector. Having created dQ as
            // such, we may happily delete the matrix e in order to free up more memory.
            // The next step is to create a max-heap data structure, which contains the entries
            // of the following form (value, s, t), where the heap-key is 'value'. Accessing the
            // root of the heap gives us the next dQ value, and the indices (s,t) of the vectors
            // in dQ which need to be updated as a result of the merge.


            // First we compute e_{i,j}, and the compute+store the a_{i} values. These will be used
            // shortly when we compute each dQ_{i,j}.
            edge current;


            double eij = (double)(0.5 / gparm.m);                               // intially each e_{i,j} = 1/m

            for (int i = 1; i < gparm.maxid; i++)
            {                                   // for each row
                if (e[i].so != 0)
                {                               //    ensure it exists
                    current = e[i];             //    grab first edge
                    a[i]    = eij;              //    initialize a[i]
                    while (current.next != null)
                    {                           //    loop through remaining edges
                        a[i]   += eij;          //       add another eij
                        current = current.next; //
                    }
                    Q[0] += -1.0 * a[i] * a[i]; // calculate initial value of Q
                }
            }

            // now we create an empty (ordered) sparse matrix dq[]
            dq = new nodenub[gparm.maxid];      // initialize dq matrix
            for (int i = 0; i < gparm.maxid; i++)
            {                                   //
                //dq[i].heap_ptr;							// no pointer in the heap at first
                if (e[i].so != 0)
                {
                    dq[i].v = new vektor(2 + (int)(gparm.m * a[i]));
                }
                else
                {
                    dq[i].v = null;
                }
            }
            h = new modmaxheap(gparm.n);                                                // allocate max-heap of size = number of nodes

            // Now we do all the work, which happens as we compute and insert each dQ_{i,j} into
            // the corresponding (ordered) sparse vector dq[i]. While computing each dQ for a
            // row i, we track the maximum dQmax of the row and its (row,col) indices (i,j). Upon
            // finishing all dQ's for a row, we insert the tuple into the max-heap hQmax. That
            // insertion returns the itemaddress, which we then store in the nodenub heap_ptr for
            // that row's vector.
            double dQ;
            tuple  dQmax = new tuple();                                                                         // for heaping the row maxes

            //tuple itemaddress;
            // stores address of item in maxheap


            for (int i = 1; i < gparm.maxid; i++)
            {
                if (e[i].so != 0)
                {
                    current       = e[i];                                          // grab first edge
                    dQ            = 2.0 * (eij - (a[current.so] * a[current.si])); // compute its dQ
                    dQmax.m       = dQ;                                            // assume it is maximum so far
                    dQmax.i       = current.so;                                    // store its (row,col)
                    dQmax.j       = current.si;                                    //
                    dQmax.enabled = true;
                    dq[i].v.insertItem(current.si, dQ);                            // insert its dQ
                    while (current.next != null)
                    {                                                              //
                        current = current.next;                                    // step to next edge
                        dQ      = 2.0 * (eij - (a[current.so] * a[current.si]));   // compute new dQ
                        if (dQ > dQmax.m)
                        {                                                          // if dQ larger than current max
                            dQmax.m = dQ;                                          //    replace it as maximum so far
                            dQmax.j = current.si;                                  //    and store its (col)
                        }
                        dq[i].v.insertItem(current.si, dQ);                        // insert it into vector[i]
                    }
                    dq[i].heap_ptr = h.insertItem(dQmax);                          // store the pointer to its loc in heap
                }
            }

            e     = null;
            elist = null;

            // free-up adjacency matrix memory in two shots
            //
            return;
        }