public int minDeletionsToMakeFrequencyOfEachLetterUnique(string s)
        {
            // counter of characters to delete
            int count = 0;
            // array of counters of occurrences for all possible characters
            Dictionary <char, int> freq = new Dictionary <char, int>();

            foreach (char c in s)
            {
                if (freq.ContainsKey(c))
                {
                    freq[c]++;
                }
                else
                {
                    freq.Add(c, 1);
                }
            }

            var heap = new C5.IntervalHeap <int>();

            heap.AddAll(freq.Select(i => i.Value));

            while (heap.Count > 0)
            {
                // take the biggest frequency of a character
                int most_frequent = heap.FindMax();

                heap.DeleteMax();

                if (heap.Count == 0)
                {
                    return(count);
                }
                // if this frequency is equal to the next one
                // and bigger than 1 decrease it to 1 and put
                // back to the queue
                if (most_frequent == heap.FindMax())
                {
                    if (most_frequent > 1)
                    {
                        heap.Add(most_frequent - 1);
                    }
                    count++;
                }
                // all frequencies which are bigger than
                // the next one are removed from the queue
                // because they are unique
            }
            return(count);
        }
        public CustomWeightedGraph MinimumSpanTree()
        {
            // Prim's Algorithm
            var minSpanTree        = new CustomWeightedGraph();
            var totalNodes         = GetTotalNodes();
            var nodes              = new HashSet <Node>();
            var edgesPriorityQueue = new C5.IntervalHeap <Edge>(new EdgeComp());

            if (itemsMap.Count == 0)
            {
                return(minSpanTree);            // Error Handling
            }
            var node = itemsMap.Values.First(); // Randomly pick the first node to add to Minimum Span Tree

            nodes.Add(node);
            minSpanTree.AddNode(node.label);
            edgesPriorityQueue.AddAll(node.GetEdges());

            while (nodes.Count < totalNodes)
            {
                var minEdge = edgesPriorityQueue.DeleteMax();

                if (minSpanTree.ContainNode(minEdge.to.label) && minSpanTree.ContainNode(minEdge.from.label))
                {
                    continue;
                }

                node = minEdge.to;
                nodes.Add(node);

                minSpanTree.AddNode(node.label);
                minSpanTree.AddEdge(minEdge.from.label, minEdge.to.label, minEdge.weight);
                edgesPriorityQueue.AddAll(node.GetEdges().Where(e => !minSpanTree.ContainNode(e.to.label)));
            }
            return(minSpanTree);
        }
Beispiel #3
0
 public void C5_IntervalHeap() => c5_intervalheap.AddAll(Items);