discrete mathematics - 國立臺灣大學d92005/dm/ch10.pdfdiscrete mathematics chapter-10 trees...

69
Discrete Mathematics Chapter-10 Trees

Upload: others

Post on 01-Jun-2020

11 views

Category:

Documents


0 download

TRANSCRIPT

  • Discrete Mathematics

    Chapter-10Trees

  • Introduction to Tree (§10.1)Def 1. A connected (undirected) graph that contains no simple circuits is called a tree.Trees are particularly useful in computer science, where they are employed in a wide range of 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.

  • 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)

    The Bernoulli Family of Mathematicians

    ΙΙΙ Jacob(1759 - 1789)

    ΙΙ

  • EXAMPLE2 Which of the graphs shown below are trees ?

    a b

    d

    f

    c

    e

    1G 2G

    b

    cd

    f

    3G 4G

    bb

    cdd

    ef f

    a aa

    c

    e e

    Tree Tree Not a Tree Not a Tree

  • Theorem 1An 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 Tcontains 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 Thas no circuit, and then T is a tree.

  • 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

    ae

    A root treewith root aA Tree

    a

    ab

    b

    c d

    def

    f

    g

    g

    c

    e

    A root treewith root c

  • Terminology

    AncestorsDescendantsparentChildSiblingsLeafInternal vertexSubtree

  • Terminology

    a

    b

    c

    d e

    fg

    ij

    k m

    a is the parent of f, b and gj is the parent of l and mf is the child of ae is the child of cf, b and g are siblingse and d are siblings

    h

    l

    A Rooted Tree T

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

    leaves: d, e, f, k, i, l, minternal vertices: others

    b

    d

    c

    e

    subtree of b

    subtree of g

  • Def 3A rooted tree is called an m-ary tree if every interval vertex has no more than m children.The tree is called a full m-ary tree if every interval vertex has exactly m children.An m-ary tree with m = 2 is called a binary tree.

  • Example 3

    1T 2T

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

  • 3T 4T

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

  • More TerminologyAn ordered rooted tree is a rooted tree where the children of each internal vertex are ordered (from left to right).In an ordered binary tree (usually called just a binary tree), if an internal vertex has two children, the first child is called the left child and the second one is called the right child.The tree rooted at the left (right) child of a vertex is called the left (right) subtree of this vertex.

  • Example 4

    a

    b c

    de

    f g j

    h i

    m

    kl

    A Binary Tree T

    The Left Subtree of the Vertex c.

    h

    j

    The Right Subtree of the Vertex c.

    i

    kl

    m

  • H

    H

    H

    H

    H

    H

    H

    H

    H

    H

    C

    C

    C

    C

    H H H

    H

    HH H

    H

    H

    H

    C C C

    C

    The Two Isomers of ButaneButane

    Isobutane

    Trees as Models

  • Representing Organizations

    President

    VPR&D

    VPMarketing

    VPFinance

    VPServices

    DirectorResourch

    DirectorSoftware

    Development

    DirectorSoftware

    Development

    AVPSales

    AVPMarketing

    ChiefField

    Operations

    ChiefField

    Operations

    DirectorAccounting

    DirectorMIS

  • Theorem 2

    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 n−1 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 kedges. Then P(k+1) is true. ♦

  • Theorem 3

    1T 2T

    A full binary treem = 2, i = 3, 7 vertices.

    A full 3-ary treem = 3, i = 4, 13 vertices

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

  • 3T 4T

    A full 5-ary treem = 5, i = 3, 16 vertices Not a full m-ary tree

    Not satisfy the theorem

  • Example 9A 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 chainletter 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.

  • Theorem 4

    A full m-ary tree with1. n vertices has i = (n −1)/m internal vertices and

    l = [(m−1)n + 1]/m leaves,2. i interval vertices has n = mi + 1 vertices and

    l = (m−1)i + 1 leaves , 3. l leaves has n = (ml−1)/(m−1) vertices and

    i = (l−1)/(m−1) internal vertices.

  • More TerminologyThe 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.

    A rooted m-ary tree of height h is balanced if all leaves are at levels h or h−1.

  • Example

    a

    b

    ef

    i

    j

    l

    m n

    Level

    c

    dg

    h

    k

    Vertices0 a1 b, j, k2 c, e, f, l3 d, g, i, m, n4 h

    The height of this rooted tree is 4.

  • Example

    1T 2T

    Balanced Not balanced

  • 3TBalanced

  • 10.2 Applications of Trees

  • IntroductionHow should items in a list be stored so that an item can be easily located ?What series of decisions should be made to find an object with a certain property in a collection of objects of a certain type ?How should a set of characters be efficiently coded by bit strings ?

  • Binary Search Tree簡介:二元搜尋樹是一種二元樹。它可能是空的,若不是空的,它具有下列特性:

    (1) 每一個元素有一鍵值,而且每一元素的鍵值都不相同,即每一個鍵值都是唯一的。

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

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

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

  • Example1Form a binary search tree for the words

    mathematicphysicsgeography zoology meteorology geology psychology chemistry (using alphabetical order)

  • Constructing a Binary Search Tree按照題目所給的字順序排,如題Mathematics就是root接下Physic>Mathematics,所以在M右下加一個childGeographyMathematics且>Physics,所以再P右下加一個childMeteorology>Mathematics但

  • GeologyGeography,所以在Geography的右下加一個childPsychology>Mathematics且>Physics但

  • Huffman Codes簡介:Huffman 編制程式是試圖減少相當數量位元必需代表標誌串的一個統計技術。

    修造Huffman 樹(1)選擇二個parentless 結點以最低的可能性。(2)創造是二個最低的可能性結點的父母的一個新結點。(3) 分配新結點可能性相等與它的孩子的可能性的總和。(4)重覆步驟1 直到有只一個parentless 結點剩下

  • Example 5Use Huffman coding to encode the fallowing symbols with the frequencies listed: A:0.08, B:0.10, C:0.12, D:0.15, E:0.20, F:0.35. What is the average number of bits used to encode a character ?

  • 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

  • 9.3 Tree Traversal

  • Universal address system

    We will describe one way to order totally thevertices of an ordered rooted tree. To produce thisordering, we must first label all the vertices. We dothis recursively :1. Label the root with the integer 0. Then label its k children (at label 1) from left to right with 1,2,...,k .

    2. For each vertex v at level n with label A, label itsk v children, as they are drawn from left to right, with A.1,A.2,…A.k v .

  • Example 1

    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 is0 < 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

  • 0

    1 2 43 5

    1.1 1.2 1.33.1

    3.2 4.1 5.1 5.2 5.3

    3.1.33.1.2

    3.1.1

    3.1.2.1 3.1.2.2 3.1.2.3 3.1.2.4

    5.1.1

  • Def. 1

    Let T be an order rooted tree with root r . If T consistsonly of r , then r is the preorder traversal of T .Otherwise , suppose that T1 , T2 , … , Tn are the subtreeat r from left to right in T . The preorder traversalbegins by visiting r . It continues by traversing T1 in preorder , then T2 in preorder , and so on , until Tn is traversed in preorder.

  • Example 2In which order does a preorder traversal visit the vertices in the ordered rooted tree T shown below.

    a

    bc

    d

    ef

    g

    h i

    jk

    l m

    n o p

  • a b e f

    j

    k

    n o p

    c d g h i

    l m

    a k n o p f c d g l m h ijb e

    The preorder traversal of T is a , b , e , j , k , n , o , p , f , c , d , g , l , m , h , i .

  • Let T be an ordered rooted tree with root r . If T consists Only of r , then r is the inorder traversal of T.Otherwise , suppose that T1 , T2 , … , Tn are the subtree at r from left to right in T . The inordertraversal begins by traversing T1 in inorder , then visiting r . It continues by traversing T2 in inorder , then T3 in inorder ,…, and Finally Tn in inorder.

    Def. 2

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

    a

    bc

    d

    ef

    g

    h i

    jk

    l m

    n o p

  • a cb

    f

    d

    g

    h i

    l m

    e

    jk

    n o p

    ab c df g h il mej 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 .

  • Let T be an ordered rooted tree with root r . If T Consists only of r , then r is the postrder traversal of T. Otherwise , suppose that T1 , T2 , … , Tn are the subtreesat r from left to right in T . The postordertraversal begins by traversing T1 in postorder , then T2 in inorder , then T3 in inorder ,…, and ends by visiting r .

    Def. 3

  • Example 4

    a

    bc

    d

    ef

    g

    h i

    jk

    l m

    n o p

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

  • ad

    g

    h i

    l m

    b c

    fe

    jk

    n o p

    ab c df g h il mej 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 .

  • Infix, Prefix, and Postfix notationExample 5

    what is the ordered rooted tree that represents theexpression((x+y)↑2)+((x-4)/3)?

  • +

    x y x

    _

    4

    + 2

    x y x

    _

    4

    3

    / ↑

    + 2

    x y x

    _

    4

    3

    /

    +

  • + 2

    x y x

    _

    4

    3

    /

    +

    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)?

    This produces the prefix expression : +↑+ x y 2 / - x 4 3This produces the postefix expression : x y + 2 ↑ x 4 -3 / +

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

    + - * 2 3 5 / 8 4

    + - * 2 3 5 2

    + - 6 5 2

    + 1 2

    3

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

  • 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 / + ?

  • 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 thisexpression.

    ^

    p q

    ┐ ┐

    p q

    ^

    p q

    ┐ ┐

    p q

    ˇ

    ^

    p q

    ┐ ┐

    p q

    ˇ

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

  • 10.4 Spanning Trees (§9.4)

    Let G be a simple graph. A spanning tree of G is a subgraph of G that is a tree containing every vertex of G.Spanning Tree of a graph is not unique.

  • TheoremA 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. Tcontains 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.

  • 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 ef

    gh

    b

    d i j

    k

    f

    ec

    d

    a b

    gh

    kj

    i

  • Algorithm: Depth-First Searchprocedure DFS(G; connected graph with vertices v1, v2, …, vn)T := tree consisting only of the vertex vivisit(vi)procedure visit(v: vertex of G)for each vertex w adjacent to v and not yet in Tbegin

    add vertex w and edge {v, w} to Tvisit(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.

  • ExampleHow can backtracking be used to decide whether a given graph can be colored using 3 colors?

    a b c

    de a 1

    b

    a b c d e

    Spanning Tree

    2

    d

    1

    3

    c c

    eno way e 3

    3

    d 1

    Done

  • Breadth-First Search

    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 fi

    j

    k

    l

    m

    e g

    h

    f

    c

    i

    e j g

    b d la h

    k

    m

  • Algorithm: Breadth-First Searchprocedure BFS(G; connected graph with vertices v1, v2, …, vn)T := tree consisting only of the vertex viL := empty listput v1 in the list L of unprocessed verticeswhile L is not emptybegin

    remove the first vertex , v, from Lfor each neighbor w of v

    if w is not in L and not in T thenbegin

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

    endend

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

  • DFS in Directed Graphsa b c d

    e f

    i j k l

    gh

    a

    bc

    gf

    e

    d

    h

    lk

    j

    i•

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

  • 10.5 Minimum Spanning TreesA minimum spanning tree in a (weighted) connected graph is a spanning tree that has the smallest possible sum of weights of its edges.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.

  • Algorithm Prim’s Algorithmprocedure Prim(G: weighted connected undirected graph with n vertices)T := a minimum-weight edgefor i := 1 to n − 2begin

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

    end (T is a minimum spanning tree of G)

  • Example

    San Francisco

    Chicago

    Atlanta

    Denver

    $2000

    $1200

    $1000

    $2200

    $900

    $1400

    $130

    0

    $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

  • Examplea b c d

    f g h

    i j k l

    2 3

    3 3

    33

    3

    32

    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

  • Algorithm Kruskal’s Algorithmprocedure Kruskal(G: weighted connected undirected graph with n vertices)T := empty treefor i := 1 to n − 1begin

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

    end (T is a minimum spanning tree of G)

  • Example

    San Francisco

    Chicago

    Atlanta

    Denver

    $2000

    $1200

    $1000

    $2200

    $900

    $1400

    $130

    0

    $800

    $700$1600

    New York

    700 (Chicago, Atlanta)800 (Atlanta, New York)900 (Denver, San Francisco)1200 (San Francisco, Chicago) Total cost = $3600

  • Examplea b c d

    f g h

    i j k l

    2 3

    3 3

    33

    3

    32

    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

    Discrete �MathematicsIntroduction to Tree (§10.1)The Bernoulli Family of MathematiciansEXAMPLE2 Which of the graphs shown below are trees ?Theorem 1TerminologyTerminologyDef 3Example 3More TerminologyExample 4Representing OrganizationsTheorem 2Theorem 3Example 9Theorem 4More TerminologyExampleExample10.2 Applications of Trees�IntroductionBinary Search TreeExample1Constructing a Binary Search TreeHuffman CodesExample 5Example 59.3Tree TraversalUniversal address systemExample 1Def. 1Example 2Def. 2Example 3Def. 3Example 4Infix, Prefix, and Postfix notation10.4 Spanning Trees (§9.4)TheoremDepth-First SearchAlgorithm: Depth-First SearchExampleBreadth-First SearchAlgorithm: Breadth-First SearchDFS in Directed Graphs10.5 Minimum Spanning TreesAlgorithm Prim’s AlgorithmExampleExampleAlgorithm Kruskal’s AlgorithmExampleExample