Discrete Mathematics

NOR AIDAWATI BINTI ABDILLAH


         TREES

                              1
Tree

Definition 1. A tree is a connected
 undirected graph with no simple circuits.

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



                                              2
Which graphs are trees?
a)                 b)




       c)




                               3
Is it a tree?


                          NO!
yes!                 All the nodes are         yes! (but not
                      not connected             a binary tree)




            NO!
       There is a cycle                  yes! (it’s actually
         and an extra                    the same graph as
        edge (5 nodes                    the blue one) – but
        and 5 edges)                       usually we draw
                                          tree by its “levels”4
Rooted Trees

q   Rooted tree is a tree in which one vertex is
    distinguished and called a root
q   Level of a vertex is the number of edges
    between the vertex and the root
q   The height of a rooted tree is the
    maximum level of any vertex
q   Children, siblings and parent vertices in a
    rooted tree
q   Ancestor, descendant relationship
    between vertices
                                               5
q   The parent of a non-root vertex is the unique vertex
    u with a directed edge from u to v.
q   A vertex is called a leaf if it has no children.
q   The ancestors of a non-root vertex are all the
    vertices in the path from root to this vertex.
q   The descendants of vertex v are all the vertices that
    have v as an ancestor.
q   The level of vertex v in a rooted tree is the length of
    the unique path from the root to v.
q   The height of a rooted tree is the maximum of the
                                                     6
    levels of its vertices.
A Tree Has a Root
                                  root node
                          a
internal vertex                               parent of g
          b                             c


    d             e           f             g

   leaf
                                     siblings
              h       i
                                                            7
a


    b                             c


d           e            f            g

                        ancestors of h and i
        h       i
The level of a vertex v in a rooted tree is
the length of the unique path from the root
to this vertex.

          level 2

     level 3
a


    b                              c


d           e           f              g

                            subtree with b as its
        h       i           root
                            subtree with c as its
                            root
Tree Properties

q   There is one and only one path between
    every pair of vertices in a tree, T.
q   A tree with n vertices has n-1 edges.
q   Any connected graph with n vertices and n-1
    edges is a tree.
q   A graph is a tree if and only if it is minimally
    connected.                                   11
Tree Properties

Theorem . There are at most 2 H leaves in a
  binary tree of height H.

Corallary. If a binary tree with L leaves is
 full and balanced, then its height is
              H =  log2 L  .


                                               12
Properties of Trees

There are at most mh leaves in an m-ary
tree of height h.
A rooted m-ary tree of height h is called
balanced if all leaves are at levels h or h-1.
Properties of Trees

 A full m-ary tree with
(i) n vertices has i = (n-1)/m internal
vertices and l = [(m-1)n+1]/m leaves.
(ii) i internal vertices has n = mi + 1
vertices and l = (m-1)i + 1 leaves.
(iii) l leaves has n = (ml - 1)/(m-1) vertices
and i = (l-1)/(m-1) internal vertices.
Properties of Trees

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

q We know n = mi+1 (previous
  theorem) and n = l+i,
q n – no. vertices
q i – no. internal vertices
q l – no. leaves
q For example, i = (n-1)/m
Binary Tree
A rooted tree in which each vertex has either
no children, one child or two children.
 The tree is called a full binary tree if every
internal vertex has exactly 2 children.
                               A



                       B               C right child of A

left subtree
        of A       D       E       F       G
                                                   right
                                                   subtree of
               H                                   C            17
                                       I       J
Ordered Binary Tree

Definition 2’’. An ordered rooted tree is a
 rooted tree where the children of each
 internal vertex are ordered.

  In an ordered binary tree, the two
  possible children of a vertex are called
  the left child and the right child, if they
  exist.

                                                18
An Ordered Binary Tree

                 A
             B
     E               C

K        F       G       D

     L               H

                 M           I

                                 J
                                 19
Is this binary tree balanced?
                     A rooted binary tree of height
               Lou   H is called balanced if all its
                     leaves are at levels H or H-1.

       Hal           Max

Ed           Ken              Sue

         Joe                         Ted
                                              20
Searching takes time . . .
So the goal in computer programs is to find
 any stored item efficiently when all stored
 items are ordered.

A Binary Search Tree can be used to store
  items in its vertices. It enables efficient
  searches.



                                                21
