You are on page 1of 10

Comsats Institute of Information Technology, Attock

Advance Algorithm Analysis (CSC511) Assignment no. 1

Submitted to: M. Ishtiaq Submitted by: Tayyaba Sohail

Advance Algorithm Analysis

Problems Given In the Assignment:

1. What is the effect of changing the statement no. 10 for j=A.length downto 1 into the statement for j=1 to A.length in algorithm of counting sort ? 2. What change would be made in the algorithm of counting sort to make it descending sort algorithm? 3. What Is Heap? 4. Array implementation of a heap. 5. What is Heapify?
Solution: Problem # 1:

If we change the statement 10 for j=A.length downto 1 into the statement for j=1 to A.length in the counting sort algorithm it will cause no difference in the result of the algorithm. The output will remain same for the given input in both the cases. Stability: However one very important property of counting sort will be compromised. It will not remain stable. The changed algorithm for counting sort is:

I shall prove my conclusion by giving this pictorial illustration. To simplify I am taking the same Array given in the book for the counting sort Algorithm, that is A[2,5,3,0,2,3,0,3].

Assignment no. 1

Page 2

Advance Algorithm Analysis

Until statement no. 8 everything is happening as it was in the actual algorithm, an array C is being assigned with the cumulative values. But in statement no. 10 where the for loop is running from 1 to A.length , here the value stored in array A is sequentially approached from the 1 st index. As we can see from the figure that at 1st index the value is 2. Now the algorithm further looks for the value in array C at index no. 2 which is 4. Hence, the value of A[1] which is 2, is placed at index no. 4 in array B. In the next statement value of C[A[i]] is decreased by 1. There will be no change in this statement as it is telling that at any index in array C the value is representing the no. of instances of any particular no. which is still left in array A and is still unsorted. Whenever any

Assignment no. 1

Page 3

Advance Algorithm Analysis

number is placed at its correct position in array B, the value of its instances in array C is decreased by 1.
Solution: Problem # 2:

There can be more than one ways which we can adopt to get descending order sorting. To change the counting sort algorithm from ascending sort to descending sort, we need to make few changes in the code. The change can be made in following ways:

In this code at statement no. 7 and 8 the change is being made. Here the value of i is decrementing from k-1 to 0. In next statement the sum of C[i] and C[i+1] is being placed in C[i]. Hence, giving us an accumulative in descending order. In the above figure at (b) the resulting values in array C will be C[8,6,6,4,1,1] So, at C[0] the value is 8 and at C[5] the value is 1. Now, at statement 11 when the value of A[j] is placed in array B where the indexes are determined by array C, the resulting array B will be in descending order. Another way of achieving this goal is:

Assignment no. 1

Page 4

Advance Algorithm Analysis

Here, in this code the change is made at statement no. 11. Rest of the code will remain same for both sorting orders. What I am doing here is, subtracting the value that I got from Array C from total length of array A and then adding 1 into it. The values which I am getting here are the indexes in descending order, where the value of A[j] in array B will be placed. There will be no change in the total running time of the algorithm. It will still take linear running time. One more way of achieving this goal is: Another way, which is I guess, is not a good programming practice is to introduce a for loop and make a new array D and copy the elements of array B starting from A.length down to 1 in the array D*1 A.length+. the resulting array D will also be in descending order.

Assignment no. 1

Page 5

Advance Algorithm Analysis

Solution: Problem # 3: Heap: A heap is a specialized tree-based data structure that satisfies the heap property. It is an array of objects that we can view as a nearly complete binary tree. Each node of the tree corresponds to an element of the array. The tree is completely filled on all levels except possibly the lowest. The HEAP property is defined as: If A is a parent node of B then key(A) is ordered with respect to key(B) with the same ordering applying across the heap. There is no implied ordering between siblings or cousins. The heap relation mentioned above applies only between nodes and their immediate parents. The maximum number of children each node can have depends on the type of heap, but in many types it is at most two. The heap is one maximally efficient implementation of an abstract data type called a priority queue. Heaps are crucial in several efficient graph algorithms such as Dijkstra's algorithm, and in the sorting algorithm heapsort. In a max heap the keys of parent nodes are always greater than or equal to those of the children and the highest key exists in the root node. In a min heap the keys of parent nodes are always less than or equal to those of the children and the root node has the lowest key. The height of a node in a heap to be the number of edges on the longest simple downward path from the node to a leaf, and we define the height of the heap to be the height of its root. Since a heap is a complete binary tree, it has a smallest possible height - a heap with N nodes always has O(log N) height.
Solution: Problem # 4:

