Explore the Starter Code: - Review the provided Autocomplete class. It handles building…
- User
Explore the Starter Code: - Review the provided Autocomplete class. It handles building the tree from a text document, setting up a basic user interface, and providing a framework for the suggest method. 3. Implement Search Algorithms: - Your main task is to complete the suggest methods. These methods should take a prefix as input and return a list of word suggestions. - You'll implement multiple versions of suggest: - suggest_bfs: Breadth-First Search - suggest_dfs: Depth-First Search - suggest_ucs: Uniform-Cost Search
Background: Autocomplete as a Search Problem
Alright! Let's give you some context before you get into the weeds of the starter code. Autocomplete might seem like some complicated magic, but at its core, it's just an application of search algorithms on a tree (that's how it's done in this assignment for your simplicity, but it's done very differently in real word). Let's break down how this works:
The Search Space: A Tree of Characters
To implement the autocomplete feature, you would build a tree of characters, which will be the search space for this search problem. In your starter code, you're given a document (a txt file) of several words. Imagine each word in your document is broken down into its individual letters. Now, picture these letters arranged in a single tree-like structure, for example look at the tree diagram below:
Tree Diagram
For example, let the document that is given to you be -
txt air ball cat car card carpet carry cap cape
mermaid graph TD; ROOT-->A_air[A]; A_air[A]-->I_air[I] I_air[I]-->R_air[R]
codeROOT-->B B-->A_ball[A] A_ball[A]-->L_ball1[L] L_ball1[L]-->L_ball2[L] ROOT-->C C-->A_cat[A] A_cat[A]-->T A_cat[A]-->R R-->D R-->P_carpet[P] P_carpet[P]-->E_carpet[E] E_carpet[E]-->T_carpet[T] R-->R_carry[R] R_carry[R]-->Y A_cat[A]-->P_cape[P] P_cape[P]-->E_cape[E]Above is a diagram of the tree that is build from the example document given above. Note how the tree starts with a common root
- This is what the search space for your search problem would look like.
- You will traverse the tree starting from the last node of the prefix that the user enters to generate autocomplete suggestions.
The Search Problem
When a user types a prefix (e.g., "ca"), the autocomplete feature needs to find all the words in the tree that start with that prefix. This translates to a search problem:
- Assistant
- User
What is the cost function for UCS
- Assistant
In the context of Uniform-Cost Search (UCS), the cost function is used to determine the cost to transition from one state to another or, more specifically in this case, from one node to its children in the search space (the trie representing the autocomplete functionality). The cost function should ideally reflect how desirable or "cost-effective" it is to take a particular action.
- User
When using UCS autocomplete on prefix "th" the suggestions are as follows (in order):
the, thag, that, thee, thou, their, there, though, thought, through
When using BFS autocomplete on prefix "th" the suggestions are as follows (in order):
the, thee, thou, that, thag, there, their, though, thought, through
When using DFS autocomplete on prefix "th" the suggestions are as follows (in order):
through, thag, that, thou, though, thought, the, thee, their, there
Explain here what differences did you see in the suggestions generated when you used BFS vs DFS vs UCS.
- Assistant
- User
What would the tree look like when build_tree is used on this text file:
"there though that the their through thee thou thought thag"
- Assistant