Question

In this project, you will work on the algorithm (discussed in Module 1) to determine the length of the longest sub sequence of consecutive integers in an array You will implement the algorithm using Hash tables. You are provided with sample code (in C++ and Java) representing the linked list-based implementation of Hash tables (as an array of Linked Lists). You could go through the code to understand the implementation of a Hash table and the functions that can be called on it In the Module I slides, it was mentioned that the array of integers for the algorithm has to be un Actually, it is not a mandatory requirement. The algorithm can still work on a random array of integers with some integers repeated. However. for an arbitrary array (that could have repeated integers), the algorithm could waste time in determining multiple occurrences of the same sub sequence of consecutive integers. For example, if the array is: 7 2 131 1 3 4. the algorithm would find three instances of the longest sub sequence 1 2 3 4 as well as two occurrences of a smaller sub sequence 3 4. On the other hand if the array is: 72134, the algorithm would find only one instance of the longest sub sequence 1 234 In this project, you will implement two versions of the algorithm: The first version would be the implementation of the algorithim (as discussed in Module 1) without any optimization. The second version uld be an implementation of the algorithm with an optimization strategy. In this regard, you are /supposed to design a strategy to prevent the algorithm from searching for the same sub sequence more than once and implement an optimized version of the algorithm incorporating the strategy. You could additional space as part of this optimization strategy. as large as O(n). where n is the number of elements in the input array. The expectation is that the additional space used would aid in reducing the run time You are given a startup code (in C++ and Java) titled: HashTable LinkedList Continuous Seq Version that has the Linked List-based impleimentation of a Hash table as well as the main function setup to run the algorithm for several trials and measure the average length of the longest sub sequence of consecuti integers and the averáge run time (referred to in the code as average detection time and is measured irn milliseconds). The number of trials is always 25. The maximum value for an element in the array is 25000 (i.e., the range of elements generated s l-25000, Note that the programs are likely to run for a longer time, even if the implementation is correct. So. patiently wait! The parameters that you will vary for a particular run of the code are as follows: (N) number of elements to be generated: 10000, 100000 (S) size o f the hash table: 11, 101, 100 Report (Submission through Canvas): Inc (1 -25 pts) Provide a description of your optimization strategy and explain how it could reduce the run time at the cost of additional space, if any (2- 45 pts) The implementation code of the algorlthm without and with optimization. (3 -15 pts) Present a table (the values of N as rows and the values of S as columns) for each of the average largest sequence length and the average detection time (in milliseconds). (4 15 pts) Discuss the impact of the size of the hash table on the average detectionti lude the following and submit as one single PDF file

media%2Fb62%2Fb6254f3d-2ae5-4362-9fae-ef

media%2F50b%2F50bf806e-de5e-4704-84d0-25

media%2F968%2F96894f0f-165f-48be-8f04-ca



media%2Fbe8%2Fbe8e7072-2253-4fe1-976b-b6

media%2F649%2F6494a7b4-6beb-41d8-9fbf-96


media%2Fbb1%2Fbb166961-4b76-479d-9c0e-51


media%2Fb24%2Fb24f5a99-e26a-41b3-816c-b2

media%2F1be%2F1be898d7-0cf8-4428-9e0e-3d

0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE:

class HashTable_LinkedList_ContinuousSeqVersion{

public static void main(String[] args){

Scanner input = new Scanner(System.in);

int numElements;

System.out.print("Enter the number of elements you want to store in

the hash table: ");

numElements = input.nextInt();

int maxValue;

System.out.print("Enter the maximum value for an element: ");

maxValue = input.nextInt();

int hashTableSize;

System.out.print("Enter the size of the hash table: ");

hashTableSize = input.nextInt();

int numTrials;

System.out.print("Enter the number of trials: ");

numTrials = input.nextInt();

Random randGen = new Random(System.currentTimeMillis());

int totalLongestSequenceLength = 0;

long totalDetectiontime = 0;

for (int trials = 1; trials <= numTrials; trials++){

long startTime = System.currentTimeMillis();

Hashtable hashTable = new Hashtable(hashTableSize);

int array[] = new int[numElements];

for (int index = 0; index < numElements; index++){

array[index] = 1 + randGen.nextInt(maxValue);

hashTable.insert(array[index]);

}

int longestSubsequenceLength = 0; // longest sequence length for theparticular trial

// Implement your algorithm here and find the longestSequenceLength for the array

int seqLen = 0;

for(int i = 1;i<maxValue;i++){

if(hashTable.hasElement(i)){

seqLen++;

}else{

if(seqLen > longestSubsequenceLength){

   longestSubsequenceLength = seqLen;

}

seqLen = 0;

}

}

long endTime = System.currentTimeMillis();

totalLongestSequenceLength += longestSubsequenceLength;

totalDetectiontime += (endTime - startTime);

}// trials

System.out.println("Average Longest Subsequence Length: "+(((double)

totalLongestSequenceLength)/numTrials));

System.out.println("Average detection time (milliseconds):

"+(((double) totalDetectiontime)/numTrials));

}

}

Add a comment
Know the answer?
Add Answer to:
In this project, you will work on the algorithm (discussed in Module 1) to determine the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need help with this in JAVA. please help me. Thank you. In this project. you...

    I need help with this in JAVA. please help me. Thank you. In this project. you are given a unimodal array of n integer and your task is to find the maximum integer in the array in theta(logn) time. An unimodal array of integers is an array with entries that monotonically increase up to the maximum integer value and then monotonically decrease for the rest of the array. For example: (2, 5, 8, 9, 12, 15, 21, 17, 10, 4)...

  • Project Description: In this project, you will combine the work you’ve done in previous assignments to...

    Project Description: In this project, you will combine the work you’ve done in previous assignments to create a separate chaining hash table. Overview of Separate Chaining Hash Tables The purpose of a hash table is to store and retrieve an unordered set of items. A separate chaining hash table is an array of linked lists. The hash function for this project is the modulus operator item%tablesize. This is similar to the simple array hash table from lab 5. However, the...

  • Coded in Java. Also advised to use class java.util.HashMap Problem 1 You are given an array...

    Coded in Java. Also advised to use class java.util.HashMap Problem 1 You are given an array A of integers and an integer k. Implement an algorithm that determines, in linear time, the smallest integer that appears at least k times in A. Problem 2 You are given an array A of integers and an integer k. Implement an algorithm that determines in linear time whether there are two distinct indices i and j in the array such that A[i] =...

  • JH: Student Name: 4) Given the following array, do the following (show all the work). A...

    JH: Student Name: 4) Given the following array, do the following (show all the work). A (56, 89, 23, 58, 22, 11, 45, 48, 90) (a - 5 pts) Construct a hash table for the given array using the hash function H(K)- K mod 5 (b- 4 pts) Determine the average number of comparisons for a successful search using the hash table of (c -3 pts) What is the worst case number of comparisons for an unsuccessful search in the...

  • Suppose you are given an array of n integers ai, az, ..., an. You need to...

    Suppose you are given an array of n integers ai, az, ..., an. You need to find out the length of its longest sub-array (not necessarily contiguous) such that all elements in the sub-array are sorted in non-decreasing order. For example, the length of longest sub-array for (5, 10, 9, 13, 12, 25, 19, 30) is 5 and one such sub-array is (5, 10, 13, 19, 303. Note you may have multiple solutions for this case. Use dynamic programming to...

  • Suppose you are given an array of n integers a1, a2, ..., an. You need to...

    Suppose you are given an array of n integers a1, a2, ..., an. You need to find out the length of its longest sub-array (not necessarily contiguous) such that all elements in the sub-array are sorted in non-decreasing order. For example, the length of longest sub-array for {5, 10, 9, 13, 12, 25, 19, 70} is 6 and one such sub-array is {5, 10, 13, 19, 70}. Note you may have multiple solutions for this case. Use dynamic programming to...

  • I need help In the lecture you got acquainted with the median algorithm, which calculates the median of an unsorted array with n∈N elements in O (n). But the algorithm can actually do much more: it is...

    I need help In the lecture you got acquainted with the median algorithm, which calculates the median of an unsorted array with n∈N elements in O (n). But the algorithm can actually do much more: it is not limited to finding only the median, but can generally find the ith element with 0≤i <n. Implement this generic version of the median algorithm by creating a class selector in the ads.set2.select package and implementing the following method: /** * Returns the...

  • Instead of using a linked list to resolve collisions, as in separate chaining, use a binary search tree. That is, create...

    Instead of using a linked list to resolve collisions, as in separate chaining, use a binary search tree. That is, create a hash table that is an array of trees. To display a small tree-based hash table, you could use an inorder traversal of each tree. The advantage of a tree over a linked list is that it can be searched in O(logN) instead of O(N) time. This time savings can be a significant advantage if very high load factors...

  • Sorting Sort the following array using the quick sort algorithm: (4 Marks) a. 12 26 8...

    Sorting Sort the following array using the quick sort algorithm: (4 Marks) a. 12 26 8 9 7 0 4 Pivot selection is defined to be the first element of each sub-list. Show the array before and after each quicksort round (when the array is partitioned after placing the pivot at its correct position). Also, clearly highlight the pivot in each partition b. Consider an unsorted array of integers of size n. Write a Java program to arrange the array...

  • for java 4. Instead of using a linked list to resolve collisions, as in separate chaining, use a binary search tre...

    for java 4. Instead of using a linked list to resolve collisions, as in separate chaining, use a binary search tree. That is, create a hash table that is an array of trees. To display a small tree-based hash table, you could use an inorder traversal of each tree. The advantage of a tree over a linked list is that it can be searched in O(logN) instead of O(N) time. This time savings can be a significant advantage if very...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT