| 
1132 | 1132 |    },  | 
1133 | 1133 |    "outputs": [],  | 
1134 | 1134 |    "source": [  | 
1135 |  | -    "def tree_search_for_vis(problem, frontier):\n",  | 
 | 1135 | +        "def tree_breadth_search_for_vis(problem):\n",  | 
1136 | 1136 |     "    \"\"\"Search through the successors of a problem to find a goal.\n",  | 
1137 | 1137 |     "    The argument frontier should be an empty queue.\n",  | 
1138 | 1138 |     "    Don't worry about repeated paths to a state. [Figure 3.7]\"\"\"\n",  | 
 | 
1143 | 1143 |     "    node_colors = {k : 'white' for k in problem.graph.nodes()}\n",  | 
1144 | 1144 |     "    \n",  | 
1145 | 1145 |     "    #Adding first node to the queue\n",  | 
1146 |  | -    "    frontier.append(Node(problem.initial))\n",  | 
 | 1146 | +    "    frontier = deque([Node(problem.initial)])\n",  | 
1147 | 1147 |     "    \n",  | 
1148 | 1148 |     "    node_colors[Node(problem.initial).state] = \"orange\"\n",  | 
1149 | 1149 |     "    iterations += 1\n",  | 
1150 | 1150 |     "    all_node_colors.append(dict(node_colors))\n",  | 
1151 | 1151 |     "    \n",  | 
1152 | 1152 |     "    while frontier:\n",  | 
1153 | 1153 |     "        #Popping first node of queue\n",  | 
1154 |  | -    "        node = frontier.pop()\n",  | 
 | 1154 | +    "        node = frontier.popleft()\n",  | 
1155 | 1155 |     "        \n",  | 
1156 | 1156 |     "        # modify the currently searching node to red\n",  | 
1157 | 1157 |     "        node_colors[node.state] = \"red\"\n",  | 
 | 
1179 | 1179 |     "        \n",  | 
1180 | 1180 |     "    return None\n",  | 
1181 | 1181 |     "\n",  | 
1182 |  | -    "def breadth_first_tree_search_(problem):\n",  | 
 | 1182 | +    "def breadth_first_tree_search(problem):\n",  | 
1183 | 1183 |     "    \"Search the shallowest nodes in the search tree first.\"\n",  | 
1184 |  | -    "    iterations, all_node_colors, node = tree_search_for_vis(problem, FIFOQueue())\n",  | 
 | 1184 | +    "    iterations, all_node_colors, node = tree_breadth_search_for_vis(problem)\n",  | 
1185 | 1185 |     "    return(iterations, all_node_colors, node)"  | 
1186 | 1186 |    ]  | 
1187 | 1187 |   },  | 
 | 
1199 | 1199 |    "outputs": [],  | 
1200 | 1200 |    "source": [  | 
1201 | 1201 |     "all_node_colors = []\n",  | 
1202 |  | -    "romania_problem = GraphProblem('Arad', 'Fagaras', romania_map)\n",  | 
1203 |  | -    "a, b, c = breadth_first_tree_search_(romania_problem)\n",  | 
 | 1202 | +    "romania_problem = GraphProblem('Arad', 'Bucharest', romania_map)\n",  | 
 | 1203 | +    "a, b, c = breadth_first_tree_search(romania_problem)\n",  | 
1204 | 1204 |     "display_visual(romania_graph_data, user_input=False, \n",  | 
1205 |  | -    "               algorithm=breadth_first_tree_search_, \n",  | 
 | 1205 | +    "               algorithm=breadth_first_tree_search, \n",  | 
1206 | 1206 |     "               problem=romania_problem)"  | 
1207 | 1207 |    ]  | 
1208 | 1208 |   },  | 
 | 
