//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>); } } }
/// <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); }