• 沒有找到結果。

# Introduction to Tree (§10.1)

N/A
N/A
Protected

Share "Introduction to Tree (§10.1)"

Copied!
69
0
0

(1)

## Mathematics

(2)





### algorithms.

 Construct efficient algorithms for locating items in a list.

 Construct efficient codes saving costs in data transmission and storage.

 Study games such as checkers and chess.

 Model procedure carried out using sequence of decisions.

(3)

Nikolaus (1623-1708)

Jacob (1654 -1705)

Nikolaus (1662 - 1716)

Nikolaus (1687 - 1759)

Johann (1667 - 1748 )

Nikolaus (1695 -1726)

ΙΙ

Ι Ι

Daniel (1700 - 1782)

Ι Johann

(1710 - 1790) ΙΙ

Johann (1746-1807)

ΙΙΙ Jacob

(1759 - 1789) ΙΙ

(4)

### EXAMPLE2

Which of the graphs shown below are trees ?

a b

d

f c

e

G1 G2

b

c

d

f

G3 G4

b b

d c

d

f e f

a a a

c

e e

Tree Tree Not a Tree Not a Tree

(5)



### An undirected graph is a tree if and only if there is a unique simple path between any two of its vertices.

 Pf: T is a tree. ⇒ T is connected, no simple circuit.

⇒ For any vertices x and y, there is a simple path.

If there is a different path from x to y, then two different paths will form a circuit. This is a contradiction. Thus the path is from x to y is unique.

Assume that there is a unique path between any two vertices in graph T. ⇒ T is connected. Suppose that T contains a simple circuit. Then every pair of vertices in this circuit, say x and y, have two different paths from x to y. That contradicts to the uniqueness of the path. Thus T

has no circuit, and then T is a tree.

(6)

 Def 1. A rooted tree is a tree in which one vertex has been designated as the root and every edge is directed away from the root.

d

f g

b

c a

e

A root tree with root a A Tree

a b a

b

c d

e d f

f g

g c

e

A root tree with root c

(7)

















(8)

### Terminology

a b

c

d e

f

g

i

j

k m

a is the parent of f, b and g j is the parent of l and m

f is the child of a e is the child of c

f, b and g are siblings e and d are siblings h

l A Rooted Tree T

a, b are ancestors of c e, c are descendants of b

leaves: d, e, f, k, i, l, m internal vertices: others b

d

c

e subtree of b

subtree of g

(9)







(10)

1

### T

2

A full binary tree. A full 3-ary tree.

(11)

3

### T

4

A full 5-ary tree. A m-ary tree. (m≥3) Not a full m-ary tree

(12)







(13)

### Example 4

a

b c

d

e

f g j

h i

m k

l

A Binary Tree T

The Left Subtree of the Vertex c.

h

j

The Right Subtree of the Vertex c.

i

k

l

m

(14)

H

H H

H

H

H H

H

H

H

C

C

C

C

H H H

H

H

H H

H

H

H

C C C

C

The Two Isomers of Butane Butane

Isobutane

(15)

### Representing Organizations

President

VP

R&D

VP

Marketing

VP

Finance

VP

Services

Director Resourch

Director Software Development

Director Software Development

AVP Sales

AVP

Marketing

Chief Field Operations

Chief Field Operations

Director Accounting

Director MIS

(16)



### A tree with n verices has n − 1 edges.

 Pf: Prove by induction. Let P(n) be the statement “A tree with n verices has n1 edges.”

Basis Step: When n = 1, a tree with one vertex has no edge.

P(1) is true.

Inductive Step: Assume that P(k) is true. Suppose that T is a tree with k+1 vertices and v is a leaf. Let w is the parent of v.

Remove v and the edge connecting v and w. We’ll get a tree T’

with k vertices and then has k−1edges. It follows that T has k edges. Then P(k+1) is true. ♦

(17)

1

### T

2

A full binary tree

m = 2, i = 3, 7 vertices.

A full 3-ary tree

m = 3, i = 4, 13 vertices

A full m-ary tree with i internal vertices contains mi+1 vertices.

(18)

3

### T

4

A full 5-ary tree

m = 5, i = 3, 16 vertices Not a full m-ary tree Not satisfy the theorem

(19)

