Question

In: Computer Science

Remarks: In all algorithm, always explain how and why they work. If not by a proof,...

Remarks: 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. In all algorithms, always try to get the fastest possible. A correct algorithm with slow running time may not get full credit. Do not write a program. Write pseudo codes or explain in words

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

Solutions

Expert Solution

Here's pseudo code for min heap implementation :

  • We have an array and we have to make it min - heap
  • in Build-min-heap function we call min-heapify function for all nodes except leaf nodes, because leaf nodes are alredy in min-heap format as they don't have any children.
  • children of i-th node are at 2*i + 1 and 2*i+2 index.
  • In min-heapify function we compare given node to its children.
  • we swap given node with the child node if the child node have value less then it.
  • Time complexity for building min_heap is O(N).
min_heapify(arr,int i,int n)
{
   
    int lowest = i;
    int l = 2*i+1;
    int r = 2*i+2;
    if(l < n && arr[l] < arr[lowest])
        lowest = l;
    if(r < n && arr[r] < arr[lowest])
        lowest = r;
    if(lowest != i)
    {
        swap(arr[lowest],arr[i]);
        min_heapify(arr,lowest,n);
    } 
}

Build_min_heap(arr, int n)
{
    for(int i = n/2-1;i >= 0; i--)
    {
        min_heapify(arr,i,n);
    }
}

Now, come to getting max-element from min-heap.

  • In min-heap parent node is lesser than child codes.
  • So, we can conclude that non-leaf node cannot be  the maximum element as its child node has a higher value.
  • So, now we need to check for leaf-nodes only that which node has highest value.
  • There is ceil(n/2) leaf nodes in min heap.
  • So time complexity will be O(N).

Pseudo-code :

int findMaximumElement(int heap[], int n) 
{ 
    int maximumElement = heap[n / 2]; 
  
    for (int i = 1 + n / 2; i < n; ++i) 
        maximumElement = max(maximumElement, heap[i]); 
  
    return maximumElement; 
} 

Like, if this helped :)


Related Solutions

Remarks: In all algorithm, always explain how and why they work. If not by proof, at...
Remarks: In all algorithm, always explain how and why they work. If not by proof, at least by a clear explanation. ALWAYS, analyze the running time complexity of your algorithms. In all algorithms, always try to get the fastest possible. A correct algorithm with a slow running time may not get full credit. Do not write a program. Write pseudo-codes or explain in words Question 1: Say that we want to maintain both a Queue and a Priority Queue. This...
Remarks: In all algorithm, always explain how and why they work. If not by a proof,...
Remarks: 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. In all algorithms, always try to get the fastest possible. A correct algorithm with slow running time may not get full credit. Do not write a program. Write pseudo codes or explain in words Question 1: Say that we want to maintain both a Queue and a Priority Queue....
Remarks: In all algorithm, always explain how and why they work. If not by a proof,...
Remarks: 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. In all algorithms, always try to get the fastest possible. A correct algorithm with slow running time may not get full credit. Do not write a program. Write pseudo codes or explain in words Question 2: 1. Show how to implement a Queue by a Priority Queue. What is...
Remarks: In all algorithm, always explain how and why they work. If not by a proof,...
Remarks: 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. In all algorithms, always try to get the fastest possible. A correct algorithm with slow running time may not get full credit. Do not write a program. Write pseudo codes or explain in words Question 4: Recall the problem in which you have k sorted array each of size...
Remarks: In all algorithm, always explain how and why they work. If not by a proof,...
Remarks: 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. In all algorithms, always try to get the fastest possible. A correct algorithm with slow running time may not get full credit. Do not write a program. Write pseudo codes or explain in words Question 5: Five an efficient data structure supporting the following operations. Insert(S, x), Delete−M ax(S),...
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...
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...
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...
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...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT