public void MyTestInitialize() { my_b_minheap = new BinaryHeap <DSInteger>(true); my_b_maxheap = new BinaryHeap <DSInteger>(false); addHeapItems(my_b_minheap); addHeapItems(my_b_maxheap); }
//---------------- HELPER METHODS ----------------- //sets up the queue initially private void setupQueueSize(bool the_min_queue, int the_size, T[] the_initial_elements, Comparator <T> the_comparator) { //flexible setup that lets the user choose Comparable or Comparator as a strategy try { if (the_comparator == null) { if (the_initial_elements == null) { my_heap = new BinaryHeap <T>(the_min_queue, the_size); } else { my_heap = new BinaryHeap <T>(the_min_queue, the_initial_elements); } } else { if (the_initial_elements == null) { my_heap = new BinaryHeap <T>(the_min_queue, the_size, the_comparator); } else { my_heap = new BinaryHeap <T>(the_min_queue, the_initial_elements, the_comparator); } } } catch (ClassCastException the_ex) //this can be thrown by buildheap() { throw new ClassCastException("You did not provide a comparator to the priority queue and your " + "elements are not comparable."); } }
private void testConstructors(BasicHeap <DSInteger> the_heap) { the_heap = new BinaryHeap <DSInteger>(true, new DSInteger[] { new DSInteger(30), new DSInteger(40), new DSInteger(60), new DSInteger(20), new DSInteger(10), new DSInteger(50) }); //make sure the count is correct Assert.AreEqual(6, the_heap.size()); //min heap if (the_heap.min_heap) { //make sure the items can be removed correctly after adding Assert.AreEqual(10, the_heap.deleteMin().value); Assert.AreEqual(20, the_heap.deleteMin().value); Assert.AreEqual(30, the_heap.deleteMin().value); Assert.AreEqual(40, the_heap.deleteMin().value); Assert.AreEqual(50, the_heap.deleteMin().value); Assert.AreEqual(60, the_heap.deleteMin().value); } else //max heap { //make sure the items can be removed correctly after adding Assert.AreEqual(60, the_heap.deleteMin().value); Assert.AreEqual(50, the_heap.deleteMin().value); Assert.AreEqual(40, the_heap.deleteMin().value); Assert.AreEqual(30, the_heap.deleteMin().value); Assert.AreEqual(20, the_heap.deleteMin().value); Assert.AreEqual(10, the_heap.deleteMin().value); } }
private void testAdd(BasicHeap <DSInteger> the_heap) { //min heap if (the_heap.min_heap) { Assert.AreEqual(5, the_heap.deleteMin().value); Assert.AreEqual(10, the_heap.deleteMin().value); Assert.AreEqual(20, the_heap.deleteMin().value); Assert.AreEqual(30, the_heap.deleteMin().value); Assert.AreEqual(40, the_heap.deleteMin().value); Assert.AreEqual(50, the_heap.deleteMin().value); Assert.AreEqual(60, the_heap.deleteMin().value); Assert.AreEqual(70, the_heap.deleteMin().value); Assert.AreEqual(80, the_heap.deleteMin().value); Assert.AreEqual(90, the_heap.deleteMin().value); Assert.AreEqual(100, the_heap.deleteMin().value); Assert.AreEqual(110, the_heap.deleteMin().value); } else //max heap { Assert.AreEqual(110, the_heap.deleteMin().value); Assert.AreEqual(100, the_heap.deleteMin().value); Assert.AreEqual(90, the_heap.deleteMin().value); Assert.AreEqual(80, the_heap.deleteMin().value); Assert.AreEqual(70, the_heap.deleteMin().value); Assert.AreEqual(60, the_heap.deleteMin().value); Assert.AreEqual(50, the_heap.deleteMin().value); Assert.AreEqual(40, the_heap.deleteMin().value); Assert.AreEqual(30, the_heap.deleteMin().value); Assert.AreEqual(20, the_heap.deleteMin().value); Assert.AreEqual(10, the_heap.deleteMin().value); Assert.AreEqual(5, the_heap.deleteMin().value); } }
private void testClear(BasicHeap <DSInteger> the_heap) { Assert.AreEqual(false, the_heap.isEmpty()); Assert.AreEqual(true, the_heap.size() > 0); the_heap.clear(); Assert.AreEqual(true, the_heap.isEmpty()); Assert.AreEqual(0, the_heap.size()); }
private void testToString(BasicHeap <DSInteger> the_heap) { //just test for no exceptions, leave the output to the programmer, user try { the_heap.ToString(); } catch (Exception the_ex) { Assert.Fail(); } }
//---------------- HELPER METHODS ----------------- private void addHeapItems(BasicHeap <DSInteger> the_heap) { the_heap.add(new DSInteger(30)); the_heap.add(new DSInteger(100)); the_heap.add(new DSInteger(40)); the_heap.add(new DSInteger(10)); the_heap.add(new DSInteger(60)); the_heap.add(new DSInteger(70)); the_heap.add(new DSInteger(20)); the_heap.add(new DSInteger(110)); the_heap.add(new DSInteger(50)); the_heap.add(new DSInteger(5)); the_heap.add(new DSInteger(90)); the_heap.add(new DSInteger(80)); }
private void testDeleteMin(BasicHeap <DSInteger> the_heap) { //removing of the default items handled in testAdd() while (!the_heap.isEmpty()) { the_heap.deleteMin(); } Assert.AreEqual(null, the_heap.deleteMin()); Assert.AreEqual(true, the_heap.isEmpty()); //adding items after all are removed, then check for correct state the_heap.add(new DSInteger(5)); Assert.AreEqual(5, the_heap.deleteMin().value); Assert.AreEqual(null, the_heap.deleteMin()); Assert.AreEqual(true, the_heap.isEmpty()); Assert.AreEqual(true, the_heap.isEmpty()); }
private void testContains(BasicHeap <DSInteger> the_heap) { //make sure the correct items are in the queue Assert.AreEqual(true, the_heap.contains(new DSInteger(5))); Assert.AreEqual(true, the_heap.contains(new DSInteger(10))); Assert.AreEqual(true, the_heap.contains(new DSInteger(20))); Assert.AreEqual(true, the_heap.contains(new DSInteger(30))); Assert.AreEqual(true, the_heap.contains(new DSInteger(40))); Assert.AreEqual(true, the_heap.contains(new DSInteger(50))); Assert.AreEqual(true, the_heap.contains(new DSInteger(60))); Assert.AreEqual(true, the_heap.contains(new DSInteger(70))); Assert.AreEqual(true, the_heap.contains(new DSInteger(80))); Assert.AreEqual(true, the_heap.contains(new DSInteger(90))); Assert.AreEqual(true, the_heap.contains(new DSInteger(100))); Assert.AreEqual(true, the_heap.contains(new DSInteger(110))); //make sure no false positives are given Assert.AreEqual(false, the_heap.contains(new DSInteger(120))); }
private void testPeek(BasicHeap <DSInteger> the_heap) { if (the_heap.min_heap) //min heap { Assert.AreEqual(5, the_heap.peek().value); Assert.AreEqual(5, the_heap.peek().value); the_heap.deleteMin(); Assert.AreEqual(10, the_heap.peek().value); Assert.AreEqual(10, the_heap.peek().value); } else //max heap { Assert.AreEqual(110, the_heap.peek().value); Assert.AreEqual(110, the_heap.peek().value); the_heap.deleteMin(); Assert.AreEqual(100, the_heap.peek().value); Assert.AreEqual(100, the_heap.peek().value); } }
private void testSize(BasicHeap <DSInteger> the_heap) { //check initial size Assert.AreEqual(12, the_heap.size()); //make sure the size alters when removing the_heap.deleteMin(); Assert.AreEqual(11, the_heap.size()); //make sure the size reduces itself to zero while (!the_heap.isEmpty()) { the_heap.deleteMin(); } Assert.AreEqual(0, the_heap.size()); //make sure the size does not go below 0 the_heap.deleteMin(); Assert.AreEqual(0, the_heap.size()); }
public Results Perform(int start) { float[] d = GetStartingTraversalCost(start); int[] p = GetStartingBestPath(start); BasicHeap Q = new BasicHeap(); debugText.text += "\n<color=#800000>Starting Traversal Cost from node 0</color>"; for (int i = 0; i != TotalNodeCount; i++) { Q.Push(i, d [i]); if (d[i] != Mathf.Infinity) { debugText.text += "\n<color=#0000ff>node " + i + " = " + d[i] + "</color>"; } else { debugText.text += "\n<color=#0000ff>node " + i + " = " + d[i] + "</color>"; } } debugText.text += "\n<color=#800000>Start Main Loop</color>"; while (Q.Count > 0) { int v = Q.Pop(); debugText.text += "\n<color=blue>[---CEK NODE " + v + "---] \n> Remaining Q = " + Q.Count + "] : </color> "; for (int i = 0; i < Q.Count; i++) { debugText.text += "\nnode " + Q.getIndex(i) + " : "; debugText.text += "cost : " + Q.getWight(i); } debugText.text += "\n<color=blue>> Cek Nearby dari node " + v + " : </color>"; foreach (int w in nodeConfigurator.GetNearbyNodes(v)) { debugText.text += "\nnode " + v + " > node " + w; float cost = nodeConfigurator.getTraversalCost(v, w); nodeConfigurator.markCurrentPath(v, w); debugText.text += ", cost = " + cost; if (d[v] + cost < d[w]) { debugText.text += "\nTravecost dari " + v + " > " + w + " = " + (d[v] + cost) + "\n<color=#660066>Lebih kecil dari cost yg sekarang = " + d[w] + "</color>"; d[w] = d[v] + cost; debugText.text += "\n> Set cost : dr node sebelumnya > " + v + " > " + w + " = " + d[w]; debugText.text += "\n> Set best path menuju " + w + " dari = " + p[w] + " menjadi = " + v; p[w] = v; debugText.text += ">\nPush index = " + w + ", weight = " + d[w] + " ke Q untuk dicek selanjutnya"; Q.Push(w, d[w]); debugText.text += "\njadi Q = " + Q.Count; } else if (d[v] + cost == d[w]) { debugText.text += "\nTravecost dari " + v + " > " + w + " = " + (d[v] + cost) + "\n<color=#006600>Harga yang sama dari cost yg sekarang = " + d[w] + "</color>"; } else if (d[v] + cost > d[w]) { debugText.text += "\nTravecost dari start > " + v + " > " + w + " = " + (d[v] + cost) + "\n<color=#006600>Harga yang lebih besar dari cost yg sekarang = " + d[w] + "</color>"; } } } debugText.text += "\n<color=#800000>Node hasil perhitungan</color>"; for (int i = 0; i < p.Length; i++) { debugText.text += "\nStart > node " + i + " melalui node " + p[i]; } debugText.text += "\n<color=#800000>Distance dari start ke masing2 node</color>"; for (int i = 0; i < d.Length; i++) { debugText.text += "\n> Start > " + i + " = " + d[i]; } return(new Results(p, d)); }
//-------------method struct untuk menentukan hasil djikstra public Results Perform(int start) { //get starting travelcost float[] d = GetStartingTraversalCost(start); //set bestpath, set semua menjadi start, misal 0 int[] p = GetStartingBestPath(start); // define basic heap BasicHeap Q = new BasicHeap(); //tampilkan di log debugText.text += "\n<color=#800000>Starting Traversal Cost from node 0</color>"; //jika i != total node for (int i = 0; i != TotalNodeCount; i++) { //masukkan nilai starting path & starting travesalcost kedalam heap Q.Push(i, d [i]); //tampilkan di log if (d[i] != Mathf.Infinity) { // neighbor dr starting node debugText.text += "\n<color=#0000ff>node " + i + " = " + d[i] + "</color>"; } else { //node yang belum terjamah debugText.text += "\n<color=#0000ff>node " + i + " = " + d[i] + "</color>"; } } //tampilkan di debug debugText.text += "\n<color=#800000>Start Main Loop</color>"; // jika Q (node yang harus di cek) lebih dari 0 while (Q.Count > 0) { // buat variable v (visited / current node) dari nilai heap paling awal int v = Q.Pop(); //tampilkan di log debugText.text += "\n<color=blue>[---CEK NODE " + v + "---] \n> Remaining Q = " + Q.Count + "] : </color> "; // tampilkan di log node dan traversalcost ned yang belum terjamah / yg harus di cek for (int i = 0; i < Q.Count; i++) { //tampilkan di log debugText.text += "\nnode " + Q.getIndex(i) + " : "; debugText.text += "cost : " + Q.getWight(i); } //tampilkan di log debugText.text += "\n<color=blue>> Cek Nearby dari node " + v + " : </color>"; //untuk setiap neighbor dari node yang sedang di cek foreach (int w in nodeConfigurator.GetNearbyNodes(v)) { //tampilkan di log debugText.text += "\nnode " + v + " > node " + w; //tentukan traversalcost dari current node ke neighbor float cost = nodeConfigurator.getTraversalCost(v, w); //tandai node yang telah dilalui nodeConfigurator.markCurrentPath(v, w); //tampilkan di log : debugText.text += ", cost = " + cost; //jika traversal cost V + cost ke neighbor yg sedang di cek 'w' < dari traversal cost sebelumnya if (d[v] + cost < d[w]) { //tampilkan di log debugText.text += "\nTravecost dari " + v + " > " + w + " = " + (d[v] + cost) + "\n<color=#660066>Lebih kecil dari cost yg sekarang = " + d[w] + "</color>"; //masukan nilai baru hasil pertambahan current ke neighbor yang di cek ke dalam travelcost yng sedang di cek d[w] = d[v] + cost; //tampilkan di log debugText.text += "\n> Set cost : dr node sebelumnya > " + v + " > " + w + " = " + d[w]; debugText.text += "\n> Set best path menuju " + w + " dari = " + p[w] + " menjadi = " + v; // set best path menjadi node yang di cek p[w] = v; //tampilkan di log debugText.text += ">\nPush index = " + w + ", weight = " + d[w] + " ke Q untuk dicek selanjutnya"; //push node dan weightnya ke heap Q.Push(w, d[w]); //tampilkan di log : debugText.text += "\njadi Q = " + Q.Count; //jika perbandingan harganya sama } else if (d[v] + cost == d[w]) { //tampilkan di log : debugText.text += "\nTravecost dari " + v + " > " + w + " = " + (d[v] + cost) + "\n<color=#006600>Harga yang sama dari cost yg sekarang = " + d[w] + "</color>"; //jika perbandingan harganya lebih besar } else if (d[v] + cost > d[w]) { //tampilkan di log : debugText.text += "\nTravecost dari start > " + v + " > " + w + " = " + (d[v] + cost) + "\n<color=#006600>Harga yang lebih besar dari cost yg sekarang = " + d[w] + "</color>"; } } } //tampilkan di log debugText.text += "\n<color=#800000>Node hasil perhitungan</color>"; // cek masing2 nilai p sekarang for (int i = 0; i < p.Length; i++) { //tampilkan di log debugText.text += "\nStart > node " + i + " melalui node " + p[i]; } //tampilkan di log : debugText.text += "\n<color=#800000>Distance dari start ke masing2 node</color>"; //cek nilai d baru for (int i = 0; i < d.Length; i++) { //tampilkan di log : debugText.text += "\n> Start > " + i + " = " + d[i]; } //return nilia p dan d return(new Results(p, d)); }