### Example 9

 A chain letter: Each people sends to 4 people. Some people do this, but others do not send any letters.

How many people have seen this letter, including the first

person, if no one receives more than one letter and if the chain letter ends after there have 100 people who read it but didn’t send it out? How many people sent out the letter?

 Sol: This is a 4-ary (m = 4) tree.

Note there are n = mi +1 vertices and l = n − i leaves.

100(= l) leaves ⇒n = 4(n−100) + 1 ⇒ 400−1 = 3n ⇒ There are n = 133 vertices.

There are 133−100 = 33 internal vertices.

(20)



1.

2.

3.

(21)



### The level of a vertex v in a rooted tree is the length of the unique path from the root to this vertex v.

 The level of the root is defined to be zero.



### The height of a rooted tree is the maximum of the levels of vertices.

 That is, the height is the length of the longest path from the root to any vertex.



(22)

### Example

a b

e

f

i j

l

m n

Level

c

d g

h

k

Vertices 0 a

1 b, j, k 2 c, e, f, l

3 d, g, i, m, n 4 h

The height of this rooted tree is 4.

(23)

1

### T

2

Balanced Not balanced

(24)

3

Balanced

(25)

(26)







(27)

### Binary Search Tree

 簡介：二元搜尋樹 是一種二元樹。它可能是空的，若不是

(１) 每一個元素有一鍵值，而且每一元素的鍵值都不相 同，即每一個鍵值都是唯一的。

(２) 在非空的左子樹上的鍵值，必小於在該子樹的根節點 中的鍵值。

(３) 在非空的右子樹上的鍵值，必大於在該子樹的根節點 中的鍵值。

(４) 左子樹和右子樹也都是二元搜尋樹。

(28)



### Form a binary search tree for the words

 mathematic

 physics

 geography

 zoology

 meteorology

 geology

 psychology

 chemistry (using alphabetical order)

(29)
(30)











(31)







(32)

### Huffman Codes

 簡介：Huffman 編制程式是試圖減少相當數量位元必需代

 修造Huffman 樹

(１)選擇二個parentless 結點以最低的可能性。

(２)創造是二個最低的可能性結點的父母的一個新結點。

(３) 分配新結點可能性相等與它的孩子的可能性的總和。

(４)重覆步驟1 直到有只一個parentless 結點剩下

(33)



(34)
(35)

### Example 5

 於題目中找尋最小兩點→A:0.08和B:0.10(A於右下B於左下

∵B>A)

 接著把A+B視為新的一點0.18，再找最小兩點→C:0.12和

D:0.15如同AB方法接起來

 C+D也視為新的一點0.27再比大小，之後依此方法作完全

 而全圖於右下角的支線都為1,左下角都之線都為0

 因此A by 111,B by 110,C by 011,D by 010, E by 10, F by 00.

 So the average number of bits used to encode a symbol using this encoding is

3×0.08+3×0.10+3×0.12+3×0.15+2×0.20+2×0.35=2.45

(36)

(37)

v

v

(38)

### we display the labelings of the universal address system next to the vertices in the ordered rooted tree shown in Figure 1.

Sol. The lexicographic ordering of the labelings is

0 < 1 < 1.1 < 1.2 < 1.3 < 2 < 3 < 3.1 < 3.1.1 < 3.1.2 < 3.1.2.1 < 3.1.2.2

< 3.1.2.3< 3.1.2.4 < 3.1.3 < 3.2 <4 < 4.1 < 5 < 5.1 < 5.1.1 < 5.2 < 5.3

(39)

0

1 2 3 4 5

1.1 1.2 1.3

3.1

3.2 4.1 5.1 5.2 5.3

3.1.3 3.1.2

3.1.1

3.1.2.1 3.1.2.2 3.1.2.3 3.1.2.4

5.1.1

(40)



1

2

n

1

2

n

(41)

### Example 2

 In which order does a preorder traversal visit the vertices in the ordered rooted tree T

shown below.

a

b c d

e f

g

h i

j k

l m

n o p

(42)

a b e f

j

k

n o p

c d g h i

l m

a b e j k n o p f c d g l m h i

The preorder traversal of T is

a , b , e , j , k , n , o , p , f , c , d , g , l , m , h , i .

(43)



1

