CS301-Lec36 handout

# CS301-Lec36 handout - CS301 Data Structures Lecture No. 36...

This preview shows pages 1–3. Sign up to view the full content.

CS301 – Data Structures Lecture No. 36 ___________________________________________________________________ Data Structures Lecture No. 36 Reading Material Data Structures and Algorithm Analysis in C++ Chapter. 8 8.4, 8.5, 8.6 Summary Running Time Analysis Union by Size Analysis of Union by Size Union by Height Sprucing up find Timing with Optimization Running Time Analysis In the previous lecture, we constructed up trees through arrays to implement disjoint sets. u nion and find methods along with their codes were discussed with the help of figures. We also talked about the ways to improve these methods. As far as the union operation is concerned, considering the parent array, it was related to changing the index of the array. The root value of the merging tree was put into that. In case of find, we have to go to the root node following the up pointers. While studying the balanced trees, we had constructed a binary search tree from the already sorted data. The resultant tree becomes a linked list with height N. Similar kind of risks involved here. Suppose, we have eight members i.e.1, 2, 3, 4, 5, 6, 7 and 8 as discussed in an example in the previous lecture. Suppose their unions are performed as union(1,2), union(2,3), union(3,4) and so on up to union(7,8). When the trees of these unions are merged together, there will be a longer tree of height 8. If we perform a find operation for an element that is present in the lowest node in the tree then N-1 links are traversed. In this particular case, 8-1=7 links. We know that with reduced tree (with lesser height), the time required for find operation will also be reduced. We have to see whether it is possible to reduce the size (in terms of height) of the resultant tree (formed after unions of trees). Therefore, our goal is: - Goal : Modify union to ensure that heights stay small We had already seen this goal in the last slide of our previous lecture, which is given below in full: Page 1 of 13

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
CS301 – Data Structures Lecture No. 36 ___________________________________________________________________ - union is clearly a constant-time operation. - Running time of find ( i ) is proportional to the height of the tree containing node i . - This can be proportional to n in the worst case (but not always) - Goal: Modify union to ensure that heights stay small You might be thinking of employing the balancing technique here. But it will not be sagacious to apply it here. Due to the use of array, the ‘balancing’ may prove an error- prone operation and is likely to decrease the performance too. We have an easier and appropriate method, called Union by Size or Union by Weight . Union by Size Following are the salient characteristics of this method: - Maintain sizes (number of nodes) of all trees, and during union. -
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 05/21/2010 for the course CS CS301 taught by Professor Dr.naveed malik during the Spring '10 term at Virtual University of Pakistan.

### Page1 / 13

CS301-Lec36 handout - CS301 Data Structures Lecture No. 36...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online