public void MinHeap_ExtractRoot_ReturnsMin() { List<int> testNumbers = GetTestNumbers(); MinHeap<int> heap = new MinHeap<int>(); heap.AddRange(testNumbers); int expected = testNumbers.Min(); int min = heap.ExtractRoot(); Assert.AreEqual(expected, min); }
/// <summary> /// Executes non-recursive Dijkstra for finding out shortest path to all the notes of a given graph from a given node. /// </summary> /// <param name="vertexList">Vertex list.</param> /// <param name="start">Node to find shortest paths from.</param> /// <returns>A pair of arrays, first - containing shortest path costs to each of the nodes, second - parent nodes visited from a given node (for traceability of the path).</returns> public static Tuple <int[], int[]> Dijkstra( Vertex[] vertexList, Vertex start) { var n = vertexList.Length; var infinity = int.MaxValue - 1000; var distance = new int[n]; var previous = new int[n]; var queue = new MinHeap <int>((x, y) => distance[x] - distance[y]); for (var i = 0; i < n; i++) { distance[i] = infinity; previous[i] = -1; } distance[start.Index] = 0; queue.AddRange(Enumerable.Range(0, n)); while (queue.Count > 0) { var u = queue.PeekMin(); foreach (var edge in vertexList[u].Edges) { var v = edge.ToVertex.Index; var newDistance = distance[u] + (edge.Weight != null ? edge.Weight.Value : 0); if (newDistance < distance[v]) { distance[v] = newDistance; previous[v] = u; } } queue.ExtractMin(); } return(new Tuple <int[], int[]>(distance, previous)); }
public static Tuple <int[], int[]> Dijkstra( Vertex[] vertexList, Vertex start) { int n = vertexList.Length; int[] distance = new int[n]; int[] previous = new int[n]; MinHeap <int> queue = new MinHeap <int>((x, y) => distance[x] - distance[y]); for (int i = 0; i < n; i++) { distance[i] = Infinity; previous[i] = -1; } distance[start.Index] = 0; queue.AddRange(Enumerable.Range(0, n)); while (queue.Count > 0) { int u = queue.ExtractMin(); foreach (Edge edge in vertexList[u].Edges) { int v = edge.ToVertex.Index; int newDistance = distance[u] + (edge.Weight != null ? edge.Weight.Value : 0); if (newDistance < distance[v]) { distance[v] = newDistance; previous[v] = u; queue.Reprioritize(v); } } } return(new Tuple <int[], int[]>(distance, previous)); }
/// <summary> /// Executes non-recursive Dijkstra for finding out shortest path to all the notes of a given graph from a given node. /// </summary> /// <param name="adjacencyList">Adjacency list.</param> /// <param name="weights">Edge weights.</param> /// <param name="start">Node to find shortest paths from.</param> /// <returns>A pair of arrays, first - containing shortest path costs to each of the nodes, second - parent nodes visited from a given node (for traceability of the path).</returns> public static Tuple <int[], int[]> Dijkstra( int[][] adjacencyList, int[,] weights, int start) { var n = adjacencyList.Length; var infinity = int.MaxValue - 1000; var distance = new int[n]; var previous = new int[n]; var queue = new MinHeap <int>((x, y) => distance[x] - distance[y]); for (var i = 0; i < n; i++) { distance[i] = infinity; previous[i] = -1; } distance[start] = 0; queue.AddRange(Enumerable.Range(0, n)); while (queue.Count > 0) { var u = queue.PeekMin(); for (var i = 0; i < adjacencyList[u].Length; i++) { var v = adjacencyList[u][i]; var newDistance = distance[u] + weights[u, v]; if (newDistance < distance[v]) { distance[v] = newDistance; previous[v] = u; } } queue.ExtractMin(); } return(new Tuple <int[], int[]>(distance, previous)); }
public static Tuple <int[], int[]> Dijkstra( int[][] adjacencyList, int[,] weights, int start) { int n = adjacencyList.Length; int[] distance = new int[n]; int[] previous = new int[n]; MinHeap <int> queue = new MinHeap <int>((x, y) => distance[x] - distance[y]); for (int i = 0; i < n; i++) { distance[i] = Infinity; previous[i] = -1; } distance[start] = 0; queue.AddRange(Enumerable.Range(0, n)); while (queue.Count > 0) { int u = queue.ExtractMin(); for (int i = 0; i < adjacencyList[u].Length; i++) { int v = adjacencyList[u][i]; int newDistance = distance[u] + weights[u, v]; if (newDistance < distance[v]) { distance[v] = newDistance; previous[v] = u; queue.Reprioritize(v); } } } return(new Tuple <int[], int[]>(distance, previous)); }