rooted tree and binary tree

23
Rooted tree and binary tree Theorem 5.19: A full binary tree with t leaves contains i=t-1 internal vertices.

Upload: joshua-justice

Post on 04-Jan-2016

35 views

Category:

Documents


1 download

DESCRIPTION

Rooted tree and binary tree Theorem 5.19: A full binary tree with t leaves contains i=t-1 internal vertices. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Rooted tree and binary tree

Rooted tree and binary tree Theorem 5.19: A full binary tree with t leaves

contains i=t-1 internal vertices.

Page 2: Rooted tree and binary tree

Theorem 5.20: Let T be a full binary tree. We denote the sum of length of simple paths from root to all internal vertices by I, and the sum of length of simple paths from root to all leaves by E. Then E=I+2i, where i is the number of internal vertices.

Proof: Let us apply induction on the number i of internal vertices.

E=2 and I=0 when i=1

Page 3: Rooted tree and binary tree

Suppose that result holds for i=k-1 For i=k, we chose internal vertex v so that its

children are leaves. We have a new tree which is obtained by omitting edges of incident v and its children.

By the inductive hypothesis, E'=I'+2(k-1) We denote the length of path from root to v by l. E'=E- l-2, I'=I-l. E= E'+l+2=I'+2(k-1)+l+2=I-l+2(k-1)+l+2=I+2k。 Let T be a full m-ary tree. Then E=(m-1)I+mi, where

i is the number of internal vertices.

Page 4: Rooted tree and binary tree

5.6 Prefix codes and optimal tree

a b c d e00 110 010 10 01The set {00,110,010,10,01} is called code010010ead or cc?The string of e is prefix of string of cc: 111The set {00,110,111,10,01} is called prefix code

Page 5: Rooted tree and binary tree

Definition 31: Codes with this property which the bit string for a letter never occurs as the first part of the bit string for another letter are called prefix codes.

Theorem 5.21: We can construct a prefix code from any binary tree, and we can construct a binary tree from the prefix codes.

Proof: (1) We can construct a prefix code from any binary tree where the left edge at each internal vertex is labeled by 0 and the right edge by a 1 and where the leaves are labeled by characters

(2)We can construct a binary tree from the prefix codes

Page 6: Rooted tree and binary tree

字母 a b c d e f g h i j k l m 频率 0. 0356 0. 0139 0. 0279 0. 0378 0. 1304 0. 0289 0. 0199 0. 0528 0. 0627 0. 0013 0. 042 0. 0339 0. 0249

字母 n o p q r s t u V w x y z 频率 0. 0707 0. 0797 0. 0199 0. 0012 0. 0677 0. 0607 0. 1045 0. 0249 0. 0092 0. 0149 0. 0017 0. 0199 0. 0008

Page 7: Rooted tree and binary tree

Definition 32: Let T be a tree with weigths w1w2...wn at its leaf nodes. The weighted leaf path length w(T) of

T is W(T)=

n

iiilw

1

, where li is the path length from the

root to vertex i. Say that a binary tree T is optimal if w(T) has its minimum value over all possible trees with the same set of leaf nodes.

Page 8: Rooted tree and binary tree

Example: Let T be a binary tree with weigths 3, 5, 7, 9 a) ,48 iilw b) ,47 iilw

Page 9: Rooted tree and binary tree

Huffman algorithm: Let a1,a2,,an be n vertex with weight w1,w2,,wn and

w1w2wn。 F:=forest of n rooted tree each consisting of the single vertex

ai with weight wi for i=1,2,...n. While F is not a tree Begin Replace the rooted trees T and T’ of least weights from F

with w(T)≥ w(T’) with a tree having a new root that has T as its left subtree and T’ as its right subtree. Assign w(T)+w(T’) as the weight of the new tree.

end

Page 10: Rooted tree and binary tree

Example: Find a optimal tree with weight 2,4,7,8,10,12

Page 11: Rooted tree and binary tree

w(T)=2*4+4*4+7*3+12*2+8*2+10*2=105

Page 12: Rooted tree and binary tree

Theorem 5.22: Let T be built according to Huffman algorithm and leaves of T with weight w1,w2,,wn. Then T is an optimal tree.

Proof: Let us apply induction on the number n of vertices.

n=2,

The results holds Suppose that result holds for n=k-1For n=k,By the inductive hypothesis,Suppose that nodes in an optimal tree T have weights w1+w2,w3,,wk. Then This is an optimal tree with weight w1,w2,w3,,wk if T’s leaf with weight w1+w2 is replaced by subtree

Page 13: Rooted tree and binary tree

Lemma 5.1 Let T1 be an optimal tree with weights w1w2w3 wk. Then there is an optimal tree T2 so that T2’s two vertices with weights w1 and w2 are brother nodes.

Proof: Let T1 be an optimal tree with weights w1w2w3wk

