discrete mathematicscis.catholic.ac.kr/sunoh/courses/discretem/dmchapter7.pdf · 2017-11-21 ·...

Post on 12-Mar-2020

0 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Chapter 7. trees

Sanguk Noh

Discrete Mathematics

Table

Trees

Labeled Trees

Tree searching

Undirected trees

Minimal Spanning Trees

Trees

Theorem : Let (T, v0) be a rooted tree. Then,

There are no cycles in T.

v0 is the only root of T.

Each vertex in T, other than v0, has in-degree one. And v0 has in-

degree zero.

Trees

Notation

v0

v1 v2 v3

v6 v7 v8 v5 v4

The root

No edges enter v0.

parent

offspring ‘leaves’ (have no offspring) Siblings(the offspring of any one vertex)

Level 0

Level 1

Level 2

‘height’ of

the tree

* ‘ordered tree’: some ordering at each level by arranging offspring from left to right

Trees

Theorem : : Let (T, v0) be a rooted tree on a set A

T is irreflexive.

T is asymmetric.

If (a,b)∈T and (b,c) ∈T, then (a,c) ∉T for all a,b,c in A

Notation

v0

v1 v2 v3

At most n offspring : n-tree

All vertices of T have exactly

n offspring

Complete n-tree

* 2-tree : binary tree

Trees Theorem

If (T, v0) is a rooted tree and v∈T, then T(v) is also a rooted

tree with root v.

T(v): the subtree of T beginning at v.

e.g.) A={v1,v2,….,v10}

T={(v2, v3), (v2, v1), (v4, v5), (v4, v6), (v5, v8), (v6, v7),

(v4, v2), (v7, v9), (v7, v10)}

v4

v6 v2 v5

v8 v3 v1 v7

v9 v10

T(v5)

T(v2)

(T,v4): the tree T(v6)

Labeled Trees

The fully parenthesized, algebraic expression

(3-(2*x)) + ((x-2)-(3+x))

+

- -

3 * - +

2 x x 2 3 x

Central operator

*tree data structure. n-tree(T,v0)

Labeled Trees

Computer representation of binary trees

Doubly linked list. (searching a set of data in either direction)

data Left

pointer

Right

pointer Data

storage

Pointer to the next cell.

(an address where the next cell is located)

For a pointer specifying no additional date.

(0: the corresponding off spring does not exist)

Labeled Trees

e.g.)

∙ start

∙ ∙ + ∙ ∙ - ∙ ∙ + x

3

∙ ∙ -

3

1

2

3

4

8

5

Fig.7.10 p277

Index Left Data Right

1

2

3

4

5

6

7

8

2

3

4

0

6

0

0

9

+

-

3

*

2

x

-

0

8

5

0

7

0

0

12

Tree searching

Tree search : the process of visiting each vertex of a tree in

some specific order

Positional binary tree(labeled digraph)

(we place in its appropriate position each offspring that

actually occurs.)

The positions for potential offspring are labeled left and right.

L R

R

R

L

L R

* T : a binary positional tree with root v. (T,v)

T(VL) : the left subtree of T

T(VR): the right subtree of T

Tree searching

Preorder search Algorithm PREORDER(T,v)

print v // visit the root

if vL≠NIL

then PREORDER(T(vL),VL)

//search the left subtree

if vR≠NIL

then PREORDER(T(vR),VR)

//search the right subtree

end PREORDER

A

B H

C

D F

E I

G J L

K

The result of the complete search of T

A B

C E

D F G

A B C

D

C B D

Nil Nil

A C B D A

C B D A E A B C E F A B C D E G D F

G F Nil Nil Nil Nil ABCDEFG H

I K

L J

Tree searching e.g.) the prefix of the given algebraic expression by applying

PREORDER to the tree

Unambiguous expression w/o parentheses

*-ab+c/de Move from left to right Fxy

F: symbol (+,-,*,/,etc)

x&y: numbers

Continue this procedure until only one number remains.

Ex) *-64+5/22 =>12

2 1

6

12

*

-

a b

+

c /

d e

* -

a b

* - +

c /

d e

a b (a-b)*((c+(d/e))

Tree searching Inorder search

Postorder search

Algorithm INORDER(T,v)

if vL≠NIL

then INORDER(T(vL),VL) //search the left subtree

print v // visit the root

if vR≠NIL

then INORDER(T(vR),VR)//search the right subtree

end INORDER

Algorithm POSTORDER(T,v)

if vL≠NIL

then POSTORDER(T(vL),VL) //search the left subtree

if vR≠NIL

then POSTORDER(T(vR),VR) //search the right subtree

print v // visit the root

end POSTORDER

Tree searching

A

B H

C

D F

E I

G J L

K

*Inorder search

A B C D D C B E

F G D

nil nil

DCBFEGA….

*Postorder search

A B C

D

D C B

E

F G

E

F G

DCFGEB….

Tree searching e.g.) algebraic expression

1. infix a-b*c+d/e : ambiguous! 2. postfix ab-cde/+* a=6, b=4, c=5, d=2, e=2 2 522/+* 2 5 1+* 2 6* 12 ⇒ unambiguous

⇒ the method of evaluating expressions in some calculations

×

-

a b

+

c /

d e

Tree searching Searching General Trees

T : ordered tree

A : the set of vertices of T

B(T) : binary positional tree

If v∈A,

then the left offspring vL of v in B(T)←the first offspring of v in T

the right offspring vR of v in B(T)←the next sibling of v in T

e.g.) general tree: T corresponding binding positional tree: B(T)

1

2

5 6

4

9 10

3

7 8

11 12 13

1

2 3

4 8

9

10 5

6

7

13

12

11

