Presentation is loading. Please wait.

Presentation is loading. Please wait.

Review of Chapter 5 張啟中. Selection Trees Selection trees can merge k ordered sequences (assume in non- decreasing order) into a single sequence easily.

Similar presentations


Presentation on theme: "Review of Chapter 5 張啟中. Selection Trees Selection trees can merge k ordered sequences (assume in non- decreasing order) into a single sequence easily."— Presentation transcript:

1 Review of Chapter 5 張啟中

2 Selection Trees Selection trees can merge k ordered sequences (assume in non- decreasing order) into a single sequence easily. Two kinds of selection trees  Winner trees A winner tree is a complete binary tree The root represents the smallest node in the tree. Each leaf node represents the first record in the corresponding run. Each non-leaf node in the tree represents the winner of its right and left subtrees.  Loser tress A loser tree is a complete binary tree. Each nonleaf node retains a pointer to the loser is called a loser tree. Each leaf node represents the first record in the corresponding run. An additional node, node 0, has been added to represent the overall winner of the tournament.

3 Winner Tree (k=8) run1 run2 run3 run4 run5 run6 run7 run8 1

4 Winner Tree (k=8) run1 run2 run3 run4 run5 run6 run7 run8 1 O(nlogk)

5 Loser Tree run1 run2 run3 run4 run5 run6 run7 run Overall winner

6 Forests Definition A forest is a set of n ≥ 0 disjoint trees. When we remove the root of a tree, we’ll get a forest. A BCD E F G HI

7 Transforming A Forest Into A Binary Tree Definition If T 1, …, T n is a forest of trees, then the binary tree corresponding to this forest, denoted by B(T 1, …, T n )  is empty if n = 0  has root equal to root (T 1 ); has left subtree equal to B(T 11, T 12, …, T 1m ), where T 11, T 12, …, T 1m are the subtrees of root (T 1 ); and has right subtree B(T 2, …, T n ).

8 Transforming A Forest Into A Binary Tree A B C D E F G H I

9 Forest Traversals Preorder Inorder Postorder (not natural) Level-order

10 The Satisfiability Problem Expression Rules  A variable is an expression  If x and y are expressions then are expressions  Parentheses can be used to alter the normal order of evaluation, which is not before and before or. The satisfiablitity problem for formulas of proposition calculus asks if there is an assignment of values to the variables that causes the values of the expression to be true. The satisfiablitity problem is NP-Complete problem.

11 The Satisfiability Problem x1x1 x3x3 x2x2 x1x1 x3x3 O(2 n )

12 Set Representation Trees can be used to represent sets.  Pairwise Disjoint Sets If S i and S j, i≠j, are two sets, then there is no element that is in both S i and S j. Set Operations  Disjoint set union If S i and S j are two disjoint sets, then their union S i ∪ S j = {all elements x such that x is in S i or S j }.  Find(i) Find the set containing element i.

13 Set Representation S 1 = {0,6,7,8}S 2 = {1, 4, 9}S 3 ={2,3,5} n=10

14 OR Disjoint Set Union S 1 U S 2

15 Data Representation for S 1, S 2, S 3 S1S1 S2S2 S3S Set Name Pointer

16 Array Representation of S 1, S 2, S 3 i[0][1][2][3][4][5][6][7][8][9] parent

17 Degenerate Tree n-1 n-2 0 union(0, 1), find(0) union(1, 2), find(1) union(n-2, n-1), find(n-1) Union operation O(n) Find operation O(n 2 )

18 Improve the performance of Set Union and Find Algorithms Weighting Rule [Weighting rule for union(I, j)]  If the number of nodes in the tree with root i is less than the number in the tree with root j, then make j the parent of i; otherwise make i the parent of j. Collapsing Rule  If j is a node on the path from i to its root and parent[i]≠ root(i), then set parent[j] to root(i).

19 Set Union with The Weighting Rule 01n

20 Set Find with Collapsing Rule 0 3 [-8] Before collapsing After collapsing

21 Equivalence Class [-1] [-1] 0368 [-2] [-1] (a) Initial trees (b) Height-2 trees following 0 ≡ 4, 3 ≡ 1, 6 ≡ 10, and 8 ≡ 9

22 Equivalence Class 0 [-3] [-4] [-3] [-2] 11 0 [-3] [-4] [-3] 1 5 (d) Thees following 11 ≡ 0 (C) Trees following 7 ≡ 4, 6 ≡ 8, 3 ≡ 5, and 2 ≡ 11

23 Counting Binary Trees Problem  Determine the number of distinct binary trees having n nodes.  Determine the number of distinct permutations of the numbers from 1 through n obtainable by a stack.  Determine the number of distinct ways of multiplying n+1 matrices.

24 Distinct binary trees n=0 or n=1  1 binary tree n=2  2 distinct binary trees. n=3  5 distinct binary trees ( 自己練習畫 ) bnbn bibi b n-i-1

25 Stack Permutations How many permutations can we obtain by passing the numbers 1 through n through stack ? See chapter 3 about stack. For example, the numbers 1, 2, 3 (1,2,3) (1,3,2) (2,1,3) (2,3,1) (3,2,1) but (3,1,2) The recursive formula is b n = b 0 b n-1 + b 1 b n-2 + …… + b n-2 b 1 + b n-1 b 0

26 Construct The Binary Tree from Preorder and Inorder Sequence Give preorder and inorder sequence as follows. preorder sequence A B C D E F G H I Inorder sequence B C A E D G H F I Problem  Does such a pair of sequences uniquely define a binary tree ? (Can this pair of sequences come from more than one binary tree?) Conclusion  Every binary tree has a unique pair of preorder / inorder sequences.

27 Construct The Binary Tree from Preorder and Inorder Sequence A B, C D, E, F, G, H, I A B C A B C D F E I G H

28 Inorder and Preorder Permutations A B C D F E I G H Preorder: 1, 2, 3, 4, 5, 6, 7, 8, 9 Inorder: 2, 3, 1, 5, 4, 7, 8, 6, 9

29 Stack Permutations Preorder permutation 1, 2, (1, 2, 3)(1, 3, 2)(2, 1, 3)(2, 3, 1)(3, 2, 1) Inorder permutations

30 Stack Permutations The number of distinct permutations by passing 1..n through stack The number of distinct Binary Trees with n nodes Inorder permutations obtainable from binary trees having the preorder permutations, 1,2,3,….,n ≡ ≡

31 Matrix Multiplication Computing the product of n matrices M 1 * M 2 * … * M n By matrix multiplication associative law, we can perform these multiplications in any order. For example, n=3 (n=4 自己練習 ) (M 1 * M 2 ) * M 3 M 1 * (M 2 * M 3 ) The number of distinct ways to obtain M 1 * M 2 * … * M n

32 Number of Distinct Binary Trees Let which is the generating function for the number of binary trees. By the recurrence relation we get


Download ppt "Review of Chapter 5 張啟中. Selection Trees Selection trees can merge k ordered sequences (assume in non- decreasing order) into a single sequence easily."

Similar presentations


Ads by Google