A Binary Search Tree (BST) is . . .
A special kind of binary tree in which:
1. Each vertex contains a distinct key value,
2. The key values in the tree can be compared using
   “greater than” and “less than”, and
3. The key value of each vertex in the tree is
   less than every key value in its right subtree, and
   greater than every key value in its left subtree.
A Binary Expression Tree is . . .
A special kind of binary tree in which:
l   Each leaf node contains a single operand,
l   Each nonleaf node contains a single binary
    operator, and
l   The left and right subtrees of an operator
    node represent subexpressions that must be
    evaluated before applying the operator at
    the root of the subtree.

                                                23
Expression Tree
q   Each node contains an
    operator or an operand
q   Operands are stored in
    leaf nodes
q   Parentheses are not stored (x + y)*((a + b)/c)
    in the tree because the tree structure
    dictates the order of operand evaluation
q   Operators in nodes at higher levels are
    evaluated after operators in nodes at lower
    levels
                                                24
A Binary Expression Tree

                             ‘*’


                       ‘+’         ‘3’


                 ‘4’     ‘2’

What value does it have?

( 4 + 2 ) * 3 = 18
                                         25
A Binary Expression Tree

                              ‘*’


                        ‘+’         ‘3’


                  ‘4’     ‘2’

Infix:      ((4+2)*3)
Prefix:     * + 4 2 3                evaluate from right
Postfix:    4 2 + 3 *                evaluate from left
                                                           26
Levels Indicate Precedence
When a binary expression tree is used to
 represent an expression, the levels of the
 nodes in the tree indicate their relative
 precedence of evaluation.

Operations at higher levels of the tree are
 evaluated later than those below them.
 The operation at the root is always the
 last operation performed.

                                              27
Evaluate
this binary expression tree
                    ‘*’


        ‘-’                       ‘/’


 ‘8’          ‘5’          ‘+’          ‘3’


                     ‘4’         ‘2’

       What expressions does it represent?
                                              28
A binary expression tree
                            ‘*’

               ‘-’                         ‘/’

         ‘8’         ‘5’           ‘+’           ‘3’

                             ‘4’         ‘2’

