public void Insert_MinHeap_InsertsAtProperPosition() { // Arrange var heap = new Heap <Element>(true); var values = new List <int> { 1, 2, 3, 4, 7, 8, 9, 10, 14, 16 } .OrderBy(a => Guid.NewGuid()) // order randomly .ToList(); var elements = new List <Element>(); elements.AddRange(values.Select(i => new Element(i))); heap.BuildHeap(elements); // Act heap.Insert(new Element(18)); // Assert var A = heap.A; Assert.True(A[0].Key == 1); Assert.True( A.Select((e, i) => i == 0 || A[heap.Parent(i)].CompareTo(A[i]) < 0) .All(e => true)); }
public T Dequeue() { if (_items.Count > 0) { T result = _items[0]; _items.RemoveAt(0); Heap.BuildHeap(_items, _items.Count, this.comparer); return(result); } throw new IndexOutOfRangeException(); }
public bool TryDequeue(out T result) { if (_items.Count > 0) { result = _items[0]; _items.RemoveAt(0); Heap.BuildHeap(_items, _items.Count, this.comparer); return(true); } else { result = default(T); return(false); } }
public static int[] HeapSort(int[] UnorderedArray, SortDirection Direction) { int[] sortedArray = new int[UnorderedArray.Length]; Heap tempHeap; if (Direction == SortDirection.Increasing) { tempHeap = Heap.BuildHeap(UnorderedArray, HeapType.MinHeap); } else { tempHeap = Heap.BuildHeap(UnorderedArray, HeapType.MaxHeap); } int index = 0; while (tempHeap.Size > 0) { sortedArray[index++] = tempHeap.PopRoot(); tempHeap.Heapify(0); } return(sortedArray); }
public void BuildHeap_MinHeapGivenNonEmptyArray_BuildsMinHeap() { // Arrange var heap = new Heap <Element>(true); var values = new List <int> { 1, 2, 3, 4, 7, 8, 9, 10, 14, 16 } .OrderBy(a => Guid.NewGuid()) // order randomly .ToList(); var elements = new List <Element>(); elements.AddRange(values.Select(i => new Element(i))); // Act heap.BuildHeap(elements); // Assert var A = heap.A; Assert.True( A.Select((e, i) => i == 0 || A[heap.Parent(i)].CompareTo(A[i]) < 0) .All(e => true)); }
public void HeapTest() { Comparison <int> comparison = (x, y) => x.CompareTo(y); for (int i = 0; i < 100; i++) { for (int j = 0; j < 100; j++) { int[] data = ArrayUtilities.CreateRandomArray(j, 0, 1000); Heap.BuildHeap(data, comparison); for (int k = 0; k < data.Length; k++) { int left = Heap.Left(k); int right = Heap.Right(k); int parent = Heap.Parent(k); Assert.IsTrue(k == 0 || comparison(data[k], data[parent]) <= 0); Assert.IsTrue(left >= data.Length || comparison(data[left], data[k]) <= 0); Assert.IsTrue(right >= data.Length || comparison(data[left], data[k]) <= 0); } } } }
// this is the algortithm for returning the shortest path public List <GraphNode> Dijkstra(Vector2_int start, Vector2_int end) // return the shortest path { //Debug.Log("Marker 1"); if (start.x < 0 || start.x >= col || end.x < 0 || end.x >= col || start.y < 0 || start.y >= row || end.y < 0 || end.y >= row) // the position is not inside board { return(null); } GraphNode[][] graphNodes = GraphSetup(); Heap graphheap = new Heap(); graphheap.BuildHeap(graphNodes); List <GraphNode> result = null; //Debug.Log("Marker 3"); graphNodes[start.x][start.y].previous = null; graphNodes[start.x][start.y].cost = 0; graphheap.PercolateUp(graphNodes[start.x][start.y].mapping_index); //Debug.Log("Marker 4"); //FileStream fs = new FileStream("Running.log", FileMode.Append); //byte[] buffer = Encoding.ASCII.GetBytes("Marker 4 \n"); //fs.Write(buffer, 0, buffer.Length); //fs.Close(); GraphNode tmp = null; while (graphheap.currentSize > 0) { tmp = graphheap.DeleteMin(); if (tmp.cost == int.MaxValue || System.Object.ReferenceEquals(tmp, graphNodes[end.x][end.y]) || tmp.cost >= graphNodes[end.x][end.y].cost) // all infinite path to the end or already hit the last { break; } foreach (GraphNode node in tmp.adjacent) { if (node.mapping_index > 0 && !obstacle_level.ContainsKey(node.pos) && tmp.cost + 1 < node.cost) // it's in the unvisited set (graph heap), and not the wall tile, and really lowers the cost { node.cost = tmp.cost + 1; node.previous = tmp; graphheap.PercolateUp(node.mapping_index); //for(int j = 1; j < 5; j++) //{ // Debug.Log(" j th cost is " + graphheap.heap_arr[j].pos.x + " " + graphheap.heap_arr[j].pos.y + " " + graphheap.heap_arr[j].cost + " " + graphheap.heap_arr[j].visited); //} } } } //Debug.Log("Marker 5"); //fs = new FileStream("Running.log", FileMode.Append); //buffer = Encoding.ASCII.GetBytes("Marker 5 \n"); //fs.Write(buffer, 0, buffer.Length); if (graphNodes[end.x][end.y].cost != int.MaxValue) { tmp = graphNodes[end.x][end.y]; result = new List <GraphNode>(); while (tmp != null) { result.Insert(0, tmp); //Debug.Log("Path is " + tmp.cost + tmp.mapping_index + "\n"); //fs.Write(buffer, 0, buffer.Length); tmp = tmp.previous; } } //Debug.Log("Cost is " + graphNodes[end.x][end.y].cost); //buffer = Encoding.ASCII.GetBytes("Cost is " + graphNodes[end.x][end.y].cost + "\n"); //fs.Write(buffer, 0, buffer.Length); //fs.Close(); //GraphNodeSettoDefault(); return(result); }
public void Enqueue(T item) { _items.Add(item); Heap.BuildHeap(_items, _items.Count, this.comparer); }
public PriorityQueue(IEnumerable <T> collection, Func <T, T, bool> comparer) { _items = new List <T>(collection); this.comparer = comparer; Heap.BuildHeap(_items, _items.Count, this.comparer); }