2

n

1

2

3

n

(44)

### Example 3

In which order does a inorder traversal visit the vertices in the ordered rooted tree T shown below .

a

b c d

e f

g

h i

j k

l m

n o p

(45)

a c b

f

d g

h i

l m

e

j k

n o p

b f a c l g m d h i

e

j k

n o p

The preorder traversal of T is

a , b , e , j , k , n , o , p , f , c , d , g , l , m , h , i .

(46)



1

2

n

1

2

3

(47)

### Example 4

a

b c d

e f

g

h i

j k

l m

n o p

In which order does a postorder traversal visit the vertices in the ordered rooted tree T shown below .

(48)

d a g

h i

l m

b c

e f

j k

n o p

b f a c l g m d h i

e

j k

n o p

The preorder traversal of T is

a , b , e , j , k , n , o , p , f , c , d , g , l , m , h , i .

(49)



(50)

+

x y x

_

4

+ 2

x y x

_

4

3

/

+ 2

x y x

_

4

3 /

+

(51)

### Example 6

what is the prefix from for ((x+y)↑2)+((x-4)/3)?

### Example 8

what is the postfix from for ((x+y)↑2)+((x-4)/3)?

### 3 / +

(52)

+ - * 2 3 5 / ↑ 2 3 4 + - * 2 3 5 / 8 4

+ - * 2 3 5 2 + - 6 5 2

+ 1 2 3

### Example 7

what is the value of the prefix expression + - * 2 3 5 / ↑ 2 3 4 ?

(53)

7 2 3 * - 4 ↑ 9 3 / + 7 6 - 4 ↑ 9 3 / +

1 4 ↑ 9 3 / + 1 9 3 / +

1 3 + 4

### Example 9

what is the value of the postfix expression 7 2 3 * - 4 ↑9 3 / + ?

(54)

### Example 10

Find the ordered rooted tree representing the

compound proposition ( ┐ (p ^ q)) (┐p ˇ ┐q) . Then use this rooted tree to find the prefix , postfix and infix firms of this

expression.

^

p q

p q

^

p q

p q

ˇ

^

p q

p q

ˇ

preorder : ┐ ^ p q ˇ ┐p ┐q postorder : p q ^ ┐p ┐q ˇ

inorder : (( ┐ (p ^ q)) ((┐p) ˇ (┐q)))

(55)





(56)



### A simple graph is connected if and only if it has a spanning tree.

 Pf: Suppose that a simple graph G has a spanning tree T. T contains every vertex of G. There is a path in T between any two of its vertices. Thus there is a path in G between any two of its vertices. Hence, G is connected.

Suppose that G is connected. If G is not a tree, it must contain a simple circuit. Removing an edge from this circuit obtains a subgraph of G, say G’. If G’ is a tree, than it is a spanning tree of G. Otherwise, we continue to remove edge from a circuit until we get a tree subgraph. This tree is a spanning tree of G.

(57)

### Depth-First Search

 Arbitrary choose a vertex as a root.

 Successively adding vertices and edges where each new edge is incident with the last vertex in the path and a vertex not already in the path. Let this path as long as possible.

 Move back to the next to last vertex in the path.

a

c e

f g

h b

d i j

k

f e

c

d

a b

g hk i j

(58)

### Algorithm: Depth-First Search

procedure DFS(G; connected graph with vertices v1, v2, …, vn) T := tree consisting only of the vertex vi

visit(vi)

procedure visit(v: vertex of G)

for each vertex w adjacent to v and not yet in T begin

add vertex w and edge {v, w} to T visit(w)

end

The procedure DFS constructs a spanning tree using O(e), or O(n2).

DFS is also called backtracking,

since the algorithm returns to vertices previously visited to add paths.

(59)

### Example

 How can backtracking be used to decide whether a given graph can be colored using 3 colors?

a b c

d

e a 1

b

a b c d e

Spanning Tree

2

d

1 3

c c

no waye e3

3

d 1

Done

(60)

 Arbitrary choose a root.

 Add all edges (and vertices) incident to this vertex. Order these vertices (in level 1) arbitrarily.

 Continue this step for the vertices we obtaining in previous step as long as it does not produce a simple circuit till all vertices have been added.

a b c

d e

f

i j