The two weights wa ,wb are on lowest level and they are brothers. We denoted by va and vb. Let v0 be the father of va, vb.

waw1, wbw2. Let la be the length of the path from root to va, and lb be the

length of the path from root to vb. la=lb , la l1 , la l2 , We obtain a new tree T2 by exchanging from leaf v1 to va and

from leaf v2 to vb. w(T1)-w(T2)=(wa-w1)(la-l1)+ (wb-w2)(la-l2)0

Page 14: Rooted tree and binary tree

Lemma 5.2: Suppose that nodes of an optimal tree T have weights w1+w2,w3,,wk. Then this is an optimal tree with weight w1,w2,w3,,wk if T*’s leaf with weight w1+w2 is replaced by subtree

Proof: By the Lemma 5.1,there is an optimal tree T2 with weight w1,w2,w3,,wk so that T2’s two vertices with weights w1 and w2 are brother nodes.Let l1 be the length of the path from root to v1 with weight w1.

Let T2* be a same tree as T2 without leaf with weight w1 and leaf with weight w2, but with a having leaf of weight w1+w2.

w(T2*)=w(T2)-w1l1 - w2l1 +(w1+w2)(l1-1)

Thus w(T2)=w(T2*)+w1+w2

Let T* be a same tree as T without leaf with w1+w2 but with subtree

Suppose that T* is not an optimal tree.

Page 15: Rooted tree and binary tree

Theorem 5.22:Proof: Let us apply induction on the number n of vertices.

n=2,

The results holds Suppose that result holds for n=k-1For n=k , By the inductive hypothesis, the tree with weight w1+w2,w3,,wk by according to Huffman algorithm is an optimal tree .By lemma 5.2, this is an optimal tree with weight w1,w2,w3,,wk if T’s leaf with weight w1+w2 is replaced by subtree

Page 16: Rooted tree and binary tree

5.7 Transport Networks

5.7.1 Transport Networks Definition 33: A transport network or a network, is

a connected digraph N(V,E,C) with the following properties:

(1)N has no loop. (2)There is a unique node s, the source, that has in-

degree 0. And there is a unique node t, the sink, that has out-degree 0.

(3)The graph is labeled. The label, cij on edge (i,j) is a nonnegative number called the capacity of the edge. Let C={cij |(i,j)E}.

Page 17: Rooted tree and binary tree

Definition 34: A flow in a network N(V,E,C) is a function that assigns to each edge (i,j) of N a nonnegative number fij that does not exceed cij. A conservation flow is a flow with the properties:

for each vertex other than the source and sink. The sum

is called the value of the conservation flow. A conservation flow f is called maximum flow, if vf ≥vf’ for any conservation flow f ’ of N.

Vj

jkVi

ki ff

fVj

jtVi

si Vff

Page 18: Rooted tree and binary tree
Page 19: Rooted tree and binary tree

Definition 35: Let N be a network with the source node and the sink node. If P is a subset of V containing source s but not sink t then E(P,V-P)is called a cut separating s from t.

In effect, a cut does “cut” a digraph into two pieces, one containing the source and one containing the sink. If the edges of a cut were removed, it has not any paths from source to sink.

Page 20: Rooted tree and binary tree

The capacity of a cut E(P,V-P)is , we

denote by C(P,V-P). i.e.

PVjPiijcPVPC

,

),(

PVjPiijc

,

Page 21: Rooted tree and binary tree

A cut E(P,V-P) of N is called minimum cut, if C(P,V-P)≤C(P',V-P') for any cut E(P',V-P') of N.

Theorem 5.23: For every conservation flow f and any cut E(P,V-P), the result holds: Vf

C(P,V-P).

Vf C(P,V-P), Vfmax

Cmin(P,V-P)

Page 22: Rooted tree and binary tree

Lemma 5.3: Let f be a conservation flow, E(P,V-P) be a cut. If Vf=C(P,V-P) , then Vfmax

=Vf , Cmin(P,V-P)=C(P,V-P).

Proof: By the theorem 5.23, Frod,Falkerson 1956

Page 23: Rooted tree and binary tree

Next: Transport Networks, 8.4 P307 Graph Matching 8.5 P315

Exercise: P259 23 1. Let T be a full m-ary tree. We denote the sum of

length of simple paths from root to all internal vertices by I, and the sum of length of simple paths from root to all leaves by E. Then E=(m-1)I+2i, (m-1)i=t-1, where i is the number of internal vertices and t is the number of leaves

2.(1)Find a optimal binary tree with weight 1,3,8,9,12, 15,16

(2)Construct a algorithms for optimal 3-ary tree (3)Find a optimal 3-ary tree with weight 1,2,3,4,5,6,7,8,9 (4)Find a optimal 3-ary tree with weight 1,2,3,4,5,6,7,8,