Array implementation of a heap Heaps are commonly implemented with an array. Any binary tree can be stored in an array, but because a heap is always an almost complete binary tree, it can be stored compactly. No space
Assignment no. 1 Page 6

Advance Algorithm Analysis

is required for pointers; instead, the parent and children of each node can be found by arithmetic on array indices. The root is placed at index 1, wasting space in order to simplify arithmetic. Let n be the number of elements in the heap and i be an arbitrary valid index of the array storing the heap. If the tree root is at index 0, with valid indices 0 through n-1, then each element a[i] has children a[2i+1] and a[2i+2] parent a*floor((i1)/2)+ Alternatively, if the tree root is at index 1, with valid indices 1 through n, then each element a[i] has children a[2i] and a[2i+1] parent a[floor(i/2)].

Complete binary tree stored in an array

Comparison between a binary heap and an array implementation

The upheap/downheap operations can then be stated in terms of an array as follows:

Assignment no. 1

Page 7

Advance Algorithm Analysis

Suppose that the heap property holds for the indices b, b+1, ..., e. The sift-down function extends the heap property to b1, b, b+1, ..., e. Only index i = b1 can violate the heap property. Let j be the index of the largest child of a[i] (for a max-heap, or the smallest child for a min-heap) within the range b, ..., e. (If no such index exists because 2i > e then the heap property holds for the newly extended range and nothing needs to be done.) By swapping the values a[i] and a[j] the heap property for position i is established. At this point, the only problem is that the heap property might not hold for index j. The sift-down function is applied tail-recursively to index j until the heap property is established for all elements. The sift-down function is fast. In each step it only needs two comparisons and one swap. The index value where it is working doubles in each iteration, so that at most log2 e steps are required. For big heaps and using virtual memory, storing elements in an array according to the above scheme is inefficient: (almost) every level is in a different page. B-heaps are binary heaps that keep subtrees in a single page, reducing the number of pages accessed by up to a factor of ten. It is possible to modify the heap structure to allow extraction of both the smallest and largest element in time. To do this, the rows alternate between min heap and max heap. The algorithms are roughly the same. Derivation of children's index in an array implementation This derivation will show how for any given node (starts from zero), its children would be found at and .

Mathematical proof From the figure in "Heap Implementation" section, it can be seen that any node can store its children only after its right siblings and its left siblings' children have been stored. This fact will be used for derivation. Total number of elements from root to any given level = , where starts at zero. Suppose the node is at level . So, the total number of nodes from root to previous level would be = Total number of nodes stored in the array till the index = number of siblings on the left of is (Counting too) So, total

Assignment no. 1

Page 8

Advance Algorithm Analysis

Hence, total number of children of these siblings = Number of elements at any given level = So, total siblings to right of is:

So, index of 1st child of node would be:-

Solution: Problem # 5:

Heapify: Maintaining the heap property in any heap is called heapify. Hence max heapify property and min heapify property is defined as: The max-heap property means that the parent value child value for every node i other than the root A* Parent( i ) + A* i + here the root is excluded as it has no parents.

Assignment no. 1

Page 9

Advance Algorithm Analysis

In order to maintain the max-heap property, we call the procedure MAX-HEAPIFY. Its inputs are an array A and an index i into the array. When it is called, MAXHEAPIFY assumes that the binary trees rooted at LEFT(i) and RIGHT(i) are maxheaps, but that A[i] might be smaller than its children, thus violating the max-heap property. MAX-HEAPIFY lets the value at A[i] float down in the max-heap so that the subtree rooted at index i obeys the max-heap property.

Max-Heapify recursively call itself until the max-heap property is achieved. The min-heap property means that the parent value child value for every node i other than the root A* Parent( i ) + A* i + Like the max-heapify function min heapify function is called to arrange the elements of the heap so it can hold its min-heap property.

Assignment no. 1

Page 10

You might also like