public void AndrewBreadthFirst(Node start, Node target) { for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { nodes[i, j].distance = Int32.MaxValue; nodes[i, j].backPtr = null; } } Queue <Node> myQueue = new Queue <Node>(); start.distance = 0; myQueue.Enqueue(start); while (myQueue.Count != 0) { currentNode = myQueue.Dequeue(); currentNode.setColor(Color.Yellow); if (currentNode.iValue == target.iValue && currentNode.jValue == target.jValue) { break; } currentNode.getEdges(this); while (currentNode.neighborStack.Count != 0) { currentNeighbor = currentNode.neighborStack.Pop(); if (currentNeighbor.distance == Int32.MaxValue) { currentNeighbor.distance = currentNode.distance + 1; currentNeighbor.backPtr = currentNode; myQueue.Enqueue(currentNeighbor); currentNeighbor.setColor(Color.Orange); } } } while (currentNode != start) { currentNode.setColor(Color.Pink); currentNode = currentNode.backPtr; } start.setColor(Color.Red); target.setColor(Color.Green); }
public void AndrewBreadthFirst(Node start, Node target) { for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { nodes[i, j].distance = Int32.MaxValue; nodes[i, j].backPtr = null; } } Queue<Node> myQueue = new Queue<Node>(); start.distance = 0; myQueue.Enqueue(start); while (myQueue.Count != 0) { currentNode = myQueue.Dequeue(); currentNode.setColor(Color.Yellow); if (currentNode.iValue == target.iValue && currentNode.jValue == target.jValue) { break; } currentNode.getEdges(this); while (currentNode.neighborStack.Count != 0) { currentNeighbor = currentNode.neighborStack.Pop(); if (currentNeighbor.distance == Int32.MaxValue) { currentNeighbor.distance = currentNode.distance + 1; currentNeighbor.backPtr = currentNode; myQueue.Enqueue(currentNeighbor); currentNeighbor.setColor(Color.Orange); } } } while (currentNode != start) { currentNode.setColor(Color.Pink); currentNode = currentNode.backPtr; } start.setColor(Color.Red); target.setColor(Color.Green); }