Survey

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Survey

Document related concepts

no text concepts found

Transcript

1.Priority queue 2.An array because the number of weekdays is fixed and access by index has to be efficient 3.Array How is queue data structure implemented using circular linked list? What is a threaded binary tree? When is it used? A threaded binary tree is a binary tree variant that facilitates traversal in a particular order (often the same order already defined for the tree). Threaded Binary Tree is also a binary tree in which all left child pointers that are NULL (in Linked list representation) points to its in-order predecessor, and all right child pointers that are NULL (in Linked list representation) points to its in-order successor. Use: The idea of threaded binary trees is to make inorder traversal of the binary tree faster and do it without using any extra space, so sometimes in small systems where hardware is very limited we use threaded binary tree for better efficiency of the software in a limited hardware space. Refer this link for solution https://www.tutorialspoint.com/data_structures_algorithms/heap_data_structure.htm#:~:text=Min%2DH eap%20%E2%88%92%20Where%20the%20value,input%20and%20order%20of%20arrival. Write an algorithm to find the transpose of sparse matrix Algorithm for Transpose of Sparse Matrix SparseMatrix SparseMatrix::Transpose() { Construct a SparseMatrix, b(cols, rows, terms); If (terms > 0) { Let rowSize be an integer array of size cols. Let rowStart be an integer array of size cols. Initialize each element in rowSize to 0. For (i=0; i rowSize [smArray[i].col]++; rowStart [0] = 0; For (i = 1; i < cols; i++) rowStart [i] = rowSize [i-1] + rowStart [i-1]; For (i=0; i < terms; i++) { j = rowStart [ smArray [i].col ]; Copy smArray[ i ] to smArray[ j ]; Interchange row and col of smArray[ j ]; rowStart [ smArray [i].col ]++; } } Return b; } Write an algorithm to evaluate a postfix expression. Trace the algorithm for the following postfix expression. 6 2 3 + - 3 8 2 / + * 2 $ 3 + Algorithm: 1) Create a stack to store operands (or values). 2) Scan the given expression and do the following for every scanned element. …..a) If the element is a number, push it into the stack …..b) If the element is an operator, pop operands for the operator from the stack. Evaluate the operator and push the result back to the stack 3) When the expression is ended, the number in the stack is the final What is doubly linked list? How is it better than singly linked list? Also, write an algorithm to reverse the doubly linked list. Doubly linked list is a complex type of linked list in which a node contains a pointer to the previous as well as the next node in the sequence. Therefore, in a doubly linked list, a node consists of three parts: node data, pointer to the next node in sequence (next pointer) , pointer to the previous node (previous pointer). Singly linked list is preferred when we have memory limitation(we can’t use much memory) and searching is not required.Doubly linked list is preferred when we don’t have memory limitation and searching is required(we need to perform search operation on the linked list).Accessing elements in a Doubly Linked List is more efficient when compared to a Singly Linked List as both forward and backward traversal is possible.In a Doubly Linked List, the traversal can be done using the next node link as well as the previous node link. Algorithm - Reversing a doubly linked list by swapping pointers: ● As we already know that a doubly linked list uses two different pointers next and prev to track its next and previous sibling nodes. ● So to reverse the doubly linked list we will just exchange the pointers, like the next node will become the previous node and previous node will become the next. Write an algorithm to implement queue operations using two stacks. Trace your algorithm with an example enQueue(q, x) 1) While stack1 is not empty, push everything from stack1 to stack2. 2) Push x to stack1 (assuming size of stacks is unlimited). 3) Push everything back to stack1. deQueue(q) 1) If both stacks are empty then error. 2) If stack2 is empty While stack1 is not empty, push everything from stack1 to stack2. 3) Pop the element from stack2 and return it. A deque (double ended queue) is an ordered set of items from which items may be deleted at either end and into which items may be inserted at either end. Call the two ends of a deque as left and right. How can a deque be represented as an array? Also write the routines to insert elements at the right and left ends of a deque What is the result of a preorder traversal, an in-order traversal, and a postorder traversal on the following expression tree? In-order (left,root,right) - 1+2+7*1+54*18Post-order(left,right,root)-12+7+15+4*18-* Given an array of n elements which contains elements from 0 to n-1, with any of these numbers appearing any number of times. Write an algorithm to find these repeating numbers in O(n) and using only constant memory space. Derive the formula for address calculation of n-dimensional arrays. Assume that the row major implementation of array is used. Write an algorithm to convert infix expression to postfix expression (without parenthesis). Hence trace the algorithm for the following infix expressions A+B*C–D/F–G%H A*B*C/D/E–F–G–H algorithm: Iterate the given expression from left to right, one character at a time Step 1: If the scanned character is an operand, put it into postfix expression. Step 2: If the scanned character is an operator and operator's stack is empty, push operator into operators' stack. Step 3: If the operator's stack is not empty, there may be following possibilities. If the precedence of scanned operator is greater than the top most operator of operator's stack, push this operator into operator 's stack. If the precedence of scanned operator is less than the top most operator of the operator's stack, pop the operators from the operator's stack until we find a low precedence operator than the scanned character. If the precedence of the scanned operator is equal then check the associativity of the operator. If associativity left to right then pop the operators from stack until we find a low precedence operator. If associativity right to left then simply put into stack. If the scanned character is opening round bracket ( '(' ), push it into operator's stack. If the scanned character is closing round bracket ( ')' ), pop out operators from operator's stack until we find an opening bracket ('(' ). Repeat Step 1,2 and 3 till expression has character Step 4: Now pop out all the remaining operators from the operator's stack and push into postfix expression. Step 5: Exit Write a C/C++ program to implement stack operations using singly linked list. Write an algorithm reverse to reverse the order of items in a queue data structure. Recursive Algorithm : 1. The pop element from the queue if the queue has elements otherwise return empty queue. 2. Call reverseQueue function for the remaining queue. 3. Push the popped element in the resultant reversed queue. What is circular queue? How is it better than linear queue? Circular Queue is a linear data structure in which the operations are performed based on FIFO (First In First Out) principle and the last position is connected back to the first position to make a circle. It is also called ‘Ring Buffer’. In a normal Queue, we can insert elements until queue becomes full. But once queue becomes full, we can not insert the next element even if there is a space in front of queue. Operations on circular queue: ● Front: Get the front item from queue. ● Rear: Get the last item from queue. ● enQueue(value) This function is used to insert an element into the circular queue. In a circular queue, the new element is always inserted at Rear position. 1. Check whether queue is Full – Check ((rear == SIZE-1 && front == 0) || (rear == front-1)). 2. If it is full then display Queue is full. If queue is not full then, check if (rear == SIZE – 1 && front != 0) if it is true then set rear=0 and insert element. ● deQueue() This function is used to delete an element from the circular queue. In a circular queue, the element is always deleted from front position. Circular queue is a bounded queue which implements arrays. It is better than a normal queue because in this we can effectively utilize the memory space. If we have a normal queue and have deleted some elements from there then empty space is created there and even if the queue has empty cells then also we cannot insert any new element because the insertion has to be done from one side only(i.e rear ) and deletion has to be done from another side(i.e front). But in case of circular queue the front and rear are adjacent to each other. Circular queue is better than a normal queue because in the former we can effectively utilise the memory space.If we have a normal queue and have deleted some elements from there then empty space is created there and even if the queue has empty cells then also we cannot insert any new element because the insertion has to be done from one side only(i.e rear or tail) and deletion has to be done from another side(i.e front or head).But in case of circular queue the front and rear are adjacent to each other. — Outline the Huffman algorithm. Trace the algorithm to find the optimal encoding for the message ABACCDA. Huffman coding algo: create a priority queue Q consisting of each unique character. sort then in ascending order of their frequencies. for all the unique characters: create a newNode extract minimum value from Q and assign it to leftChild of newNode extract minimum value from Q and assign it to rightChild of newNode calculate the sum of these two minimum values and assign it to the value of newNode insert this newNode into the tree return rootNoderefert Refer this link for tracing the algorithm : https://www.programiz.com/dsa/huffman-coding What is threaded binary tree? Give its applications. Complete using this link https://iq.opengenus.org/threaded-binary-tree/#:~:text=The%20idea%20of%20threaded %20binary,in%20a%20limited%20hardware%20space. Explain the different methods of obtaining hash functions. https://www.geeksforgeeks.org/hash-functions-and-list-types-of-hash-functions/ Consider an open addressing hash table with ten slots. For the hash function h(x) = x mod 10, insert the keys{33, 54, 69, 74, 18, 19} (in the order given) into the table for each of the three scenarios below: When collisions are handled by separate chaining When collisions are handled by linear probing When collisions are handled by double hashing using a second hash function h’(k)= 1 + (k mod 9) Refer this link for the model sum not exact sum https://www.geeksforgeeks.org/practice-problems-on-hashing/ Write algorithms for insert, delete in both front and rear end of a double ended queue data structure. https://scanftree.com/Data_Structure/duble-ended-queue-dequeue What is priority queue? How is it implemented using an array? https://prepinsta.com/c-program/priority-queue-using-arrays/#:~:text=Priority%20Queue %20implementation%20using%20array,element%20has%20its%20own%20priority. 17. For a binary tree a) Write recursive algorithms to do inorder, preorder and post order traversals b) Write a non recursive algorithm to do inorder traversal using right thread c) Write a non recursive algorithm to find the minimum node a) Refer this link https://www.geeksforgeeks.org/tree-traversals-inorder-preorder-and-postorder/ c) Algorithm to find minimum node of a binary tree Let "root" be the root node of given binary tree. ● If root is a leaf node, then return root node's value. ● Recursively find the minimum value node in left and right sub tree. Let it be "leftMin" and "rightMin". ● Return maximum of leftMin, rightMin and root. Outline insertion sort algorithm for sorting ‘n’ elements. Also derive the best case and worst case time complexity of insertion sort algorithm. What is the running time of insertion sort when the array elements are already sorted? In a sequential search of linear list, what is the worst case and best case time complexity? Worst Case Time Complexity: O(N) When the search value is not present in the array Best Case Time Complexity: O(1) When the search value is the first element of the array. Average Case This is a little different. Let N be the size of the array. Let X be the value we want to search for. Now, Number of Comparisons when X is at 0th index = 1 Number of Comparisons when X is at 1st index = 2 Number of Comparisons when X is at 2nd index = 3 …… Number of Comparisons when X is at (N-1) index = N Average Comparisions = (Sum of Comparisons at all indices) / N Average Comparisions = (1+2+3+….+N) = (N(N+1)/2)/N = (N+1)/2 Time Complexity = O((N+1)/2) But since we ignore constant Time Complexity = O(N) What data structure implements LIFO behaviour? Justify your answer with an example The data structure implementing LIFO is stack. LIFO is short for “Last In First Out”. The last element pushed onto the stack will be the first element that gets popped off. This is analogous to a stack of plates where the last plate put on top of the stack will be the first plate that gets removed. If you were to pop all of the elements from the stack one at a time then they would appear in reverse order to the order that they were pushed on.In contrast, queues are known as FIFO (First In First Out). The first element that gets put into the queue will be the first element that gets retrieved. What is a sparse matrix? Obtain its array representation. Sparse matrix is a matrix which contains very few non-zero elements. Write an algorithm to reverse a singly linked list. What is sparse matrix? Describe how an array can be effectively used to store a sparse matrix. The non-zero elements in the sparse matrix can be stored using triplets that are rows, columns, and values. There are two ways to represent the sparse matrix that are listed as follows - Array representation. Linked list representation. Representing a sparse matrix by a 2D array leads to wastage of lots of memory as zeroes in the matrix are of no use in most of the cases. So, instead of storing zeroes with non-zero elements, we only store non-zero elements. This means storing non-zero elements with triples- (Row, Column, value). Distinguish:- Doubly linked list and circular linked list. The main difference between the doubly linked list and doubly circular linked list is that the doubly circular linked list does not contain the NULL value in the previous field of the node.A circular linked list is one where there are no start or end nodes, but instead they follow a circular pattern. A doubly-linked list is one where each node points not only to the next node but also to the previous node. What is a tournament tree? Mention a few of its applications. The Tournament tree is a complete binary tree with n external nodes and n – 1 internal nodes. The external nodes represent the players, and the internal nodes represent the winner of the match between the two players. This tree is also known as the Selection tree. Applications: 1. 2. 3. 4. It is used for finding the smallest and largest element in the array. It is used for sorting purposes. Tournament trees may also be used in M-way merges. Tournament replacement algorithm selection sort is used to gather the initial run for external sorting algorithms. Given an array of size n, write an algorithm to check if it is sorted in ascending order or not. Use iterative approach. Refer this link: https://www.tutorialspoint.com/program-to-check-if-an-array-is-sorted-or-not-iterative-an d-recursive-in-c Mention the advantages of doubly linked lists over singly linked lists. In a singly linked list, to delete a node, a pointer to the previous node is needed. To get this previous node, sometimes the list is traversed. In the DLL, we can get the previous node using the previous pointer. (or) Write algorithms to implement insert and remove operations of linear queue using singly linked list Main Queue Operations: 1)EnQueue(): Inserts an element at the rear of the Queue. 2)DeQueue(): Remove and return the front element of the Queue. EnQueue Inserting an element in Queue. ● Initially both of our pointers pointing to the front and rear are NULL. ● Now,If We enter the 12 in Queue we will checking weather there is any element present or not.If rear is NULL,We make our front and rear pointers to point our newly inserted Node with having data '12'. ● Now,again if we want to enter new data,Let say 45.We will check for rear NULL or not.Now our rear would be pointing to '12'.So 45 will added next to the rear Node and 45 would be our rear Node. ● Same way if i add 26, it would be added after 45 and rear will point to the Node having data '26'. DeQueue Deleting an element from Queue. ● Now, by doing three EnQueue operation we will be having Queue having 3 elements as shown in figure. ● First we check that front is NULL?If it is then Queue is empty we can not perform DeQueue operation.But here front will be pointing to '12'. ● So if we want to delete element from Queue. As our Queue is FIFO(first in first out) 12 is first inserted, so 12 will be removed.Our front pointer will be pointing to the '12'.To,remove it first we make our front pointer to point the element inserted after '12'.And then simply we free the memory. ● As simple as that 45 and 26 will be removed if we perform two more DeQueue operation.And again our Queue will be empty(rear and front will be equal to NULL). What is the priority queue? Mention different types of priority queues with its advantages. A priority queue is a special type of queue. Each queue’s item has an additional piece of information, namely priority. Unlike a regular queue, the values in the priority queue are removed based on priority instead of the first-in-first-out (FIFO) rule. The following example illustrates a priority queue with an ordering imposed on the values from least to the greatest: Here, , etc. denotes the value of items while item with the highest priority in this example is the lowest priority item, , etc. denotes the priority of items. So the (with the priority of 1) that is removed first. And (with the priority of 19), will be removed at the end of the process. In this tutorial, from now on, we’ll use priority as the value of items since other information can be easily attached to the queue’s elements. The main operations on a priority queue include: ● add: adds an item to the queue ● peek: returns the item in the queue with the highest priority without deleting the node ● remove: removes and returns the item in the queue with the highest priority Types of priority queue: ● Min-priority queue: in a min-priority queue, a lower priority number is given as a higher priority ● Max-priority queue: in a max-priority queue, a higher priority number is given as a higher priority Advantages of priority queue Outline quick sort algorithm. What is the best case and worst case running time of quick sort algorithm? What needs to be true about partition function in order for the running time to be best case? Quicksort is a sorting algorithm based on the divide and conquer approach where 1. An array is divided into subarrays by selecting a pivot element (element selected from the array). While dividing the array, the pivot element should be positioned in such a way that elements less than pivot are kept on the left side and elements greater than pivot are on the right side of the pivot. 2. The left and right subarrays are also divided using the same approach. This process continues until each subarray contains a single element. 3. At this point, elements are already sorted. Finally, elements are combined to form a sorted array. Best case occurs generally when the list is in completely random manner. Example : 1 4 2 9 5 7 3 4 10 2 complexity : O(n logn) Worst case occurs when the array is already sorted either in ascending or descending order. complexity : O(n^2) Example : 1 2 3 4 5 6 7 8 9 10 Partition function: One call of Partition takes O(1) time plus time proportional to the number of iterations of FOR-loop. If X is the number of comparisons A[j] ≤ x performed in Partition over the entire execution of RandQuicksort then the running time is O(n + X). Illustrate insertion sort algorithm with an example. Step 1 − If it is the first element, it is already sorted. return 1; Step 2 − Pick next element Step 3 − Compare with all elements in the sorted sub-list Step 4 − Shift all the elements in the sorted sub-list that is greater than the value to be sorted Step 5 − Insert the value Step 6 − Repeat until list is sorted Example: Eg: 12, 11, 13, 5, 6 Let us loop for i = 1 (second element of the array) to 4 (last element of the array) i = 1. Since 11 is smaller than 12, move 12 and insert 11 before 12 11, 12, 13, 5, 6 i = 2. 13 will remain at its position as all elements in A[0..I-1] are smaller than 13 11, 12, 13, 5, 6 i = 3. 5 will move to the beginning and all other elements from 11 to 13 will move one position ahead of their current position. 5, 11, 12, 13, 6 i = 4. 6 will move to position after 5, and elements from 11 to 13 will move one position ahead of their current position. 5, 6, 11, 12, 13 Define Hash table data structure. Hash Table is a data structure which stores data in an associative manner. In a hash table, data is stored in an array format, where each data value has its own unique index value. Access of data becomes very fast if we know the index of the desired data. Thus, it becomes a data structure in which insertion and search operations are very fast irrespective of the size of the data. Hash Table uses an array as a storage medium and uses hash technique to generate an index where an element is to be inserted or is to be located from. The two heuristic methods are hashing by division and hashing by multiplication which are as follows: 1. The mod method: ○ In this method for creating hash functions, we map a key into one of the slots of table by taking the remainder of key divided by table_size. That is, the hash function is h(key) = key mod table_size i.e. key % table_size ● Since it requires only a single division operation, hashing by division is quite fast. ● When using the division method, we usually avoid certain values of table_size like table_size should not be a power of a number suppose r, since if table_size = r^p, then h(key) is just the p lowest-order bits of key. Unless we know that all low-order p-bit patterns are equally likely, we are better off designing the hash function to depend on all the bits of the key. ● It has been found that the best results with the division method are achieved when the table size is prime. However, even if table_size is prime, an additional restriction is called for. If r is the number of possible character codes on an computer, and if table_size is a prime such that r % table_size equal 1, then hash function h(key) = key % table_size is simply the sum of the binary representation of the characters in the key mod table_size. ● Suppose r = 256 and table_size = 17, in which r % table_size i.e. 256 % 17 = 1. ● So for key = 37599, its hash is 37599 % 17 = 12 ● But for key = 573, its hash function is also 573 % 17 = 12 ● Hence it can be seen that by this hash function, many keys can have the same hash. This is called Collision. ● A prime not too close to an exact power of 2 is often good choice for table_size. 1. The multiplication method: ○ In multiplication method, we multiply the key k by a constant real number c in the range 0 < c < 1 and extract the fractional part of k * c. ○ Then we multiply this value by table_size m and take the floor of the result. It can be represented as h(k) = floor (m * (k * c mod 1)) or h(k) = floor (m * frac (k * c)) ● where the function floor(x), available in standard library math.h, yields the integer part of the real number x, and frac(x) yields the fractional part. [frac(x) = x – floor(x)] ● An advantage of the multiplication method is that the value of m is not critical, we typically choose it to be a power of 2 (m = 2p for some integer p), since we can then easily implement the function on most computers ● Suppose that the word size of the machine is w bits and that key fits into a single word. ● We restrict c to be a fraction of the form s / (2w), where s is an integer in the range 0 < s < 2w. ● Referring to figure, we first multiply key by the w-bit integer s = c * 2w. The result is a 2w-bit value r1 * 2w + r0 where r1 = high-order word of the product r0 = lower order word of the product ● Although this method works with any value of the constant c, it works better with some values than the others. c ~ (sqrt (5) – 1) / 2 = 0.618033988 . . . ● is likely to work reasonably well. ● Suppose k = 123456, p = 14, ● m = 2^14 = 16384, and w = 32. ● Adapting Knuth’s suggestion, c to be fraction of the form s / 2^32. ● Then key * s = 327706022297664 = (76300 * 2^32) + 17612864, ● So r1 = 76300 and r0 = 176122864. ● The 14 most significant bits of r0 yield the value h(key) = 67.