public HeapNode deleteMin() { HeapNode n; if (queueSize == 0) { return(null); } else if (queueSize == 1) { queueSize = 0; HeapNode temp = myQueue[1]; myQueue[1] = new HeapNode(double.MaxValue, -1); //pointer array stuff //-1 means it's not on the queue anymore nodeInfo[temp.getPointListIndex()].setHeapIndex(-1); return(temp); } else { n = myQueue[1]; HeapNode temp = myQueue[queueSize]; myQueue[queueSize] = new HeapNode(double.MaxValue, -1); queueSize--; siftDown(temp, 1); //pointer array stuff nodeInfo[n.getPointListIndex()].setHeapIndex(-1); return(n); } }
//all paths constructor public BinaryHeap(int size, Node[] info, int start, int toMakeTwoConstructors) { nodeInfo = info; myQueue = new HeapNode[size]; //put the start node on the queue myQueue[1] = new HeapNode(0, start); nodeInfo[start].setKey(0); nodeInfo[start].setHeapIndex(1); //insert all the rest of the nodes int j = 0; for (int i = 2; i < myQueue.Count(); i++) { if (j != start) { myQueue[i] = new HeapNode(double.MaxValue, j); nodeInfo[j].setHeapIndex(i); } else { j++; myQueue[i] = new HeapNode(double.MaxValue, j); nodeInfo[j].setHeapIndex(i); } j++; } queueSize = size - 1; }
Node[] nodeInfo; //a pointer to an array in the solveButton_click function //one path constructor public BinaryHeap(int size, Node[] info, int start) { nodeInfo = info; myQueue = new HeapNode[size]; //put the start node on the queue myQueue[1] = new HeapNode(0, start); nodeInfo[start].setKey(0); nodeInfo[start].setHeapIndex(1); queueSize = 1; }
private void SwapIndices(int i1, int i2) { // Swap lookupTable values. lookupTable[heap[i1].LOOKUPINDEX].heapIndex = i2; lookupTable[heap[i2].LOOKUPINDEX].heapIndex = i1; HeapNode tempNode = heap[i1]; // Perform Node swap. heap[i1] = heap[i2]; heap[i2] = tempNode; }
public int PopMin() { HeapNode minNode = heap[0]; heap[0] = heap[heap.Count - 1]; lookupTable[heap[0].LOOKUPINDEX].heapIndex = 0; lookupTable[heap[0].LOOKUPINDEX].pathCost = minNode.pathCost; heap.RemoveAt(heap.Count - 1); lookupTable[minNode.LOOKUPINDEX].backPointer = minNode.backPointer; BubbleDown(0); return(minNode.LOOKUPINDEX); }
public void bubbleUp(HeapNode n, int i) { int p = (i / 2); while ((i != 1) && (myQueue[p].getKey() > n.getKey())) { myQueue[i] = myQueue[p]; //pointer array stuff nodeInfo[myQueue[i].getPointListIndex()].setHeapIndex(i); i = p; p = (i / 2); } myQueue[i] = n; //pointer array stuff nodeInfo[n.getPointListIndex()].setHeapIndex(i); }
public void siftDown(HeapNode n, int i) { int c = minChild(i); while ((c != 0) && (myQueue[c].getKey() < n.getKey())) { myQueue[i] = myQueue[c]; //pointer array stuff nodeInfo[myQueue[i].getPointListIndex()].setHeapIndex(i); i = c; c = minChild(i); } myQueue[i] = n; //pointer array stuff nodeInfo[n.getPointListIndex()].setHeapIndex(i); }
public void allPaths(bool foundEnd, int numPoints) { //start timer System.Diagnostics.Stopwatch timeAll = new System.Diagnostics.Stopwatch(); timeAll.Start(); //these indices correspond with the points, keep track of points, keys, and place in the queue Node[] nodeInfoAll = new Node[numPoints]; for (int i = 0; i < numPoints; i++) { nodeInfoAll[i] = new Node(points[i], double.MaxValue, -1); } BinaryHeap bhAll = new BinaryHeap(numPoints + 1, nodeInfoAll, startNodeIndex, 0); while (bhAll.getQueueSize() > 0) { HeapNode n = bhAll.deleteMin(); int i = n.getPointListIndex(); //find out all the other nodes this is connected to, decrease keys PointF a = points[i]; foreach (int edge in adjacencyList[i]) { PointF b = points[edge]; double dist = Math.Sqrt(Math.Pow(a.X - b.X, 2) + Math.Pow(a.Y - b.Y, 2)); if ((n.getKey()) + dist < nodeInfoAll[edge].getKey()) { nodeInfoAll[edge].setKey(n.getKey() + dist); bhAll.decreaseKey(nodeInfoAll[edge].getHeapIndex(), n.getKey() + dist); nodeInfoAll[edge].setPrevNode(i); } } } //we already know if there's a path from onePath, still ran algorithm to measure time if (foundEnd) { //drawPath int currentIndexAll = stopNodeIndex; double pathLeft = nodeInfoAll[stopNodeIndex].getKey(); while (currentIndexAll != startNodeIndex) { int nextIndex = nodeInfoAll[currentIndexAll].getPrevNode(); PointF a = points[currentIndexAll]; PointF b = points[nextIndex]; graphics.DrawLine(new Pen(Color.Black), points[currentIndexAll], points[nextIndex]); //draw the edge length double distance = pathLeft - nodeInfoAll[nextIndex].getKey(); pathLeft -= distance; graphics.DrawString(Math.Round(distance, 2).ToString(), new Font(this.Font, FontStyle.Regular), new SolidBrush(Color.Red), new PointF((a.X + b.X) / 2, (a.Y + b.Y) / 2)); currentIndexAll = nextIndex; } Refresh(); } //report time allPathTime = timeAll.Elapsed.Milliseconds; allPathTime = allPathTime / 1000; Console.Write("here"); allTimeBox.Text = allPathTime.ToString(); differenceBox.Text = ((allPathTime - onePathTime) * 100 / allPathTime).ToString(); }
public bool onePath(int numPoints) { //start a timer System.Diagnostics.Stopwatch timeOne = new System.Diagnostics.Stopwatch(); timeOne.Start(); //these indices correspond with the points, keep track of points, keys, and place in the queue Node[] nodeInfoOne = new Node[numPoints]; for (int i = 0; i < numPoints; i++) { nodeInfoOne[i] = new Node(points[i], double.MaxValue, -1); } BinaryHeap bhOne = new BinaryHeap(numPoints + 1, nodeInfoOne, startNodeIndex); //need this just for One Path bool foundEnd = false; //this will run until the stop node is found, or through all the nodes if never found while (bhOne.getQueueSize() > 0) { HeapNode n = bhOne.deleteMin(); int i = n.getPointListIndex(); //we can stop our looping if (i == stopNodeIndex) { //we already know its distance and previous pointer, we're done foundEnd = true; break; } //find out all the other nodes this one is connected to, decrease keys if applicable PointF a = points[i]; foreach (int edge in adjacencyList[i]) { PointF b = points[edge]; double dist = Math.Sqrt(Math.Pow(a.X - b.X, 2) + Math.Pow(a.Y - b.Y, 2)); if ((n.getKey()) + dist < nodeInfoOne[edge].getKey()) { //------------------------------------------------------------------- //insert onto the queue if not already in there, decrease key if it is //------------------------------------------------------------------- if (nodeInfoOne[edge].getHeapIndex() != -1) { nodeInfoOne[edge].setKey(n.getKey() + dist); bhOne.decreaseKey(nodeInfoOne[edge].getHeapIndex(), n.getKey() + dist); //the previous node changes when key is decreased nodeInfoOne[edge].setPrevNode(i); } else { bhOne.insert(new HeapNode(n.getKey() + dist, edge)); nodeInfoOne[edge].setKey(n.getKey() + dist); nodeInfoOne[edge].setPrevNode(i); } } } } if (!foundEnd) { pathCostBox.Text = "unreachable"; //report time onePathTime = timeOne.Elapsed.Milliseconds; onePathTime = onePathTime / 1000; oneTimeBox.Text = onePathTime.ToString(); return(false); } else { //draw the path int currentIndexOne = stopNodeIndex; double pathLeft = nodeInfoOne[stopNodeIndex].getKey(); while (currentIndexOne != startNodeIndex) { int nextIndex = nodeInfoOne[currentIndexOne].getPrevNode(); PointF a = points[currentIndexOne]; PointF b = points[nextIndex]; graphics.DrawLine(new Pen(Color.Black), a, b); //draw the edge length double distance = pathLeft - nodeInfoOne[nextIndex].getKey(); pathLeft -= distance; graphics.DrawString(Math.Round(distance, 2).ToString(), new Font(this.Font, FontStyle.Regular), new SolidBrush(Color.Red), new PointF((a.X + b.X) / 2, (a.Y + b.Y) / 2)); currentIndexOne = nextIndex; } //report time onePathTime = timeOne.Elapsed.Milliseconds; onePathTime = onePathTime / 1000; oneTimeBox.Text = onePathTime.ToString(); //report cost pathCostBox.Text = nodeInfoOne[stopNodeIndex].getKey().ToString(); return(true); } }
public void insert(HeapNode n) { myQueue[queueSize + 1] = n; queueSize++; bubbleUp(n, queueSize); }