1222 | 1222 |    },  | 
1223 | 1223 |    "outputs": [],  | 
1224 | 1224 |    "source": [  | 
1225 |  | -    "def depth_first_tree_search_graph(problem):\n",  | 
 | 1225 | +        "def tree_depth_search_for_vis(problem):\n",  | 
 | 1226 | +    "    \"\"\"Search through the successors of a problem to find a goal.\n",  | 
 | 1227 | +    "    The argument frontier should be an empty queue.\n",  | 
 | 1228 | +    "    Don't worry about repeated paths to a state. [Figure 3.7]\"\"\"\n",  | 
 | 1229 | +    "    \n",  | 
 | 1230 | +    "    # we use these two variables at the time of visualisations\n",  | 
 | 1231 | +    "    iterations = 0\n",  | 
 | 1232 | +    "    all_node_colors = []\n",  | 
 | 1233 | +    "    node_colors = {k : 'white' for k in problem.graph.nodes()}\n",  | 
 | 1234 | +    "    \n",  | 
 | 1235 | +    "    #Adding first node to the stack\n",  | 
 | 1236 | +    "    frontier = [Node(problem.initial)]\n",  | 
 | 1237 | +    "    \n",  | 
 | 1238 | +    "    node_colors[Node(problem.initial).state] = \"orange\"\n",  | 
 | 1239 | +    "    iterations += 1\n",  | 
 | 1240 | +    "    all_node_colors.append(dict(node_colors))\n",  | 
 | 1241 | +    "    \n",  | 
 | 1242 | +    "    while frontier:\n",  | 
 | 1243 | +    "        #Popping first node of stack\n",  | 
 | 1244 | +    "        node = frontier.pop()\n",  | 
 | 1245 | +    "        \n",  | 
 | 1246 | +    "        # modify the currently searching node to red\n",  | 
 | 1247 | +    "        node_colors[node.state] = \"red\"\n",  | 
 | 1248 | +    "        iterations += 1\n",  | 
 | 1249 | +    "        all_node_colors.append(dict(node_colors))\n",  | 
 | 1250 | +    "        \n",  | 
 | 1251 | +    "        if problem.goal_test(node.state):\n",  | 
 | 1252 | +    "            # modify goal node to green after reaching the goal\n",  | 
 | 1253 | +    "            node_colors[node.state] = \"green\"\n",  | 
 | 1254 | +    "            iterations += 1\n",  | 
 | 1255 | +    "            all_node_colors.append(dict(node_colors))\n",  | 
 | 1256 | +    "            return(iterations, all_node_colors, node)\n",  | 
 | 1257 | +    "        \n",  | 
 | 1258 | +    "        frontier.extend(node.expand(problem))\n",  | 
 | 1259 | +    "           \n",  | 
 | 1260 | +    "        for n in node.expand(problem):\n",  | 
 | 1261 | +    "            node_colors[n.state] = \"orange\"\n",  | 
 | 1262 | +    "            iterations += 1\n",  | 
 | 1263 | +    "            all_node_colors.append(dict(node_colors))\n",  | 
 | 1264 | +    "\n",  | 
 | 1265 | +    "        # modify the color of explored nodes to gray\n",  | 
 | 1266 | +    "        node_colors[node.state] = \"gray\"\n",  | 
 | 1267 | +    "        iterations += 1\n",  | 
 | 1268 | +    "        all_node_colors.append(dict(node_colors))\n",  | 
 | 1269 | +    "        \n",  | 
 | 1270 | +    "    return None\n",  | 
 | 1271 | +    "\n",  | 
 | 1272 | +    "def depth_first_tree_search(problem):\n",  | 
1226 | 1273 |     "    \"Search the deepest nodes in the search tree first.\"\n",  | 
1227 |  | -    "    # This algorithm might not work in case of repeated paths\n",  | 
1228 |  | -    "    # and may run into an infinite while loop.\n",  | 
1229 |  | -    "    iterations, all_node_colors, node = tree_search_for_vis(problem, Stack())\n",  | 
 | 1274 | +    "    iterations, all_node_colors, node = tree_depth_search_for_vis(problem)\n",  | 
1230 | 1275 |     "    return(iterations, all_node_colors, node)"  | 
1231 | 1276 |    ]  | 
1232 | 1277 |   },  | 
 | 
