Last active
September 19, 2018 15:42
-
-
Save pattoM/e791aa5ea515bc97cd73a5e5a08a813e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# [INSERT CODE HERE] Initial state | |
state = 1 | |
# [INSERT CODE HERE] Define the goal here | |
goal = 1024 | |
# Frontier | |
from collections import deque | |
frontier = deque([state]) | |
# Step counter | |
steps = 0 | |
# Breadth-first search algorithm (continue while there are nodes in the frontier) | |
while frontier: | |
#increment counter | |
steps += 1 #debatable. you can add 2 steps or 1 depending on your interpretation | |
res = frontier.popleft() | |
#start generating children right away then test for the condition on each child | |
#generate two children and add them to end of frontier. | |
if (res*2 == goal or res*2+1 == goal): | |
break | |
frontier.extend([res*2, res*2+1]) | |
# [INSERT CODE HERE] Find the length of the frontier by changing the line below. | |
frontier_size = len(frontier) | |
print("Breadth-first search terminated after %d steps\n" % steps) | |
print("Size of frontier at termination is %d" % frontier_size) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Result depending on step increment. If the increment is one per loop result is 512, and 511 frontier size. 512 frontier size can occur depending on where you apply popleft()