Assignment 2
Assignment 2
Important Guidelines:
• You must ensure that your submitted code runs in the JDK 11.0.17 environment.
• You are not allowed to share your code with anyone. Cheating of any form will lead
to strict disciplinary action. Typical penalty would include Fail grade in the course.
• The assignment must be done individually, and no group submissions are allowed.
• All starter code files must be included in the submission, which must be uploaded on
the gradescope without compressing the files.
• The names of files and method signatures must not be changed in the starter code.
• You should write the code in the portion of the starter code labelled "To be filled in
by the student".
1
1 Polynomial Implementation using Linked Lists
1.1 Creating a Linked List for representing Polynomials
In this part of the assignment, you will use linked lists for implementing basic polynomial
operations such as addition and multiplication. Each node of the linked list will be used to
represent an individual term of the polynomial.
1. Each node in the linked list will contain a single integer representing the coefficient of
the polynomial term
2. The nodes of the linked will be arranged in the decreasing order of the exponent of the
term.
Your task is to implement the ’insert’ and the ’len’ function in the file LinkedList.java.
You can create other helper functions if you like, but don’t change the signature of the given
functions.
2. The len function should return the length of the linked list.
TestCase1:-
LinkedList l = new LinkedList();
l.insert(2);
l.insert(0);
l.insert(7);
2
TestCase2:-
LinkedList l = new LinkedList();
l.insert(-7);
l.insert(0);
l.insert(0);
l.insert(15);
l.insert(0)
NOTE:- Proceed to the next part of the question only if you have completed this part.
We will be using your implementation of the Linked List for the next part of the question.
You will have to implement two functions ’add’ and ’mult’ in the file Polynomial.java.
Again, you can create other helper functions, but don’t change the already existing function
signatures.
1. add:- For two polynomials p1 and p2, the usage of add function is:-
This function adds the two polynomials p1 and p2 represented as Linked Lists and
returns the polynomial representing their sum.(Please note that this function will have
only parameter, the polynomial p2, which needs to be added to the polynomial in
which this function is called).
2. mult:- For two polynomials p1 and p2, the usage of mult function is:-
This function multiplies the two polynomials p1 and p2 represented as Linked Lists
and returns the polynomial representing their product.(Please note that this function
will have only parameter, the polynomial p2, which needs to be multiplied to the
polynomial in which this function is called).
3
TestCase1:-
First Linked List:-
1 -> -1
Second Lined List:-
1 -> 1
The two given linked list represents x − 1 and x + 1. Their summation should be 2x and
multiplication should be x2 − 1.
TestCase2:-
First Linked List:-
3 -> 1 -> 0 -> 2
Second Lined List:-
2 -> 1
In this case the two polynomials are 3x2 + x2 + 2 and 2x + 1. Their summation should
be 3x3 + x2 + 2x + 3 and product should be 6x4 + 5x3 + x2 + 4x + 2.
4
2 Implementing Dictionary
A Dictionary is a list of Key-Value pairs, such that each key has a single associated value.
And when presented with a key the dictionary returns the associated value. A dictionary
uses a hash-table to keep a record of addresses where the associated values are stored in the
linked-list, to make lookup time complexity O(1).
Using the Java’s in-built Linked-List (java.util.LinkedList) and Array, build a Generic
Dictionary by defining the following methods in COL106Dictionary.java:-
Input Format:
The method will be argumented with a key of type K and a value of type V.
Return Format:
Nothing is to be returned by this method.
Example:
Consider the following dictionary where hash-table of size 7 is already built. We
next insert a new key-value pair k, val into the dictionary, where k is a string
and val is an integer.
Hash-table:
0 | Head -> null
1 | Head -> ["IITD", 0x34ef17] -> ["COL106", 0x0019ff] -> null
2 | Head -> null
3 | Head -> null
4 | Head -> ["Hello", 0x123121] -> null
5 | Head -> null
6 | Head -> null
NOTE:- Above Hash-table is just for illustration and not the actual hash-table
that will be created. The actual hash table will depend on the hash function.
Input:
5
["Assignment2", 9]
Linked-list:
Head -> (0x123121)["Hello", 2] -> (0x0019ff)["COL106", 5] ->
(0x34ef17)["IITD", 10] -> (0x098fd1)["Assignment2", 9] -> null
Hash-table:
0 | Head -> null
1 | Head -> ["IITD", 0x34ef17] -> ["COL106", 0x0019ff] -> null
2 | Head -> null
3 | Head -> null
4 | Head -> ["Hello", 0x123121] -> ["Assignment2", 0x098fd1] ->
null
5 | Head -> null
6 | Head -> null
Input Format:
The method will be argumented with a key of type K.
Return Format:
The value of type V corresponding to the argumented key is to be returned.
Example:
Consider the following dictionary where a hash-table of size is 7 is already built.
We next delete an element from the Dictionary.
Linked-list:
Head -> (0x098fd1)["Assignment2", 9] -> (0x123121)["Hello", 2] ->
(0x0019ff)["COL106", 5] -> (0x34ef17)["IITD", 10] -> null
Hash-table:
0 | Head -> null
1 | Head -> ["IITD", 0x34ef17] -> ["COL106", 0x0019ff] -> null
2 | Head -> null
3 | Head -> null
6
4 | Head -> ["Hello", 0x123121] -> ["Assignment2", 0x098fd1] ->
null
5 | Head -> null
6 | Head -> null
NOTE: Above Hash-table is just for illustration and not the actual hash-
table that will be created. The actual hash table will depend on the hash
function.
Input 1:
["COL106"]
In the example, the hash of string "COL106" is 1, so will we go to index 1 of
hash-table and start searching for "COL106" in the linked list. We will next go
to address of node of linked-list corresponding to key "COL106". Finally, we will
delete the node from linked-list as well as remove the corresponding entry from
the Hash-table.
Hence, the Dictionary after the deletion will be as follows:
Linked-list:
Head -> (0x098fd1)["Assignment2", 9] -> (0x123121)["Hello", 2] ->
(0x34ef17)["IITD", 10] -> null
Hash-table:
0 | Head -> null
1 | Head -> ["IITD", 0x34ef17] -> null
2 | Head -> null
3 | Head -> null
4 | Head -> ["Assignment2", 0x098fd1] -> ["Hello", 0x123121] ->
null
5 | Head -> null
6 | Head -> null
And before deletion we found that the value corresponding to key "COL106"
was 5. So,
Return 1:
5
Input 2:
["COL100"]
Let the hash of string "COL100" be 3, and since index 3 of hash-table is null,
we will throw exception.
7
Return 2:
KeyNotFoundExecption
5. int size():* Returns the number of key-value pairs stored in the dictionary.
8. int hash(K key): Returns the hash of the argumented key using the polynomial
rolling hash function.
NOTE: To use the polynomial rolling hash function, convert the type of key i.e. K to
String say s, and perform the following mathematical equation to compute the hash.
or we can say,
n−1
s[i].pi
X
hash(s) = mod m
i=0
where, s[i] =ASCII-value(s[i]) + 1, m will be the size of your hash-table and consider
p = 131, since we are allowing all ASCII characters (128 characters) in string s.
NOTE:- Proceed to the next question only if you have completed this part. We will be
using your implementation of the COL106Dictionary for the next question.
8
3 Frequency Analysis
Given a text containing alphabets and special characters, your task is to perform a frequency
analysis of the words by ignoring the special characters in the given string and considering
[SPACE] as the only separator of the words.
To perform the frequency analysis of the given string use the COL106Dictionary imple-
mented in the previous part of the question, to store the word and frequency as key-value
pairs.
Input Format :
A string consisting of alphabets and special characters only. Please consider
[SPACE] as the only separator for words and ignore the special characters and
consider string to be case-insensitive while performing frequency analysis.
2. long[] profile(): Returns an array of long of size 4 that represents the average
time taken to lookup the hash-table for every word in the given string if a hash-table
of size 173, 6733, 100003 is computed. Compare the time with Built-in Java Dictionary
(java.util.Dictionary) respectively are used.
NOTE: No marks will be allotted for the definition of this method, it is Just for
Practice and understanding the benchmarks analysis for different Dictionaries by the
student.
3. int[] noOfCollisions(): Returns an array of int of size 3 that represents the number
of collisions in the hash-table if a hash-table of size 173, 6733, 100003 respectively are
used for frequency analysis.
Input Format :
This method doesn’t take any inputs, and uses the hash-table of the dictionaries
used for frequency analysis.
Return Format :
An array of int of size 3 is to be returned.
Example:
Consider the hash-table to be of size 11, (This is just for the example, your
dictionaries will be having hash-tables of sizes 173, 6733, 100003.)
9
Let the hash-table be:
0 -> 0x0090cc -> 0x198233
1 -> null
2 -> null
3 -> 0x11bd34
4 -> 0x78ee12 -> 0x943ff1
5 -> null
6 -> 0xdf123e
7 -> 0x9087cc
8 -> null
9 -> 0x08fed1 -> 0x43e441 -> 0x1902ac
10 -> null
Input Format :
This method doesn’t take any inputs, and uses the hash-table of the dictionaries
used for frequency analysis.
Return Format:
An array of strings of size 3 is to be returned.
Example:
Consider the hash-table to be of size 11, (This is just for the example, your
dictionaries will be having hash-tables of sizes 173, 6733, 100003)
10
1 -> null
2 -> null
3 -> 0x11bd34
4 -> 0x78ee12 -> 0x943ff1
5 -> null
6 -> 0xdf123e
7 -> 0x9087cc
8 -> null
9 -> 0x08fed1 -> 0x43e441 -> 0x1902ac
10 -> null
Example 1 :
The Indian Institute of Technology Delhi (IIT Delhi) is a public
institute of technology located in New Delhi, India. It is one
of the twenty-three Indian Institutes of Technology created
to be Centres of Excellence for India’s training, research and
development in science, engineering and technology.
Return 1 :
["the", "indian", "institute", "of", "technology", "delhi", "iit",
"is", "a", "public", "located", "in", "new", "india", "it", "one",
"twentythree", "institutes", "created", "to", "be", "centres",
"excellence", "for", "indias", "training", "research", "and",
"development", "science", "engineering"]
11
Example 1 :
The Indian Institute of Technology Delhi (IIT Delhi) is a public
institute of technology located in New Delhi, India. It is one
of the twenty-three Indian Institutes of Technology created
to be Centres of Excellence for India’s training, research and
development in science, engineering and technology.
Return 1 :
[2 ,2 ,2 ,5 ,4 ,3 ,1 ,2 ,1 ,1 ,1 ,2 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1
,1 ,1 ,1 ,1 ,1 ,2 ,1 ,1 ,1]
12