Infix:         ((8-5)*((4+2)/3))
Prefix:        *-85 /+423          evaluate from right
Postfix:       85- 42+3/*          evaluate from left
                                                        29
Binary Tree for Expressions




                              30
Complete Binary Tree
Also be defined as a full binary tree in which all
leaves are at depth n or n-1 for some n.

In order for a tree to be the latter kind of complete
binary tree, all the children on the last level must
occupy the leftmost spots consecutively, with no
spot left unoccupied in between any two
                   A                                           A


           B                                   B                               C
                           C

       D               F       G       D               E               F               G
               E

                                   H       I       J       K       L       M       N       O
   H       I                                                                   31
    Complete binary tree               Full binary tree of depth 4
Difference between binary and
        complete binary tree
     BINARY TREE ISN'T NECESSARY THAT ALL OF
          LEAF NODE IN SAME LEVEL BUT
    COMPLETE BINARY TREE MUST HAVE ALL LEAF
               NODE IN SAME LEVEL.




Binary Tree

                                           32
    Complete Binary Tree
SPANNING TREES

q   A spanning tree of a connected graph G is
    a sub graph that is a tree and that includes
    every vertex of G.
q   A minimum spanning tree of a weighted
    graph is a spanning tree of least weight
    (the sum of the weights of all its edges is
    least among all spanning tree).
q   Think: “smallest set of edges needed to
    connect everything together”
                                               33
A graph G and three of its
spanning tree


  We can delete any edge without deleting any vertex (to remove
            the cycle), but leave the graph connected.




                                                           34
PRIM’S ALGORITHM

q   Choose any edge with smallest weight,
    putting it into the spanning tree.
q   Successively add to the tree edges of
    minimum weight that are incident to a
    vertex already in the tree and not forming a
    simple circuit with those edges already in
    the tree.
q   Stop when n – 1 edges have been added.

                                              35
EXAMPLE
         PRIM’S ALGORITHM
Use Prim’s algorithm to find a minimum spanning tree
in the weighted graph below:

            a   2       b   3       c   1   d

         3          1           2               5
        e       4   f       3   g       3       h

         4          2           4               3

            i   3       j   3       k   1   l       36
SOLUTION

                                             Choice   Edge      Weight
                                               1      {b, f}      1
    a   2       b           c    1   d         2      {a, b}      2
                                               3      {f, j}      2
3           1           2                      4      {a, e}      3
            f       3   g        3       h     5       {i, j}     3
e
                                               6      {f, g}      3
            2                            3     7      {c, g}      2
                                               8      {c, d}      1
    i           j                              9      {g, h}      3
        3                   k    1   l
                                              10      {h, l}      3
                                              11      {k, l}      1
                                                      Total:      24
                                                                 37
KRUSKAL’S ALGORITHM

q Choose an edge in the graph with
  minimum weight.
q Successively add edges with minimum
  weight that do not form a simple circuit
  with those edges already chosen.
q Stop after n – 1 edges have been
  selected.

                                             38
Kruskal’s Algorithm

q Pick the cheapest link (edge)
  available and mark it
q Pick the next cheapest link available
  and mark it again
q Continue picking and marking link
  that does not create the circuit

***Kruskal’s algorithm is efficient and
  optimal                                 39
EXAMPLE
     KRUSKAL’S ALGORITHM
Use Kruskal’s algorithm to find a minimum spanning
tree in the weighted graph below:

            a   2       b   3       c   1   d

         3          1           2               5
        e       4   f       3   g       3       h

         4          2           4               3

            i   3       j   3       k   1   l       40
SOLUTION

                                             Choice   Edge      Weight
                                               1      {c, d}      1
                                               2      {k, l}      1
    a   2       b   3       c   1    d         3      {b, f}      1
                                               4      {c, g}      2
3           1           2                      5      {a, b}      2
            f           g       3        h     6      {f, j}      2
e
                                               7      {b, c}      3
            2                                  8      {j, k}      3
                                               9      {g, h}      3
    i           j   3                         10       {i, j}     3
        3                   k    1   l
                                              11      {a, e}      3
                                                      Total:     24
                                                                  41
TRAVELLING SALESMAN
       PROBLEM (TSP)

The goal of the Traveling Salesman Problem
(TSP) is to find the “cheapest” tour of a select
number of “cities” with the following
restrictions:

●
  You must visit each city once and only once
●
  You must return to the original starting point



                                              42
TSP
TSP is similar to these variations of Hamiltonian Circuit
  problems:

   ●
       Find the shortest Hamiltonian cycle in a
       weighted graph.
   ●
       Find the Hamiltonian cycle in a weighted
       graph with the minimal length of the
       longest edge. (bottleneck TSP).
   A route returning to the beginning is known as a
                  Hamiltonian Circuit

   A route not returning to the beginning is known as a
                                                          43
                    Hamiltonian Path
THANK YOU


            44

Trees

  • 1.
    Discrete Mathematics NOR AIDAWATIBINTI ABDILLAH TREES 1
  • 2.
    Tree Definition 1. Atree is a connected undirected graph with no simple circuits. Theorem 1. An undirected graph is a tree if and only if there is a unique simple path between any two of its vertices. 2
  • 3.
    Which graphs aretrees? a) b) c) 3
  • 4.
    Is it atree? NO! yes! All the nodes are yes! (but not not connected a binary tree) NO! There is a cycle yes! (it’s actually and an extra the same graph as edge (5 nodes the blue one) – but and 5 edges) usually we draw tree by its “levels”4
  • 5.
    Rooted Trees q Rooted tree is a tree in which one vertex is distinguished and called a root q Level of a vertex is the number of edges between the vertex and the root q The height of a rooted tree is the maximum level of any vertex q Children, siblings and parent vertices in a rooted tree q Ancestor, descendant relationship between vertices 5
  • 6.
    q The parent of a non-root vertex is the unique vertex u with a directed edge from u to v. q A vertex is called a leaf if it has no children. q The ancestors of a non-root vertex are all the vertices in the path from root to this vertex. q The descendants of vertex v are all the vertices that have v as an ancestor. q The level of vertex v in a rooted tree is the length of the unique path from the root to v. q The height of a rooted tree is the maximum of the 6 levels of its vertices.
  • 7.
    A Tree Hasa Root root node a internal vertex parent of g b c d e f g leaf siblings h i 7
  • 8.
    a b c d e f g ancestors of h and i h i
  • 9.
    The level ofa vertex v in a rooted tree is the length of the unique path from the root to this vertex. level 2 level 3
  • 10.
    a b c d e f g subtree with b as its h i root subtree with c as its root
  • 11.
    Tree Properties q There is one and only one path between every pair of vertices in a tree, T. q A tree with n vertices has n-1 edges. q Any connected graph with n vertices and n-1 edges is a tree. q A graph is a tree if and only if it is minimally connected. 11
  • 12.
    Tree Properties Theorem .There are at most 2 H leaves in a binary tree of height H. Corallary. If a binary tree with L leaves is full and balanced, then its height is H =  log2 L  . 12
  • 13.
    Properties of Trees Thereare at most mh leaves in an m-ary tree of height h. A rooted m-ary tree of height h is called balanced if all leaves are at levels h or h-1.
  • 14.
    Properties of Trees A full m-ary tree with (i) n vertices has i = (n-1)/m internal vertices and l = [(m-1)n+1]/m leaves. (ii) i internal vertices has n = mi + 1 vertices and l = (m-1)i + 1 leaves. (iii) l leaves has n = (ml - 1)/(m-1) vertices and i = (l-1)/(m-1) internal vertices.
  • 15.
    Properties of Trees Afull m-ary tree with i internal vertices contains n = mi+1 vertices.
  • 16.
    Proof q We known = mi+1 (previous theorem) and n = l+i, q n – no. vertices q i – no. internal vertices q l – no. leaves q For example, i = (n-1)/m
  • 17.
    Binary Tree A rootedtree in which each vertex has either no children, one child or two children. The tree is called a full binary tree if every internal vertex has exactly 2 children. A B C right child of A left subtree of A D E F G right subtree of H C 17 I J
  • 18.
    Ordered Binary Tree Definition2’’. An ordered rooted tree is a rooted tree where the children of each internal vertex are ordered. In an ordered binary tree, the two possible children of a vertex are called the left child and the right child, if they exist. 18
  • 19.
    An Ordered BinaryTree A B E C K F G D L H M I J 19
  • 20.
    Is this binarytree balanced? A rooted binary tree of height Lou H is called balanced if all its leaves are at levels H or H-1. Hal Max Ed Ken Sue Joe Ted 20
  • 21.
    Searching takes time. . . So the goal in computer programs is to find any stored item efficiently when all stored items are ordered. A Binary Search Tree can be used to store items in its vertices. It enables efficient searches. 21
  • 22.
    A Binary SearchTree (BST) is . . . A special kind of binary tree in which: 1. Each vertex contains a distinct key value, 2. The key values in the tree can be compared using “greater than” and “less than”, and 3. The key value of each vertex in the tree is less than every key value in its right subtree, and greater than every key value in its left subtree.
  • 23.
    A Binary ExpressionTree is . . . A special kind of binary tree in which: l Each leaf node contains a single operand, l Each nonleaf node contains a single binary operator, and l The left and right subtrees of an operator node represent subexpressions that must be evaluated before applying the operator at the root of the subtree. 23
  • 24.
    Expression Tree q Each node contains an operator or an operand q Operands are stored in leaf nodes q Parentheses are not stored (x + y)*((a + b)/c) in the tree because the tree structure dictates the order of operand evaluation q Operators in nodes at higher levels are evaluated after operators in nodes at lower levels 24
  • 25.
    A Binary ExpressionTree ‘*’ ‘+’ ‘3’ ‘4’ ‘2’ What value does it have? ( 4 + 2 ) * 3 = 18 25
  • 26.
    A Binary ExpressionTree ‘*’ ‘+’ ‘3’ ‘4’ ‘2’ Infix: ((4+2)*3) Prefix: * + 4 2 3 evaluate from right Postfix: 4 2 + 3 * evaluate from left 26
  • 27.
    Levels Indicate Precedence Whena binary expression tree is used to represent an expression, the levels of the nodes in the tree indicate their relative precedence of evaluation. Operations at higher levels of the tree are evaluated later than those below them. The operation at the root is always the last operation performed. 27
  • 28.
    Evaluate this binary expressiontree ‘*’ ‘-’ ‘/’ ‘8’ ‘5’ ‘+’ ‘3’ ‘4’ ‘2’ What expressions does it represent? 28
  • 29.
    A binary expressiontree ‘*’ ‘-’ ‘/’ ‘8’ ‘5’ ‘+’ ‘3’ ‘4’ ‘2’ Infix: ((8-5)*((4+2)/3)) Prefix: *-85 /+423 evaluate from right Postfix: 85- 42+3/* evaluate from left 29
  • 30.
    Binary Tree forExpressions 30
  • 31.
    Complete Binary Tree Alsobe defined as a full binary tree in which all leaves are at depth n or n-1 for some n. In order for a tree to be the latter kind of complete binary tree, all the children on the last level must occupy the leftmost spots consecutively, with no spot left unoccupied in between any two A A B B C C D F G D E F G E H I J K L M N O H I 31 Complete binary tree Full binary tree of depth 4
  • 32.
    Difference between binaryand complete binary tree BINARY TREE ISN'T NECESSARY THAT ALL OF LEAF NODE IN SAME LEVEL BUT COMPLETE BINARY TREE MUST HAVE ALL LEAF NODE IN SAME LEVEL. Binary Tree 32 Complete Binary Tree
  • 33.
    SPANNING TREES q A spanning tree of a connected graph G is a sub graph that is a tree and that includes every vertex of G. q A minimum spanning tree of a weighted graph is a spanning tree of least weight (the sum of the weights of all its edges is least among all spanning tree). q Think: “smallest set of edges needed to connect everything together” 33
  • 34.
    A graph Gand three of its spanning tree We can delete any edge without deleting any vertex (to remove the cycle), but leave the graph connected. 34
  • 35.
    PRIM’S ALGORITHM q Choose any edge with smallest weight, putting it into the spanning tree. q Successively add to the tree edges of minimum weight that are incident to a vertex already in the tree and not forming a simple circuit with those edges already in the tree. q Stop when n – 1 edges have been added. 35
  • 36.
    EXAMPLE PRIM’S ALGORITHM Use Prim’s algorithm to find a minimum spanning tree in the weighted graph below: a 2 b 3 c 1 d 3 1 2 5 e 4 f 3 g 3 h 4 2 4 3 i 3 j 3 k 1 l 36
  • 37.
    SOLUTION Choice Edge Weight 1 {b, f} 1 a 2 b c 1 d 2 {a, b} 2 3 {f, j} 2 3 1 2 4 {a, e} 3 f 3 g 3 h 5 {i, j} 3 e 6 {f, g} 3 2 3 7 {c, g} 2 8 {c, d} 1 i j 9 {g, h} 3 3 k 1 l 10 {h, l} 3 11 {k, l} 1 Total: 24 37
  • 38.
    KRUSKAL’S ALGORITHM q Choosean edge in the graph with minimum weight. q Successively add edges with minimum weight that do not form a simple circuit with those edges already chosen. q Stop after n – 1 edges have been selected. 38
  • 39.
    Kruskal’s Algorithm q Pickthe cheapest link (edge) available and mark it q Pick the next cheapest link available and mark it again q Continue picking and marking link that does not create the circuit ***Kruskal’s algorithm is efficient and optimal 39
  • 40.
    EXAMPLE KRUSKAL’S ALGORITHM Use Kruskal’s algorithm to find a minimum spanning tree in the weighted graph below: a 2 b 3 c 1 d 3 1 2 5 e 4 f 3 g 3 h 4 2 4 3 i 3 j 3 k 1 l 40
  • 41.
    SOLUTION Choice Edge Weight 1 {c, d} 1 2 {k, l} 1 a 2 b 3 c 1 d 3 {b, f} 1 4 {c, g} 2 3 1 2 5 {a, b} 2 f g 3 h 6 {f, j} 2 e 7 {b, c} 3 2 8 {j, k} 3 9 {g, h} 3 i j 3 10 {i, j} 3 3 k 1 l 11 {a, e} 3 Total: 24 41
  • 42.
    TRAVELLING SALESMAN PROBLEM (TSP) The goal of the Traveling Salesman Problem (TSP) is to find the “cheapest” tour of a select number of “cities” with the following restrictions: ● You must visit each city once and only once ● You must return to the original starting point 42
  • 43.
    TSP TSP is similarto these variations of Hamiltonian Circuit problems: ● Find the shortest Hamiltonian cycle in a weighted graph. ● Find the Hamiltonian cycle in a weighted graph with the minimal length of the longest edge. (bottleneck TSP). A route returning to the beginning is known as a Hamiltonian Circuit A route not returning to the beginning is known as a 43 Hamiltonian Path
  • 44.