1237 | 1282 |    "outputs": [],  | 
1238 | 1283 |    "source": [  | 
1239 | 1284 |     "all_node_colors = []\n",  | 
1240 |  | -    "romania_problem = GraphProblem('Arad', 'Oradea', romania_map)\n",  | 
 | 1285 | +    "romania_problem = GraphProblem('Arad', 'Bucharest', romania_map)\n",  | 
1241 | 1286 |     "display_visual(romania_graph_data, user_input=False, \n",  | 
1242 |  | -    "               algorithm=depth_first_tree_search_graph, \n",  | 
 | 1287 | +    "               algorithm=depth_first_tree_search, \n",  | 
1243 | 1288 |     "               problem=romania_problem)"  | 
1244 | 1289 |    ]  | 
1245 | 1290 |   },  | 
 | 
1262 | 1307 |    },  | 
1263 | 1308 |    "outputs": [],  | 
1264 | 1309 |    "source": [  | 
1265 |  | -    "def breadth_first_search_graph(problem):\n",  | 
 | 1310 | +"def breadth_first_search_graph(problem):\n",  | 
1266 | 1311 |     "    \"[Figure 3.11]\"\n",  | 
1267 | 1312 |     "    \n",  | 
1268 | 1313 |     "    # we use these two variables at the time of visualisations\n",  | 
 | 
1282 | 1327 |     "        all_node_colors.append(dict(node_colors))\n",  | 
1283 | 1328 |     "        return(iterations, all_node_colors, node)\n",  | 
1284 | 1329 |     "    \n",  | 
1285 |  | -    "    frontier = FIFOQueue()\n",  | 
1286 |  | -    "    frontier.append(node)\n",  | 
 | 1330 | +    "    frontier = deque([node])\n",  | 
1287 | 1331 |     "    \n",  | 
1288 | 1332 |     "    # modify the color of frontier nodes to blue\n",  | 
1289 | 1333 |     "    node_colors[node.state] = \"orange\"\n",  | 
 | 
1292 | 1336 |     "        \n",  | 
1293 | 1337 |     "    explored = set()\n",  | 
1294 | 1338 |     "    while frontier:\n",  | 
1295 |  | -    "        node = frontier.pop()\n",  | 
 | 1339 | +    "        node = frontier.popleft()\n",  | 
1296 | 1340 |     "        node_colors[node.state] = \"red\"\n",  | 
1297 | 1341 |     "        iterations += 1\n",  | 
1298 | 1342 |     "        all_node_colors.append(dict(node_colors))\n",  | 
 | 
1315 | 1359 |     "        node_colors[node.state] = \"gray\"\n",  | 
1316 | 1360 |     "        iterations += 1\n",  | 
1317 | 1361 |     "        all_node_colors.append(dict(node_colors))\n",  | 
1318 |  | -    "    return None"  | 
1319 |  | -   ]  | 
 | 1362 | +    "    return None"   ]  | 
