Lecture Data Structures: Lesson 36
Số trang: 24
Loại file: ppt
Dung lượng: 226.50 KB
Lượt xem: 22
Lượt tải: 0
Xem trước 3 trang đầu tiên của tài liệu này:
Thông tin tài liệu:
Lecture Data Structures: Lesson 36 provide students with knowledge about union by size; maintain sizes (number of nodes) of all trees, and during union; make smaller tree the subtree of the larger one; implementation: for each root node i, instead of setting parent[i] to -1, set it to -k if tree rooted at i has k nodes;...
Nội dung trích xuất từ tài liệu:
Lecture Data Structures: Lesson 36Running Time Analysis 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 LectureNo.36 DataStructures Dr.SohailAslam Union by Size Maintain sizes (number of nodes) of all trees, and during union. Make smaller tree the subtree of the larger one. Implementation: for each root node i, instead of setting parent[i] to -1, set it to -k if tree rooted at i has k nodes. This also called union-by-weight.Union by Sizeunion(i,j): root1 = find(i); root2 = find(j); if (root1 != root2) if (parent[root1] Union by Size 1 2 3 4 5 6 7 8 -1 -1 -1 -1 -1 -1 -1 -1 1 2 3 4 5 6 7 8 Eight elements, initially in different sets.Union by Size 1 2 3 4 5 7 8 6 -1 -1 -1 -2 -1 4 -1 -1 1 2 3 4 5 6 7 8 Union(4,6)Union by Size 1 2 4 5 7 8 3 6 -1 -2 2 -2 -1 4 -1 -1 1 2 3 4 5 6 7 8 Union(2,3)Union by Size 2 4 5 7 8 3 1 6 4 -2 2 -3 -1 4 -1 -1 1 2 3 4 5 6 7 8 Union(1,4)Union by Size 4 5 7 8 2 1 6 3 4 4 2 -5 -1 4 -1 -1 1 2 3 4 5 6 7 8 Union(2,4)Union by Size 4 7 8 2 1 6 5 3 4 4 2 -6 4 4 -1 -1 1 2 3 4 5 6 7 8 Union(5,4)Analysis of Union by Size If unions are done by weight (size), the depth of any element is never greater than log2n.Analysis of Union by Size Intuitive Proof: Initially, every element is at depth zero. When its depth increases as a result of a union operation (it’s in the smaller tree), it is placed in a tree that becomes at least twice as large as before (union of two equal size trees). How often can each union be done? -- log 2n times, because after at most log2n unions, the tree will contain all n elements.Union by Height Alternative to union-by-size strategy: maintain heights, During union, make a tree with smaller height a subtree of the other. Details are left as an exercise.Sprucing up Find So far we have tried to optimize union. Can we optimize find? Yes, using path compression (or compaction).Sprucing up Find During find(i), as we traverse the path from i to root, update parent entries for all these nodes to the root. This reduces the heights of all these nodes. Pay now, and reap benefits later! Subsequent find may do less workSprucing up Find Updated code for find find (i) { if (parent[i] < 0) return i; else return parent[i] = find(parent[i]); }Path Compression Find(1) 7 13 8 3 22 6 4 5 9 31 32 11 30 10 2 35 20 16 14 12 1 13 17 18 19Path Compression Find(1) 7 13 8 3 22 6 4 5 9 31 32 11 30 10 2 35 20 16 14 12 1 13 17 18 19Path Compression Find(1) 7 13 8 3 22 6 4 5 9 31 32 11 30 10 ...
Nội dung trích xuất từ tài liệu:
Lecture Data Structures: Lesson 36Running Time Analysis 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 LectureNo.36 DataStructures Dr.SohailAslam Union by Size Maintain sizes (number of nodes) of all trees, and during union. Make smaller tree the subtree of the larger one. Implementation: for each root node i, instead of setting parent[i] to -1, set it to -k if tree rooted at i has k nodes. This also called union-by-weight.Union by Sizeunion(i,j): root1 = find(i); root2 = find(j); if (root1 != root2) if (parent[root1] Union by Size 1 2 3 4 5 6 7 8 -1 -1 -1 -1 -1 -1 -1 -1 1 2 3 4 5 6 7 8 Eight elements, initially in different sets.Union by Size 1 2 3 4 5 7 8 6 -1 -1 -1 -2 -1 4 -1 -1 1 2 3 4 5 6 7 8 Union(4,6)Union by Size 1 2 4 5 7 8 3 6 -1 -2 2 -2 -1 4 -1 -1 1 2 3 4 5 6 7 8 Union(2,3)Union by Size 2 4 5 7 8 3 1 6 4 -2 2 -3 -1 4 -1 -1 1 2 3 4 5 6 7 8 Union(1,4)Union by Size 4 5 7 8 2 1 6 3 4 4 2 -5 -1 4 -1 -1 1 2 3 4 5 6 7 8 Union(2,4)Union by Size 4 7 8 2 1 6 5 3 4 4 2 -6 4 4 -1 -1 1 2 3 4 5 6 7 8 Union(5,4)Analysis of Union by Size If unions are done by weight (size), the depth of any element is never greater than log2n.Analysis of Union by Size Intuitive Proof: Initially, every element is at depth zero. When its depth increases as a result of a union operation (it’s in the smaller tree), it is placed in a tree that becomes at least twice as large as before (union of two equal size trees). How often can each union be done? -- log 2n times, because after at most log2n unions, the tree will contain all n elements.Union by Height Alternative to union-by-size strategy: maintain heights, During union, make a tree with smaller height a subtree of the other. Details are left as an exercise.Sprucing up Find So far we have tried to optimize union. Can we optimize find? Yes, using path compression (or compaction).Sprucing up Find During find(i), as we traverse the path from i to root, update parent entries for all these nodes to the root. This reduces the heights of all these nodes. Pay now, and reap benefits later! Subsequent find may do less workSprucing up Find Updated code for find find (i) { if (parent[i] < 0) return i; else return parent[i] = find(parent[i]); }Path Compression Find(1) 7 13 8 3 22 6 4 5 9 31 32 11 30 10 2 35 20 16 14 12 1 13 17 18 19Path Compression Find(1) 7 13 8 3 22 6 4 5 9 31 32 11 30 10 2 35 20 16 14 12 1 13 17 18 19Path Compression Find(1) 7 13 8 3 22 6 4 5 9 31 32 11 30 10 ...
Tìm kiếm theo từ khóa liên quan:
Lecture Data Structures Bài giảng Cấu trúc dữ liệu Data structures Union by size Analysis of union by size Union-by-size strategyGợi ý tài liệu liên quan:
-
Bài giảng Cấu trúc dữ liệu và thuật toán: Chương 3 - Một số mô hình thuật toán
42 trang 74 0 0 -
Bài giảng Cấu trúc dữ liệu và giải thuật: Chương 2 - Ngô Công Thắng
8 trang 66 0 0 -
Bài giảng Cấu trúc dữ liệu và giải thuật: Chương 5 - ThS. Trịnh Quốc Sơn (ĐH Công nghệ Thông tin)
20 trang 49 0 0 -
Bài giảng Cấu trúc dữ liệu và thuật toán: Chương 1 - Khái niệm cơ bản
19 trang 32 0 0 -
Ebook Eloquent JavaScript - A modern introduction to programming: Part 1
199 trang 32 0 0 -
Lecture Introduction to computing systems (2/e): Chapter 19 - Yale N. Patt, Sanjay J. Patel
28 trang 30 0 0 -
Bài giảng Cấu trúc dữ liệu và giải thuật: Bài 14a - Hoàng Thị Điệp (2014)
35 trang 29 0 0 -
Giáo trình cấu trúc dữ liệu part 4
16 trang 28 0 0 -
Giáo trình cấu trúc dữ liệu part 3
16 trang 28 0 0 -
Ebook Introduction to algorithms (3rd edition)
1313 trang 27 0 0