public MinHeapNode BuildHuffmanTree(char[] data, int[] freq, int size) { MinHeapNode left, right, top; MinHeap minHeap = CreateAndBuildMinHeap(data, freq, size); while (!IsSizeOne(minHeap)) { // extract two min freq items from min heap left = ExtractMin(minHeap); right = ExtractMin(minHeap); top = NewNode('$', left.Freq + right.Freq); top.Left = left; top.Right = right; InsertMinHeap(minHeap, top); } return(ExtractMin(minHeap)); }
public void MinHeap_Test() { var minHeap = new MinHeap <int>(); minHeap.Insert(5); minHeap.Insert(3); minHeap.Insert(1); minHeap.Insert(4); minHeap.Insert(2); var result = minHeap.ExtractMin(); Assert.That(result, Is.EqualTo(1)); var secondResult = minHeap.ExtractMin(); Assert.That(secondResult, Is.EqualTo(2)); }
//for directed Graph public static IReadOnlyDictionary <GraphNode <T>, GraphNode <T> > RunDijkstraFrom(DirectedGraph <T> graph, T src) { if (!graph.Nodes.TryGetValue(new GraphNode <T>(src), out var source)) { return(null); } var predecessors = new Dictionary <GraphNode <T>, GraphNode <T> >();//keeps track of the shortest path predecessor //using Dijkstra's algorithm var priorityQ = new MinHeap <GraphNode <T> >(new GraphNode <T>(default(T))); foreach (var node in graph.Nodes) { node.Weight = source.Equals(node) ? 0 : int.MaxValue; priorityQ.Add(node); predecessors[source] = null; } while (priorityQ.Count != 0) { var minNode = priorityQ.GetMin(); if (!graph.Neighbors.ContainsKey(minNode)) { priorityQ.ExtractMin(); continue; } foreach (var neighbor in graph.Neighbors[minNode]) { graph.Nodes.TryGetValue(neighbor, out var neighborNode); var edges = graph.GetEdges(minNode, neighbor); foreach (var edge in edges) { if (neighborNode.Weight > minNode.Weight + edge.Weight) { neighborNode.Weight = minNode.Weight + edge.Weight; predecessors[neighborNode] = minNode; } } } priorityQ.ExtractMin(); } return(predecessors); }
/// <summary> /// Runtime O(n * log(n)) /// </summary> /// <returns>The kth smallest.</returns> /// <param name="arr">Arr.</param> /// <param name="k">K.</param> public int FindKthSmallest(int[] arr, int k) { // push into a heap var minHeap = new MinHeap <int>(); for (var i = 0; i < arr.Length; i++) { minHeap.Insert(arr[i]); } // what is insert run time for a heap - log(n) var min = int.MaxValue; for (int i = 0; i < k; i++) { min = minHeap.ExtractMin(); } return(min); }
public void MinHeapify(MinHeap minHeap, int index) { int smallest = index; int left = 2 * index + 1; int right = 2 * index + 2; if (left < minHeap.Size && minHeap.Nodes[left].Freq < minHeap.Nodes[smallest].Freq) { smallest = left; } if (right < minHeap.Size && minHeap.Nodes[right].Freq < minHeap.Nodes[smallest].Freq) { smallest = right; } if (smallest != index) { SwapMinHeapNode(minHeap.Nodes, smallest, index); MinHeapify(minHeap, smallest); } }
public void MinimumSpanningTreePrim(VertexMST <T> root) { foreach (var vertex in vertices.OfType <VertexMST <T> >()) { vertex.Key = int.MaxValue; vertex.Predecessor = null; } root.Key = 0; var heap = new MinHeap <VertexMST <T> >(vertices.Count, vertices.OfType <VertexMST <T> >().ToArray()); while (!heap.IsEmpty) { //had to call heapify before extract min since it's not a min heap anymore due to the change of keys //instead of decrease key heap.Heapify(0); var min = heap.Extract(); foreach (var edge in edges.Where((e) => e.From.Equals(min) || e.To.Equals(min))) { if (edge.To.Equals(min)) { if (heap.Contains(edge.From as VertexMST <T>) && edge.Weight < (edge.From as VertexMST <T>).Key) { edge.From.Predecessor = min; (edge.From as VertexMST <T>).Key = edge.Weight; } } else { if (heap.Contains(edge.To as VertexMST <T>) && edge.Weight < (edge.To as VertexMST <T>).Key) { edge.To.Predecessor = min; (edge.To as VertexMST <T>).Key = edge.Weight; } } } } }
//edges' weights cannot be negative public void Dijkstra(IVertex <T> source) { Initialize(source); //var set = new List<VertexSSSP<T>>(); var heap = new MinHeap <VertexSSSP <T> >(vertices.Count + edges.Count); heap.Insert(source as VertexSSSP <T>); while (!heap.IsEmpty) { var vertex = heap.Extract(); if (vertex.Visited) { continue; } vertex.Visited = true; //set.Add(node.Key); foreach (var edge in edges.Where((e) => e.From.Equals(vertex))) { Relax(edge.From, edge.To, edge.Weight); heap.Insert(edge.To as VertexSSSP <T>); } } }
public bool IsSizeOne(MinHeap minHeap) { return(minHeap.Size == 1); }