Review of Chapter 5 - PowerPoint PPT Presentation

1 / 32
About This Presentation
Title:

Review of Chapter 5

Description:

Title: Review of Chapter 5 Author: Last modified by: Created Date: 5/11/2004 3:40:07 AM Document presentation format – PowerPoint PPT presentation

Number of Views:65
Avg rating:3.0/5.0
Slides: 33
Provided by: 73016
Category:

less

Transcript and Presenter's Notes

Title: Review of Chapter 5


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 (k8)
1
6
2
3
6
8
4
5
7
6
9
6
8
17
8
9
10
11
12
13
14
15
10
9
20
6
8
9
90
17
20
20
15
15
11
95
18
15
38
30
25
50
16
99
20
16
28
run1
run2
run4
run3
run5
run6
run8
run7
4
Winner Tree (k8)
O(nlogk)
1
8
2
3
9
8
4
5
7
6
9
15
8
17
8
9
10
11
12
13
14
15
10
9
20
15
8
9
90
17
20
20
25
15
11
95
18
15
38
30
28
50
16
99
20
16
run1
run2
run4
run3
run5
run6
run8
run7
5
Loser Tree
Overall winner
0
6
1
8
2
3
9
17
4
5
7
6
10
20
9
90
8
9
10
11
12
13
14
15
10
9
20
6
8
9
90
17
20
20
15
15
11
95
18
15
38
30
25
50
16
99
20
16
28
run1
run2
run4
run8
run3
run5
run6
run7
6
Forests
  • Definition
  • A forest is a set of n 0 disjoint trees.
  • When we remove the root of a tree, well get a
    forest.

A
E
G
B
C
D
F
H
I
7
Transforming A Forest Into A Binary Tree
  • Definition
  • If T1, , Tn is a forest of trees, then the
    binary tree corresponding to this forest, denoted
    by B(T1, , Tn)
  • is empty if n 0
  • has root equal to root (T1) has left subtree
    equal to B(T11, T12,, T1m), where T11, T12,,
    T1m are the subtrees of root (T1) and has right
    subtree B(T2, , Tn).

8
Transforming A Forest Into A Binary Tree
A
B
E
C
F
G
D
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
x3
x1
x3
O(2n)
x2
x1
12
Set Representation
  • Trees can be used to represent sets.
  • Pairwise Disjoint Sets
  • If Si and Sj, i?j, are two sets, then there is
    no element that is in both Si and Sj.
  • Set Operations
  • Disjoint set union
  • If Si and Sj are two disjoint sets, then
    their union Si?Sj all elements x such that x
    is in Si or Sj.
  • Find(i)
  • Find the set containing element i.

13
Set Representation
n10
0
2
4
6
7
8
3
5
9
1
S1 0,6,7,8
S2 1, 4, 9
S32,3,5
14
Disjoint Set Union
S1 U S2
4
0
OR
4
0
9
1
6
7
8
9
6
7
8
1
15
Data Representation for S1, S2, S3
Set Name
Pointer
S1
0
2
4
S2
6
7
8
3
5
1
9
S3
16
Array Representation of S1, S2, S3
i
0
1
2
3
4
5
6
7
8
9
parent
-1
4
-1
2
-1
2
0
0
0
4
17
Degenerate Tree
n-1
union(0, 1), find(0)
Union operation O(n)
union(1, 2), find(1)
n-2
Find operation O(n2)
union(n-2, n-1), find(n-1)
0
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
    parenti? root(i), then set parentj to root(i).

19
Set Union with The Weighting Rule
0
1
n-1
0
2
n-1
0
3
n-1
1
1
2
0
0
4
n-1
1
3
2
1
3
2
n-1
20
Set Find with Collapsing Rule
-8
-8
0
0
2
4
2
6
1
7
1
4
3
5
5
6
3
After collapsing
7
Before collapsing
21
Equivalence Class
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
0
1
2
3
4
5
6
7
8
9
10
11
(a) Initial trees
-2
-2
-2
-2
-1
-1
-1
-1
0
3
6
8
2
5
7
11
4
1
10
9
(b) Height-2 trees following 04, 31, 610, and
89
22
Equivalence Class
(C) Trees following 74, 68, 35, and 211
-2
-4
-3
-3
6
2
0
3
11
10
8
7
1
5
4
9
-3
-3
-4
0
3
6
1
5
10
8
4
7
2
11
(d) Thees following 110
9
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 n1 matrices.

24
Distinct binary trees
  • n0 or n1 ? 1 binary tree
  • n2 ? 2 distinct binary trees.
  • n3 ? 5 distinct binary trees (?????)

bn
bi
bn-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
  • bn b0bn-1 b1bn-2 bn-2b1 bn-1b0

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
A
B, C
D, E, F, G, H, I
B
D
C
F
E
A
I
G
B
D, E, F, G, H, I
H
C
28
Inorder and Preorder Permutations
1
A
2
4
B
D
3
6
5
C
F
E
9
7
I
G
8
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, 3

1
1
1
1
1
2
3
2
2
2
2
3
3
3
3
(1, 2, 3)
(1, 3, 2)
(2, 1, 3)
(2, 3, 1)
(3, 2, 1)
Inorder permutations
30
Stack Permutations
The number of distinct Binary Trees with n nodes

Inorder permutations obtainable from binary trees
having the preorder permutations, 1,2,3,.,n

The number of distinct permutations by passing
1..n through stack
31
Matrix Multiplication
  • Computing the product of n matrices
  • M1 M2 Mn
  • By matrix multiplication associative law, we can
    perform these multiplications in any order.
  • For example, n3 (n4 ????)
  • (M1 M2) M3
  • M1 (M2 M3)
  • The number of distinct ways to obtain M1 M2
    Mn

32
Number of Distinct Binary Trees
  • Let
  • which is the generating function for the
    number of binary trees.
  • By the recurrence relation we get
Write a Comment
User Comments (0)
About PowerShow.com