Tree searching 7.3 Problem #2 Fig. 7.19

- preorder

- inorder

- postorder

Undirected trees Def.) Undirected tree:

the symmetric closure of a tree (bidirectional edges)

Undirected edge of T

The set {a,b}, where (a,b) and (b,a) are in T.

(a, b: adjacent vertices)

Def.) spanning tree

R : a connected, undirected relation on A

T : a spanning tree of R

If |A| vertices and |A|-1 edges, then the edges connect all the vertices.

Undirected trees

e.g.) a connected, undirected relation R

Spanning trees (not unique!) undirected spanning tree

a

b c

d e

f

a

b

d

f

e

c

b a c

d e

f f

d e

c

a

b

Undirected trees Algorithm Merging-process

// R : a relation on a set A

// a, b∈A

while (R′ is not undirected spanning tree)

A0=A-{a,b}

A′=A0∪{a′}, a′∉A

// R′ on A′ // u,v∈A′, where u≠a′, v≠a′ (a′, u)∈R′ iff (a,u)∈R or (b,u)∈R

(u, a′)∈R′ iff (u,a)∈R or (u,b)∈R

(u, v)∈R′ iff (u,v) ∈R

end while

End Merging-process

Undirected trees

e.g.) a symmetric relation

v1

v3

v4

v5

v0

v2

v6 v3 v4

v′0

v5

v2

v6

{v0, v1}

A0={v2, v3 , v4 , v5 , v6}

A′= A0∪{v′0}

={v′0, v2, v3 , v4 , v5 , v6}

{v′0, v2} A0={v3 , v4 , v5 , v6}

A′= A0∪{v′′0}

={v′′0, v3 , v4 , v5 , v6}

* Merging process

v′′0

v3 v4 v5

v6

Undirected trees

Matrix of R′(merging two vertices into a new vertex)

Step 1. row i-vertex a

row j-vertex b

replace row i by the join (either has a 1) of rows i and j

Step 2. Replace column i by the join of columns i and j

Step 3. Restore the main diagonal to its original values in R

(avoid cycle of length 1)

Step 4. Delete row j and column j

Undirected trees

v1

v3

v4

v5

v2

v6

v0

v0

v1

v2

v3

v4

v5

v6

v0

0

1

1

0

0

1

0

v1

1

0

0

1

1

0

0

v2

1

0

0

0

0

1

1

v3

0

1

0

0

0

0

0

v4

0

1

0

0

0

0

0

v5

1

0

1

0

0

0

0

v6

0

0

1

0

0

0

0

v′0

v2

v3

v4

v5

v6

v′0

0

1

1

1

1

0

v2

1

0

0

0

1

1

v3

1

0

v4

1

0

v5

1

1

v6

0

1

v′′0

v3

v4

v5

v6

v′′0 v3 v4 v5 v6

0 1 1 1 1

1

1

1

1

v3 v4

v′0

v5

v2

v6

v′′0

v3 v4 v5

v6

Undirected trees Algorithm Prim

//finding a spanning tree/

Do

Choose v1(∈R) and v1 is the 1st row in the matrix.

Choose v2 s.t. (v1, v2)∈R.

Merge v1 &v2 into a new vertex v′1 .

Compute the matrix of the resulting relation R′.

v′1←{v1, v2}

ST ←ST∪{(v1, v2)}

Until (|A|=1)

End Prim

// Single vertex is obtained

Undirected trees e.g.)

a

c

b

d c d

a b

root

<by deleting some edges of R>

a

b

c

d

a

0

0

1

1

b

0

0

1

1

c

1

1

0

0

d

1

1

0

0

a′ b

d

a′ 0

1

1

b

1

0

1

d

1

1

0

a′′ d

a′′ 0

1

a

1

0

a′′′ a′′′ 0

Merged vertices New vertex

-

a′←{a,c}

a′′←{a,c,b}

a′′′ ←{a,c,b,d}

c (a,c) a=root

b (c,b) or (a,b)

d (a,d) or

(c,d) or

(b,d)

-

Minimal Spanning Trees Minimal Spanning Trees

Def.) weighted graph

each edge is labeled with a numerical value (weight).

Def.) nearest neighbor of vertex

if vertices u and v are adjacent and the weight on the edge

is minimum.

Def.) minimal spanning tree

undirected spanning tree for which the total weight of the

edges in the tree is as small as possible.

Minimal Spanning Trees Algorithm MST-Prim

// R with n vertices

V←{v1}

E ←{ }

Do

vj∈V

vi ← nearest-neighbor(vj)

(vi, vj) does not form a cycle with members of E.

// whether the edge is “safe” or not

V ←V∪{vi}

E ←E ∪ {(vi,vj)}

Until (|E|=n-1)

End MST-Prim

Minimal Spanning Trees

The greedy strategy advocates making the choice that is the

best at the moment.

e.g.)

A C

B D H

E

F G

A

C

B D H

E

F

G

3

2 5 2

3

4

2

6

2 6

3 3

2 5

5 4 3

4

The root vertex : A

2

Minimal Spanning Trees Algorithm MST-Kruskal

// R with n vertices

// S={e1,e2,…ek}: the set of weighted edges

// e1 of least weight

E←{e1}

S←S-{e1}

Do

Select ei∈S of least weight that will not make a cycle.

E ←E∪{ei}

S ←S-{ei}

Until (|E|=n-1)

End MST-Kruskal

Minimal Spanning Trees

e.g.) Figure 7.49 & Fig 7.55

4

8

8 7 9

10

14

4

1 2

6

2

7

11

4

8

1 2

2

4

7

9 4

8 7 9 2 4

2 1

Mst-Kruskal Mst-Prim

top related