review of chapter 5 張啟中. selection trees selection trees can merge k ordered sequences (assume...

32
Review of Chapter 5 張張張

Upload: annabel-robey

Post on 15-Dec-2015

217 views

Category:

Documents


3 download

TRANSCRIPT

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

Review of Chapter 5

張啟中

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

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 l

eft 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.

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

Winner Tree (k=8)

2038

2030

152528

1550

1116

9599

1820

1516

10 9 20 6 8 9 90 17

9 6 8 17

6

6 82 3

4 5 6 7

8 9 10 11 12 13 14 15

run1 run2 run3 run4 run5 run6 run7 run8

1

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

Winner Tree (k=8)

2038

2030

2528

1550

1116

9599

1820

1516

10 9 20 15 8 9 90 17

9 15 8 17

8

9 82 3

4 5 6 7

8 9 10 11 12 13 14 15

run1 run2 run3 run4 run5 run6 run7 run8

1

O(nlogk)

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

Loser Tree

2038

2030

152528

1550

1116

9599

1820

1516

10 9 20 6 8 9 90 17

10 20 9 90

8

9 172 3

4 5 6 7

8 9 10 11 12 13 14 15

run1 run2 run3 run4 run5 run6 run7 run8

1

60 Overall

winner

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

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

B C D

E

F

G

H I

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

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 t

o B(T11, T12,…, T1m), where T11, T12,…, T1m are the subtrees of root (T1); and has right subtree B(T2, …, Tn).

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

Transforming A Forest Into A Binary Tree

A

B

C

D

E

FG

H

I

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

Forest Traversals

Preorder Inorder Postorder (not natural) Level-order

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

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 evalu

ation, 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.

xandyxyx ,,

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

The Satisfiability Problem

x1 x3

x2 x1

x3

33121 )()( xxxxx

O(2n)

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

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 S

i S∪ j = {all elements x such that x is in Si or Sj}. Find(i)

Find the set containing element i.

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

Set Representation

4

1 9

2

3 5

0

6 7 8

S1= {0,6,7,8} S2 = {1, 4, 9} S3={2,3,5}

n=10

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

0

6 7 8 4

1 9

0

6 7 8

4

1 9OR

Disjoint Set Union

S1 U S2

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

Data Representation for S1, S2, S3

S1

S2

S3

4

1 9

2

3 5

0

6 7 8

Set

Name Pointer

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

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

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

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(n2)

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

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 pare

nt[i]≠ root(i), then set parent[j] to root(i).

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

Set Union with The Weighting Rule

0 1 n-1 0 2 n-1

1

0 3 n-1

1 2

0 4 n-1

1 32

0

1 32 n-1

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

Set Find with Collapsing Rule

0

3

[-8]

1 2 4

7

5 6

0

3

[-8]

1 2 4 7

5

6

Before collapsing

After collapsing

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

Equivalence Class

0 1 2 3 4 5 6 7

[-1] [-1] [-1] [-1] [-1] [-1] [-1] [-1]

8 9 10 11

[-1] [-1] [-1] [-1]

0 3 6 8

[-2] [-2] [-2] [-2]

2 5 7 11

[-1] [-1] [-1] [-1]

4 1 10 9

(a) Initial trees

(b) Height-2 trees following 0≡4, 3≡1, 6≡10, and 8≡9

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

Equivalence Class

0

[-3]

4 7

6

9

[-4]

10 8

3

[-3]

1 5

2

[-2]

11

0

[-3]

4 7 2

11

6

9

[-4]

10 8

3

[-3]

1 5

(d) Thees following 11≡0

(C) Trees following 7≡4, 6≡8, 3≡5, and 2≡11

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

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.

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

Distinct binary trees

n=0 or n=1 1 binary tree n=2 2 distinct binary trees.

n=3 5 distinct binary trees ( 自己練習畫 )

bn

bi bn-i-1

1,1, 01 bandnbbb inin

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

Stack Permutations How many permutations can we obtain by pas

sing 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

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

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 IInorder sequence B C A E D G H F I

Problem Does such a pair of sequences uniquely define a bi

nary tree ? (Can this pair of sequences come from more than one binary tree?)

Conclusion Every binary tree has a unique pair of preorder / ino

rder sequences.

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

Construct The Binary Tree from Preorder and Inorder Sequence

A

B, C D, E, F, G, H, I

A

D, E, F, G, H, IB

C

A

B

C

D

FE

IG

H

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

Inorder and Preorder Permutations

A

B

C

D

FE

IG

H

1

2

3

4

65

97

8

Preorder: 1, 2, 3, 4, 5, 6, 7, 8, 9

Inorder: 2, 3, 1, 5, 4, 7, 8, 6, 9

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

Stack PermutationsPreorder permutation 1, 2, 3

1

2

3

1

2

3

1

32

1

2

3

1

2

3

(1, 2, 3) (1, 3, 2) (2, 1, 3) (2, 3, 1) (3, 2, 1)

Inorder permutations

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

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

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

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, n=3 (n=4 自己練習 )

(M1 * M2) * M3

M1 * (M2 * M3)

The number of distinct ways to obtain M1 * M2 * … * Mn

1

1

1,n

iinin nbbb

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

Number of Distinct Binary Trees Let

which is the generating function for the number of binary trees.

By the recurrence relation we get

i

ii xbxB

0

)(

1)()(2 xBxxB

x

xxB

2

411)(

mmm

mn

n xm

xnx

xB 12

00

2)1(1

2/1)4(

2/11

2

1)(

)/4(2

1

1 2/3nObn

n

nb n

nn