Question

In: Computer Science

For each algorithm, always explain how and why they work. If not by a proof, at...

For each algorithm, always explain how and why they work. If not by a proof, at least by a clear explanation. ALWAYS, analyze the running time complexity of your algorithms. Do not write a program. Write pseudo codes or explain in words

Q2: Give a data structure that implements a Min-Heap and the command Max(S) that returns the maximum in the heap. Try and make the last operation as fast as possible.

Q3: (a)Show how to implement a Queue by a Priority Queue. What is the run time per operation?

(b) Show how to implement a Stack using Priority Queue. What is the run time per operation?

Solutions

Expert Solution

Solution 1: There are many ways of implementing a heap tree in the memory and one such way is to use the linked lists to perform the operations over the binary heap but the complexity of one such method is very high as you would have to maintain a lot of pointers in the memory to not just traverse the elements of the heap but to implement the algorithms as well. On the other hand, arrays can also be used for the implementation of the same, and the complexity in that is much less as compared to that using the linked lists this is because of the fact that the space complexity of this solution is much less as compared to the space complexity of the solution using the linked lists.

So if you want to get the minimum element from the min-heap then its always going to cost you O(1) as the minimum element is always present at the top of the tree or the first element of the array. The extraction of the minimum element would take O(logN) of time as once the key is removed, the heap needs to be heapified or balanced to its original state as the swappings have to be carried out and other movements have to be performed too and the same is the complexity for the deletion of the elements from the tree. The insertion of the elements into the tree may also take O(logN) of time as the insertion of a new node may violate the principles of the heap tree and hence it has to be heapified after the insertion.

See, if you are trying to find the maximum element from a max heap then you can find it in O(1) of time as it is present on the top of the tree but if you are trying to find the maximum in a min tree then you may have to traverse through all the levels of the tree in order to get the maximum elements present in the tree and in that worst case the time complexity of the algorithm is actually O(logN), where N is the number of nodes present within the tree.

Solution 2: A priority queue is a data structure that can be used for storage as well as the retrieval of the data elements on the basis of their priorities. An element with a higher priority would be dequeued first than an element with a lower priority. As far as the implementation of a queue is concerned using the priority queue, you must understand that when you are implementing a priority queue then you are already implementing a queue and the arrays can be used for implementing them. In order to get the element with the highest priority, the time taken by the algorithm in the worst-case would be O(n) where n is the number of elements present within the queue. The same amount of time would be required to delete the element with the highest priority in O(n) of time in the worst-case scenario.

There is one way that you can implement the queue using the priority queue and that is by sorting the elements present in the priority queue by their priority but this would take additional O(NlogN) of time for any sorting algorithm and then you can implement the simple enqueue and dequeue operations over the elements in a linear time.

Solution 3: A stack can be implemented using the priority queue by keeping track of the count that determines when the element was actually pushed into the stack. On the basis of this count, the priority of the elements within the priority queue can be decided. So basically you will have to use the key-value pairs for this implementation as the first element always serving the value and the first element serving as the key. In order to traverse and model this queue you can use the heap data structure and hence the traversal of the elements and all other operations would only take O(logN) amount of time even in the worst-case scenario for the push and pop operations given the fact that the elements are pushed and popped in a first-in-first-out manner, and hence it would be the most efficient solution to this problem.

Here's the solution to your question, please provide it a 100% rating. Thanks for asking and happy learning!!


Related Solutions