1320 | 1363 |   },  | 
1321 | 1364 |   {  | 
1322 | 1365 |    "cell_type": "code",  | 
 | 
1346 | 1389 |     "collapsed": true  | 
1347 | 1390 |    },  | 
1348 | 1391 |    "outputs": [],  | 
1349 |  | -   "source": [  | 
1350 |  | -    "def graph_search_for_vis(problem, frontier):\n",  | 
 | 1392 | +   "source": [    "def graph_search_for_vis(problem):\n",  | 
1351 | 1393 |     "    \"\"\"Search through the successors of a problem to find a goal.\n",  | 
1352 | 1394 |     "    The argument frontier should be an empty queue.\n",  | 
1353 | 1395 |     "    If two paths reach a state, only use the first one. [Figure 3.7]\"\"\"\n",  | 
 | 
1356 | 1398 |     "    all_node_colors = []\n",  | 
1357 | 1399 |     "    node_colors = {k : 'white' for k in problem.graph.nodes()}\n",  | 
1358 | 1400 |     "    \n",  | 
1359 |  | -    "    frontier.append(Node(problem.initial))\n",  | 
 | 1401 | +    "    frontier = [(Node(problem.initial))]\n",  | 
1360 | 1402 |     "    explored = set()\n",  | 
1361 | 1403 |     "    \n",  | 
1362 | 1404 |     "    # modify the color of frontier nodes to orange\n",  | 
 | 
1365 | 1407 |     "    all_node_colors.append(dict(node_colors))\n",  | 
1366 | 1408 |     "      \n",  | 
1367 | 1409 |     "    while frontier:\n",  | 
1368 |  | -    "        # Popping first node of queue\n",  | 
 | 1410 | +    "        # Popping first node of stack\n",  | 
1369 | 1411 |     "        node = frontier.pop()\n",  | 
1370 | 1412 |     "        \n",  | 
1371 | 1413 |     "        # modify the currently searching node to red\n",  | 
 | 
1401 | 1443 |     "\n",  | 
1402 | 1444 |     "def depth_first_graph_search(problem):\n",  | 
1403 | 1445 |     "    \"\"\"Search the deepest nodes in the search tree first.\"\"\"\n",  | 
1404 |  | -    "    iterations, all_node_colors, node = graph_search_for_vis(problem, Stack())\n",  | 
 | 1446 | +    "    iterations, all_node_colors, node = graph_search_for_vis(problem)\n",  | 
1405 | 1447 |     "    return(iterations, all_node_colors, node)"  | 
1406 | 1448 |    ]  | 
1407 | 1449 |   },  | 
 | 
1462 | 1504 |     "        all_node_colors.append(dict(node_colors))\n",  | 
1463 | 1505 |     "        return(iterations, all_node_colors, node)\n",  | 
1464 | 1506 |     "    \n",  | 
1465 |  | -    "    frontier = PriorityQueue(min, f)\n",  | 
 | 1507 | +    "    frontier = PriorityQueue('min', f)\n",  | 
1466 | 1508 |     "    frontier.append(node)\n",  | 
1467 | 1509 |     "    \n",  | 
1468 | 1510 |     "    node_colors[node.state] = \"orange\"\n",  | 
 | 
1558 | 1600 |    "metadata": {},  | 
1559 | 1601 |    "outputs": [],  | 
1560 | 1602 |    "source": [  | 
1561 |  | -    "def depth_limited_search(problem, frontier, limit = -1):\n",  | 
 | 1603 | +    "def depth_limited_search(problem, limit = -1):\n",  | 
1562 | 1604 |     "    '''\n",  | 
1563 | 1605 |     "    Perform depth first search of graph g.\n",  | 
1564 | 1606 |     "    if limit >= 0, that is the maximum depth of the search.\n",  | 
 | 
1568 | 1610 |     "    all_node_colors = []\n",  | 
1569 | 1611 |     "    node_colors = {k : 'white' for k in problem.graph.nodes()}\n",  | 
1570 | 1612 |     "    \n",  | 
1571 |  | -    "    frontier.append(Node(problem.initial))\n",  | 
 | 1613 | +    "    frontier = [Node(problem.initial)]\n",  | 
1572 | 1614 |     "    explored = set()\n",  | 
1573 | 1615 |     "    \n",  | 
1574 | 1616 |     "    cutoff_occurred = False\n",  | 
 | 
1622 | 1664 |     "\n",  | 
1623 | 1665 |     "def depth_limited_search_for_vis(problem):\n",  | 
1624 | 1666 |     "    \"\"\"Search the deepest nodes in the search tree first.\"\"\"\n",  | 
1625 |  | -    "    iterations, all_node_colors, node = depth_limited_search(problem, Stack())\n",  | 
 | 1667 | +    "    iterations, all_node_colors, node = depth_limited_search(problem)\n",  | 
1626 | 1668 |     "    return(iterations, all_node_colors, node)     "  | 
1627 | 1669 |    ]  | 
1628 | 1670 |   },  | 
 | 
0 commit comments