Great Deal! Get Instant $10 FREE in Account on First Order + 10% Cashback on Every Order Order Now

this is python coding , and there is provided starter code to change and need to do a report based on the python code which is based on data structureUsing Array, Linked List, and Trie. Requirement is...

2 answer below »
COSC 2123/1285 Algorithms and Analysis
Assignment 1: Word Completion
Assessment Type (Group of 1 or 2) Assignment. Submit online via Canvas → As-
signments → Assignment 1. Clarifications/Updates/FAQ: check
Ed Discussion Forum → Assignment 1: General Discussion.
Due Date Week 7, 11:59pm, Friday, September 9, 2022
Marks 30
1 Objectives
There are a number of key objectives for this assignment:
• Understand how a real-world problem can be implemented by different data structures and/o
algorithms.
• Evaluate and contrast the performance of the data structures and/or algorithms with respect to
different usage scenarios and input data.
In this assignment, we focus on the word completion problem.
2 Background
Word/sentence (auto-)completion is a very handy feature of nowadays text editors and email
owsers
(you must have used it in your Outlook). While sentence completion is a much more challenging task
and perhaps requires advanced learning methods, word completion is much easier to do as long as
you have a dictionary available in the memory. In this assignment, we will focus on implementing a
dictionary comprising of words and their frequencies that allows word completion. We will try several
data structures and compare their performances, which are a
ay (that is, Python list), linked list,
and trie, which are described one by one below. Please read them very carefully. Latest updates and
answers for questions regarding this assignment will be posted on the Discussion Forum. It is you
esponsibility to check the post frequently for important updates.
A
ay-Based Dictionary
Python’s built-in ‘list’ is equivalent to ‘a
ay’ in other language. In fact, it is a dynamic a
ay in the
sense that its resizing operation (when more elements are inserted into the a
ay than the original
size) is managed automatically by Python. You can initialize an empty a
ay in Python, add elements
at the end of the a
ay, remove the first instant of a given value by typing the following commands
(e.g., on Python’s IDLE Shell).
a
ay = []
a
ay.append(5)
a
ay.append(10)
a
ay.append(5)
a
ay
[5, 10, 5]
a
ay.remove(5)
a
ay
[10, 5]
In the a
ay-based dictionary implementation, we use the Python list (a data structure) to im-
plement common operations for a dictionary (an abstract data type). We treat each element of the
a
ay as a pair (word, frequency) (defined as an object of the simple class WordFrequency), where
word is an English word (a string), e.g., ‘ant’, and frequency is its usage frequency (a non-negative
integer), e.g., 1000, in a certain context, e.g., in some forums or social networks.
The a
ay must be sorted in the alphabetical order, i.e., ‘ant’ appears before ‘ape’, to facilitate
search. A new word, when added to the dictionary, should be inserted at a co
ect location that
preserves the alphabetical order (using the module bisect is allowed - but you need to know what
it does). An example of a valid a
ay is [(‘ant’, 1000), (‘ape’, 200), (‘auxiliary’, 2000)].
Adding (‘app’, 500) to the a
ay, we will have [(‘ant’, 1000), (‘ape’, 200), (‘app’, 500),
(‘auxiliary’, 2000)]. Note that the pair (‘ant’, 1000) in our actual implementation should be
an object of the class WordFrequency and not a tuple.
A Search for ‘ape’ from the a
ay above should return its frequency 200. If the word doesn’t exist,
0 is returned.
A Delete for a word in the dictionary should return True and remove the word from the dictionary
if it exists, and return False otherwise.
An Autocompletion for a string should return a sorted list (most frequent words appear first) of
the three words (if any) of highest frequencies in the dictionary that have the given string as a prefix.
For the a
ay above, an autocompletion for ‘ap’ should return the list [(‘app’, 500),(‘ape’, 200)].
Notice that while both ‘app’ and ‘ape’ have ‘ap’ as a prefix, ‘app’ has a larger frequency and appears
first in the returned list of autocompletion.
Linked-List-Based Dictionary
A linked list is precisely what it is called: a list of nodes linked together by references. In a singly
linked list, each node consists of a data item, e.g., a string or a number, and a reference that holds the
memory location of the next node in the list (the reference in the last node is set to Null). Each linked
list has a head, which is the reference holding memory location of the first node in the list. Once we
know the head of the list, we can access all nodes sequentially by going from one node to the next
using references until reaching the last node.
In the linked-list-based implementation of dictionary, we use an unsorted singly linked list. You
can use the implementation of the linked list in the workshop as a reference for your implementation.
Each node stores as data a pair of (word, frequency) (an object of the class WordFrequency) and
a reference to the next node. A word and its frequency are added as a new node at the front of the
list by updating the head reference. Apart from the fact that they are ca
ied out in the linked list,
Search, Delete, and Autocomplete work similarly as in the a
ay-based implementation. Note that
unlike the a
ay-based dictionary, the words in the linked list are not sorted.
2
Trie-Based Dictionary
Trie (pronounced as either ‘tree’ or ‘try’) is a data structure storing (key, value) pairs where keys
are strings that allows fast operations such as spell checking and auto-completion. Introduced in the
context of computer decades ago, it is no longer the most efficient data structure around. However,
our purpose is to focus more on the understanding of what data structures mean, how they can be
used to implement an abstract data type, and to empirically evaluate their performance. Thus, we
stick to the original/simple idea of ‘trie’. You are strongly encouraged to read about more advanced
data structures evolving from trie.
Each node of a trie contains the following fields:
• a lower-case letter from the English alphabet (‘a’ to ‘z’), or Null if it is the root,
• a boolean variable that is True if this letter is the last letter of a word in the dictionary and False
otherwise,
• a positive integer indicating the word’s frequency (according to some dataset) if the letter is the
last letter of a word in the dictionary,
• an a
ay of A = 26 elements (could be Null) storing references pointing to the children nodes.
In our implementation, for convenience, we use a hashtable/Python’s dictionary to store the
children.
As an example, consider Figure 1.
Figure 1: An example of a trie storing six words and their frequencies. The boolean value True
indicates that the letter is the end of a word. In that case, a frequency (an integer) is shown, e.g., 10
for ‘cut’. Note that a word can be a prefix of another, e.g., ‘cut’ is a prefix of ‘cute’.
Construction. A trie can be built by simply adding words to the tree one by one (order of words
eing added is not important). If a new word is the prefix of an existing word in the trie then we can
3
simply change the boolean field of the node storing the last letter to True and update its frequency.
For instance, in the example in Figure 1, if (‘cut’, 10) is added after (‘cute’, 50), then one can simply
change the boolean field of the node containing the letter ‘t’ to True and set the frequency to 10,
signifying that now (‘cut’, 10) is part of the tree. In another case, when (‘cup’, 30) is added, a new
node has to be constructed to store ‘p’, which has a True in the boolean field and 30 as its frequency.
In this case, ‘cup’ can reuse the two nodes storing ‘c’ and ‘u’ from the previous words.
Searching. To search for a word (and to get its frequency) in a trie, we use its letters to navigate
the tree by following the co
esponding child node. The search is successful if we can reach a node
storing the last letter in the word and has the boolean field True. In that case, the frequency stored
in this node is returned. The search fails, that is, the word is not in the tree, if either a) the search
algorithm couldn’t find a child node that matches a letter in the word, or b) it finds all the nodes
matching all the letters of the words but the boolean field of the node co
esponding to the last lette
is False.
Deletion. The deletion succeeds if the word is already included in the tree. If the word is a prefix
of another word then it can be deleted by simply setting the boolean field of the node storing the last
letter to False. For example, if (cut, 10) is to be deleted from the trie in Figure 1, then we only need
to change the boolean field of the node storing ‘t’ to False. Otherwise, if the word has a unique suffix,
then (only) nodes co
esponding to the suffix are to be deleted. For example, if (cup, 30) is to be
emoved, then the node storing the last letter ‘p’ must be deleted from the trie to save space but not
‘c’ and ‘u’ because these still form part of other words.
Auto-completion. Auto-completion returns a list of three words (if any) in the dictionary (trie)
of highest frequencies that have the given string as a prefix. For example, in the trie given in Figure 1,
• the auto-completion list for ‘cu’ is: [(cute, 50), (cup, 30), (cut, 10)],
• the auto-completion list for ‘far’ is: [(farm, 40)].
Suppose we add one more word (curiosity, 60) into this tree, then the auto-completion list of ‘cu’ will
e changed to [[(curiosity, 60), (cute, 50), (cup, 30)]. In this example, although ‘cut’ contains ‘cu’ as
a prefix, it is not in the top three of the most common words having ‘cu’ as a prefix. In general, the
auto-completion list contains either three, two, one, or no words. They must be sorted in decreasing
frequencies, that is, the first word has the highest frequency and the last has the lowest frequency.
3 Tasks
The assignment is
oken up into a number of tasks, to help you progressively complete the assignment.
3.1 Task A: Implement the Dictionary and Its Operations Using A
ay, Linked
List, and Trie (12 marks)
In this task, you will implement a dictionary of English words that allows Add, Search, Delete,
and Auto-completion, using three different
Answered 19 days After Aug 22, 2022

Solution

Aditi answered on Sep 11 2022
77 Votes
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here