{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Measuring Problem-Solving Performance (Section 3.3.2)\n", "\n", " * **Completeness**: Is algorithm guaranteed to find a solution if one exists?\n", " * **Optimality**: Will the algorithm find the optimal solution, the one with the lowest path cost?\n", " * **Time complexity**: How long does the algorithm take to find a solution?\n", " * **Space complexity**: How much memory does the algorithm need to perform the search?\n", "\n", "Measuring complexity can be tricky. The complexity of an algorithm\n", "varies depending on the problem. To deal with this, the following\n", "definitions are often followed to characterize a general search problem and to\n", "express complexity in general.\n", " * **branching factor**: maximum number of successors of any node\n", " * **depth**: the shallowest goal node in number of steps or levels\n", " * **maximum depth**: the maximum depth of the search tree.\n", " \n", "Time and space complexity for an algorithm can then be expressed as\n", "formulas involving these terms. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For example, consider a search tree. A search graph becomes a search\n", "tree if we do not explore repeated states. If every node in a search\n", "tree has $b$ successors, and the search progresses through depth $d$, how many nodes must\n", "be generated by a breadth-first search?\n", "\n", "$$\n", "b + (b)\\, b + (b\\, b)\\, b \\ldots = b + b^2 + b^3 + \\cdots + b^d\n", "$$\n", "\n", "Big-O notation is the highest power term, so time complexity is\n", "$O(b^d)$.\n", "\n", "Space complexity is the number of nodes held in memory. We need to\n", "store all unexplored nodes, and all explored nodes to generate the\n", "solution path. So, if a breadth-first search has generated $d$ levels, there are\n", "$b^d$ nodes in the unexpanded set, and $b + b^2 + \\cdots + b^{(d-1)}$ in\n", "the expanded set. Again, $b^d$ dominates this sum, so the space\n", "complexity is $O(b^d)$." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "What is the complexity of depth-first search? For a state space with\n", "branching factor $b$ and maximum depth $m$, the number of nodes\n", "generated (time complexity) is $O(b^m)$ because the whole tree might need to be\n", "explored. However, space complexity is a nicer story. Once all\n", "descendants of \n", "a node have been expanded, that node can be removed from memory. Why?\n", "So, space complexity is $O(bm)$.\n", "\n", "If a state space has $b=5$ and $m=10$ and the shallowest goal node is at $d=10$,\n", "breadth-first requires storage of $b^d = 5^{10} = 9,765,625$. (What tool would you\n", "use to calculate this?) Depth-first would require storage of $bm =\n", "5 \\times 10 = 50$ at the most. Time complexities are similar, unless $d <<\n", "m$. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Two variations on depth-first implementation reduce its space\n", "complexity. One variation is **backtracking depth-first search**. If\n", "you have a way of knowing how to generate the next child of the same\n", "parent given a previous child (where previous and next are defined by\n", "left-to-right order of our simple graph, for example), then you only\n", "need to store one child of a parent. Explore the path from that child\n", "as deep as you can go. If goal is not found, back up one level from\n", "the deepest level and try the next child at that level. If no next\n", "child at that level, back up another level. This way only one path of\n", "nodes from the start node to the deepest level needs to be stored.\n", "Its space complexity is $O(m)$, rather than $O(bm)$ for our earlier\n", "definition of depth-first search.\n", "\n", "The second variation requires a procedure to undo the effects of each\n", "action on a state. If this is available, then storage for only a single\n", "state is required, plus storage for a sequence of actions. To\n", "backtrack, the inverse of the most recent action is applied to the\n", "deepest state to get to its parent. So now we are storing one state\n", "and $O(m)$ actions. If the representation of actions is much smaller\n", "than the representation of the state, this is a huge win." ] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.5" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": true, "toc_window_display": false } }, "nbformat": 4, "nbformat_minor": 1 }