public void OnMergedCommunities
            (Community mergedCommunity1,Community mergedCommunity2,Community newMergedCommunity,Single newCommunityPairDeltaQ,DeltaQMaxHeap deltaQMaxHeap)
        {
            Debug.Assert(mergedCommunity1 != null);
            Debug.Assert(mergedCommunity2 != null);
            Debug.Assert(newMergedCommunity != null);
            Debug.Assert(deltaQMaxHeap != null);

            // If only one of the two merged communities was connected to this
            // community, fPreviousCommunityPairDeltaQ is the delta Q for this
            // community's community pair for the merged community.  If both were
            // connected, fPreviousCommunityPairDeltaQ is the larger of the two
            // delta Q values.

            Single fPreviousCommunityPairDeltaQ = Single.MinValue;

            Int32 iMergedCommunity1ID = mergedCommunity1.ID;
            Int32 iMergedCommunity2ID = mergedCommunity2.ID;

            Debug.Assert(iMergedCommunity1ID != iMergedCommunity2ID);

            Int32 iSmallerMergedCommunityID =
                Math.Min(iMergedCommunity1ID, iMergedCommunity2ID);

            Int32 iLargerMergedCommunityID =
                Math.Max(iMergedCommunity1ID, iMergedCommunity2ID);

            // Delete the community pair or pairs that connect to one of the merged
            // communities.
            //
            // Go backwards through the community pairs so that they can be deleted
            // while looping.  (Don't use foreach, because you can't delete while
            // enumerating.)

            for (Int32 i = m_oCommunityPairs.Count - 1; i >= 0; i--)
            {
                Int32 iOtherCommunityID = m_oCommunityPairs.Keys[i];

                if (iOtherCommunityID > iLargerMergedCommunityID)
                {
                    // We haven't yet reached the range of community pairs that
                    // might connect to either merged community.

                    continue;
                }

                if (iOtherCommunityID < iSmallerMergedCommunityID)
                {
                    // We're beyond the range of community pairs that might connect
                    // to either merged community.

                    break;
                }

                CommunityPair oCommunityPair = m_oCommunityPairs.Values[i];

                if (iOtherCommunityID == iLargerMergedCommunityID)
                {
                    // This community pair connects to the merged community with
                    // the larger ID.

                    fPreviousCommunityPairDeltaQ = oCommunityPair.DeltaQ;

                    m_oCommunityPairs.RemoveAt(i);
                }
                else if (iOtherCommunityID == iSmallerMergedCommunityID)
                {
                    // This community pair connects to the merged community with
                    // the smaller ID.

                    fPreviousCommunityPairDeltaQ = Math.Max(
                        fPreviousCommunityPairDeltaQ, oCommunityPair.DeltaQ);

                    m_oCommunityPairs.RemoveAt(i);

                    // There is no reason to continue looking at community pairs.

                    break;
                }
                else
                {
                    // This community pair does not connect to either merged
                    // community.

                    continue;
                }
            }

            Debug.Assert(fPreviousCommunityPairDeltaQ != Single.MinValue);

            // Add a new community pair that connects to the new merged community.

            CommunityPair oNewCommunityPair = new CommunityPair();
            oNewCommunityPair.Community1 = this;
            oNewCommunityPair.Community2 = newMergedCommunity;
            oNewCommunityPair.DeltaQ = newCommunityPairDeltaQ;
            m_oCommunityPairs.Add(newMergedCommunity.ID, oNewCommunityPair);

            // Update m_oCommunityPairWithMaximumDeltaQ if necessary.  These rules
            // come from section 4.1 of "Finding Community Structure in Mega-scale
            // Social Networks," by Ken Wakita and Toshiyuki Tsurumi.

            Single fOldMaximumDeltaQ = this.MaximumDeltaQ;

            if (fPreviousCommunityPairDeltaQ < fOldMaximumDeltaQ)
            {
                // The deleted community pair (or pairs) was not the one with the
                // maximum delta Q.

                if (newCommunityPairDeltaQ <= fPreviousCommunityPairDeltaQ)
                {
                    // The delta Q value for the new community pair is less than or
                    // equal to the delta Q value of the deleted community pair (or
                    // pairs).  Do nothing.
                }
                else
                {
                    // The delta Q value for the new community pair is greater than
                    // the delta Q value of the deleted community pair (or pairs).

                    if (newCommunityPairDeltaQ > fOldMaximumDeltaQ)
                    {
                        // The new community pair is the one with the maximum
                        // delta Q.

                        m_oCommunityPairWithMaximumDeltaQ = oNewCommunityPair;
                    }
                }
            }
            else
            {
                // The deleted community pair (or pairs) was the one with the
                // maximum delta Q.

                if (newCommunityPairDeltaQ >= fPreviousCommunityPairDeltaQ)
                {
                    // The new community pair is the one with the maximum
                    // delta Q.

                    m_oCommunityPairWithMaximumDeltaQ = oNewCommunityPair;
                }
                else
                {
                    // Worst case: All community pairs must be scanned.

                    Single fNewMaximumDeltaQ = Single.MinValue;

                    foreach (CommunityPair oCommunityPair in
                        m_oCommunityPairs.Values)
                    {
                        if (oCommunityPair.DeltaQ > fNewMaximumDeltaQ)
                        {
                            m_oCommunityPairWithMaximumDeltaQ = oCommunityPair;
                            fNewMaximumDeltaQ = oCommunityPair.DeltaQ;
                        }
                    }
                }
            }

            if (fOldMaximumDeltaQ != this.MaximumDeltaQ)
            {
                // Update the max heap.

                deltaQMaxHeap.UpdateValue(this, this.MaximumDeltaQ);
            }
        }
        protected void MergeCommunities
            (LinkedList<Community> oCommunities,CommunityPair oCommunityPairToMerge,DeltaQMaxHeap oDeltaQMaxHeap,Int32 iEdgesInGraph,IDGenerator oIDGenerator)
        {
            Debug.Assert(oCommunityPairToMerge != null);
            Debug.Assert(oCommunities != null);
            Debug.Assert(oDeltaQMaxHeap != null);
            Debug.Assert(iEdgesInGraph > 0);
            Debug.Assert(oIDGenerator != null);

            // Merge Community1 and Community2 into a NewCommunity.

            Community oCommunity1 = oCommunityPairToMerge.Community1;
            Community oCommunity2 = oCommunityPairToMerge.Community2;

            Community oNewCommunity = new Community();

            oNewCommunity.ID = oIDGenerator.GetNextID();
            oNewCommunity.Degree = oCommunity1.Degree + oCommunity2.Degree;

            ICollection<IVertex> oNewCommunityVertices = oNewCommunity.Vertices;

            foreach (IVertex oVertex in oCommunity1.Vertices)
            {
                oNewCommunityVertices.Add(oVertex);
            }

            foreach (IVertex oVertex in oCommunity2.Vertices)
            {
                oNewCommunityVertices.Add(oVertex);
            }

            // In the following sorted lists, the sort key is the ID of
            // CommunityPair.Community2 and the value is the CommunityPair.

            SortedList<Int32, CommunityPair> oCommunity1CommunityPairs =
                oCommunity1.CommunityPairs;

            SortedList<Int32, CommunityPair> oCommunity2CommunityPairs =
                oCommunity2.CommunityPairs;

            SortedList<Int32, CommunityPair> oNewCommunityCommunityPairs =
                oNewCommunity.CommunityPairs;

            Int32 iCommunity1CommunityPairs = oCommunity1CommunityPairs.Count;
            Int32 iCommunity2CommunityPairs = oCommunity2CommunityPairs.Count;

            IList<Int32> oCommunity1Keys = oCommunity1CommunityPairs.Keys;

            IList<CommunityPair> oCommunity1Values =
                oCommunity1CommunityPairs.Values;

            IList<Int32> oCommunity2Keys = oCommunity2CommunityPairs.Keys;

            IList<CommunityPair> oCommunity2Values =
                oCommunity2CommunityPairs.Values;

            // Step through the community pairs in oCommunity1 and oCommunity2.

            Int32 iCommunity1Index = 0;
            Int32 iCommunity2Index = 0;

            Single fMaximumDeltaQ = Single.MinValue;
            CommunityPair oCommunityPairWithMaximumDeltaQ = null;
            Single fTwoTimesEdgesInGraph = 2F * iEdgesInGraph;

            while (iCommunity1Index < iCommunity1CommunityPairs ||
                   iCommunity2Index < iCommunity2CommunityPairs)
            {
                Int32 iCommunity1OtherCommunityID =
                    (iCommunity1Index < iCommunity1CommunityPairs) ?
                    oCommunity1Keys[iCommunity1Index] : Int32.MaxValue;

                Int32 iCommunity2OtherCommunityID =
                    (iCommunity2Index < iCommunity2CommunityPairs) ?
                    oCommunity2Keys[iCommunity2Index] : Int32.MaxValue;

                CommunityPair oNewCommunityPair = new CommunityPair();
                oNewCommunityPair.Community1 = oNewCommunity;

                if (iCommunity1OtherCommunityID == oCommunity2.ID)
                {
                    // This is an internal connection eliminated by the merge.
                    // Skip it.

                    iCommunity1Index++;
                    continue;
                }
                else if (iCommunity2OtherCommunityID == oCommunity1.ID)
                {
                    // This is an internal connection eliminated by the merge.
                    // Skip it.

                    iCommunity2Index++;
                    continue;
                }
                else if (iCommunity1OtherCommunityID == iCommunity2OtherCommunityID)
                {
                    // The other community is connected to both commmunity 1 and
                    // community 2.
                    //
                    // This is equation 10a from the paper "Finding Community
                    // Structure in Very Large Networks," by Clauset, Newman, and
                    // Moore.

                    oNewCommunityPair.Community2 =
                        oCommunity1Values[iCommunity1Index].Community2;

                    oNewCommunityPair.DeltaQ =
                        oCommunity1Values[iCommunity1Index].DeltaQ +
                        oCommunity2Values[iCommunity2Index].DeltaQ;

                    iCommunity1Index++;
                    iCommunity2Index++;
                }
                else if (iCommunity1OtherCommunityID < iCommunity2OtherCommunityID)
                {
                    // The other community is connected only to commmunity 1.
                    //
                    // This is equation 10b from the same paper.

                    Community oOtherCommunity =
                        oCommunity1Values[iCommunity1Index].Community2;

                    oNewCommunityPair.Community2 = oOtherCommunity;

                    Single fAj = oCommunity2.Degree / fTwoTimesEdgesInGraph;
                    Single fAk = oOtherCommunity.Degree / fTwoTimesEdgesInGraph;

                    oNewCommunityPair.DeltaQ =
                        oCommunity1Values[iCommunity1Index].DeltaQ -
                        2F * fAj * fAk;

                    iCommunity1Index++;
                }
                else
                {
                    // The other community is connected only to commmunity 2.
                    //
                    // This is equation 10c from the same paper.

                    Community oOtherCommunity =
                        oCommunity2Values[iCommunity2Index].Community2;

                    oNewCommunityPair.Community2 = oOtherCommunity;

                    Single fAi = oCommunity1.Degree / fTwoTimesEdgesInGraph;
                    Single fAk = oOtherCommunity.Degree / fTwoTimesEdgesInGraph;

                    oNewCommunityPair.DeltaQ =
                        oCommunity2Values[iCommunity2Index].DeltaQ -
                        2F * fAi * fAk;

                    iCommunity2Index++;
                }

                oNewCommunityCommunityPairs.Add(oNewCommunityPair.Community2.ID,
                    oNewCommunityPair);

                Single fNewCommunityPairDeltaQ = oNewCommunityPair.DeltaQ;

                if (fNewCommunityPairDeltaQ > fMaximumDeltaQ)
                {
                    fMaximumDeltaQ = oNewCommunityPair.DeltaQ;
                    oCommunityPairWithMaximumDeltaQ = oNewCommunityPair;
                }

                // The other community is connected to one or both of the merged
                // communities.  Update it.

                oNewCommunityPair.Community2.OnMergedCommunities(
                    oCommunity1, oCommunity2, oNewCommunity,
                    fNewCommunityPairDeltaQ, oDeltaQMaxHeap);
            }

            oNewCommunity.CommunityPairWithMaximumDeltaQ =
                oCommunityPairWithMaximumDeltaQ;

            // Update the community list.

            oCommunities.Remove(oCommunity1);
            oCommunities.Remove(oCommunity2);
            oCommunities.AddLast(oNewCommunity);

            // Update the max heap.

            oDeltaQMaxHeap.Remove(oCommunity1);
            oDeltaQMaxHeap.Remove(oCommunity2);
            oDeltaQMaxHeap.Add(oNewCommunity, oNewCommunity.MaximumDeltaQ);
        }
        public Boolean TryCalculateClustersWakitaTsurumi
            (IGraph oGraph, BackgroundWorker oBackgroundWorker,out ICollection<Community> oGraphMetrics)
        {
            Debug.Assert(oGraph != null);
            

            IVertexCollection oVertices = oGraph.Vertices;
            Int32 iVertices = oVertices.Count;
            Int32 iEdges = oGraph.Edges.Count;

            IDGenerator oIDGenerator = new IDGenerator(1);

            // Create and populate a community for each of the graph's vertices.

            LinkedList<Community> oCommunities =
                CreateCommunities(oVertices, oIDGenerator);

            oGraphMetrics = oCommunities;

            if (iVertices == 0 || iEdges == 0)
            {
                // There is no point in going any further.

                return (true);
            }

            // This max heap is used to keep track of the maximum delta Q value in
            // each community.  There is an element in the max heap for each
            // community.  The key is the Community and the value is the
            // Community's maximum delta Q.

            DeltaQMaxHeap oDeltaQMaxHeap = new DeltaQMaxHeap(iVertices);

            // Initialize all the delta Q values.

            InitializeDeltaQs(oCommunities, oDeltaQMaxHeap, iEdges);

            // Run the algorithm outlined in the Wakita/Tsurumi paper.

            BinaryHeapItem<Community, Single> oBinaryHeapItemWithMaximumDeltaQ;
            Int32 iMergeCycles = 0;

            // Retrieve the community pair with the largest delta Q.

            while (oDeltaQMaxHeap.TryGetTop(out oBinaryHeapItemWithMaximumDeltaQ))
            {
                // Check for cancellation and report progress every
                // MergeCyclesPerProgressReport calculations.

                if (
                    iMergeCycles % MergeCyclesPerProgressReport == 0
                    &&
                    !ReportProgressAndCheckCancellationPending(
                        iMergeCycles, iVertices, oBackgroundWorker)
                    )
                {
                    return (false);
                }

                Community oCommunityWithMaximumDeltaQ =
                    oBinaryHeapItemWithMaximumDeltaQ.Key;

                Single fMaximumGlobalDeltaQ =
                    oBinaryHeapItemWithMaximumDeltaQ.Value;

                if (fMaximumGlobalDeltaQ < 0)
                {
                    // Merging additional communities would yield worse results, so
                    // quit.

                    break;
                }

                // Merge the communities in the community pair with maximum
                // delta Q, update the maximum delta Q values for all communities,
                // and update the global max heap.

                CommunityPair oCommunityPairWithMaximumDeltaQ =
                    oCommunityWithMaximumDeltaQ.CommunityPairWithMaximumDeltaQ;

                MergeCommunities(oCommunities, oCommunityPairWithMaximumDeltaQ,
                    oDeltaQMaxHeap, iEdges, oIDGenerator);

                iMergeCycles++;
            }

            return (true);
        }
        protected void InitializeDeltaQs
            (LinkedList<Community> oCommunities, DeltaQMaxHeap oDeltaQMaxHeap, Int32 iEdgesInGraph)
        {
            Debug.Assert(oCommunities != null);
            Debug.Assert(oDeltaQMaxHeap != null);
            Debug.Assert(iEdgesInGraph > 0);

            foreach (Community oCommunity in oCommunities)
            {
                // Initialize the delta Q values for the community.

                oCommunity.InitializeDeltaQs(oCommunities, iEdgesInGraph);

                Single fMaximumDeltaQ = oCommunity.MaximumDeltaQ;

                if (fMaximumDeltaQ != Community.DeltaQNotSet)
                {
                    // Store the community's maximum delta Q on the max heap.

                    oDeltaQMaxHeap.Add(oCommunity, fMaximumDeltaQ);
                }
            }
        }