k

l

m

e g

h

f c

i

e j g

b d

l

a h k

m

(61)

procedure BFS(G; connected graph with vertices v1, v2, …, vn) T := tree consisting only of the vertex vi

L := empty list

put v1 in the list L of unprocessed vertices while L is not empty

begin

remove the first vertex , v, from L for each neighbor w of v

if w is not in L and not in T then begin

add w to the end of the list L add w and edge {v, w} to T end

end

The procedure DFS constructs a spanning tree using O(e), or O(n2).

(62)

### DFS in Directed Graphs

a b c d

e f

i j k l

g h

a b c g

f

e

d h

l k j

i

The output of DFS of a directed graph is not a tree, but a spanning forest.

(63)





### The first algorithm was given by Robert Prim in 1957.

 Choose any edge with smallest weight.

 Successively add to the tree edges of minimum weight that are incident to the vertex already in the tree and not

forming a simple circuit.

 Stop when n − 1 edges have been added.

(64)

### Algorithm Prim’s Algorithm

procedure Prim(G: weighted connected undirected graph with n vertices) T := a minimum-weight edge

for i := 1 to n − 2 begin

e := an edge of minimum weight incident to a vertex in T and not forming a simple circuit in T if added to T

T := T with e added

end (T is a minimum spanning tree of G)

(65)

### Example

San Francisco

Chicago

Atlanta Denver

\$2000

\$1200

\$1000

\$2200

\$900

\$1400

\$1300

\$800

\$700

\$1600

New York

700 (Chicago, Atlanta) 800 (Atlanta, New York) 900 (Denver, San Francisco) 1200 (San Francisco, Chicago)

Total cost = \$3600

Chicago Atlanta S F

New York Denver

(66)

### Example

a b c d

f g

h

i j k l

2 3

3 3

3 3

3

3 2

4 4

2

4

5 1 1

1

e

1 (c, d)

1 (b, f)

1 (k, l) 2 (c, g)

2 (a, b) 2 (f, j) 3 (b, c)

3 (a, e) 3 (i, j) 3 (g, h) 3 (h, l)

Total = 24

(67)

### Algorithm Kruskal’s Algorithm

procedure Kruskal(G: weighted connected undirected graph with n vertices) T := empty tree

for i := 1 to n − 1 begin

e := an edge in G with smallest weight that does not form a simple circuit when added to T

T := T with e added

end (T is a minimum spanning tree of G)

(68)

### Example

San Francisco

Chicago

Atlanta Denver

\$2000

\$1200

\$1000

\$2200

\$900

\$1400

\$1300

\$800

\$700

\$1600

New York

700 (Chicago, Atlanta) 800 (Atlanta, New York) 900 (Denver, San Francisco)

1200 (San Francisco, Chicago) Total cost = \$3600

(69)

### Example

a b c d

f g

h

i j k l

2 3

3 3

3 3

3

3 2

4 4

2

4

5 1 1

1

e

1 (c, d) 1 (b, f) 1 (k, l) 2 (a, b) 2 (c, g) 2 (f, j) 3 (b, c) 3 (g, h) 3 (a, e) 3 (h, l) 3 (i, j)

Total = 24

• Goal is to construct a no-arbitrage interest rate tree consistent with the yields and/or yield volatilities of zero-coupon bonds of all maturities.. – This procedure is

• Non-uniform space subdivision (for example, kd tree and octree) is better than uniform grid kd-tree and octree) is better than uniform grid if the scene is

An n×n square is called an m–binary latin square if each row and column of it filled with exactly m “1”s and (n–m) “0”s. We are going to study the following question: Find

• One technique for determining empirical formulas in the laboratory is combustion analysis, commonly used for compounds containing principally carbon and

• Definition: A max tree is a tree in which the key v alue in each node is no smaller (larger) than the k ey values in its children (if any). • Definition: A max heap is a

Parent effectiveness training: The tested way to raise responsible children. Parent-child relations: An introduction to parenting

(It is also acceptable to have either just an image region or just a text region.) The layout and ordering of the slides is specified in a language called SMIL.. SMIL is covered in

• Non-uniform space subdivision (for example, kd tree and octree) is better than uniform grid kd-tree and octree) is better than uniform grid if the scene is