Q1. In all algorithm, always explain how and why they work. If not by a proof,...
Q1. In all algorithm, always explain how and why they work. If not by a proof, at least by a clear explanation. ALWAYS, analyze the running time complexity of your algorithms. Do not write a program. Write pseudo codes or explain in words. (A) Find an efficient data structure supporting the following operations. Insert(S, x), Delete−Max(S), and Delete−100'th(S) which deletes from H the 100 largest element in the structure. Assume that the number of elements is more than 100. Also...
DATA STRUCTURES: For each algorithm, always explain how and why they work. If not by a...
DATA STRUCTURES: For each algorithm, always explain how and why they work. If not by a proof, at least by a clear explanation. ALWAYS, analyze the running time complexity of your algorithms. Do not write a program. Write pseudo codes or explain in words Q1: We want to maintain both a Queue and a Priority Queue. When you do Enqueue you also add the item to the Priority Queue and when you do Dequeue you also remove the item from...
In all algorithm, always explain how and why they work. ALWAYS, analyze the complexity of your...
In all algorithm, always explain how and why they work. ALWAYS, analyze the complexity of your algorithms. In all algorithms, always try to get the fastest possible. A correct algorithm with slow running time may not get full credit. In all data structures, try to minimize as much as possible the running time of any operation. 1.Show that if a binary tree has a vertex with a single child, then this can not be an optimal Huffman tree. 2. Question...
A) What is the Production Possibilities Curve and how does it work? Explain why each of...
A) What is the Production Possibilities Curve and how does it work? Explain why each of the following would cause the Production Possibilities Curve for a country to move outward or inward: B) The average work week increases. C) There is an increase in the retirement age. D) Immigration decreases. E) There are decreases in smoking, alcohol, and drug abuse.
I need to provide proof that these statements are FALSE. But I don't always understand why...
I need to provide proof that these statements are FALSE. But I don't always understand why they are false. 1. "drugs that block L-type Ca channels, reduce the contraction-efficiency of cardiac muscle, but not of skeletal muscles." (dont the skeletal muscles just need the tiniest bit of Ca to use the DHP channels?) 2. "the length of a skeletal muscle twitch is about 10-100 ms" (is this maybe for all muscle types? and not just for skeletal muscles?) thx in...
Explain why prices are always flexible.
Explain why prices are always flexible.
A systems analyst tests a new algorithm designed to work faster than the currently-used algorithm. Each...
A systems analyst tests a new algorithm designed to work faster than the currently-used algorithm. Each algorithm is applied to a group of 52 sample problems. The new algorithm completes the sample problems with a mean time of 16.34 hours. The current algorithm completes the sample problems with a mean time of 16.93 hours. The standard deviation is found to be 3.913 hours for the new algorithm, and 3.6243.624 hours for the current algorithm. Conduct a hypothesis test at the...
A systems analyst tests a new algorithm designed to work faster than the currently-used algorithm. Each...
A systems analyst tests a new algorithm designed to work faster than the currently-used algorithm. Each algorithm is applied to a group of 41sample problems. The new algorithm completes the sample problems with a mean time of 23.42 hours. The current algorithm completes the sample problems with a mean time of 26.39 hours. Assume the population standard deviation for the new algorithm is 3.442 hours, while the current algorithm has a population standard deviation of 5.364 hours. Conduct a hypothesis...
A systems analyst tests a new algorithm designed to work faster than the currently-used algorithm. Each...
A systems analyst tests a new algorithm designed to work faster than the currently-used algorithm. Each algorithm is applied to a group of 5454 sample problems. The new algorithm completes the sample problems with a mean time of 11.9011.90 hours. The current algorithm completes the sample problems with a mean time of 14.0714.07 hours. Assume the population standard deviation for the new algorithm is 5.1155.115 hours, while the current algorithm has a population standard deviation of 5.7365.736 hours. Conduct a...
A systems analyst tests a new algorithm designed to work faster than the currently-used algorithm. Each...
A systems analyst tests a new algorithm designed to work faster than the currently-used algorithm. Each algorithm is applied to a group of 51 51 sample problems. The new algorithm completes the sample problems with a mean time of 24.43 24.43 hours. The current algorithm completes the sample problems with a mean time of 24.68 24.68 hours. The standard deviation is found to be 3.481 3.481 hours for the new algorithm, and 3.420 3.420 hours for the current algorithm. Conduct...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT