○ Any submission using the containers in STL will be disregardedFile(s) you need to submit: ● Files you need to submit. (Do not change the filename.) ○ pa3.cpp ○ sort.cpp and sort.h ○ tree.cpp and tree.h ○ bst.cpp and bst.h ○ avl.cpp and avl.h ○ open_hash_function.cpp and open_hash_function.h ○ open_hash_table.cpp and open_hash_table.h ○ closed_hash_function.cpp and closed_hash_function.h ○ closed_hash_table.cpp and closed_hash_table.hAny questions? Please use PLMS – Q&A board.1. Bubble Sort (2 pts)a. Implement a function that sorts a given array using the Bubble Sort algorithm. (k: iteration, A[ ]: array) On path k, the k-th lowest key rises to k-th position. Iteratively swap the adjacent items if the below item has a lower key value. If there were no swap in the current iteration, the array is sorted. You can modify sort.cpp and sort.h files for this problem.b. Input & Output Input: A sequence of commands – (‘insertion’,integer): insert integer into the array (there will be no duplicated integers). – (‘bubbleSort’,NULL): sort the array using the Bubble Sort algorithm.Output: – Print every value in the array whenever the k-th lowest key rises to k-th position, including the initial state, separating the values with white space. Please use a built-in function to print the array. – We won’t test array size over 20 or array size of 0.c. Example Input & Output Input Output “[(‘insertion’,42), (‘insertion’,20), 42 20 17 13 28 14 (‘insertion’,17), (‘insertion’,13), 13 42 20 17 14 28 (‘insertion’,28), (‘insertion’,14), 13 14 42 20 17 28 (‘bubbleSort’,NULL)]” 13 14 17 42 20 28 13 14 17 20 42 28 13 14 17 20 28 42 “[(‘insertion’,17), (‘insertion’,20), 17 20 2 21 4 (‘insertion’,2), (‘insertion’,21), 2 17 20 4 21 (‘insertion’,4), (‘bubbleSort’,NULL)]” 2 4 17 20 21“[(‘insertion’,7), 7 6 5 4 3 2 1 (‘insertion’,6),(‘insertion’,5), 1 7 6 5 4 3 2 (‘insertion’,4),(‘insertion’,3), 1 2 7 6 5 4 3 (‘insertion’,2), (‘insertion’,1), 1 2 3 7 6 5 4 (‘bubbleSort’,NULL)]” 1 2 3 4 7 6 5 1 2 3 4 5 7 6 1 2 3 4 5 6 7d. Example execution >> ./pa3.exe 1 “[(‘insertion’,42), (‘insertion’,20), (‘insertion’,17), (‘insertion’,13), (‘insertion’,28), (‘insertion’,14), (‘bubbleSort’,NULL)]” [Task 1] 42 20 17 13 28 14 13 42 20 17 14 28 13 14 42 20 17 28 13 14 17 42 20 28 13 14 17 20 42 28 13 14 17 20 28 422. Non-recursive Merge Sort (2 pts)a. Implement a function that sorts a given array using the Merge Sort algorithm in descending order using non-recursive merge sort. Start with the sorted segments of size 1 and do pairwise merging of these sorted segments as in the upward pass. You can modify sort.cpp and sort.h files for this problem.b. Input & Output Input : A sequence of commands – (‘insertion’,integer): insert integer into the array. – (‘mergeSort’,NULL): sort the array using the Merge Sort algorithm. Output – Starting from the initial state of the array, print all values of the array at each iteration step after performing the necessary sorting. Print the array values at the initial state and at each iteration step where sorting is performed. – Separate the values in the array with a space for each iteration step. – You don’t need to consider exceptional cases such as overflow or an empty array. We will not test such cases.c. Example Input & Output Input Output “[(‘insertion’,56),(‘insertion’,42), 56 42 20 17 13 (‘insertion’,20),(‘insertion’,17), 28 14 (‘insertion’,13),(‘insertion’,28), 56 42 20 17 28 (‘insertion’,14),(‘mergeSort’,NULL)]” 13 14 56 42 20 17 28 14 13 56 42 28 20 17 14 13“[(‘insertion’,36), (‘insertion’,20), 36 20 17 13 14 (‘insertion’,17), (‘insertion’,13), 28 15 23 32 (‘insertion’,14), (‘insertion’,28), 36 20 17 13 28 (‘insertion’,15), (‘insertion’,23), 14 23 15 32 (‘insertion’,32),(‘mergeSort’,NULL)]” 36 20 17 13 28 23 15 14 32 36 28 23 20 17 15 14 13 32 36 32 28 23 20 17 15 14 13“[(‘insertion’,1), 1 2 3 4 5 6 7 (‘insertion’,2),(‘insertion’,3), 2 1 4 3 6 5 7 (‘insertion’,4),(‘insertion’,5), 4 3 2 1 7 6 5 (‘insertion’,6), (‘insertion’,7), 7 6 5 4 3 2 1 (‘mergeSort’,NULL)]”d. Example execution >> ./pa3.exe 2 “[(‘insertion’,56),(‘insertion’,42), (‘insertion’,20),(‘insertion’,17), (‘insertion’,13),(‘insertion’,28), (‘insertion’,14),(‘mergeSort’,NULL)]” [Task 2] 56 42 20 17 13 28 14 56 42 20 17 28 13 14 56 42 20 17 28 14 13 56 42 28 20 17 14 133. BST Insertion / Deletion / Summation (3pts)a. Implement functions that inserts and deletes an element into a binary search tree (BST). Also you should find the sum of nodes that is larger than specific node value, and print the pre-order and in-order of the tree. You can modify bst.cpp and bst.h files for this problem. – Input of nodes are integers from 1 to 99b. Input & output of BinarySearchTree::insertion Input: – (‘insertion’,integer): Key of the element to be inserted. The key has a positive integer value. Output: – Return the -1 if the key already exists in the tree, 0 otherwise. (If the key already exists, do not insert the element)c. Input & output of BinarySearchTree::deletion Input: – (‘deletion’,integer): Key of the element to be deleted. Output: – Return -1 if the key does not exist in the tree, 0 otherwise. If the key does not exist, do not delete any element. Note that replace the smallest key in right subtree when delete the node with degree 2d. Input & output of BinarySearchTree::sum Input: – (‘sum’,integer): Sum of the nodes greater than the integer. Output: – Return the sum of nodes that is larger than the key value.e. task_3 prints i. the return for each insertion/deletion for command (‘insertion’, integer)/ (‘deletion’, integer) and ii. the return summation of nodes that is larger than a specific node value for command (‘sum’, integer) and iii. the results of preorder and inorder traversal of the constructed tree for command (‘print’, NULL). (If empty tree, don’t return preorder or inorder traveral resultsf. Example Input & Output Input Output [(‘insertion’,4), (‘insertion’,6), 0 (‘insertion’,6), (‘insertion’,7), 0 (‘deletion’,7), (‘print’, NULL)] -1 0 0 4 6 4 6 [(‘insertion’,4), (‘insertion’,2), (‘sum’, 0 1), (‘insertion’,10), (‘insertion’,9), 0 (‘insertion’,15), (‘insertion’,1), 6 (‘deletion’,1), (‘deletion’,4), 0 (‘deletion’,10), (‘sum’, 5), (‘print’, NULL)] 0 0 0 0 0 0 24 9 2 15 2 9 15 [(‘deletion’, 3),(‘insertion’, -1 10),(‘deletion’, 10), (‘sum’,2), 0 (‘print’,NULL)] 0 0g. Example execution4. Duplicate Letter Counting Problem (4 pts)Example of left rotation to resolve RR imbalancea. Implement a class ‘AVLTree’ to efficiently count and report the frequency of each alphabet letter in a string using AVL tree operations. This involves handling imbalances that might occur during insertions and deletions, as well as ensuring that alphabet letters are counted accurately. As the string is received, insert each letter sequentially from the beginning to the end into the AVL tree.b. AVL Tree Implementations Modify or implement functions in the ‘AVLTree’ class that insert and delete elements (letters in this case). z Implement functions in the ‘AVLTree’ class that print the all node information in pre-order, in-order, and reverse-in-order manner. z Ensure the AVL tree can resolve imbalances (LL, LR, RL, RR) that might occur after modifications. z You can modify ‘avl.cpp’ and ‘avl.h’ files, and if needed, add public members to the ‘Node’ class implemented in ‘tree.h’.c. Details of the process Sequentially insert each letter from the string. If a node for that letter already exists, delete the existing node, increment the count, and re-insert the node. z During the deletion process, if a node has both children, use the replacement method where you find the minimum in the right subtree to replace the node z If counts are equal, prioritize based on the lexicographical order of the letters. For example, (c, 1) < (a, 4) < (b, 4).d. Input & Output: z Input: A string consisting of at least one alphabetical letter. You don’t need to consider any other input cases and uppercase and lowercase of letters are same. z Output: (1) Frequency counts of letters, displayed from the most to the least frequent. If multiple letters have the same frequency, they should be displayed in lexicographical order. z Output: (2) The results of the preorder and inorder traversal of the constructed AVL tree after all operations.e. task_4 prints i. The results of reverse-inorder, inorder, preorder traversal of the constructed tree. ii. The format of node for printing is “(letter, count)”f. Example Input & Output Input Output “aAbbbcccc” Frequencies of alphabetic letters in the given string (c, 4) (b, 3) (a, 2) Inorder (a, 2) (b, 3) (c, 4) Preorder (b, 3) (a, 2) (c, 4) “a” Frequencies of alphabetic letters in the given string (a, 1) Inorder (a, 1) Preorder (a, 1) “aabbbccccddDDd” Frequencies of alphabetic letters in the given string (d, 5) (c, 4) (b, 3) (a, 2) Inorder (a, 2) (b, 3) (c, 4) (d, 5) Preorder (b, 3) (a, 2) (c, 4) (d, 5)g. Example execution5. Open hash table (1 pts)M which is the size of the hash table, insertions, and deletions of integers are given as instructions. First, for each insertion, record if a collision occurred. A collision occurs when a key is inserted into a non-empty slot. After all operations are complete, print the total number of collisions that occurred and the length of the longest chain in the hash table. You don’t need to consider a deletion of an unseen key and multiple insertions of the same key. You can modify open_hash_function.cpp, open_hash_table.cpp, open_hash_function.h and open_hash_table.h files for this problem.b. Input & Output Input: A sequence of commands – (‘M’,integer): the size of a hash table. – (The first command should always be ‘M’) – (‘insertion’,integer): insert integer into the hash table. – (‘deletion’,integer): delete integer from the hash table. Output: For each slot of the hash table, print out – the number of collisions occurred during execution. – the length of the longest chain in the final table.c. Example Input & Output Input Output [(‘M’,4), (‘insertion’,32615)] 0 1 [(‘M’,4), (‘insertion’,32615), 1 2 (‘insertion’,315), (‘insertion’,6468), (‘insertion’,94833)] [(‘M’,4), (‘insertion’,32615), 1 1 (‘insertion’,315), (‘insertion’,6468), (‘insertion’,94833), (‘deletion’,32615), (‘deletion’,6468)]d. Example execution6. Closed hash table (3 pts)a. Implement insertion of a closed hash table with rehashing implementation. This hash table is used with integer keys and hashing into a table of size M. This hash table uses pseudo-random probing as a collision handling method. The index of the key ݇ after ݅-th collision, ݄(݇), is: ܯ ݀݉ (݀ + (݇)݄) = (݇)݄where ݄(݇) is the digit-folding method hash function. It works same with the hash function in task 5. ݀ଵ, ݀ଶ, … , ݀ெିଵ is a pseudo-random permutation of integers 1, … , M-1 and it is generated by shift-register sequence. Please refer to slide “Example: Given M, k, ݀ଵ for the shift-register sequence and inserted integers, print the result hash table. If the hash table is full or the collision cannot be resolved, stop the insertion, and print results of the insertions made up to that point, then print FAIL. Refer to instruction b & c for more detail. You don’t need to consider a deletion of a key, multiple insertions of the same key. You can modify closed_hash_function.cpp, closed_hash_table.cpp, closed_hash_function.h and closed_hash_table.h files for this problem.b. Input & Output Input: A sequence of commands – (‘M’,integer): the size of a hash table. (The first command is always ‘M’) – (‘k’,integer): a constant used for shift-register sequence. (The second command is always ‘k’) – (‘d’,integer): the first probing offset for the shift-register sequence . (The third command is always ‘d’) – (‘insertion’,integer): insert integer into the hash table. Output: For each slot of the hash table, print out – the value, if the state of the slot is occupied. – ‘EMPTY’ if the state of the slot is empty. ‘FAIL’ if the hash table is full or the collision cannot be resolved.c. Example Input & Output Input Output [(‘M’,4), (‘k’,3), (‘d’,2), 0: 6468 (‘insertion’,32615), (‘insertion’,315), 1: 32615 (‘insertion’,6468), (‘insertion’,94833)] 2: 94833 3: 315 [(‘M’,4), (‘k’,3), (‘d’,2), (‘insert’,32615), 0: 6468 (‘insertion’,315), (‘insertion’,6468), 1: 32615 (‘insertion’,94833), (‘insertion’,22)] 2: 94833 3: 315 FAIL [(‘M’,5), (‘k’,3), (‘d’,2), (‘insertion’,2), 0: EMPTY (‘insertion’,7), (‘insertion’,11), 1: 11 (‘insertion’,20), (‘insertion’,18)] 2: 2 3: EMPTY 4: 7 FAILd. Example execution
**** PLEASE READ THIS GRAY BOX CAREFULLY BEFORE STARTING THE ASSIGNMENT ****Evaluation policy: ■ 100% penalty is applied for that submission ● Your code will be automatically tested using an evaluation program ○ Each problem has the maximum score ● Do not modify auxiliary files. ○ Such as: utils.h/cpp, evaluate.cpp ● Compile your file(s) using ‘Replit’ or ‘CLion’ and check your program before the submission. ● All characters in submit.txt should be in uppercase letters, e.g., ‘TURE’,’FALSE’,(except for [Task1], [Task2]) ● Please do not use the containers in C++ standard template library (STL) ○ Such as: ■ #include ■ #include ■ #include ○ Any submission using the containers in STL will be disregardedFile(s) you need to submit: ● pa2.cpp, tree.cpp, tree.h, heap.cpp, heap.h (Do not change the filename!)Any questions? Please use PLMS – Q&A board. 0. Basic Instruction a. Please refer to the attached file named “DataStructure_PA_instructions.pdf”.1. Quiz (2 pts)1.1. For such binary’s in-order and post-order traversal, which has the correct answer for preorder traversal?Inorder : 20, 30, 35, 40, 45, 50, 55, 60, 70 Postorder : 20, 35, 30, 45, 40, 55, 70, 60, 50(1) Preorder : 50, 40, 60, 30, 45, 55, 70, 20, 35 (2) Preorder : 50, 40, 30, 20, 35, 45, 60, 55, 70. (3) Preorder : 50, 40, 20, 30, 35, 45, 60, 55, 70. (4) None of the aboves1.2. What is the time complexity of insertion into Min-heap? (1) O(1) (2) O(log n) (3) O(n) (4) O(2^n)● Example execution – If you choose “(1) Preorder and Postorder traversal of T” for 1-1., print your– If you choose “(1) O(1)” for 1-2., print your answer as shown belowpre-2. Construct Binary TreeNote: pre-2 is not a problem that will be evaluated, but this is a short prerequisite to solve problems 2,3, and 4. Don’t worry. We are providing utility functions to help you.a. For problems 2, 3, and 4, you would need to implement member functions of BinaryTree class. To construct a BinaryTree class instance from an input, we use the string with bracket representation as input. The recursive definition of the bracket representation is as follows. Tree = Root(LeftChild)(RightChild). Below are some examples. The left tree is represented as 1(2)(3), and the right tree is 1(2()(4))(3()(5))b. To implement “a”, we provide a function to construct BinaryTree class from the bracket representation, which is BinaryTree::buildFromString function. It creates a pointer-based BinaryTree class instance from the given string. It would be helpful to read the implementation details of BinaryTree::buildFromStringc. To sum up, you will need to use BinaryTree class for problems 2, 3 and 4. Please try to understand the code for BinaryTree class.2. Parents and Children in a Binary Tree (3 pts)a. Implement a function that takes a binary tree in the form of a string with bracket representation, and outputs each parent node along with the number of children it has.b. Input & Output Input: – String with bracket representation of binary tree. – It is assumed that the bracket representation input received is in the correct format. – Duplicated node names are not considered in the input. Output: – String with parent node and number of children for each parent node. – If the input is in the form of ‘A(B(D)())(C)’, the output should be ‘A2B1D0C0’, indicating the number of children each node has, following a preorder traversal of the binary tree.c. Example input & output Input Output “A(B(D)())(C)” A2B1D0C0 “A(B)(C()(D))” A2B0C1D0 “A(B(C()(D()(E)))(F))()” A1B2C1D1E0F0d. Example executionpre-3. Height, Level and Level OrderNote: pre-3 is not a problem that will be evaluated, but this is a short prerequisite to solve problems 3. Don’t worry. We are providing information to help you.a. For problems 3, you would need to implement level order traverse. Level Order traversal of a binary tree involves visiting all the nodes of the tree level by level. Starting at the root, you visit all nodes at level 1, then all nodes at level 2, and so on. b. Below are some examples. Level order of left tree : 1 2 3, right tree : 1 2 3 4 51) Here is a simple step-by-step breakdown of how level order traversal works: c. Start at root node d. Move to the next level and visit all the nodes at that level from left to right e. Repeat step 2 for each subsequent level until you reach the last level of the tree2) Here are the definition of height and level. We define root level as 0.3. Height, Level and Level Order of Binary Tree (2 pts)a. Implement BinaryTree::findHeightOfNode, BinaryTree::findLevelOfNode and BinaryTree::levelOrder function that can traverse a binary tree with given level order traverse mode and finds the level and height of a specific node. You can additionally implement other functions to facilitate the traversal of a binary tree (HINT : BinaryTree::_currentLevel used in level order traverse mode.) ** We only consider 1 to 9 integer inputs for binary tree nodes.b. Input & Output Input: – String with bracket representation and String representing specific node. Output: – A sequence of node values acquired from the ① level order traverse mode and the ② height and ③ level of a specific node. The value is separated with a white space for traverse mode, and height level with enter. Check the example below. You should print out -1 for each height and level for nodes that are not in the binary tree.c. Example input & output Input Output “1(2)(3)” “2” 1 2 3 0 1 “1(2()(4))(3()(5))” “1” 1 2 3 4 5 2 0 “4(2(3)(1))(6(5))” “8” 4 2 6 3 1 5 -1 -1 d. Example execution4. Leaf count & Width of Binary Tree (3 pts) a. Implement BinaryTree::leafCount & BinaryTree::getWidth function. BinaryTree::leafCount calculates the number of leaves of the given binary tree. BinaryTree::getWidth calculates width, the largest number of nodes at any single level. Maximum depth of the tree is limited to 100.b. Input & Output Input: – String with bracket representation – Specify what you want to check. Either “leaf” or “width” Output: – The number of the leaves of the given binary tree for “leaf” command. – Width of the the given binary tree for “width” instruction.c. Example Input Output “1(2)(3)” “leaf” 2 “1(2(4)(5))(3(6))” “leaf” 3 “1(2)(3)” “width” 2 “1(2(4(7))(5))(3(6))” “width” 3d. Example execution5. Priority Queue Implementation (2 pts)Note: For solving problems 5 and 6, the similar utility functions provided in PA1 will be used to parse an input string. Therefore, you won’t need to try implementing a string parser. Please read pa2.cpp, and find the lines where your code would be located.a. You are required to implement several functions that operate on a Priority queue using a heap data structure. The Priority Queue must preserve its properties following each operation. The specific tasks are as follows: i. Insert: implement a function to insert a new element into the Priority Queue. Ensure that the queue maintains its heap property after each insertion. You will be tested with scenarios involving the insertion of fewer than 100 input values. ii. Delete: Implement a function to delete the node with the highest priority from the Priority Queue. The queue should continue to uphold its heap property after the node is deleted.b. Input & Output Input: A sequence of commands – (‘insert’,integer): insert an integer into the current priority queue. If the priority queue is full with other nodes, print “ERROR” and exit the program. If a node with the specified priority already exists in the queue, print “ERROR” and do not proceed with the insertion. The integer value indicates both the input value and its own priority. – (‘delete’,NULL): delete node with the maximum priority value from current priority queue and rearrange the priority queue to maintain the its property. If the current priority queue is empty, print “Empty” and continue the program. – (‘isEmpty’, NULL): Reply whether the priority queue is currently empty. The answer is either “TRUE” or “FALSE” – (‘getMax’, NULL): Return a value with the maximum priority. If the priority queue is empty, print “EMPTY” and continue the program.Output: – Values in a priority queue in a node number order, in a string separated with the white space (automatically printed with built-in function) – Do not consider the exceptional cases such as overflow, underflow and other input types. We will not use the test cases for those scenarios.c. Example Input & Output Input Output [(‘insert’,5),(‘insert’,-3),(‘insert’,2)] 5 -3 2 [(‘insert’,4),(‘insert’,-2),(‘insert’,9), (‘insert’,10),(‘insert’,15),(‘insert’,-25)] 15 10 4 -2 9 -25 [(‘insert’,28),( ‘insert’,9),( ‘insert’,27), (‘insert’,10),( ‘insert’,3),( ‘insert’,45), (‘delete’,NULL),( ‘insert’,22)] 28 10 27 9 3 22 [(‘getMax’, NULL), (‘insert’, 3)] EMPTY 3 [(‘isEmpty’, NULL), (‘insert’, 3), (‘isEmpty’, NULL)] TRUE FALSE 3d. Example execution >> ./pa2.exe 5 “[(‘insert’,5),(‘insert’,-3),(‘insert’,2)]” [Task 5] 5 3 26. Advanced Priority Queue (3 pts)a. You are required to implement an advanced functions that operate on a Priority queue using a heap data structure. We recommend that utilize the implementation in task 6. The Priority Queue must preserve its properties following each operation. The specific tasks are as follows: i. changeMax: Implement an operation named “changeMax” to modify the priority of a specified node to become the new maximum priority node within the Priority Queue. This operation should find a node with a given priority (e.g., 3) and change its priority to one more than the current maximum priority. If the max priority is 10, the node’s new priority would be 11. This change should promote the selected node to become the new max priority node. Ensure that after this operation, the priority queue maintains its heap property.b. Input & Output Input: A sequence of commands, which is one of the following – (‘changeMax’,integer): modify the priority of a node specified by the given integer so that it becomes the new maximum priority node. This is done by increasing the node’s priority to one more than the current maximum priority value in the Priority Queue. If there is no corresponding node, print “ERROR”.Output: – Values in a priority queue in a node number order, in a string separated with the white space (automatically printed with built-in function) – Do not consider the exceptional cases such as overflow, underflow and other input types. We will not use the test cases for those scenarios.c. Example Input & Output Input Output [(‘insert’,5),(‘insert’,-3),(‘insert’,2), 6 6 -3 5 (‘changeMax’, 2), (‘getMax’, NULL)] [(‘insert’,5),(‘insert’,-3),(‘insert’,2), ERROR 5 -3 2 (‘changeMax’, 6), (‘getMax’, NULL)]d. Example execution >> ./pa2.exe 6 “[(‘insert’,5),(‘insert’,-3),(‘insert’,2), (‘changeMax’, 2), (‘getMax’, NULL)]” [Task 6] 6 6 -3 5
Instructions • Your submission should be a single zip file 2020xxx_HW1.zip (Where 2020xxx is your roll number). Include all the files (code and report with theory questions) arranged with proper names. A single .pdf report explaining your codes with results, relevant graphs, visualization and solution to theory questions should be there. The structure of submission should follow: 2020xxx_HW |− code_rollno.py/.ipynb |− report_rollno.pdf |− (All other files for submission) • Anything not in the report will not be graded. • Your code should be neat and well-commented. • You have to do either Section B or C. • Section A is mandatory. (c) (3 points) Now, consider the scenario where there are Q kernels (Q ≥ 1) of size K ×K. Derive the computational time complexity of the forward pass for the entire image in Big-O notation as a function of the relevant dimensions. Additionally, provide another Big-O notation assuming min(M,N) ≫ K. (b) (5 points) Explain the Assignment Step and Update Step in the K-Means algorithm. Discuss any one method that helps in determining the optimal number of clusters. Can we randomly assign cluster centroids and arrive at global minima? 2. (15 points) Section B (Scratch Implementation) You are tasked with implementing the KMeans clustering algorithm from scratch using Python. Use the Euclidean distance as the distance function where k is chosen as 2. The initial centroids for the 2 clusters are given as: u1 = (3.0,3.0) u2 = (2.0,2.0) The matrix X consists of the following data points: 5.1 4.9 5.8 6.0 6.7 4.5 6.1 5.2 5.5 5.0 8.0 7.5 X = 8.1 2.5 1.0 4.5 3.0 5.1 6.0 3.5 4.0 6.1 5.4 5.3 5.8 3.5 3.0 2.7 3.0 3.1 2.3 2.8 3.2 2.6 2.0 0.5 0.8 −0.1 3.5 3.0 −1.0 −0.5 −0.2 −1.5 −0.1 0.0 0.5 −0.5 0.3 0.6 (a) Implement the k-means clustering algorithm from scratch. Ensure that your implementation includes: (a) (1 point) Initialization: Use the given centroids as starting points. (b) (2 points) Assignment: Assign each data point to the nearest centroid based on the Euclidean distance. (d) (1 point) Convergence Check: Terminate the algorithm if centroids do not significantly change between iterations or after a maximum of 100 iterations. Use a convergence threshold of 1e-4. (b) (2 points) Find the values of final centroids after the algorithm converges. Plot the two clusters at the start of the process and at the end. (c) (2 points) Compare the results using the provided initial centroids versus using random initialization of centroids. (d) (5 points) Determine the optimal number of clusters, M, using the Elbow method. Plot the Within-Cluster Sum of Squares (WCSS) against different values of k to find the elbow point. Randomly initialize M centroids, perform clustering and plot the resulting clusters OR 1. (3 points) Data Preparation: Use PyTorch to load the CIFAR-10 dataset, perform a stratified random split in the ratio of 0.8:0.2 for the training and validation datasets. Here, the 15,000 images from the training dataset are split into train-val via 0.8:0.2 split, and 3,000 images (1,000 per class) are retained as the testing data from the original test dataset of CIFAR-10. Create a custom Dataset class for the data and create data loaders for all the dataset splits – train, val, and test. 2. (0.5 points) Visualization: Load the dataset and visualize 5 images of each class from both the training and validation datasets. 3. (2.5 points) CNN Implementation: Create a CNN architecture with 2 convolutional layers (using in-built PyTorch implementations) having a kernel size of 5 x 5, 16 channels, padding and stride of 1 for the first layer, and kernel size of 3 x 3, 32 channels, stride of 1, and padding of 0 for the second layer. Use max-pooling layers with a kernel size of 3 x 3 with a stride of 2 after the first convolutional layer and a kernel size of 3 x 3 with stride 3 after the second convolutional layer. After the second max pooling layer, flatten out the output and add it to a multi-layer perceptron, with 16 neurons in the first layer and the classification head as the second layer. Use the ReLU activation function after each layer other than the last layer (the classification head layer). 4. (2.5 points) Training the model: Train the model using the cross-entropy loss function with Adam optimizer for 15 epochs. Log the training and validation loss and accuracy after each epoch. Save the trained models as .pth files, which are to be submitted along with the code for the assignment. 5. (1.5 points) Testing: Observe the training and validation plots for loss and accuracy and comment on your understanding of the results. Report the accuracy and F1score on the test dataset. Plot the confusion matrix for the train, val and test dataset. 6. (3 points) Training an MLP: Create an MLP model with 2 fully connected layers, the first layer with 64 neurons and the second layer as the classification head. Flatten out the image before processing it into the MLP. Use a ReLU layer after the first fully connected layer, and use the cross-entropy loss function with adam optimizer to train the model for 15 epochs. Log the training and validation loss and accuracy after each epoch. Save the models as .pth files, which must be submitted along with the assignment. 7. (2 points) Infer and Compare: Compute the test accuracy and F1-score and plot the confusion matrix for the MLP model. Now, compare the results and plots obtained from both the models and comment on their performance and differences. Note: During the demos, students will be expected to reproduce the evaluation results on the test dataset for both CNN and MLP models. Hence, it is critical to submit the .pth files of the trained models.
Assignment-2 Naive Bayes, Decision Trees, Random ForestsInstructions • Your submission should be a single zip file 2020xxx_HW1.zip (Where 2020xxx is your roll number). Include all the files (code and report with theory questions) arranged with proper names. A single .pdf report explaining your codes with results, relevant graphs, visualization and solution to theory questions should be there. The structure of submission should follow: 2020xxx_HW2 |− code_rollno.py/.ipynb |− report_rollno.pdf |− (All other files for submission) • Anything not in the report will not be graded. • Your code should be neat and well-commented. • You have to do either Section B or C. • Section A is mandatory.1. (10 points) Section A (Theoretical) (a) (3 mark) An investment firm is analyzing the likelihood of companies issuing dividends based on last year’s percentage profit. After reviewing extensive data, the following insights were noted: Companies that issued dividends last year tended to have an average profit increase of 10%, whereas companies that did not issue dividends typically had no significant profit change. The variation in profit among all companies was observed to be consistent and symmetric, with a spread of 36% around their respective averages. On average, 80% of companies opt to issue dividends. If a company reported a 4% profit increase last year, estimate the likelihood that it will issue a dividend this year, considering the above trends. Class Time Had Proper Sleep Weather Attended ML Class Morning YES COOL YES Morning NO RAINY NO Morning NO COOL YES Morning YES HOT YES Noon YES COOL YES Noon NO HOT NO Noon NO COOL NO Noon YES HOT YES Afternoon YES COOL YES Afternoon NO RAINY NO Afternoon NO HOT NO Afternoon YES HOT YES (b) (2 mark) Build the decision tree for the following table using Information Gain: (c) (2 mark) Given a sequence of labeled examples S that is linearly separable by a margin γ, where γ represents the minimum margin across all examples, a special type of Perceptron algorithm is applied. The algorithm initializes the weight vector using the first example and makes predictions based on whether the margin condition is met. It updates the weight vector if there is a classification mistake or a margin mistake, where a margin mistake occurs if the current hypothesis classifies an example with a margin less than γ/2. Assuming all examples are normalized to have a Euclidean length of 1, we need to prove that this special type of Perceptron algorithm will halt after a number of updates (mistakes) that is polynomial in 1/γ. Specifically, we demonstrate that the number of updates (including margin mistakes) is at most . Additionally, we will consider the case where the margin threshold γ/2 is replaced by (1 − ϵ)γ and derive the corresponding mistake bound. (d) (3 mark) You are building a spam filter using the Naive Bayes algorithm with a small dataset of labeled emails. Each email is represented by a feature vector indicating the presence (1) or absence (0) of two words: “buy” and “cheap”. Your task is to classify a new email containing the word “cheap” but not “buy”. Given the following training data: Email Word “buy” (Feature 1) Word “cheap” (Feature 2) Spam (label) 1 1 0 1 2 1 1 1 3 0 1 0 4 1 0 0 Tasks- (a) Calculate the probability estimates for each feature given the class label (spam and non-spam). (b) Compute the posterior probabilities for the new email (with “cheap” but not “buy”) being spam or non-spam. (c) Identify the problem with zero probabilities and suggest methods to address it. 2. (15 points) Section B (Scratch Implementation) About the Dataset: The dataset contains one-second .wav audio files, each with a single spoken English word. There are 105,829 audio files organized into folders by the word spoken. The audios are collected through crowdsourcing, with contributors recording words in various environments, which are converted and stored in a wave file at a 16000 sampling rate. There is also a background_noise folder with audio clips for training models in noisy environments. Tasks: 1. (4 points) Perform extensive exploratory data analysis (EDA) by performing the below-given sub-tasks: (a) Find the statistical summary of the amplitude values and duration distribution of the audio files for each class. Write your observations. (b) Plot graphical representations such as waveform plots, spectrograms, Melspectrograms, etc for randomly selected 3 audio files from randomly selected 3 classes out of 35 classes. Write your observations. (c) Investigate any class imbalances in the dataset, and if present, handle them through techniques like oversampling, undersampling, or data augmentation. (d) Perform data cleaning by detecting and addressing outliers like silent segments etc. 2. (3 points) Perform Feature Extraction: Extract relevant features from the audio data, such as MFCCs, chroma features, spectral features, and features extracted using filters like bandpass filter, etc. 3. (3 points) Model Selection and Implementation: (a) You are allowed to use only any one or ensemble of Naive Bayes, Decision Tree, Random Forest, and Perceptron models, all implemented from scratch. Train and test these models on the prepared dataset using an 80:20 train-test split. (b) Evaluate the selected models using accuracy as the metric. Document the results and compare the performance of different models. (Note: The use of libraries like scikit-learn, PyTorch, etc., is strictly prohibited for this task. All steps must be implemented from scratch.)Note: You are only allowed to use libraries like librosa only for loading the audio. All other functions must be implemented from scratch using basic libraries like numpy, pandas, matplotlib, etc. Strict penalties will be imposed otherwise. 3. (15 points) Section C (Algorithm implementation using packages) About the Dataset.: The dataset contains about 12k+ images for 15 different classes of human activities. Along with the data there is a csv file label.csv containing the label for each image. This image dataset involves labeling human actions to understand human behavior. There is only a single label attached to every image. Task: (a) Provide an overview of the dataset, including the number of images per class, the distribution of image sizes, and any other relevant statistics. Write your observations. (b) Create visualizations to represent the distribution of classes, and display a few sample images from each class. Write your observations. (c) Investigate the class distribution and discuss whether there are any class imbalances. If imbalances are present, propose strategies to address them, such as data augmentation or resampling techniques. (a) You are allowed to use only any one or ensemble of Naive Bayes, Decision Tree, Random Forest, and Perceptron models. Train and test these models on the prepared dataset using an 80:20 train-test split. (b) Evaluate the selected models using accuracy as the metric. Document the results and compare the performance of different models. Note: You are allowed to use any library functions for EDA and preprocessing. You are also allowed to use scikit-learn for train-test split as well as model implementation. Use of any deep learning module for any feature extraction or model implementation is strictly not allowed.
Instructions • Your submission should be a single zip file 202xxxx_HW3.zip (Where 2020xxx is your roll number). Include all the files (code and report with theory questions) arranged with proper names. A single .pdf report explaining your codes with results, relevant graphs, visualization and solution to theory questions should be there. The structure of submission should follow: 202xxxx_HW3 |− code_rollno.py/.ipynb |− report_rollno.pdf |− (All other files for submission) • Anything not in the report will not be graded. • Your code should be neat and well-commented. • You have to do either Section B or C. • Section A is mandatory.1. (10 points) Section A (Theoretical) (a) (3 points) Consider a regression problem where you have an MLP with one hidden layer (ReLU activation) and a linear output (refer to attached Fig 1). Train the network using mean squared error loss. Given a dataset with inputs [1, 2, 3] and corresponding targets [3, 4, 5], perform a single training iteration and update the weights. Assume appropriate initial weights and biases and a learning rate of 0.01. (b) (4 points) You have the following dataset: (a) Are the points linearly separable? Support your answer by plotting the points.Figure 1: Figure for Question 1.a Class x1 x2 Label + 0 0 + + 1 0 + + 0 1 + – 1 1 – – 2 2 – – 2 0 – Table 1: Figure for Question 1.b (b) Find out the weight vector corresponding to the maximum margin hyperplane. Also find the support vectors present. (c) (3 points) Consider the dataset with features x1 and x2 and 2 classes y = −1 and y=+1. Training Dataset: Sample No. x1 x2 y 1 1 2 +1 2 2 3 +1 3 3 3 -1 4 4 1 -1 Table 2: Table for Question 1.c The SVM formulates a decision boundary of format: w.x + b = 0 Given the values: w1 = 1, w2 = -1, b = 0 Solve the following parts: (a) Calculate the margin of the classifier. (b) Identify the support vectors. (given samples 1 to 4). (c) Predict the class of a new point : x1=1, x2=3. 2. (15 points) Section B (Scratch Implementation) 1. (5.5 points) Implement a class named NeuralNetwork with the following parameters during initialization: (a) N: Number of layers in the network. (b) A list of size N specifying the number of neurons in each layer. (c) lr: Learning rate (d) Activation function (same activation function is used in all layers of the network except the last layer). (e) Weight initialization function. (f) Number of epochs. (g) Batch size. The NeuralNetwork class should also implement the following functions: (a) fit(X, Y): trains a model on input data X and labels Y. (b) predict(X): gives the prediction for input X. (c) predict_proba(X): gives the class-wise probability for input X. (d) score(X, Y): gives the accuracy of the trained model on input X and labels Y. 2. (2 points) Implement the following activation functions (along with their gradient functions): sigmoid, tanh, ReLU, Leaky ReLU and softmax (only used in the last layer). 3. (1.5 points) Implement the following weight initialization functions: zero init, random init, and normal init (Normal(0, 1)). Choose appropriate scaling factors. 4. (6 points) Train the implemented network on the MNIST dataset. Perform appropriate preprocessing and use a 80:10:10 train-validation-test split. Use the following configurations for training the network: (a) Number of hidden layers = 4. (b) Layer sizes = [256,128,64,32]. (c) Number of epochs = 100 (can be less if computation is taking too long). (d) Batch size = 128 (or any other appropriate batch size if taking too long). (e) Learning rate = 2e-5. Plot training loss vs. epochs and validation loss vs. epochs for each activation function and weight initialization function and report your findings in the report (such as which function combination performed the best and where did it perform suboptimally). Also, save all the 12 trained models as .pkl files. You will be asked to run them during the demo to reproduce your results on the test set. OR 3. (15 points) Section C (Algorithm implementation using packages) For this question, you would need to download the Fashion-MNIST dataset. It contains a train.csv and a test.csv. You need to take the first 8000 images from the train data and the first 2000 from the test data. These will be your training and testing splits. 1. (1 point) Perform appropriate preprocessing on the data (for eg: normalization) and visualize any 10 samples from the test dataset. 2. (4 points) Train a MLP Classifier from sklearn’s neural network module on the training dataset. The network should have 3 layers of size [128, 64, 32], should be trained for 100 iterations using an ‘adam’ solver with a batch size of 128 and learning rate of 2e-5. Train it using all the 4 activation functions i.e. ‘logistic’, ‘tanh’, ‘relu’ and ‘identity’. For each activation function, plot the training loss vs epochs and validation loss vs epochs curves and comment on which activation function gave the best performance on the test set in the report. 3. (3 points) Perform grid search using the best activation function from part 2 to find the best hyperparameters (eg: solver, learning rate, batch size) for the MLP classifier and report them in the report. 4. (4 points) For this part, you need to train a MLPRegressor from sklearn’s neural network module on a regeneration task: (a) This means you will need to design a 5 layer neural network with layer sizes following the format: [c, b, a, b, c] where c > b > a. (b) By regeneration task, it means that you will try to regenerate the input image using your designed neural network and plot the training and validation losses per epoch to see if your model is training correctly. (c) Train 2 neural networks on the task above. One using a ‘relu’ activation and the other using the ‘identity’ activation function. Set the solver as adam and use a constant learning rate of 2e-5. (d) Post training both the networks, visualize the generations for the 10 test samples you visualized in part 1 and describe your observations in the report. (4 points, 1 for each part). 5. (3 points) Lastly, from the two neural networks trained above extract the feature vector of size ‘a’ for the train and test data samples. Using this vector as your new set of image features, train two new smaller MLP Classifiers with 2 layers, each of size ‘a’ on the training dataset and report accuracy metrics for both these classifiers. Train it for 200 iterations with the same solver and learning rate as part 2. Contrast this with the MLP Classifier you trained in part 2 and report possible reasons why this method still gives you a decent classifier?
Assignment-1 Linear and Logistic Regression, ML in Practice, Empirical Risk MinimizationInstructions • Your submission should be a single zip file 2020xxx_HW1.zip (Where 2020xxx is your roll number). Include all the files (code and report with theory questions) arranged with proper names. A single .pdf report explaining your codes with results, relevant graphs, visualization and solution to theory questions should be there. The structure of submission should follow: 2020xxx_HW |− code_rollno.py/.ipynb |− report_rollno.pdf |− (All other files for submission) • Anything not in the report will not be graded. • Your code should be neat and well-commented. • You have to do either Section B or C. • Section A is mandatory. x y 3 15 6 30 10 55 15 85 18 100 Table 1: Table of x and y values equation obtained, predict the value of y when x=12. 2. (15 points) Section B (Scratch Implementation) Dataset: Heart Disease OR 3. (15 points) Section C (Algorithm implementation using packages) Split the given dataset into 80:20 (train: test) and perform the following tasks: Dataset: Electricity Bill Dataset
Assignment 02 Indraprastha Institute of Information Technology, Delhi https://www.iiitd.ac.in/sites/default/files/docs/education/AcademicDishonesty.pdf NOTE: We won’t respond to any query because you missed to read the description carefully. We will ONLY respond to valid questions. Make sure you ask all your doubts in advance and not at the last minute. Please follow the following good programming practices that are as follows. ● All the fields in the class should be private unless there is a good reason to have otherwise. ● Every field should have a getter and a setter. ● All the methods of the class should be public unless there is a good reason to have otherwise (for example, helper methods need not be public). ● Every class should be public. ● Add comments to the class and methods except the getter and setter methods. ● Follow the naming conventions for classes, fields, methods, objects, and variables (including constants). Topic: Interfaces, Polymorphism & Object Class Welcome to ZOOtopia! You must employ Object-Oriented Programming (OOP) principles, such as interfaces, inheritance, abstract classes, and polymorphism, to design the application effectively. Application Flow: The application will have a user-friendly command-line interface where you can enter as either an admin or a visitor. Visitors can enjoy different levels of zoo experiences: “Basic,” “Premium,”. These levels offer varying perks and access to different areas of the zoo. Admins can manage the zoo’s attractions, animals, and visitor information. They can also add special events and discounts. Admin Functionalities: Enter as Admin: Admins must provide their username and password to access all admin privileges. Make use of a constant for username and password with fixed values to be compared against. Manage Attractions/Events: Admins can view, add, modify, or remove attractions in the zoo. Each attraction should have a unique ID. Attractions can be any cultural exhibit, such as a safari experience, a botanical garden, a dinosaur show, etc. The attractions are free of cost for the premium members. However, the basic members have to buy a ticket for a fixed price for each attraction. Manage Animals: Admins can add animals to specific attractions. They can also update animal details and remove animals when necessary. Animals can be of 3 types – Mammals, Amphibians and Reptiles. Note that there must be at least 2 kinds of animals for each type. For e.g., elephants and monkeys could be the 2 kinds of mammals required. When adding any new animal to the zoo, the animal should belong to one of these 3 categories only. Set Discounts: Admins can set discount codes on ticket prices for different visitor categories and attractions, such as minors or seniors. ● Every minor visitor to the zoo (60) should get a 20% discount on their tickets and membership amount Admin should be able to add or remove any of these discounts as required. ● If a person buys more than 2 attractions, they get a special discount of 15% on the total amount ● If a person buys more than 3 attractions, they get a special discount of 30% on the total amount Admin should be able to add or remove any of these deals as required. View Visitor Stats: Admins can access statistics on the number of visitors, revenue, and popular attractions. View Feedback: Admins should also be able to see the feedback left by the visitors. Visitor Functionalities: Register: Visitors can register by providing their name, age, phone number, balance, email, and password. Log In: Registered visitors can log in using their email and password to access their account. Explore the Zoo: Visitors can browse through different attractions and learn about the animals. Buy Membership: Visitors can purchase tickets based on their chosen experience level (Basic, Premium). Buy Tickets: Only basic users can buy tickets for each attraction they want to visit. They can buy tickets for as many attractions as they want and also get special deals. The ticket is a one-time usage ticket and will be invalid after a visit to the attraction. View Discounts: During the purchase of tickets, visitors can see all the discounts available and select the appropriate discount code. Apply Discounts: Visitors can apply any available discount (using the discount codes) to their ticket purchase if they satisfy the eligibility criteria. Visit Animal: The visitor will choose to visit an animal, this functionality will, in turn, ask the visitor to either choose to feed the animal or read about the animal. Choosing to feed the animal will result in the animal making a noise, for example, if you choose to feed a lion it will “Roar”, reading about the animal will return a brief passage about the animal’s history. Visit Attractions/Visit Event: Visitors can access attractions based on their experience level and the ticket bought to enjoy unique experiences. A simple “Welcome to the attraction X” statement is enough to be counted as a visit to the attraction. Provide Feedback: Visitors can leave feedback about their zoo experience and suggest improvements. Sample Run Welcome to ZOOtopia! 1. Enter as Admin 2. Enter as a Visitor 3. View Special Deals Enter your choice: 1 Enter Admin Username: admin Enter Admin Password: admin123 Logged in as Admin. Admin Menu: 1. Manage Attractions 2. Manage Animals 4. Set Discounts 5. Set Special Deal 6. View Visitor Stats 7. View Feedback 8. Exit Enter your choice: 1 Manage Attractions: 1. Add Attraction 2. View Attractions 3. Modify Attraction 4. Remove Attraction 5. Exit Enter your choice: 1 Enter Attraction Name: Jungle Safari Enter Attraction Description: A thrilling adventure in the jungle. Attraction added successfully. Admin Menu: 1. Manage Attractions 2. Manage Animals 4. Set Discounts 5. Set Special Deal 6. View Visitor Stats 7. View Feedback 8. Exit Enter your choice: 2 Manage Animals: 1. Add Animal 2. Update Animal Details 3. Remove Animal 4. Exit Enter your choice: 1 Enter Animal Name: Lion Enter Animal Type: Mammal Animal added to Zoo. Admin Menu: 1. Manage Attractions 2. Manage Animals 4. Set Discounts 5. Set Special Deal 6. View Visitor Stats 7. View Feedback 8. Exit Enter your choice: 4 Set Discounts: 1. Add Discount 2. Modify Discount 3. Remove Discount 4. Exit Enter your choice: 1 Enter Discount Category: Students Enter Discount Percentage (e.g., 20 for 20%): 15 Discount added successfully. Admin Menu: 1. Manage Attractions 2. Manage Animals 4. Set Discounts 5. Set Special Deal 6. View Visitor Stats 7. View Feedback 8. Exit Enter your choice: 6 Visitor Statistics: – Total Visitors: 1200 – Total Revenue: $15,000 – Most Popular Attraction: Jungle Safari Admin Menu: 1. Manage Attractions 2. Manage Animals 4. Set Discounts 5. Set Special Deal 6. View Visitor Stats 7. View Feedback 8. Exit Enter your choice: 8 Logged out. 1. Enter as Admin 2. Enter as a Visitor 3. View Special Deals Enter your choice: 2 1. Register 2. Login Enter your choice: 1 Enter Visitor Name: Alice Enter Visitor Age: 32 Enter Visitor Phone Number: 7568743321 Enter Visitor Balance: 100 Enter Visitor Email: [email protected] Enter Visitor Password: pass123 Registration is successful. 1. Register 2. Login Enter your choice: 2 Enter Visitor Email: [email protected] Enter Visitor Password: pass123 Login Successful. Visitor Menu: 1. Explore the Zoo 2. Buy Membership 3. Buy Tickets 4. View Discounts 5. View Special Deals 6. Visit Animals 7. Visit Attractions 8. Leave Feedback 9. Log Out Enter your choice: 1 Explore the Zoo: 1. View Attractions 2. View Animals 3. Exit Enter your choice: 1 Attractions in the Zoo: 1. Jungle Safari 2. Botanical Garden 3. Dinosaur Show Enter your choice: 1 ZOOtopia offers an adventure ride that allows you to explore unexplored trails. Buy your ticket now! Explore the Zoo: 1. View Attractions 2. View Animals 3. Exit Enter your choice: 3 Visitor Menu: 1. Explore the Zoo 2. Buy Membership 3. Buy Tickets 4. View Discounts 5. View Special Deals 6. Visit Animals 7. Visit Attractions 8. Leave Feedback 9. Log Out Enter your choice: 2 Buy Membership: 1. Basic Membership (₹20) 2. Premium Membership (₹50) Enter your choice: 2 Apply Discount Code: None Basic Membership purchased successfully. Your balance is now ₹80. Visitor Menu: 1. Explore the Zoo 2. Buy Membership 3. Buy Tickets 4. View Discounts 5. View Special Deals 6. Visit Animals 7. Visit Attractions 8. Leave Feedback 9. Log Out Enter your choice: 3 Buy Tickets: Select an Attraction to Buy a Ticket: 1. Jungle Safari (₹10) 2. Botanical Garden (₹15) 3. Dinosaur Show (₹12) Enter your choice: 1 The ticket for Jungle Safari was purchased successfully. Your balance is now ₹70. Visitor Menu: 1. Explore the Zoo 2. Buy Membership 3. Buy Tickets 4. View Discounts 5. View Special Deals 6. Visit Animals 7. Visit Attractions 8. Leave Feedback 9. Log Out Enter your choice: 4 View Discounts: 1. Minor (10% discount) – MINOR10 2. Senior Citizen (20% discount) – SENIOR20 Visitor Menu: 1. Explore the Zoo 2. Buy Membership 3. Buy Tickets 4. View Discounts 5. View Special Deals 6. Visit Animals 7. Visit Attractions 8. Leave Feedback 9. Log Out Enter your choice: 5 Special Deals: 1. Buy 2 tickets and get 15% off 2. Buy 3 tickets and get 30% off Visitor Menu: 1. Explore the Zoo 2. Buy Membership 3. Buy Tickets 4. View Discounts 5. View Special Deals 6. Visit Animals 7. Visit Attractions 8. Leave Feedback 9. Log Out Enter your choice: 7 Visit Attractions: Select an Attraction to Visit: 1. Jungle Safari 2. Botanical Garden 3. Dinosaur Show 4. Exit Enter your choice: 2 Ticket not available. Basic Members need to buy separate tickets for the attractions. Select an Attraction to Visit: 1. Jungle Safari 2. Botanical Garden 3. Dinosaur Show 4. Exit Enter your choice: 1 1 Ticket Used. Thank you for visiting Jungle Safari. Hope you enjoyed the attraction. Visitor Menu: 1. Explore the Zoo 2. Buy Membership 3. Buy Tickets 4. View Discounts 5. View Special Deals 6. Visit Animals 7. Visit Attractions 8. Leave Feedback 9. Log Out Enter your choice: 8 Leave Feedback: Enter your feedback (max 200 characters): The Jungle Safari was terrific! Thank you for your feedback. Visitor Menu: 1. Explore the Zoo 2. Buy Membership 3. Buy Tickets 4. View Discounts 5. View Special Deals 6. Visit Animals 7. Visit Attractions 8. Leave Feedback 9. Log Out Enter your choice: 9 Logged out.
Before starting the assignment clone the skeleton code for the assignment from the below github repo: https://github.com/Rahul-Agrawal-09/MeMS-Skeleton-code.git Problem Statement: Implement a custom memory management system (MeMS) using the C programming language. MeMS should utilize the system calls mmap and munmap for memory allocation and deallocation, respectively. The system must satisfy the following constraints and requirements outlined below: Constraints and Requirements: 1. MeMS can solely use the system calls mmap and munmap for memory management. The use of any other memory management library functions such as malloc , calloc , free , and realloc are STRICTLY PROHIBITED . 2. MeMS should request memory from the OS using mmap in multiples of the system’s PAGE_SIZE, which can be determined using the command getconf PAGE_SIZE . For most Linux distributions, the PAGE_SIZE is 4096 bytes (4 KB); however, it might differ for other systems. 3. MeMS should deallocate memory only through munmap and deallocation should only occur in multiples of PAGE_SIZE. 4. As the value of PAGE_SIZE can differ from system to system hence use the macro “PAGE_SIZE” provided in the template wherever you need the value of PAGE_SIZE in your code so that this size can be modified if required for evaluation purposes. 5. The user program must use the functions provided by MeMS for memory allocation and deallocation. It is not allowed to use any other memory management library functions, including malloc , calloc , free , realloc , mmap, and munmap . 6. Although MeMS requests memory from the OS in multiples of PAGE_SIZE, it only allocates that much amount of memory to the user program as requested by the user program. MeMS maintains a free list data structure to keep track of the heap memory which MeMS has requested from the OS. This free list keeps track of two items: a. memory allocated to each user program. We will call this memory as PROCESS in the free list (details below). b. Memory which has not been allocated to any user program. We will call this memory as a HOLE in the free list (details below). Free List Structure: Free List is represented as a doubly linked list. Let’s call this doubly linked list as the main chain of the free list. The main features of the main chain are: 1. Whenever MeMS requests memory from the OS (using mmap), it adds a new node to the main chain. 2. Each node of the main chain points to another doubly linked list which we call as sub-chain . This sub-chain can contain multiple nodes. Each node corresponds to a segment of memory within the range of the memory defined by its main chain node. Some of these nodes (segments) in the sub-chain are mapped to the user program. We call such nodes (segments) as PROCESS nodes. Rest of the nodes in the sub-chain are not mapped to the user program and are called as HOLES or HOLE nodes. Whenever the user program requests for memory from MeMS, MeMS first tries to find a sufficiently large segment in any sub-chain of any node in the main chain. If a sufficiently large segment is found, MeMS uses it to allocate memory to the user program and updates the segment’s type from HOLE to PROCESS. Else, MeMS requests the OS to allocate more memory on the heap (using mmap) and add a new node corresponding to it in the main chain. The structure of free list looks like below:The main features of the chain (sub-chain) are: 1. Each chain is broken into segments. 2. Each segment represents a part of memory and represents whether that segment is of type PROCESS i.e. is mapped to the user process or is of type HOLE i.e. not allocated/mapped to the user program. 3. The segments of type HOLE can be reallocated to any new requests by the user process. In this scenario, if some space remains after allocation then the remaining part becomes a new segment of type HOLE in that sub-chain. Graphaphically it looks something like below:In the above picture, the Node1 of sub-chain-4 is reused by the user process but only 600 bytes out of 1000 bytes are used. Hence a HOLE of 400 bytes is created and the node of 600 bytes is marked as PROCESS and the MeMS virtual address corresponding to 600 bytes node is returned to the user process for further use. NOTE: You must handle the corner cases and make sure that your system should avoids memory fragmentation within the free list. MeMS Virtual Address and MeMS Physical Address: Let us call the address (memory location) returned by mmap as the MeMS physical address. In reality, the address returned by mmap is actually a virtual address in the virtual address space of the process in which MeMS is running. But for the sake of this assignment, since we are simulating memory management by the OS, we will call the virtual address returned by mmap as MeMS physical address. Just like a call to mmap returns a virtual address in the virtual address space of the calling process, a call to mems_malloc will return a MeMS virtual address in the MeMS virtual address space of the calling process. For the sake of this assignment, MeMS manages heap memory for only one process at a time. Any time the user process wants to write/store anything to the heap, it has to make use of the MeMS virtual address. But we cannot directly write using MeMS virtual address as the OS does not have any understanding of MeMS virtual address space. Therefore, we first need to get the MeMS physical address for that MeMS virtual address. Then, the user process needs to use this MeMS physical address to write on the heap. For example in the below figure 1. MeMS virtual address 0 corresponds to MeMS physical address 1000 2. MeMS virtual address 500 corresponds to MeMS physical address 1500 3. MeMS virtual address 1024 corresponds to MeMS physical address 5024We can get the MeMS physical address (i.e. the actual address returned by mmap) corresponding to a MeMS virtual address by using the function mems_get function ( see below for more details). Function Implementations: Implement the following functions within MeMS: 1. void mems_init(): Initializes all the required parameters for the MeMS system. The main parameters to be initialized are a. the head of the free list i.e. the pointer that points to the head of the free list b. the starting MeMS virtual address from which the heap in our MeMS virtual address space will start. c. any other global variable that you want for the MeMS implementation can be initialized here. Input Parameter : Nothing Returns : Nothing 2. void mems_finish(): This function will be called at the end of the MeMS system and its main job is to unmap the allocated memory using the munmap system call. Input Parameter : Nothing Returns : Nothing 3. void* mems_malloc(size_t size): Allocates memory of the specified size by reusing a segment from the free list if a sufficiently large segment is available. Else, uses the mmap system call to allocate more memory on the heap and updates the free list accordingly. Parameter : The size of the memory the user program wants Returns : MeMS Virtual address (that is created by MeMS) 4. void mems_free(void* ptr): Frees the memory pointed by ptr by marking the corresponding sub-chain node in the free list as HOLE. Once a sub-chain node is marked as HOLE, it becomes available for future allocations. Parameter : MeMS Virtual address (that is created by MeMS) Returns : nothing 5. void mems_print_stats(): Prints the total number of mapped pages (using mmap) and the unused memory in bytes (the total size of holes in the free list). It also prints details about each node in the main chain and each segment (PROCESS or HOLE) in the sub-chain. Parameter : Nothing Returns : Nothing but should print the necessary information on STDOUT 6. void *mems_get(void*v_ptr): Returns the MeMS physical address mapped to ptr ( ptr is MeMS virtual address). Parameter : MeMS Virtual address (that is created by MeMS) Returns : MeMS physical address mapped to the passed ptr (MeMS virtual address). Submission Guidelines: Please download the skeleton template from the below github repository: https://github.com/Rahul-Agrawal-09/MeMS-Skeleton-code Students are required to submit the C code containing the implementation of all the functions there in the skeleton template provided, along with a detailed explanation of their approach. Additionally, students should provide a demonstration of their implementation with test cases that validate the functionality of MeMS under different scenarios. (Note: Do not change any function’s name or signature provided in the skeleton template. You are free to include more files if you want.) You will be graded upon: 1. Documentation 2. Viva/Demo 3. Error handling 4. Covering all the edge cases (like joining 2 adjacent hole nodes to make a new hole node) 5. Correct mems_virtual_address corresponding to mems_physical_address. 6. We will run various test cases (variants of the example file which we have shared) and each of them will have individual weightage. 7. Successful compilation and correct output format. 8. Use the data structures which have been described in the documentation. 9. Any other implementation specific detail necessary for the assignment. Note: 1. Use the PAGE_SIZE macro in the code so that we can test your code with different page size values. Suggestion: 1. Maintain good program structure and proper naming convention so that evaluation can be done without any hassle
Lab Assignment 01 Indraprastha Institute of Information Technology, Delhi https://www.iiitd.ac.in/sites/default/files/docs/education/AcademicDishonesty.pdf NOTE: We won’t respond to any query because you missed to read the description carefully. We will ONLY respond to valid questions. Make sure you ask all your doubts in advance and not at the last minute. Topics: Classes, Objects, Encapsulation & Class Relationships Instructions ● Develop a Library Management System application using Object-Oriented Programming (OOP) principles. ● Make appropriate classes that represent different entities and functionalities within the library system. Design a menu-driven interface for users to interact with the library system. ● Ensure that your code works for all possible scenarios. ● If the code does not compile using Maven, you will get a zero. Functionalities: 1. Add Book: ● Allow a librarian to add a new book to the library. ● Take input for book details (title, author, total copies). 2. Remove Book: ● Allow a librarian to remove a book from the library. ● Take input for book ID. 3. Register Member: ● Allow a librarian to add a new member to the library. ● Take input for member details (name, age, etc). ● The Phone Number can act as an unique input to identify the members uniquely. 4. Remove Member: ● Allow a librarian to remove a member from the library. Take input for member ID. Here also, we can take input as username and phone no etc. 5. Enter as a member ● Takes the member name and phone number as input and logs in as the particular member, if already registered . ● If not registered, gives an error message. 6. Issue Book: ● Allow a member to borrow a book from the library, given his penalty amount is zero. If he owes the library penalty money, he should first clear that before issuing a new book. ● Display available books and take input for book ID. 7. Return Book: ● Allow a member to return a borrowed book. ● Display borrowed books and take input for book ID. 8. List Books: ● Display a list of all available books in the library. 9. List Members: ● Display a list of all registered members of the library. 10. Calculate Fine: 11. Exit: ● Terminate the application. Guidelines: ● Implement input validation to handle incorrect or invalid user inputs. ● Ensure proper error handling and display user-friendly error messages. Apply OOP principles to improve code organization and readability.Submission: Submit File Name format: A1_RollNumber.zip Important: The code should be composed and presented using Maven. Additionally, please provide a Readme document that comprehensively outlines the procedures for executing your code. Ensure that the pom.xml file is included in your submission as well. These requirements are essential to facilitate the evaluation of your submission. If any of these is missing, you will get a zero. SAMPLERUN Library Portal Initialized…. ——————————— 1. Enter as a librarian 2. Enter as a member 3. Exit ——————————— 1 ——————————— 1. Register a member 2. Remove a member 3. Add a book 4. Remove a book 5. View all members along with their books and fines to be paid 6. View all books 7. Back ——————————— 1 ——————————— Name: Rick Dunphy Age: 21 Phone no: 9999999993 ——————————— Member Successfully Registered with ! ——————————— 3 ——————————— 1. Book title: Harry Potter 2. Author: J.K. Rowling 3. Copies: 2 ——————————— Book Added Successfully! ——————————— 1. Register a member 2. Remove a member 3. Add a book 4. Remove a book 5. View all members along with their books and fines to be paid 6. View all books 7. Back ——————————— 6 ——————————— Book ID – 1 Name – Harry Potter Author – JK Rowling Book ID – 2 Name – Harry Potter Author – JK Rowling ——————————— ——————————— 1. Register a member 2. Remove a member 3. Add a book 4. Remove a book 5. View all members along with their books and fines to be paid 6. View all books 7. Back ——————————— 7 ——————————— 1. Enter as a librarian 2. Enter as a member 3. Exit ——————————— 2 Name : Rick Dunphy Phone No: 9999999990 Member with Name: Rick Dunphy and Phone No: 9999999990 doesn’t exist. ——————————— 1. Enter as a librarian 2. Enter as a member 3. Exit ——————————— 2 Name: Rick Dunphy Phone No: 9999999993 Welcome Rick. Member ID: ——————————— 1. List Available Books 2. List My Books 3. Issue book 4. Return book 5. Pay Fine 6. Back ——————————— 3 ——————————— Book ID: 1 Book Name: Harry Potter ——————————— Book Issued Successfully! ——————————— 1. List Available Books 2. List My Books 3. Issue book 4. Return book 5. Pay Fine 6. Back ——————————— 4 ——————————— Enter Book ID: 1 ——————————— Book ID: 1 successfully returned. 12 Rupees has been charged for a delay of 4 days. ——————————— 1. List Available Books 2. List My Books 3. Issue book 4. Return book 5. Pay Fine 6. Back ——————————— 5 ——————————— You had a total fine of Rs. 12. It has been paid successfully! ——————————— 1. List Available Books 2. List My Books 3. Issue book 4. Return book 5. Pay Fine 6. Back ——————————— 6 ——————————— 1. Enter as a librarian 2. Enter as a member 3. Exit ——————————— 3 ——————————— Thanks for visiting! ———————————
Requirements: A basic RDP design is provided, and you can extend the design as you see fit. RDP follows HTTP design, using full text, line-by-line control headers to establish (SYN) and release (FIN) connection, and Sequence number in data (DAT) packet of PAYLOAD Length and Acknowledgment number in acknowledgment (ACK) packet to reorder out-of-order packets, acknowledge received packets and identify lost packets for retransmission. To be efficient, RDP cannot use stop-and-wait (i.e., transmit a packet, wait for acknowledgment, and retransmit until it is received), and has to support flow control using Window size (i.e., multiple packets can be transmitted back-to-back within the Window, and lost packets will be recovered through retransmission by timeout or three duplicate acknowledgments). You can model your RDP design after TCP, but you do not need to implement the entire TCP. Congestion control is not needed for RDP in P2. RDP packet format COMMAND Header: Value … Header: Value PAYLOAD where, COMMAND := SYN|FIN|DAT|ACK|RST, and SYN indicates to establish connection, FIN to close connection, RST to reset connection, DATA to send data, and ACK to acknowledge received data. Header := Sequence|Acknowledgment|Window|Length, and Sequence provides the sequence number associated with the packet (the first byte of the PAYLOAD if any), Acknowledgment the sequence number expected by the receiver (the next byte of the PAYLOAD if any), Window the size of the receiver’s window in bytes, and Length the length of the PAYLOAD in bytes. Note that an empty line indicates the end of headers. PAYLOAD, if existing, is the actual data sent from the sender to the receiver. PAYLOAD is at most 1024 bytes. The Echo Server On H2 mkfifo fifo which makes a FIFO (named pipe) called fifo. you only need to run this command once at /home/jovyan Then cat fifo | nc -u -l 8888 > fifo which runs nc (netcat) in UDP server mode at port 8888, redirects output to fifo, and pipes through fifo to nc. To test the echo server, on H1 nc -u h2 8888 and type any input—it will be echoed back by H2 and shown on H1. Use tcpdump on R tcpdump -n -l -i r-eth1 udp port 8888 to verify it. Connection Management PAYLOAD are all case sensitive. For unrecognized or incompatible COMMAND and Headers (e.g., DAT packets missing Sequence number and Length, ACK packets missing Acknowledgment number and Window, and so on), RDP will reset the connection with RST, and there is no ACK for RST. Data Transfer Once connected, the RDP sender can send DAT (data) packets to the receiver, each with a Sequence number header indicating the sequence number of the first bytes of the PAYLOAD, and Length indicating the size of the PAYLOAD in bytes. The RDP receiver will acknowledge the received DAT packets cumulatively, using the Acknowledgment header indicating the first byte expected in the next PAYLOAD, as well as the Window size. How to emulate Internet delay On R, tc qdisc add dev r-eth1 root netem delay 100ms will add 100 millisecond delay at the output queue of r-eth1. Flow Control The RDP receiver will advertise its receiver window in ACK packets with the Window header. The RDP sender shall respect the Window size and shall not send any data with sequence number equal to or above Acknowledgment+Windows. How to emulate Internet delay and loss On R tc qdisc change dev r-eth1 root netem delay 100ms loss 25% after the above command will add 100 millisecond delay and set 25% packet loss at the output queue of r-eth1. Error Control on H1 will bind rdp to ip_address and port_number to send or receive UDP packets, and to transfer a file with read_file_name from the RDP sender to receiver saved with write_file_name. After the file transfer is finished, diff read_file_name write_file_name on H1 can tell you whether the received file is different from the sent file of length greater than 10,240 bytes. What RDP outputs How to test and evaluate RDP Run the echo server on H2, run tcpdump on R to capture the interaction between H1 and H2, and run rdp.py on H1. It is very important to correlate the packet exchange between H1 and H2 and the endpoint reaction at H1 for both the RDP sender and receiver through the RDP log, which can help you debug your code as well. Although RDP sender and receiver are in the same rdp.py on H1, RDP protocol and logic cannot be bypassed. What to submit: rdp.py source file, and the tcpdump files on R, showing the interaction between H1 and H2. Please also copy&paste the content of rdp.py and tcpdump -r the capture file to the text input box on connex. Questions and answers (Please read P2 Spec carefully first): brightspace => forums => p2 Appendix A: How does PicoNet emulate an Internet with delay and loss?Appendix B: A basic design for your reference
SWS requirements: SWS only supports the “GET /filename HTTP/1.0” command, and “Connection: keep-alive” and “Connection: close” request and response header when supporting persistent HTTP connection. The request header is terminated by an empty line known as “ ”, where “ ” indicates a carriage return and “ ” a (new) line feed. If unsupported commands are received or in unrecognized format, SWS will respond “HTTP/1.0 400 Bad Request”. If the file indicated by filename is inaccessible, SWS will return “HTTP/1.0 404 Not Found”. Such responses will be followed by the response header if any, an empty line, indicating the end of the response. For successful requests, SWS will respond “HTTP/1.0 200 OK”, followed by the response header if any, an empty line indicating the end of the response header, and the content of the file. How to run SWS: On H2 in PicoNet, “python3 sws.py ip_address port_number”, where ip_address and port_number indicate where SWS binds its socket for incoming requests. 1 On H1 in PicoNet, “nc sws_ip_address sws_port_number” to connect to SWS, and type “GET /sws.py HTTP/1.0” followed by “Connection: keep-alive” and an empty line to request the file sws.py from SWS (in this case, SWS shall keep the connection alive after sending back sws.py following an empty line after “Connection: keep-alive”, and wait for the next request from the same client through the same TCP connection, until the connection times out, i.e., “Connection: close”). If the client does not include “Connection: keep-alive” or does include “Connection: close” in its request, SWS will close the connection after serving the request. How to test SWS: Capture and analyze the interaction between SWS and its clients (nc, wget or even a regular web browser) with tcpdump and Wireshark. Your code will be evaluated in PicoLab as you have in ECS360. What to submit: sws.py source file, and the tcpdump files on R, showing the interaction between SWS and its clients, in both persistent (sws-persistent.cap) and non-persistent (sws-non-persistent.cap) connections. Please also copy and paste the content of sws.py and tcpdump -r the capture file to the text input box on brightspace. When: 2Appendix: A simple design for your reference 3
Objective: In this assignment, you will work with text data from different sources to build models for text summarization using Recurrent Neural Networks (RNNs). You will then apply the trained models to summarize on a new dataset and perform qualitative analysis based on your findings.● Description: You need to perform an exploratory data analysis on the CNN/Daily Mail dataset to better understand its structure. ○ Inspect the data structure (headlines and stories). ○ Explore the most frequent words and sentence lengths. ○ Perform basic text cleaning (lowercasing, tokenization, removing special characters). ○ Provide visualizations of word frequency distributions and sentence lengths. * For more information on how to load dataset from huggingface refer here● Description: You are required to implement a Seq2Seq model using RNNs (or LSTMs) to summarize the texts in the CNN/Daily Mail dataset. ○ Step 1: Preprocess the text (tokenization, padding, and vocabulary creation). ○ Step 2: Build the Seq2Seq architecture (encoder, decoder, optional attention mechanism). You are free to choose the hyperparameters. ○ Step 3: Train the model on the CNN/Daily Mail dataset (train split) and evaluate (on test split) using ROUGE scores (use Rouge-2 and Rouge-L). Expected Outcome: The trained model should be able to summarize unseen articles with reasonable accuracy. Task 3: Test the Model on Wikipedia Summary Data ● Description: Using the same Seq2Seq model trained previously, test the model on the Wikipedia summary dataset (consider the first 10k rows as test data). ○ Note: No need for data exploration here. Preprocess the data and report the evaluation results.Deliverables: 1. Code (75): Submit a well-structured Python notebook with all code, including data preprocessing, model training, and summarization for both datasets. 2. Report (25): Provide a comprehensive report discussing: ○ Data exploration findings. ○ Model training and evaluation results. ○ Observations
Emulating End-to-End Reliable Flow Control over Unreliable Communication ChannelsIn this assignment, you will be building support for end-to-end reliable data transfer on top of an unreliable communication channel, with a window-based flow control approach.You have been introduced to the function calls socket, bind, sendto, and recvfrom to work with UDP sockets. Assume that the TCP sockets are not there. We know that UDP sockets are not reliable, meaning that there is no guarantee that a message sent using a UDP socket will reach the receiver. We want to implement our own socket type, called MTP (My Transport Protocol) socket, that will guarantee that any message sent using a MTP socket is always delivered to the receiver in order. Also, like UDP sockets. MTP is message-oriented, and not byte-oriented.For each MTP socket, the sender and the receiver maintain a sending window (swnd) and a receiving window (rwnd), respectively. swnd is a data structure that contains the sequence numbers of the messages sent but not yet acknowledged, and the window size indicating the maximum number of messages that can be sent without receiving the corresponding acknowledgement (ideally, this should not be more than the available space at the receiver buffer at any instance of time). Similarly, rwnd is a data structure indicating the sequence numbers that the receiver is expecting to receive and a window size indicating the maximum number of messages it can receive depending on the available space at the receiver-side message buffer. The messages are of fixed size (assume that message size is 1 KB), and the messages have increasing sequence numbers (sequence number is of 4 bit length). The sender always starts with the sequence number 1. The receiver maintains a message buffer of size equivalent to 5 messages. The sender maintains a message buffer of size equal to 10 messages. Once the receiver receives an in-order message, the message is written to the buffer after removing the MTP header, the free space in the buffer is computed and the rwnd size is updated accordingly. The receiver then sends an ACK message to the sender which piggybacks the updated rwnd size, and the sequence number of the last in-order message received within rwnd. If the receiver receives an out of order message, it keeps the message in the buffer (if the message sequence number is within rwnd), but sends an ACK message to the sender with the sequence number of the last in-order message received and the updated rwnd size. Note that the MTP receiver also needs to handle the duplicate messages by identifying them with the sequence number and then dropping them if already received once. However, for the duplicated messages also, it sends an ACK message to the sender with the sequence number of the last in-order message received and the updated rwnd size. The receiver side application periodically reads the messages (in 1 KB block, the entire message is read atomically) from the message buffer and frees it up. Message are always delivered in order to the application.At the sender side, MTP maintains a sender-side message buffer where the sender-side application writes data in a block of 1KB. MTP initially sets swnd = 5 and starts sending the messages after adding the required header (the header contains the message sequence number; think of if you need any additional field in the header). Once the MTP sender receives an ACK message, it updates the swnd accordingly (slides the window till the last message acknowledged if not a duplicate ACK and increases/decreases the window size based on the piggybacked rwnd size in the ACK message). The MTP sender also sets a timeout equivalent to T seconds. If it does not receive an ACK message within the timeout duration, it retransmits all the messages from the swnd (which had been transmitted earlier but not been acknowledged).Multiple user applications can create MTP sockets simultaneously; however, in your implementation, you can assume that at any instance, there can be a maximum of N = 25 number of active MTP sockets. To implement these MTP sockets, we use the following:1. UDP sockets through which all transport layer communication happens for the corresponding MTP socket. 2. An init process P that initializes two threads R and S, and a shared memory as mentioned next. Thread R handles all messages received from the UDP socket, and thread S handles sending messages, and the timeouts and retransmissions. More details of R and S are given later. 4. The init process should also start a garbage collector process G to clean up the corresponding entry in the MTP socket if the corresponding process is killed and the socket has not been closed explicitly.You will be implementing a set of function calls m_socket, m_bind, m_sendto, m_recvfrom, and m_close that implement MTP sockets. The parameters and return values to these functions are exactly the same as the corresponding functions of the UDP socket, except for m_bind. The functions will be implemented as a library. Any user wishing to use MTP sockets will write a C program that will call these functions in the same sequence as when using UDP sockets. A brief description of the functions is given below.● m_socket – This function opens an UDP socket with the socket call. The parameters to these are the same as the normal socket() call, except that it will take only SOCK_MTP as the socket type. m_socket() checks whether a free entry is available in the SM, creates the corresponding UDP socket if a free entry is available, and initializes SM with corresponding entries. If no free entry is available, it returns -1 with the global error variable set to ENOBUFS. If any error is received while creating the UDP socket, errno is set to the corresponding error.● m_bind – binds the socket with some address-port using the bind call. Bind is necessary for each MTP socket irrespective of whether it is used as a server or a client. This function takes the source IP, the source port, the destination IP and the destination port. It binds the UDP socket with the source IP and source port, and updates the corresponding SM with the destination IP and destination port. If any error is received while binding the UDP socket, errno is set to the corresponding error, and SM is not updated. The idea here is that a single MTP socket is used to communicate with another pre-specified MTP socket only (different from how an UDPP socket behaves, even though MTP uses UDP underneath).● m_sendto – writes the message to the sender side message buffer if the destination IP/Port matches with the bounded IP/Port as set through m_bind(). If not, it drops the message, returns -1 and sets the global error variable to ENOTBOUND. If there is no space is the send buffer, return -1 and set the global error variable to ENOBUFS. So the m_sendto call is non-blocking.● m_recvfrom – looks up the receiver-side message buffer to see if any message is already received. If yes, it returns the first message (in-order) and deletes that message from the table. If not, it returns with -1 and sets a global error variable to ENOMSG, indicating no message has been available in the message buffer. So the m_recvfrom call is non-blocking.The thread R behaves in the following manner. It waits for a message to come in a recvfrom() call from any of the UDP sockets (you need to use select() to keep on checking whether there is any incoming message on any of the UDP sockets, on timeout check whether a new MTP socket has been created and include it in the read/write set accordingly). When it receives a message, if it is a data message, it stores it in the receiver-side message buffer for the corresponding MTP socket (by searching SM with the IP/Port), and sends an ACK message to the sender. In addition it also sets a flag nospace if the available space at the receive buffer is zero. On a timeout over select(), it additionally checks whether the flag nospace was set but now there is space available in the receive buffer. In that case, it sends a duplicate ACK message with the last acknowledged sequence number but with the updated rwnd size, and resets the flag (there might be a problem here – try to find it out and resolve!). If the received message is an ACK message in response to a previously sent message, it updates the swnd and removes the message from the sender-side message buffer for the corresponding MTP socket. If the received message is a duplicate ACK message, it just updates the swnd size.The thread S behaves in the following manner. It sleeps for some time ( < T/2 ), and wakes up periodically. On waking up, it first checks whether the message timeout period (T) is over (by computing the time difference between the current time and the time when the messages within the window were sent last) for the messages sent over any of the active MTP sockets. If yes, it retransmits all the messages within the current swnd for that MTP socket. It then checks the current swnd for each of the MTP sockets and determines whether there is a pending message from the sender-side message buffer that can be sent. If so, it sends that message through the UDP sendto() call for the corresponding UDP socket and updates the send timestamp.Design the message formats and the shared memory properly. Note that different fields in the shared memory are accessed by different threads and processes and would require proper mutual exclusion.Testing your code:To test the program, write two programs user1.c and user2.c. user1.c creates an MTP socket M1, binds it to IP_1, Port_1 (local IP and Port), IP_2, Port_2 (remote IP and Port). User2.c creates an MTP socket M2 and binds it to IP_2, Port_2 (local IP and Port), IP_1, Port_1 (remote IP and Port). M1 then uses the m_sendto() call to transfer a large file (size > 100 KB), and M2 uses the m_recvfrom() to receive the contents of that file and write it in a new file. You can run user1.c and user2.c with different IP/Port pairs to create multiple MTP sockets.As the actual number of drops in your machine or in the lab environment will be near 0, you will need to simulate an unreliable link. To do this, in the library created, add a function called dropMessage() with the following prototype:int dropMessage(float p)where p is a probability between 0 and 1. This function first generates a random number between 0 and 1. If the generated number is < p, then the function returns 1, else it returns 0. Now, in the code for thread R, after a message is received (by the recvfrom() call on the UDP socket), first make a call to dropMessage(). If it returns 1, do not take any action on the message (irrespective of whether it is data or ack) and just return to wait to receive the next message. If it returns 0, continue with the normal processing in R. Thus, if dropMessage() returns 1, the message received is not processed and hence can be thought of as lost. Link the programs in user1.c and user2.c with this new library. Submit your code with the dropMessage() calls in R, do NOT remove these calls from your code before you submit.The value of T should be 5 seconds (do not hardcode it deep inside your code, specify it in a .h file (see below)). The value of the parameter p (the probability) should also be specified in the same .h file (see below). When you test your program, vary p from 0.05 to 0.05 in steps of 0.05 (0.05, 0.1, 0.15, 0.2….,0.45, 0.5). For each p value, for the same file, count the average number of transmissions made to send each message (total number of transmissions that are made to send the message / no. of messages generated from the file). Report this in a table in the beginning of the file documentation.txt (see below).Even though it is not needed for this assignment, you should try to also vary T (especially low values) and see its effect. This is an extremely important parameter that affects the transmission. If you do this, set p to 0, and use the nanosleep() call of Linux in S as the sleep() call has a resolution of seconds only, so you cannot set T to anything less than 1 second using a sleep() call.What to submit:The five required functions, plus the function dropMessage(), should be implemented as a static library called libmsocket.a so that a user can write a C program using these calls for reliable communication and link with it (the function dropMessage() will not be called by the user but you will call it when you test your program). Look up the ar command under Linux to see how to build a static library. Building a library means creating a .h file containing all definitions needed for the library (for ex., among other things, you will #define SOCK_MTP here), and a .c file containing the code for the functions in the library. This .c file should not contain any main() function, and will be compiled using ar command to create a .a library file. Thus, you will write the .h header file (name it msocket.h) and the .c file (name it msocket.c) from which the .a library file will be generated. Any application wishing to call these functions will include the .h file and link with the libmsocket.a library. For example, when we want to use functions in the math library like sqrt(), we include math.h in our C file, and then link with the math library libm.a.The value of the parameter T should be #defined as “#define T 5” in the msocket.h file. The value of the parameter p should also be #defined in this .h file.You should submit the following files in a single tar.gz file: ● msocket.h and msocket.c ● initmsocket.c that initializes the MTP socket by starting the R and S threads and the garbage collector process ● user1.c and user2.c ● a makefile to generate libmsocket.a ● a makefile to create the executable to run initmsocket.c ● a makefile to create the two executable files to run from user1.c and user2,c respectively ● a file called documentation.txt containing the following: ○ For msocket.h and msocket.c, as well as for initmsocket.c, give a list of all data structures used and a brief description of their fields and their purpose, and a list of all functions along with what they do in msocket.c and initmsocket.c. ○ The table with the results for varying p values as described earlier.
CS 39006: Networks Lab Assignment 2: Using TCP socketsA Caesar Cipher is a simple encryption scheme in which each letter of a plaintext file is replaced by k letters ahead of it in the alphabet, where k is the key of the Cipher. So if k = 2, then A is replaced by C, B by D, …, X by Z, Y by A, and Z by B.In this assignment, a client will send a plaintext file and a key k to an encryption server using a TCP socket. The server will encrypt the file using Caesar Cipher and send it back to the client using the same TCP socket.Your task will be to write the two programs – one program corresponding to the server process and another program corresponding to the client process. The sever will be a concurrent TCP server.The file is a text file of arbitrary size (> 0 bytes). You can assume (no need to check) that the file will contain only alphabets (lower or uppercase) and space,The transfer of the contents of the file works using a communication protocol as follows. 1. The client reads the filename to be sent from the user (keyboard). If the file does not exist, an error message is printed and the user is asked to enter a filename again. 2. If a correct filename is entered, the user is asked to enter the key k. 3. The client establishes a connection to the server using the connect() call. 4. The client first sends the key k to the server, and then sends the file content, in that order. Since the file can be arbitrarily long, the client cannot send the entire file in a single send() call, and sends the file in small chunks using multiple send() calls until the entire file is transferred. The chunk size used by the client is not known to the server. 5. The server reads the file contents sent by the client and copies it into a temporary file in the current directory. The file should be named as .txt where IP is the IP address of the client and port is the port of the client. For example, if the client has IP address 10.200.10.20 and port 15000, the file should be named 10.200.10.20.15000.txt. 6. After the complete file is received, the server encrypts the file read and stores the encrypted file in another file named X.enc, where X is the name of the original file. For example, for the above example, the encrypted file will be stored in a file named 10.200.10.20.15000.txt.enc. 7. The server sends the file to the client, once again in chunks. The chunk size used by the server is not known to the client. 8. After the complete file is sent, the server closes the connection. 9. The client stores the file in the same directory as the original file with the filename Y.enc, where Y is the original filename of the file. For example, if the filename was dummy.txt, the encrypted file is named dummy.txt.enc. 10. The client then prints a message stating the file is encrypted, giving the filenames of the original and the encrypted file. 11. The whole process then repeats, i.e., the client waits for the next filename to be encrypted from the user.You need to ensure the following in your code: 1. No buffer of size > 100 can be used in either client or server. 2. The client cannot make more than one pass over the file, nor can it find the size of the file in any other way. Same goes for the server. More strictly, you cannot use the size of the file anywhere in your code. 3. You cannot use fopen/fscanf/fprintf functions. You must use open/read/write functions to read/write from/to the file.Submission Instruction:
1. Log in to GitHub. 2. Fork this repo(sitory). See this video on how to carry out this step and step 3. 3. Clone your fork, using either the web site or the GitHub Desktop client. 4. Checkout your personalized branch, the one with your name and GitHub handle.â– Introduction â– Using basic Java graphics via the Processing programming language, create a tic tac toe game in which the user plays against the computer. A short movie of how this assignment should look and behave in its resolved state is available here.â– Rules â– 1. The size of the canvas must be 500×500. 2. The computer always plays an “Xâ€. 3. The user always renders a circle. 4. The computer always starts. 5. Any content that should be reported to the user must be done via Processing’s println() method. 6. Use only the numeric keys 0–8 to choose a square. The number 0 must map to the top left corner square, then assign the subsequent numbers across the top and down until you reach the bottom right corner square, which must map to the number 8. 7. When the user presses any key between 0–8 that corresponds to a blank square, your program should fill that square with a circle. This is akin to the user taking a turn at the game. 8. Immediately after the user plays, the computer takes its turn. 9. When the user presses any key other than 0–8, your program should report a message to the user that they pressed an incorrect key. 10. After the user takes their turn, and if neither the user nor the computer has won, report to the user that the game is still in play. 11. If all squares are played and neither the computer nor the user has won, report to the user that no one has won. 12. When either the user or the computer has won, report this to the user. 13. Subsequent key presses after a game has ended should report to the user that the game has ended.â– File List â– This repo contains the following files, all required to carry out this assignment. Before you start working, ensure you understand the role of each file below. 1. tic_tac_toe/tic_tac_toe.pde — This is the springboard from where your Processing app will run using setup() and draw(). 2. tic_tac_toe/constants.pde — All global constants must go in this file. 3. tic_tac_toe/shapes.pde — All methods you author to draw shapes, such as “Oâ€, “Xâ€, and the tic tac toe board must go in this file. 4. tic_tac_toe/board.pde — The logic of your program goes in this file. That is, all methods that will process the logic of your tic tac toe game. 5. .editorconfig — The settings in this version of .editorconfig by default indents code by 2 spaces. If you use VS Code instead of Processing to write code for this project, ensure EditorConfig is working before beginning this assignment. (The Processing IDE by default also indents by 2 spaces.) 6. .gitignore — Do not modify any .gitignore files.â– Grading â– | Item | Points | |—————————————————————————–|:——-:| | Program works according to instructions | 20 | | Code avoids being clever in favor of readability | 20 | | Code is neat and professional | 20 | | Variable naming is logical | 20 | | Comments are used thoughtfully in places where code is not self documenting | 20 |â– Submission â– You will need to issue a pull request back into the original repo, the one from which your fork was created for this project. See the Issuing Pull Requests section of this site for help on how to submit your assignment.
1. Log in to GitHub. 2. Fork this repo(sitory). See this video on how to carry out this step and step 3. 3. Clone your fork, using either the web site or the GitHub Desktop client. 4. Checkout your personalized branch, the one with your name and GitHub handle.â– Introduction â– Write a program that does the following: 1. Requests from the user a positive number representing the size of a matrix 2. Prints a matrix consisting of 0s 3. Populates the matrix with values from 1–(input × input) 4. Prints the matrix with the newly-populated values 5. Swaps the contents of the matrix on one side of the diagonal formed from top right to bottom left with the other side, highlighting the elements along the diagonal and leaving them untouched/unswapped 6. Prints the flipped matrix As mentioned in item 5, the elements in the diagonal must not be touched, while the other elements in the matrix must be swapped. The numbers in every cell of the matrix is arbitrary; each cell could have contained a letter, string, image, or other object/primitive. Numbers are being used in this assignment because they produce a sequence that is easy to follow when working with the matrix. A movie of how this assignment should behave in its resolved state is available here.â– File List â– This repo contains the following files, all required to carry out this assignment. Before you start working, ensure you understand the role of each file below. 1. Makefile — Run make in your command line interface, or CLI, from this repo’s root folder to build your assignment. As you test input, run make run to only run a previously-compiled Java program, not compile it. Run make clean to remove the class file. 2. .editorconfig — Everything in this file is a redundancy of what’s already in the .editorconfig file you installed in your home folder. It’s here to make sure you’re working with the formatting rules defined in .editorconfig, as this is part of your grade. Ensure EditorConfig is working in VS Code, before beginning this assignment. 3. .gitignore — Do not modify this file. 4. Matrix.java — Where the entire Matrix class and its methods will be defined. 5. RunMatrix.java — Method main goes in this file, along withe the instantiation of the Matrix object and all the required behavior.â– Rules â– 1. Your Matrix class should contain a single field called matrix of the primitive type required to create a matrix. 2. The Matrix constructor should accept an integer value as an argument named size, then create a matrix field whose width and height are equivalent to the integer passed to it. The constructor should also report to the user the dimensions of the matrix. 3. As part of the Matrix class, write a private method called swap that will accept four arguments: x1, y1, x2, and y2, each representing the indices of the two elements in the matrix that should be swapped. 4. Write a public method called printMatrix that accepts no arguments and returns nothing. It should print the matrix, highlighting in yellow the diagonal from top right to bottom left. 5. Write a public method called populateMatrix that accepts no arguments and returns nothing. It should simply populate the matrix with values from 1–(size × size). 6. Write a public method called flipMatrix that accepts no arguments and returns nothing. It should “flip†the matrix’s values along the diagonal from top right to bottom left. The elements that comprise the diagonal should be highlight and not swapped.â– Grading â– | Item | Points | |—————————————————————————–|:——-:| | Program works according to instructions | 20 | | Code avoids being clever in favor of readability | 20 | | Code is neat and professional | 20 | | Variable naming is logical | 20 | | Comments are used thoughtfully in places where code is not self documenting | 20 |â– Submission â– You will need to issue a pull request back into the original repo, the one from which your fork was created for this project. See the Issuing Pull Requests section of this site for help on how to submit your assignment.
1. Log in to GitHub. 2. Fork this repo(sitory). See this video on how to carry out this step and step 3. 3. Clone your fork, using either the web site or the GitHub Desktop client. 4. Checkout your personalized branch, the one with your name and GitHub handle.â– Introduction â– For this assignment, you’ll be writing a program that accepts input from the user in the form of an integer, then creates a diamond whose width (in columns) and height (in rows) are dictated by user input. For example, if the user enters 5, then the height of the diamond is 5 rows and the width of the diamond is 5 columns. See http://vanegas.cs.hartford.edu/uploads/videos/diamond-program-in-java.mp4. Important Note: Odd and even versions of the diamond are rendered differently. Review the aforementioned video carefully for an example.â– File List â– This repo contains the following files, all required to carry out this assignment. Before you start working, ensure you understand the role of each file below. 1. Makefile — Run make in your command line interface, or CLI, from this repo’s root folder to build your assignment. As you test input, run make run to only run a previously-compiled Java program, not compile it. Run make clean to remove the class file. 2. .editorconfig — Everything in this file is a redundancy of what’s already in the .editorconfig file you installed in your home folder. It’s here to make sure you’re working with the formatting rules defined in .editorconfig, as this is part of the neatness portion of your grade. Ensure EditorConfig is working in VS Code, before beginning this assignment. 3. .gitignore — Do not modify this file. 4. Diamond.java — Where you will do your assignment. Do not write your code in anything other than this file.â– Example Output â– Here are two examples of output, one for an even situation, the other for an odd. “`bash Enter a number 8 * * * * * * ** * * * * * * Enter a number 5 ** “`â– Grading â– | Item | Points | |—————————————————————————–|:——-:| | Program works according to instructions | 20 | | Code avoids being clever in favor of readability | 20 | | Code is neat and professional | 20 | | Variable naming is logical | 20 | | Comments are used thoughtfully in places where code is not self documenting | 20 |â– Submission â– You will need to issue a pull request back into the original repo, the one from which your fork was created for this project. See the Issuing Pull Requests section of this site for help on how to submit your assignment.
1. Log in to GitHub. 2. Fork this repo(sitory). See this video on how to carry out this step and step 3. 3. Clone your fork, using either the web site or the GitHub Desktop client. 4. Checkout your personalized branch, the one with your name and GitHub handle.â–・Introduction・■For this assignment, you’ll be writing a single program that creates a string based on temperature and random factors. 1. Your program should print your first and last initials in large block letters. Use each letter’s corresponding characters to form the letters. (See the Example Output section below.) 2. Next, ask the user to enter a number in Fahrenheit. Your program should convert the value to Celsius and save it. For example, entering 32 should yield 0. 3. Now take input from the user in the form of a 5-character string, then save the string in reverse without the first and last characters. In other words, trim the outside characters. For example, if the user enters abewz, your program would save web. 4. In this penultimate step, generate and save a random number between 32 – 16,384, inclusive. 5. And, finally, combine the results of steps 2 – 4 and print it to the screen. See the Example Output section below.â–・File List・■This repo contains the following files, all required to carry out this assignment. Before you start working, ensure you understand the role of each file below. 1. Makefile — Run make in your command line interface, or CLI, from this repo’s root folder to build your assignment. As you test input, run make run to only run a previously-compiled Java program, not compile it. 2. .editorconfig — Everything in this file is a redundancy of what’s already in the .editorconfig file you installed in your home folder during the first week of class. It’s here to make sure you’re working with the formatting rules defined in .editorconfig, as this is part of your grade. Do not move this file. Also, ensure EditorConfig is working in VS Code, before beginning this assignment. 3. .gitignore — Do not modify this file. 4. Assignment1.java — Where you will do your assignment. Do not write your code in anything other than this file.â–・Example Output・■If I was creating this program, it’s output would look like this: “`bash RRRRRRRR VVV VVV RRR RRR VVV VVV RRR RRR VVV VVV RR RRR VVV VVV RRR RRR VVV VVV RRR RRR VVVVV RRR RRR VVV Please enter a number in Fahrenheit: 32 Please enter a 5-character string: abewz Random number generated. Continuing… Your new string is 0.0web15769 “`â–・Grading・■| Item | Points | |————————————————|:——:| | Program works according to instructions | 34 | | Code compliant with EditorConfig rules | 33 | | Code is neat and variable names are logical | 33 |â–・Due・â–â–・Submission・■You will need to issue a pull request back into the original repo, the one from which your fork was created for this project. See the Issuing Pull Requests section of this site for help on how to submit your assignment.