SlideShare ist ein Scribd-Unternehmen logo
1 von 72
Module #22 - Graphs



                           University of Florida
           Dept. of Computer & Information Science & Engineering

                          COT 3100
         Applications of Discrete Structures
                         Dr. Michael P. Frank

              Slides for a Course Based on the Text
             Discrete Mathematics & Its Applications
                            (5th Edition)
                      by Kenneth H. Rosen



11/06/12                  (c)2001-2003, Michael P. Frank 1
Module #22 - Graphs




                        Module #22:
                       Graph Theory

                      Rosen 5th ed., chs. 8-9
                           ~44 slides



11/06/12                (c)2001-2003, Michael P. Frank 2
Module #22 - Graphs



                      What are Graphs?                     Not


     • General meaning in everyday math:
       A plot or chart of numerical data using a
       coordinate system.
     • Technical meaning in discrete mathematics:
       A particular class of discrete structures (to
       be defined) that is useful for representing
       relations and has a convenient webby-
       looking graphical representation.

11/06/12                (c)2001-2003, Michael P. Frank 3
Module #22 - Graphs



               Applications of Graphs
     • Potentially anything (graphs can represent
       relations, relations can describe the
       extension of any predicate).
     • Apps in networking, scheduling, flow
       optimization, circuit design, path planning.
     • More apps: Geneology analysis, computer
       game-playing, program compilation, object-
       oriented design, …
11/06/12              (c)2001-2003, Michael P. Frank 4
Module #22 - Graphs



                      Simple Graphs
     • Correspond to symmetric,
       irreflexive binary relations R.
     • A simple graph G=(V,E)
                                    Visual Representation
       consists of:                   of a Simple Graph
         – a set V of vertices or nodes (V corresponds to
           the universe of the relation R),
         – a set E of edges / arcs / links: unordered pairs
           of [distinct] elements u,v ∈ V, such that uRv.

11/06/12               (c)2001-2003, Michael P. Frank 5
Module #22 - Graphs



           Example of a Simple Graph
     • Let V be the set of states in the far-
       southeastern U.S.:
         –I.e., V={FL, GA, AL, MS, LA, SC, TN, NC}
     • Let E={{u,v}|u adjoins v}
                                                         TN   NC
         ={{FL,GA},{FL,AL},{FL,MS},
           {FL,LA},{GA,AL},{AL,MS}, MS              AL        SC
           {MS,LA},{GA,SC},{GA,TN},
                                                               GA
           {SC,NC},{NC,TN},{MS,TN}, LA
                                                               FL
           {MS,AL}}
11/06/12              (c)2001-2003, Michael P. Frank 6
Module #22 - Graphs



                      Multigraphs
     • Like simple graphs, but there may be more
       than one edge connecting two given nodes.
     • A multigraph G=(V, E, f ) consists of a set V
       of vertices, a set E of edges (as primitive
       objects), and a function                    Parallel
       f:E→{{u,v}|u,v∈V ∧ u≠v}.                     edges

     • E.g., nodes are cities, edges
       are segments of major highways.
11/06/12              (c)2001-2003, Michael P. Frank 7
Module #22 - Graphs



                      Pseudographs
     • Like a multigraph, but edges connecting a node to
       itself are allowed. (R may be reflexive.)
     • A pseudograph G=(V, E, f ) where
       f:E→{{u,v}|u,v∈V}. Edge e∈E is a loop if
       f(e)={u,u}={u}.
     • E.g., nodes are campsites
       in a state park, edges are
       hiking trails through the
       woods.


11/06/12              (c)2001-2003, Michael P. Frank 8
Module #22 - Graphs



                      Directed Graphs
     • Correspond to arbitrary binary relations R,
       which need not be symmetric.
     • A directed graph (V,E) consists of a set of
       vertices V and a binary relation E on V.
     • E.g.: V = set of People,
       E={(x,y) | x loves y}



11/06/12               (c)2001-2003, Michael P. Frank 9
Module #22 - Graphs



                 Directed Multigraphs
     • Like directed graphs, but there may be more
       than one arc from a node to another.
     • A directed multigraph G=(V, E, f ) consists
       of a set V of vertices, a set E of edges, and a
       function f:E→V×V.
     • E.g., V=web pages,
       E=hyperlinks. The WWW is
       a directed multigraph...

11/06/12              (c)2001-2003, Michael P. Frank 10
Module #22 - Graphs



           Types of Graphs: Summary
     • Summary of the book’s definitions.
     • Keep in mind this terminology is not fully
       standardized across different authors...
                             Edge       Multiple      Self-
      Term                    type     edges ok?   loops ok?
      Simple graph           Undir.       No           No
      Multigraph             Undir.       Yes          No
      Pseudograph            Undir.       Yes         Yes
      Directed graph        Directed      No          Yes
      Directed multigraph   Directed      Yes         Yes


11/06/12              (c)2001-2003, Michael P. Frank 11
Module #22 - Graphs



             §8.2: Graph Terminology
     You need to learn the following terms:
     • Adjacent, connects, endpoints, degree,
       initial, terminal, in-degree, out-degree,
       complete, cycles, wheels, n-cubes, bipartite,
       subgraph, union.




11/06/12              (c)2001-2003, Michael P. Frank 12
Module #22 - Graphs



                       Adjacency
     Let G be an undirected graph with edge set E.
       Let e∈E be (or map to) the pair {u,v}. Then
       we say:
     • u, v are adjacent / neighbors / connected.
     • Edge e is incident with vertices u and v.
     • Edge e connects u and v.
     • Vertices u and v are endpoints of edge e.

11/06/12              (c)2001-2003, Michael P. Frank 13
Module #22 - Graphs



                      Degree of a Vertex
     • Let G be an undirected graph, v∈V a vertex.
     • The degree of v, deg(v), is its number of
       incident edges. (Except that any self-loops
       are counted twice.)
     • A vertex with degree 0 is called isolated.
     • A vertex of degree 1 is called pendant.



11/06/12                 (c)2001-2003, Michael P. Frank 14
Module #22 - Graphs



                Handshaking Theorem
     • Let G be an undirected (simple, multi-, or
       pseudo-) graph with vertex set V and edge
       set E. Then

                      ∑ deg(v) = 2 E
                      v∈V
     • Corollary: Any undirected graph has an
       even number of vertices of odd degree.

11/06/12               (c)2001-2003, Michael P. Frank 15
Module #22 - Graphs



                   Directed Adjacency
     • Let G be a directed (possibly multi-) graph,
       and let e be an edge of G that is (or maps to)
       (u,v). Then we say:
         –   u is adjacent to v, v is adjacent from u
         –   e comes from u, e goes to v.
         –   e connects u to v, e goes from u to v
         –   the initial vertex of e is u
         –   the terminal vertex of e is v

11/06/12                 (c)2001-2003, Michael P. Frank 16
Module #22 - Graphs



                      Directed Degree
     • Let G be a directed graph, v a vertex of G.
         – The in-degree of v, deg−(v), is the number of
           edges going to v.
         – The out-degree of v, deg+(v), is the number of
           edges coming from v.
         – The degree of v, deg(v):≡deg−(v)+deg+(v), is the
           sum of v’s in-degree and out-degree.



11/06/12               (c)2001-2003, Michael P. Frank 17
Module #22 - Graphs



       Directed Handshaking Theorem
     • Let G be a directed (possibly multi-) graph
       with vertex set V and edge set E. Then:
                                  1
      ∑ δεγ (v) = ∑ δεγ (v) = 2 ∑ δεγ( v) = E
      v∈V
                −

                    v∈V
                              +

                                    v∈V
     • Note that the degree of a node is unchanged
       by whether we consider its edges to be
       directed or undirected.

11/06/12              (c)2001-2003, Michael P. Frank 18
Module #22 - Graphs



              Special Graph Structures
     Special cases of undirected graph structures:
     • Complete graphs Kn
     • Cycles Cn
     • Wheels Wn
     • n-Cubes Qn
     • Bipartite graphs
     • Complete bipartite graphs Km,n
11/06/12              (c)2001-2003, Michael P. Frank 19
Module #22 - Graphs



                      Complete Graphs
     • For any n∈N, a complete graph on n
       vertices, Kn, is a simple graph with n nodes
       in which every node is adjacent to every
       other node: ∀u,v∈V: u≠v↔{u,v}∈E.


           K1   K2           K3       K4
                                                K5          K6
                         ν( ν − 1)
                      ν −1

   Note that Kn has ∑ ι = 2 edges.
                      ι=1



11/06/12                      (c)2001-2003, Michael P. Frank 20
Module #22 - Graphs



                           Cycles
     • For any n≥3, a cycle on n vertices, Cn, is a
       simple graph where V={v1,v2,… ,vn} and
       E={{v1,v2},{v2,v3},…,{vn−1,vn},{vn,v1}}.


         C3       C4      C5        C6              C8
                                            C7
       How many edges are there in Cn?

11/06/12               (c)2001-2003, Michael P. Frank 21
Module #22 - Graphs



                              Wheels
     • For any n≥3, a wheel Wn, is a simple graph
       obtained by taking the cycle Cn and adding
       one extra vertex vhub and n extra edges
       {{vhub,v1}, {vhub,v2},…,{vhub,vn}}.


           W3         W4        W5        W6              W8
                                                  W7
        How many edges are there in Wn?
11/06/12                   (c)2001-2003, Michael P. Frank 22
Module #22 - Graphs



                   n-cubes (hypercubes)
     • For any n∈N, the hypercube Qn is a simple
       graph consisting of two copies of Qn-1
       connected together at corresponding nodes.
       Q0 has 1 node.
         Q0
              Q1      Q2                  Q4
                             Q3
      Number of vertices: 2n. Number of edges:Exercise to try!


11/06/12                   (c)2001-2003, Michael P. Frank 23
Module #22 - Graphs



                 n-cubes (hypercubes)
     • For any n∈N, the hypercube Qn can be
       defined recursively as follows:
         – Q0={{v0},∅} (one node and no edges)
         – For any n∈N, if Qn=(V,E), where V={v1,…,va}
           and E={e1,…,eb}, then Qn+1=(V∪{v1´,…,va´},
           E∪{e1´,…,eb´}∪{{v1,v1´},{v2,v2´},…,
           {va,va´}}) where v1´,…,va´ are new vertices, and
           where if ei={vj,vk} then ei´={vj´,vk´}.

11/06/12               (c)2001-2003, Michael P. Frank 24
Module #22 - Graphs



                         Bipartite Graphs
     • Def’n.: A graph G=(V,E) is bipartite (two-
       part) iff V = V1∩V2 where V1∪V2=∅ and
       ∀e∈E: ∃v1∈V1,v2∈V2: e={v1,v2}.
     • In English: The graph can
       be divided into two parts
       in such a way that all edges
       go between the two parts.                             V1        V2
        This definition can easily be adapted for the
        case of multigraphs and directed graphs as well.   Can represent with
                                                           zero-one matrices.
11/06/12                      (c)2001-2003, Michael P. Frank 25
Module #22 - Graphs



            Complete Bipartite Graphs
     • For m,n∈N, the complete bipartite graph
       Km,n is a bipartite graph where |V1| = m,
       |V2| = n, and E = {{v1,v2}|v1∈V1 ∧ v2∈V2}.
         – That is, there are m nodes               K4,3
           in the left part, n nodes in
           the right part, and every
           node in the left part is
           connected to every node        Km,n has _____ nodes
           in the right part.             and _____ edges.


11/06/12                (c)2001-2003, Michael P. Frank 26
Module #22 - Graphs



                           Subgraphs
     • A subgraph of a graph G=(V,E) is a graph
       H=(W,F) where W⊆V and F⊆E.




                      G                         H


11/06/12                  (c)2001-2003, Michael P. Frank 27
Module #22 - Graphs



                          Graph Unions
     • The union G1∪G2 of two simple graphs
       G1=(V1, E1) and G2=(V2,E2) is the simple
       graph (V1∪V2, E1∪E2).


                                  ∪
               a      b   c                   a   b   c


               d      e                       d       f




11/06/12                      (c)2001-2003, Michael P. Frank 28
Module #22 - Graphs

           §8.3: Graph Representations &
                    Isomorphism
     • Graph representations:
         – Adjacency lists.
         – Adjacency matrices.
         – Incidence matrices.
     • Graph isomorphism:
         – Two graphs are isomorphic iff they are
           identical except for their node names.


11/06/12              (c)2001-2003, Michael P. Frank 29
Module #22 - Graphs



                          Adjacency Lists
     • A table with 1 row per vertex, listing its
       adjacent vertices.         Adjacent
         a       b                     Vertex   Vertices
                                         a      b, c
        c    d                           b      a, c, e, f
                          e
                      f                  c      a, b, f
                                         d
                                         e      b
                                         f      c, b


11/06/12                      (c)2001-2003, Michael P. Frank 30
Module #22 - Graphs



             Directed Adjacency Lists
     • 1 row per node, listing the terminal nodes of
       each edge incident from that node.




11/06/12              (c)2001-2003, Michael P. Frank 31
Module #22 - Graphs



                  Adjacency Matrices
     • A way to represent simple graphs
         – possibly with self-loops.
     • Matrix A=[aij], where aij is 1 if {vi, vj} is an
       edge of G, and is 0 otherwise.
     • Can extend to pseudographs by letting each
       matrix elements be the number of links
       (possibly >1) between the nodes.

11/06/12               (c)2001-2003, Michael P. Frank 32
Module #22 - Graphs



                  Graph Isomorphism
     • Formal definition:
         – Simple graphs G1=(V1, E1) and G2=(V2, E2) are
           isomorphic iff ∃ a bijection f:V1→V2 such that ∀
           a,b∈V1, a and b are adjacent in G1 iff f(a) and
           f(b) are adjacent in G2.
         – f is the “renaming” function between the two
           node sets that makes the two graphs identical.
         – This definition can easily be extended to other
           types of graphs.
11/06/12               (c)2001-2003, Michael P. Frank 33
Module #22 - Graphs


     Graph Invariants under Isomorphism

     Necessary but not sufficient conditions for
     G1=(V1, E1) to be isomorphic to G2=(V2, E2):
         – We must have that |V1|=|V2|, and |E1|=|E2|.
         – The number of vertices with degree n is the
           same in both graphs.
         – For every proper subgraph g of one graph, there
           is a proper subgraph of the other graph that is
           isomorphic to g.

11/06/12              (c)2001-2003, Michael P. Frank 34
Module #22 - Graphs



                   Isomorphism Example
     • If isomorphic, label the 2nd graph to show
       the isomorphism, else identify difference.
                      b                                    d
           a                                       b           a
                          d        c
                                                                   e
               e                               c       f
                              f



11/06/12                          (c)2001-2003, Michael P. Frank 35
Module #22 - Graphs



                Are These Isomorphic?
     • If isomorphic, label the 2nd graph to show
       the isomorphism, else identify difference.
                                                 • Same # of
            a                                     vertices
                      b                          • Same # of
                                                  edges
                                                 • Different # of
                  d                               verts of
           c              e                       degree 2!
                                                  (1 vs 3)


11/06/12                  (c)2001-2003, Michael P. Frank 36
Module #22 - Graphs



                      §8.4: Connectivity
     • In an undirected graph, a path of length n
       from u to v is a sequence of adjacent edges
       going from vertex u to vertex v.
     • A path is a circuit if u=v.
     • A path traverses the vertices along it.
     • A path is simple if it contains no edge more
       than once.

11/06/12                 (c)2001-2003, Michael P. Frank 37
Module #22 - Graphs



             Paths in Directed Graphs
     • Same as in undirected graphs, but the path
       must go in the direction of the arrows.




11/06/12              (c)2001-2003, Michael P. Frank 38
Module #22 - Graphs



                      Connectedness
    • An undirected graph is connected iff there
       is a path between every pair of distinct
       vertices in the graph.
    • Theorem: There is a simple path between
       any pair of vertices in a connected
       undirected graph.
    • Connected component: connected subgraph
    • A cut vertex or cut edge separates 1
       connected component into 2 if P. Frank 39
11/06/12             (c)2001-2003, Michael
                                           removed.
Module #22 - Graphs



              Directed Connectedness
     • A directed graph is strongly connected iff
       there is a directed path from a to b for any
       two verts a and b.
     • It is weakly connected iff the underlying
       undirected graph (i.e., with edge directions
       removed) is connected.
     • Note strongly implies weakly but not vice-
       versa.
11/06/12              (c)2001-2003, Michael P. Frank 40
Module #22 - Graphs



                 Paths & Isomorphism
     • Note that connectedness, and the existence
       of a circuit or simple circuit of length k are
       graph invariants with respect to
       isomorphism.




11/06/12              (c)2001-2003, Michael P. Frank 41
Module #22 - Graphs


       Counting Paths w Adjacency Matrices

     • Let A be the adjacency matrix of graph G.
     • The number of paths of length k from vi to vj
       is equal to (Ak)i,j.
         – The notation (M)i,j denotes mi,j where
           [mi,j] = M.




11/06/12               (c)2001-2003, Michael P. Frank 42
Module #22 - Graphs



         §8.5: Euler & Hamilton Paths
     • An Euler circuit in a graph G is a circuit
       containing every edge of G.
     • An Euler path in G is a path containing
       every edge of G.
     • A Hamilton circuit is a circuit that
       traverses each vertex in G exactly once.
     • A Hamilton path is a path that traverses
       each vertex in G exactly once.

11/06/12              (c)2001-2003, Michael P. Frank 43
Module #22 - Graphs



       Bridges of Königsberg Problem
     • Can we walk through town, crossing each
       bridge exactly once, and return to start?
                            A


                      B                D


                            C
                                            Equivalent multigraph
             The original problem


11/06/12                  (c)2001-2003, Michael P. Frank 44
Module #22 - Graphs



                 Euler Path Theorems
   • Theorem: A connected multigraph has an
     Euler circuit iff each vertex has even degree.
       – Proof:
           • (→) The circuit contributes 2 to degree of each node.
           • (←) By construction using algorithm on p. 580-581
   • Theorem: A connected multigraph has an
     Euler path (but not an Euler circuit) iff it has
     exactly 2 vertices of odd degree.
       – One is the start, the other is the end.

11/06/12                 (c)2001-2003, Michael P. Frank 45
Module #22 - Graphs



              Euler Circuit Algorithm
     • Begin with any arbitrary node.
     • Construct a simple path from it till you get
       back to start.
     • Repeat for each remaining subgraph,
       splicing results back into original cycle.




11/06/12              (c)2001-2003, Michael P. Frank 46
Module #22 - Graphs



             Round-the-World Puzzle
     • Can we traverse all the vertices of a
       dodecahedron, visiting each once?`




                            Equivalent
      Dodecahedron puzzle                 Pegboard version
                            graph

11/06/12              (c)2001-2003, Michael P. Frank 47
Module #22 - Graphs



           Hamiltonian Path Theorems
     • Dirac’s theorem: If (but not only if) G is
       connected, simple, has n≥3 vertices, and ∀v
       deg(v)≥n/2, then G has a Hamilton circuit.
         – Ore’s corollary: If G is connected, simple, has
           n≥3 nodes, and deg(u)+deg(v)≥n for every pair
           u,v of non-adjacent nodes, then G has a
           Hamilton circuit.



11/06/12              (c)2001-2003, Michael P. Frank 48
Module #22 - Graphs



      HAM-CIRCUIT is NP-complete
     • Let HAM-CIRCUIT be the problem:
         – Given a simple graph G, does G contain a
           Hamiltonian circuit?
     • This problem has been proven to be
       NP-complete!
         – This means, if an algorithm for solving it in
           polynomial time were found, it could be used to
           solve all NP problems in polynomial time.

11/06/12              (c)2001-2003, Michael P. Frank 49
Module #22 - Graphs



            §9.1: Introduction to Trees
     • A tree is a connected undirected graph that
       contains no circuits.
         – Theorem: There is a unique simple path between any
           two of its nodes.
     • A (not-necessarily-connected) undirected graph
       without simple circuits is called a forest.
         – You can think of it as a set of trees having disjoint sets
           of nodes.
     • A leaf node in a tree or forest is any pendant or
       isolated vertex. An internal node is any non-leaf
       vertex (thus it has degree ≥ ___ ).
11/06/12                  (c)2001-2003, Michael P. Frank 50
Module #22 - Graphs



            Tree and Forest Examples
                Leaves in green, internal nodes in brown.
     • A Tree:                        • A Forest:




11/06/12                 (c)2001-2003, Michael P. Frank 51
Module #22 - Graphs



                      Rooted Trees
     • A rooted tree is a tree in which one node
       has been designated the root.
         – Every edge is (implicitly or explicitly) directed
           away from the root.
     • You should know the following terms about
       rooted trees:
         – Parent, child, siblings, ancestors, descendents,
           leaf, internal node, subtree.

11/06/12               (c)2001-2003, Michael P. Frank 52
Module #22 - Graphs



                Rooted Tree Examples
     • Note that a given unrooted tree with n
       nodes yields n different rooted trees.
                             Same tree
                               except
                  root       for choice
                               of root
                                                             root




11/06/12                 (c)2001-2003, Michael P. Frank 53
Module #22 - Graphs


      Rooted-Tree Terminology Exercise

     • Find the parent,          o               h          n   r
       children, siblings,
       ancestors, &                      d
                                                                m
                             b
       descendants                               root
       of node f.                            a          c       g
                       e
               i                 q
                                                        f
           p            j            k                          l


11/06/12              (c)2001-2003, Michael P. Frank 54
Module #22 - Graphs



                         n-ary trees
     • A rooted tree is called n-ary if every vertex
       has no more than n children.
         – It is called full if every internal (non-leaf)
           vertex has exactly n children.
     • A 2-ary tree is called a binary tree.
         – These are handy for describing sequences of
           yes-no decisions.
             • Example: Comparisons in binary search algorithm.


11/06/12                (c)2001-2003, Michael P. Frank 55
Module #22 - Graphs



                Which Tree is Binary?
     • Theorem: A given rooted tree is a binary tree iff
       every node other than the root has degree ≤ ___,
       and the root has degree ≤ ___.




11/06/12              (c)2001-2003, Michael P. Frank 56
Module #22 - Graphs



                 Ordered Rooted Tree
     • This is just a rooted tree in which the
       children of each internal node are ordered.
     • In ordered binary trees, we can define:
         – left child, right child
         – left subtree, right subtree
     • For n-ary trees with n>2, can use terms like
       “leftmost”, “rightmost,” etc.

11/06/12               (c)2001-2003, Michael P. Frank 57
Module #22 - Graphs



                      Trees as Models
     • Can use trees to model the following:
         – Saturated hydrocarbons
         – Organizational structures
         – Computer file systems
     • In each case, would you use a rooted or a
       non-rooted tree?



11/06/12               (c)2001-2003, Michael P. Frank 58
Module #22 - Graphs



                 Some Tree Theorems
     • Any tree with n nodes has e = n−1 edges.
         – Proof: Consider removing leaves.
     • A full m-ary tree with i internal nodes has n=mi+1
       nodes, and =(m−1)i+1 leaves.
         – Proof: There are mi children of internal nodes,
           plus the root. And,  = n−i = (m−1)i+1. □
     • Thus, when m is known and the tree is full, we can
       compute all four of the values e, i, n, and , given
       any one of them.


11/06/12                 (c)2001-2003, Michael P. Frank 59
Module #22 - Graphs



           Some More Tree Theorems
     • Definition: The level of a node is the length of the
       simple path from the root to the node.
         – The height of a tree is maximum node level.
         – A rooted m-ary tree with height h is called balanced if
           all leaves are at levels h or h−1.
     • Theorem: There are at most mh leaves in an m-ary
       tree of height h.
         – Corollary: An m-ary tree with  leaves has height
           h≥logm . If m is full and balanced then h=logm.


11/06/12                 (c)2001-2003, Michael P. Frank 60
Module #22 - Graphs



           §9.2: Applications of Trees
     • Binary search trees
         – A simple data structure for sorted lists
     • Decision trees
         – Minimum comparisons in sorting algorithms
     • Prefix codes
         – Huffman coding
     • Game trees

11/06/12               (c)2001-2003, Michael P. Frank 61
Module #22 - Graphs



                  Binary Search Trees
     • A representation for sorted sets of items.
         – Supports the following operations in Θ(log n)
           average-case time:
             • Searching for an existing item.
             • Inserting a new item, if not already present.
         – Supports printing out all items in Θ(n) time.
     • Note that inserting into a plain sequence ai
       would instead take Θ(n) worst-case time.

11/06/12                 (c)2001-2003, Michael P. Frank 62
Module #22 - Graphs



           Binary Search Tree Format
     • Items are stored at                  Example:
       individual tree nodes.                                 7
     • We arrange for the
       tree to always obey                            3               12
       this invariant:
         – For every item x,
             • Every node in x’s left
                                                  1       5           9        15
               subtree is less than x.
             • Every node in x’s right
               subtree is greater than x.     0       2           8       11


11/06/12                    (c)2001-2003, Michael P. Frank 63
Module #22 - Graphs



           Recursive Binary Tree Insert
     procedure insert(T: binary tree, x: item)
       v := root[T]
       if v = null then begin
           root[T] := x; return “Done” end
       else if v = x return “Already present”
       else if x < v then
           return insert(leftSubtree[T], x)
       else {must be x > v}
           return insert(rightSubtree[T], x)

11/06/12              (c)2001-2003, Michael P. Frank 64
Module #22 - Graphs



           Decision Trees (pp. 646-649)
     • A decision tree represents a decision-making
       process.
         – Each possible “decision point” or situation is
           represented by a node.
         – Each possible choice that could be made at that
           decision point is represented by an edge to a child node.
     • In the extended decision trees used in decision
       analysis, we also include nodes that represent
       random events and their outcomes.


11/06/12                 (c)2001-2003, Michael P. Frank 65
Module #22 - Graphs



              Coin-Weighing Problem
  • Imagine you have 8 coins, one
    of which is a lighter counterfeit,
    and a free-beam balance.
      – No scale of weight markings
        is required for this problem!
  • How many weighings are
                                                    ?
    needed to guarantee that the
    counterfeit coin will be found?

11/06/12              (c)2001-2003, Michael P. Frank 66
Module #22 - Graphs


           As a Decision-Tree Problem
  • In each situation, we pick two disjoint and equal-
    size subsets of coins to put on the scale.
The balance then                                  A given sequence of
“decides” whether                                 weighings thus yields
to tip left, tip right,                           a decision tree with
or stay balanced.                                 branching factor 3.




11/06/12                  (c)2001-2003, Michael P. Frank 67
Module #22 - Graphs


       Applying the Tree Height Theorem
     • The decision tree must have at least 8 leaf
       nodes, since there are 8 possible outcomes.
         – In terms of which coin is the counterfeit one.
     • Recall the tree-height theorem, h≥logm.
         – Thus the decision tree must have height
           h ≥ log38 = 1.893… = 2.
     • Let’s see if we solve the problem with only
       2 weighings…

11/06/12               (c)2001-2003, Michael P. Frank 68
Module #22 - Graphs


        General Solution
           Strategy
 • The problem is an example of searching for 1 unique particular
   item, from among a list of n otherwise identical items.
     – Somewhat analogous to the adage of “searching for a needle in haystack.”
 • Armed with our balance, we can attack the problem using a divide-
   and-conquer strategy, like what’s done in binary search.
     – We want to narrow down the set of possible locations where the desired
       item (coin) could be found down from n to just 1, in a logarithmic fashion.
 • Each weighing has 3 possible outcomes.
     – Thus, we should use it to partition the search space into 3 pieces that are as
       close to equal-sized as possible.
 • This strategy will lead to the minimum possible worst-case number
   of weighings required.

11/06/12                     (c)2001-2003, Michael P. Frank 69
Module #22 - Graphs



             General Balance Strategy
     • On each step, put n/3 of the n coins to be
       searched on each side of the scale.
         – If the scale tips to the left, then:
             • The lightweight fake is in the right set of n/3 ≈ n/3 coins.
         – If the scale tips to the right, then:
             • The lightweight fake is in the left set of n/3 ≈ n/3 coins.
         – If the scale stays balanced, then:
             • The fake is in the remaining set of n − 2n/3 ≈ n/3 coins that
               were not weighed!
     • Except if n mod 3 = 1 then we can do a little better
       by weighing n/3 of the coins on each side.
 You can prove that this strategy always leads to a balanced 3-ary tree.
11/06/12                    (c)2001-2003, Michael P. Frank 70
Module #22 - Graphs



        Coin Balancing Decision Tree
     • Here’s what the tree looks like in our case:

                         left:   123 vs 456
                         123                  balanced:
                                 right:          78
                                  456

                      1 vs. 2      4 vs. 5       7 vs. 8
              L:1      R:2 B:3 L:4 R:5 B:6 L:7        R:8



11/06/12                    (c)2001-2003, Michael P. Frank 71
Module #22 - Graphs



                  §9.3: Tree Traversal
     • Universal address systems
     • Traversal algorithms
         – Depth-first traversal:
             • Preorder traversal
             • Inorder traversal
             • Postorder traversal
         – Breadth-first traversal
     • Infix/prefix/postfix notation


11/06/12                 (c)2001-2003, Michael P. Frank 72

Weitere ähnliche Inhalte

Was ist angesagt?

AlgoPerm2012 - 09 Vincent Pilaud
AlgoPerm2012 - 09 Vincent PilaudAlgoPerm2012 - 09 Vincent Pilaud
AlgoPerm2012 - 09 Vincent PilaudAlgoPerm 2012
 
Graph theory concepts complex networks presents-rouhollah nabati
Graph theory concepts   complex networks presents-rouhollah nabatiGraph theory concepts   complex networks presents-rouhollah nabati
Graph theory concepts complex networks presents-rouhollah nabatinabati
 
8th alg -l4.1--nov26
8th alg -l4.1--nov268th alg -l4.1--nov26
8th alg -l4.1--nov26jdurst65
 
Introduction to Epistemic Network Analysis
Introduction to Epistemic Network AnalysisIntroduction to Epistemic Network Analysis
Introduction to Epistemic Network AnalysisVitomir Kovanovic
 
An O(log^2 k)-approximation algorithm for k-vertex connected spanning subgraph
An O(log^2 k)-approximation algorithm for k-vertex connected spanning subgraphAn O(log^2 k)-approximation algorithm for k-vertex connected spanning subgraph
An O(log^2 k)-approximation algorithm for k-vertex connected spanning subgraphlbundit
 
Ppt of graph theory
Ppt of graph theoryPpt of graph theory
Ppt of graph theoryArvindBorge
 
Floyd warshall algo {dynamic approach}
Floyd warshall algo {dynamic approach}Floyd warshall algo {dynamic approach}
Floyd warshall algo {dynamic approach}Shubham Shukla
 
Physics M1 Graphs II
Physics M1 Graphs IIPhysics M1 Graphs II
Physics M1 Graphs IIeLearningJa
 

Was ist angesagt? (10)

Graph theory
Graph theoryGraph theory
Graph theory
 
AlgoPerm2012 - 09 Vincent Pilaud
AlgoPerm2012 - 09 Vincent PilaudAlgoPerm2012 - 09 Vincent Pilaud
AlgoPerm2012 - 09 Vincent Pilaud
 
Graph theory concepts complex networks presents-rouhollah nabati
Graph theory concepts   complex networks presents-rouhollah nabatiGraph theory concepts   complex networks presents-rouhollah nabati
Graph theory concepts complex networks presents-rouhollah nabati
 
8th alg -l4.1--nov26
8th alg -l4.1--nov268th alg -l4.1--nov26
8th alg -l4.1--nov26
 
Introduction to Epistemic Network Analysis
Introduction to Epistemic Network AnalysisIntroduction to Epistemic Network Analysis
Introduction to Epistemic Network Analysis
 
An O(log^2 k)-approximation algorithm for k-vertex connected spanning subgraph
An O(log^2 k)-approximation algorithm for k-vertex connected spanning subgraphAn O(log^2 k)-approximation algorithm for k-vertex connected spanning subgraph
An O(log^2 k)-approximation algorithm for k-vertex connected spanning subgraph
 
Graph
GraphGraph
Graph
 
Ppt of graph theory
Ppt of graph theoryPpt of graph theory
Ppt of graph theory
 
Floyd warshall algo {dynamic approach}
Floyd warshall algo {dynamic approach}Floyd warshall algo {dynamic approach}
Floyd warshall algo {dynamic approach}
 
Physics M1 Graphs II
Physics M1 Graphs IIPhysics M1 Graphs II
Physics M1 Graphs II
 

Ähnlich wie Module 22-graphs

Graph Theory,Graph Terminologies,Planar Graph & Graph Colouring
Graph Theory,Graph Terminologies,Planar Graph & Graph ColouringGraph Theory,Graph Terminologies,Planar Graph & Graph Colouring
Graph Theory,Graph Terminologies,Planar Graph & Graph ColouringSaurabh Kaushik
 
graph.pptx
graph.pptxgraph.pptx
graph.pptxhijigaf
 
1. Graph and Graph Terminologiesimp.pptx
1. Graph and Graph Terminologiesimp.pptx1. Graph and Graph Terminologiesimp.pptx
1. Graph and Graph Terminologiesimp.pptxswapnilbs2728
 
Analytic construction of points on modular elliptic curves
Analytic construction of points on modular elliptic curvesAnalytic construction of points on modular elliptic curves
Analytic construction of points on modular elliptic curvesmmasdeu
 
Graph ASS DBATU.pptx
Graph ASS DBATU.pptxGraph ASS DBATU.pptx
Graph ASS DBATU.pptxARVIND SARDAR
 
Graph Introduction.ppt
Graph Introduction.pptGraph Introduction.ppt
Graph Introduction.pptFaruk Hossen
 
Some New Families on H-Cordial Graphs
Some New Families on H-Cordial GraphsSome New Families on H-Cordial Graphs
Some New Families on H-Cordial GraphsIJMER
 
IJCER (www.ijceronline.com) International Journal of computational Engineeri...
 IJCER (www.ijceronline.com) International Journal of computational Engineeri... IJCER (www.ijceronline.com) International Journal of computational Engineeri...
IJCER (www.ijceronline.com) International Journal of computational Engineeri...ijceronline
 
Chapter 5 Graphs (1).ppt
Chapter 5 Graphs (1).pptChapter 5 Graphs (1).ppt
Chapter 5 Graphs (1).pptishan743441
 
Graph theory
Graph theoryGraph theory
Graph theoryGaditek
 
Lecture 1--Graph Algorithms -- Basics.pptx
Lecture 1--Graph Algorithms -- Basics.pptxLecture 1--Graph Algorithms -- Basics.pptx
Lecture 1--Graph Algorithms -- Basics.pptxChandanGiri21
 

Ähnlich wie Module 22-graphs (20)

graph theory
graph theorygraph theory
graph theory
 
Unit 2: All
Unit 2: AllUnit 2: All
Unit 2: All
 
Graph Theory,Graph Terminologies,Planar Graph & Graph Colouring
Graph Theory,Graph Terminologies,Planar Graph & Graph ColouringGraph Theory,Graph Terminologies,Planar Graph & Graph Colouring
Graph Theory,Graph Terminologies,Planar Graph & Graph Colouring
 
Graph Theory
Graph TheoryGraph Theory
Graph Theory
 
graph.pptx
graph.pptxgraph.pptx
graph.pptx
 
graph.ppt
graph.pptgraph.ppt
graph.ppt
 
Graph-Basics.pptx
Graph-Basics.pptxGraph-Basics.pptx
Graph-Basics.pptx
 
ae_722_unstructured_meshes.ppt
ae_722_unstructured_meshes.pptae_722_unstructured_meshes.ppt
ae_722_unstructured_meshes.ppt
 
Graph
GraphGraph
Graph
 
graph ASS (1).ppt
graph ASS (1).pptgraph ASS (1).ppt
graph ASS (1).ppt
 
1. Graph and Graph Terminologiesimp.pptx
1. Graph and Graph Terminologiesimp.pptx1. Graph and Graph Terminologiesimp.pptx
1. Graph and Graph Terminologiesimp.pptx
 
LEC 1.pptx
LEC 1.pptxLEC 1.pptx
LEC 1.pptx
 
Analytic construction of points on modular elliptic curves
Analytic construction of points on modular elliptic curvesAnalytic construction of points on modular elliptic curves
Analytic construction of points on modular elliptic curves
 
Graph ASS DBATU.pptx
Graph ASS DBATU.pptxGraph ASS DBATU.pptx
Graph ASS DBATU.pptx
 
Graph Introduction.ppt
Graph Introduction.pptGraph Introduction.ppt
Graph Introduction.ppt
 
Some New Families on H-Cordial Graphs
Some New Families on H-Cordial GraphsSome New Families on H-Cordial Graphs
Some New Families on H-Cordial Graphs
 
IJCER (www.ijceronline.com) International Journal of computational Engineeri...
 IJCER (www.ijceronline.com) International Journal of computational Engineeri... IJCER (www.ijceronline.com) International Journal of computational Engineeri...
IJCER (www.ijceronline.com) International Journal of computational Engineeri...
 
Chapter 5 Graphs (1).ppt
Chapter 5 Graphs (1).pptChapter 5 Graphs (1).ppt
Chapter 5 Graphs (1).ppt
 
Graph theory
Graph theoryGraph theory
Graph theory
 
Lecture 1--Graph Algorithms -- Basics.pptx
Lecture 1--Graph Algorithms -- Basics.pptxLecture 1--Graph Algorithms -- Basics.pptx
Lecture 1--Graph Algorithms -- Basics.pptx
 

Kürzlich hochgeladen

Unraveling Hypertext_ Analyzing Postmodern Elements in Literature.pptx
Unraveling Hypertext_ Analyzing  Postmodern Elements in  Literature.pptxUnraveling Hypertext_ Analyzing  Postmodern Elements in  Literature.pptx
Unraveling Hypertext_ Analyzing Postmodern Elements in Literature.pptxDhatriParmar
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 
Mental Health Awareness - a toolkit for supporting young minds
Mental Health Awareness - a toolkit for supporting young mindsMental Health Awareness - a toolkit for supporting young minds
Mental Health Awareness - a toolkit for supporting young mindsPooky Knightsmith
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Seán Kennedy
 
Multi Domain Alias In the Odoo 17 ERP Module
Multi Domain Alias In the Odoo 17 ERP ModuleMulti Domain Alias In the Odoo 17 ERP Module
Multi Domain Alias In the Odoo 17 ERP ModuleCeline George
 
4.11.24 Mass Incarceration and the New Jim Crow.pptx
4.11.24 Mass Incarceration and the New Jim Crow.pptx4.11.24 Mass Incarceration and the New Jim Crow.pptx
4.11.24 Mass Incarceration and the New Jim Crow.pptxmary850239
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Projectjordimapav
 
Scientific Writing :Research Discourse
Scientific  Writing :Research  DiscourseScientific  Writing :Research  Discourse
Scientific Writing :Research DiscourseAnita GoswamiGiri
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfVanessa Camilleri
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management SystemChristalin Nelson
 
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...DhatriParmar
 
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...Nguyen Thanh Tu Collection
 
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptxDIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptxMichelleTuguinay1
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4JOYLYNSAMANIEGO
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxHumphrey A Beña
 
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxBIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxSayali Powar
 
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...DhatriParmar
 
Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operationalssuser3e220a
 
Mythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITWMythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITWQuiz Club NITW
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfPatidar M
 

Kürzlich hochgeladen (20)

Unraveling Hypertext_ Analyzing Postmodern Elements in Literature.pptx
Unraveling Hypertext_ Analyzing  Postmodern Elements in  Literature.pptxUnraveling Hypertext_ Analyzing  Postmodern Elements in  Literature.pptx
Unraveling Hypertext_ Analyzing Postmodern Elements in Literature.pptx
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 
Mental Health Awareness - a toolkit for supporting young minds
Mental Health Awareness - a toolkit for supporting young mindsMental Health Awareness - a toolkit for supporting young minds
Mental Health Awareness - a toolkit for supporting young minds
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...
 
Multi Domain Alias In the Odoo 17 ERP Module
Multi Domain Alias In the Odoo 17 ERP ModuleMulti Domain Alias In the Odoo 17 ERP Module
Multi Domain Alias In the Odoo 17 ERP Module
 
4.11.24 Mass Incarceration and the New Jim Crow.pptx
4.11.24 Mass Incarceration and the New Jim Crow.pptx4.11.24 Mass Incarceration and the New Jim Crow.pptx
4.11.24 Mass Incarceration and the New Jim Crow.pptx
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Project
 
Scientific Writing :Research Discourse
Scientific  Writing :Research  DiscourseScientific  Writing :Research  Discourse
Scientific Writing :Research Discourse
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdf
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
 
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
 
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
 
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptxDIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
 
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxBIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
 
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
 
Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operational
 
Mythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITWMythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITW
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdf
 

Module 22-graphs

  • 1. Module #22 - Graphs University of Florida Dept. of Computer & Information Science & Engineering COT 3100 Applications of Discrete Structures Dr. Michael P. Frank Slides for a Course Based on the Text Discrete Mathematics & Its Applications (5th Edition) by Kenneth H. Rosen 11/06/12 (c)2001-2003, Michael P. Frank 1
  • 2. Module #22 - Graphs Module #22: Graph Theory Rosen 5th ed., chs. 8-9 ~44 slides 11/06/12 (c)2001-2003, Michael P. Frank 2
  • 3. Module #22 - Graphs What are Graphs? Not • General meaning in everyday math: A plot or chart of numerical data using a coordinate system. • Technical meaning in discrete mathematics: A particular class of discrete structures (to be defined) that is useful for representing relations and has a convenient webby- looking graphical representation. 11/06/12 (c)2001-2003, Michael P. Frank 3
  • 4. Module #22 - Graphs Applications of Graphs • Potentially anything (graphs can represent relations, relations can describe the extension of any predicate). • Apps in networking, scheduling, flow optimization, circuit design, path planning. • More apps: Geneology analysis, computer game-playing, program compilation, object- oriented design, … 11/06/12 (c)2001-2003, Michael P. Frank 4
  • 5. Module #22 - Graphs Simple Graphs • Correspond to symmetric, irreflexive binary relations R. • A simple graph G=(V,E) Visual Representation consists of: of a Simple Graph – a set V of vertices or nodes (V corresponds to the universe of the relation R), – a set E of edges / arcs / links: unordered pairs of [distinct] elements u,v ∈ V, such that uRv. 11/06/12 (c)2001-2003, Michael P. Frank 5
  • 6. Module #22 - Graphs Example of a Simple Graph • Let V be the set of states in the far- southeastern U.S.: –I.e., V={FL, GA, AL, MS, LA, SC, TN, NC} • Let E={{u,v}|u adjoins v} TN NC ={{FL,GA},{FL,AL},{FL,MS}, {FL,LA},{GA,AL},{AL,MS}, MS AL SC {MS,LA},{GA,SC},{GA,TN}, GA {SC,NC},{NC,TN},{MS,TN}, LA FL {MS,AL}} 11/06/12 (c)2001-2003, Michael P. Frank 6
  • 7. Module #22 - Graphs Multigraphs • Like simple graphs, but there may be more than one edge connecting two given nodes. • A multigraph G=(V, E, f ) consists of a set V of vertices, a set E of edges (as primitive objects), and a function Parallel f:E→{{u,v}|u,v∈V ∧ u≠v}. edges • E.g., nodes are cities, edges are segments of major highways. 11/06/12 (c)2001-2003, Michael P. Frank 7
  • 8. Module #22 - Graphs Pseudographs • Like a multigraph, but edges connecting a node to itself are allowed. (R may be reflexive.) • A pseudograph G=(V, E, f ) where f:E→{{u,v}|u,v∈V}. Edge e∈E is a loop if f(e)={u,u}={u}. • E.g., nodes are campsites in a state park, edges are hiking trails through the woods. 11/06/12 (c)2001-2003, Michael P. Frank 8
  • 9. Module #22 - Graphs Directed Graphs • Correspond to arbitrary binary relations R, which need not be symmetric. • A directed graph (V,E) consists of a set of vertices V and a binary relation E on V. • E.g.: V = set of People, E={(x,y) | x loves y} 11/06/12 (c)2001-2003, Michael P. Frank 9
  • 10. Module #22 - Graphs Directed Multigraphs • Like directed graphs, but there may be more than one arc from a node to another. • A directed multigraph G=(V, E, f ) consists of a set V of vertices, a set E of edges, and a function f:E→V×V. • E.g., V=web pages, E=hyperlinks. The WWW is a directed multigraph... 11/06/12 (c)2001-2003, Michael P. Frank 10
  • 11. Module #22 - Graphs Types of Graphs: Summary • Summary of the book’s definitions. • Keep in mind this terminology is not fully standardized across different authors... Edge Multiple Self- Term type edges ok? loops ok? Simple graph Undir. No No Multigraph Undir. Yes No Pseudograph Undir. Yes Yes Directed graph Directed No Yes Directed multigraph Directed Yes Yes 11/06/12 (c)2001-2003, Michael P. Frank 11
  • 12. Module #22 - Graphs §8.2: Graph Terminology You need to learn the following terms: • Adjacent, connects, endpoints, degree, initial, terminal, in-degree, out-degree, complete, cycles, wheels, n-cubes, bipartite, subgraph, union. 11/06/12 (c)2001-2003, Michael P. Frank 12
  • 13. Module #22 - Graphs Adjacency Let G be an undirected graph with edge set E. Let e∈E be (or map to) the pair {u,v}. Then we say: • u, v are adjacent / neighbors / connected. • Edge e is incident with vertices u and v. • Edge e connects u and v. • Vertices u and v are endpoints of edge e. 11/06/12 (c)2001-2003, Michael P. Frank 13
  • 14. Module #22 - Graphs Degree of a Vertex • Let G be an undirected graph, v∈V a vertex. • The degree of v, deg(v), is its number of incident edges. (Except that any self-loops are counted twice.) • A vertex with degree 0 is called isolated. • A vertex of degree 1 is called pendant. 11/06/12 (c)2001-2003, Michael P. Frank 14
  • 15. Module #22 - Graphs Handshaking Theorem • Let G be an undirected (simple, multi-, or pseudo-) graph with vertex set V and edge set E. Then ∑ deg(v) = 2 E v∈V • Corollary: Any undirected graph has an even number of vertices of odd degree. 11/06/12 (c)2001-2003, Michael P. Frank 15
  • 16. Module #22 - Graphs Directed Adjacency • Let G be a directed (possibly multi-) graph, and let e be an edge of G that is (or maps to) (u,v). Then we say: – u is adjacent to v, v is adjacent from u – e comes from u, e goes to v. – e connects u to v, e goes from u to v – the initial vertex of e is u – the terminal vertex of e is v 11/06/12 (c)2001-2003, Michael P. Frank 16
  • 17. Module #22 - Graphs Directed Degree • Let G be a directed graph, v a vertex of G. – The in-degree of v, deg−(v), is the number of edges going to v. – The out-degree of v, deg+(v), is the number of edges coming from v. – The degree of v, deg(v):≡deg−(v)+deg+(v), is the sum of v’s in-degree and out-degree. 11/06/12 (c)2001-2003, Michael P. Frank 17
  • 18. Module #22 - Graphs Directed Handshaking Theorem • Let G be a directed (possibly multi-) graph with vertex set V and edge set E. Then: 1 ∑ δεγ (v) = ∑ δεγ (v) = 2 ∑ δεγ( v) = E v∈V − v∈V + v∈V • Note that the degree of a node is unchanged by whether we consider its edges to be directed or undirected. 11/06/12 (c)2001-2003, Michael P. Frank 18
  • 19. Module #22 - Graphs Special Graph Structures Special cases of undirected graph structures: • Complete graphs Kn • Cycles Cn • Wheels Wn • n-Cubes Qn • Bipartite graphs • Complete bipartite graphs Km,n 11/06/12 (c)2001-2003, Michael P. Frank 19
  • 20. Module #22 - Graphs Complete Graphs • For any n∈N, a complete graph on n vertices, Kn, is a simple graph with n nodes in which every node is adjacent to every other node: ∀u,v∈V: u≠v↔{u,v}∈E. K1 K2 K3 K4 K5 K6 ν( ν − 1) ν −1 Note that Kn has ∑ ι = 2 edges. ι=1 11/06/12 (c)2001-2003, Michael P. Frank 20
  • 21. Module #22 - Graphs Cycles • For any n≥3, a cycle on n vertices, Cn, is a simple graph where V={v1,v2,… ,vn} and E={{v1,v2},{v2,v3},…,{vn−1,vn},{vn,v1}}. C3 C4 C5 C6 C8 C7 How many edges are there in Cn? 11/06/12 (c)2001-2003, Michael P. Frank 21
  • 22. Module #22 - Graphs Wheels • For any n≥3, a wheel Wn, is a simple graph obtained by taking the cycle Cn and adding one extra vertex vhub and n extra edges {{vhub,v1}, {vhub,v2},…,{vhub,vn}}. W3 W4 W5 W6 W8 W7 How many edges are there in Wn? 11/06/12 (c)2001-2003, Michael P. Frank 22
  • 23. Module #22 - Graphs n-cubes (hypercubes) • For any n∈N, the hypercube Qn is a simple graph consisting of two copies of Qn-1 connected together at corresponding nodes. Q0 has 1 node. Q0 Q1 Q2 Q4 Q3 Number of vertices: 2n. Number of edges:Exercise to try! 11/06/12 (c)2001-2003, Michael P. Frank 23
  • 24. Module #22 - Graphs n-cubes (hypercubes) • For any n∈N, the hypercube Qn can be defined recursively as follows: – Q0={{v0},∅} (one node and no edges) – For any n∈N, if Qn=(V,E), where V={v1,…,va} and E={e1,…,eb}, then Qn+1=(V∪{v1´,…,va´}, E∪{e1´,…,eb´}∪{{v1,v1´},{v2,v2´},…, {va,va´}}) where v1´,…,va´ are new vertices, and where if ei={vj,vk} then ei´={vj´,vk´}. 11/06/12 (c)2001-2003, Michael P. Frank 24
  • 25. Module #22 - Graphs Bipartite Graphs • Def’n.: A graph G=(V,E) is bipartite (two- part) iff V = V1∩V2 where V1∪V2=∅ and ∀e∈E: ∃v1∈V1,v2∈V2: e={v1,v2}. • In English: The graph can be divided into two parts in such a way that all edges go between the two parts. V1 V2 This definition can easily be adapted for the case of multigraphs and directed graphs as well. Can represent with zero-one matrices. 11/06/12 (c)2001-2003, Michael P. Frank 25
  • 26. Module #22 - Graphs Complete Bipartite Graphs • For m,n∈N, the complete bipartite graph Km,n is a bipartite graph where |V1| = m, |V2| = n, and E = {{v1,v2}|v1∈V1 ∧ v2∈V2}. – That is, there are m nodes K4,3 in the left part, n nodes in the right part, and every node in the left part is connected to every node Km,n has _____ nodes in the right part. and _____ edges. 11/06/12 (c)2001-2003, Michael P. Frank 26
  • 27. Module #22 - Graphs Subgraphs • A subgraph of a graph G=(V,E) is a graph H=(W,F) where W⊆V and F⊆E. G H 11/06/12 (c)2001-2003, Michael P. Frank 27
  • 28. Module #22 - Graphs Graph Unions • The union G1∪G2 of two simple graphs G1=(V1, E1) and G2=(V2,E2) is the simple graph (V1∪V2, E1∪E2). ∪ a b c a b c d e d f 11/06/12 (c)2001-2003, Michael P. Frank 28
  • 29. Module #22 - Graphs §8.3: Graph Representations & Isomorphism • Graph representations: – Adjacency lists. – Adjacency matrices. – Incidence matrices. • Graph isomorphism: – Two graphs are isomorphic iff they are identical except for their node names. 11/06/12 (c)2001-2003, Michael P. Frank 29
  • 30. Module #22 - Graphs Adjacency Lists • A table with 1 row per vertex, listing its adjacent vertices. Adjacent a b Vertex Vertices a b, c c d b a, c, e, f e f c a, b, f d e b f c, b 11/06/12 (c)2001-2003, Michael P. Frank 30
  • 31. Module #22 - Graphs Directed Adjacency Lists • 1 row per node, listing the terminal nodes of each edge incident from that node. 11/06/12 (c)2001-2003, Michael P. Frank 31
  • 32. Module #22 - Graphs Adjacency Matrices • A way to represent simple graphs – possibly with self-loops. • Matrix A=[aij], where aij is 1 if {vi, vj} is an edge of G, and is 0 otherwise. • Can extend to pseudographs by letting each matrix elements be the number of links (possibly >1) between the nodes. 11/06/12 (c)2001-2003, Michael P. Frank 32
  • 33. Module #22 - Graphs Graph Isomorphism • Formal definition: – Simple graphs G1=(V1, E1) and G2=(V2, E2) are isomorphic iff ∃ a bijection f:V1→V2 such that ∀ a,b∈V1, a and b are adjacent in G1 iff f(a) and f(b) are adjacent in G2. – f is the “renaming” function between the two node sets that makes the two graphs identical. – This definition can easily be extended to other types of graphs. 11/06/12 (c)2001-2003, Michael P. Frank 33
  • 34. Module #22 - Graphs Graph Invariants under Isomorphism Necessary but not sufficient conditions for G1=(V1, E1) to be isomorphic to G2=(V2, E2): – We must have that |V1|=|V2|, and |E1|=|E2|. – The number of vertices with degree n is the same in both graphs. – For every proper subgraph g of one graph, there is a proper subgraph of the other graph that is isomorphic to g. 11/06/12 (c)2001-2003, Michael P. Frank 34
  • 35. Module #22 - Graphs Isomorphism Example • If isomorphic, label the 2nd graph to show the isomorphism, else identify difference. b d a b a d c e e c f f 11/06/12 (c)2001-2003, Michael P. Frank 35
  • 36. Module #22 - Graphs Are These Isomorphic? • If isomorphic, label the 2nd graph to show the isomorphism, else identify difference. • Same # of a vertices b • Same # of edges • Different # of d verts of c e degree 2! (1 vs 3) 11/06/12 (c)2001-2003, Michael P. Frank 36
  • 37. Module #22 - Graphs §8.4: Connectivity • In an undirected graph, a path of length n from u to v is a sequence of adjacent edges going from vertex u to vertex v. • A path is a circuit if u=v. • A path traverses the vertices along it. • A path is simple if it contains no edge more than once. 11/06/12 (c)2001-2003, Michael P. Frank 37
  • 38. Module #22 - Graphs Paths in Directed Graphs • Same as in undirected graphs, but the path must go in the direction of the arrows. 11/06/12 (c)2001-2003, Michael P. Frank 38
  • 39. Module #22 - Graphs Connectedness • An undirected graph is connected iff there is a path between every pair of distinct vertices in the graph. • Theorem: There is a simple path between any pair of vertices in a connected undirected graph. • Connected component: connected subgraph • A cut vertex or cut edge separates 1 connected component into 2 if P. Frank 39 11/06/12 (c)2001-2003, Michael removed.
  • 40. Module #22 - Graphs Directed Connectedness • A directed graph is strongly connected iff there is a directed path from a to b for any two verts a and b. • It is weakly connected iff the underlying undirected graph (i.e., with edge directions removed) is connected. • Note strongly implies weakly but not vice- versa. 11/06/12 (c)2001-2003, Michael P. Frank 40
  • 41. Module #22 - Graphs Paths & Isomorphism • Note that connectedness, and the existence of a circuit or simple circuit of length k are graph invariants with respect to isomorphism. 11/06/12 (c)2001-2003, Michael P. Frank 41
  • 42. Module #22 - Graphs Counting Paths w Adjacency Matrices • Let A be the adjacency matrix of graph G. • The number of paths of length k from vi to vj is equal to (Ak)i,j. – The notation (M)i,j denotes mi,j where [mi,j] = M. 11/06/12 (c)2001-2003, Michael P. Frank 42
  • 43. Module #22 - Graphs §8.5: Euler & Hamilton Paths • An Euler circuit in a graph G is a circuit containing every edge of G. • An Euler path in G is a path containing every edge of G. • A Hamilton circuit is a circuit that traverses each vertex in G exactly once. • A Hamilton path is a path that traverses each vertex in G exactly once. 11/06/12 (c)2001-2003, Michael P. Frank 43
  • 44. Module #22 - Graphs Bridges of Königsberg Problem • Can we walk through town, crossing each bridge exactly once, and return to start? A B D C Equivalent multigraph The original problem 11/06/12 (c)2001-2003, Michael P. Frank 44
  • 45. Module #22 - Graphs Euler Path Theorems • Theorem: A connected multigraph has an Euler circuit iff each vertex has even degree. – Proof: • (→) The circuit contributes 2 to degree of each node. • (←) By construction using algorithm on p. 580-581 • Theorem: A connected multigraph has an Euler path (but not an Euler circuit) iff it has exactly 2 vertices of odd degree. – One is the start, the other is the end. 11/06/12 (c)2001-2003, Michael P. Frank 45
  • 46. Module #22 - Graphs Euler Circuit Algorithm • Begin with any arbitrary node. • Construct a simple path from it till you get back to start. • Repeat for each remaining subgraph, splicing results back into original cycle. 11/06/12 (c)2001-2003, Michael P. Frank 46
  • 47. Module #22 - Graphs Round-the-World Puzzle • Can we traverse all the vertices of a dodecahedron, visiting each once?` Equivalent Dodecahedron puzzle Pegboard version graph 11/06/12 (c)2001-2003, Michael P. Frank 47
  • 48. Module #22 - Graphs Hamiltonian Path Theorems • Dirac’s theorem: If (but not only if) G is connected, simple, has n≥3 vertices, and ∀v deg(v)≥n/2, then G has a Hamilton circuit. – Ore’s corollary: If G is connected, simple, has n≥3 nodes, and deg(u)+deg(v)≥n for every pair u,v of non-adjacent nodes, then G has a Hamilton circuit. 11/06/12 (c)2001-2003, Michael P. Frank 48
  • 49. Module #22 - Graphs HAM-CIRCUIT is NP-complete • Let HAM-CIRCUIT be the problem: – Given a simple graph G, does G contain a Hamiltonian circuit? • This problem has been proven to be NP-complete! – This means, if an algorithm for solving it in polynomial time were found, it could be used to solve all NP problems in polynomial time. 11/06/12 (c)2001-2003, Michael P. Frank 49
  • 50. Module #22 - Graphs §9.1: Introduction to Trees • A tree is a connected undirected graph that contains no circuits. – Theorem: There is a unique simple path between any two of its nodes. • A (not-necessarily-connected) undirected graph without simple circuits is called a forest. – You can think of it as a set of trees having disjoint sets of nodes. • A leaf node in a tree or forest is any pendant or isolated vertex. An internal node is any non-leaf vertex (thus it has degree ≥ ___ ). 11/06/12 (c)2001-2003, Michael P. Frank 50
  • 51. Module #22 - Graphs Tree and Forest Examples Leaves in green, internal nodes in brown. • A Tree: • A Forest: 11/06/12 (c)2001-2003, Michael P. Frank 51
  • 52. Module #22 - Graphs Rooted Trees • A rooted tree is a tree in which one node has been designated the root. – Every edge is (implicitly or explicitly) directed away from the root. • You should know the following terms about rooted trees: – Parent, child, siblings, ancestors, descendents, leaf, internal node, subtree. 11/06/12 (c)2001-2003, Michael P. Frank 52
  • 53. Module #22 - Graphs Rooted Tree Examples • Note that a given unrooted tree with n nodes yields n different rooted trees. Same tree except root for choice of root root 11/06/12 (c)2001-2003, Michael P. Frank 53
  • 54. Module #22 - Graphs Rooted-Tree Terminology Exercise • Find the parent, o h n r children, siblings, ancestors, & d m b descendants root of node f. a c g e i q f p j k l 11/06/12 (c)2001-2003, Michael P. Frank 54
  • 55. Module #22 - Graphs n-ary trees • A rooted tree is called n-ary if every vertex has no more than n children. – It is called full if every internal (non-leaf) vertex has exactly n children. • A 2-ary tree is called a binary tree. – These are handy for describing sequences of yes-no decisions. • Example: Comparisons in binary search algorithm. 11/06/12 (c)2001-2003, Michael P. Frank 55
  • 56. Module #22 - Graphs Which Tree is Binary? • Theorem: A given rooted tree is a binary tree iff every node other than the root has degree ≤ ___, and the root has degree ≤ ___. 11/06/12 (c)2001-2003, Michael P. Frank 56
  • 57. Module #22 - Graphs Ordered Rooted Tree • This is just a rooted tree in which the children of each internal node are ordered. • In ordered binary trees, we can define: – left child, right child – left subtree, right subtree • For n-ary trees with n>2, can use terms like “leftmost”, “rightmost,” etc. 11/06/12 (c)2001-2003, Michael P. Frank 57
  • 58. Module #22 - Graphs Trees as Models • Can use trees to model the following: – Saturated hydrocarbons – Organizational structures – Computer file systems • In each case, would you use a rooted or a non-rooted tree? 11/06/12 (c)2001-2003, Michael P. Frank 58
  • 59. Module #22 - Graphs Some Tree Theorems • Any tree with n nodes has e = n−1 edges. – Proof: Consider removing leaves. • A full m-ary tree with i internal nodes has n=mi+1 nodes, and =(m−1)i+1 leaves. – Proof: There are mi children of internal nodes, plus the root. And,  = n−i = (m−1)i+1. □ • Thus, when m is known and the tree is full, we can compute all four of the values e, i, n, and , given any one of them. 11/06/12 (c)2001-2003, Michael P. Frank 59
  • 60. Module #22 - Graphs Some More Tree Theorems • Definition: The level of a node is the length of the simple path from the root to the node. – The height of a tree is maximum node level. – A rooted m-ary tree with height h is called balanced if all leaves are at levels h or h−1. • Theorem: There are at most mh leaves in an m-ary tree of height h. – Corollary: An m-ary tree with  leaves has height h≥logm . If m is full and balanced then h=logm. 11/06/12 (c)2001-2003, Michael P. Frank 60
  • 61. Module #22 - Graphs §9.2: Applications of Trees • Binary search trees – A simple data structure for sorted lists • Decision trees – Minimum comparisons in sorting algorithms • Prefix codes – Huffman coding • Game trees 11/06/12 (c)2001-2003, Michael P. Frank 61
  • 62. Module #22 - Graphs Binary Search Trees • A representation for sorted sets of items. – Supports the following operations in Θ(log n) average-case time: • Searching for an existing item. • Inserting a new item, if not already present. – Supports printing out all items in Θ(n) time. • Note that inserting into a plain sequence ai would instead take Θ(n) worst-case time. 11/06/12 (c)2001-2003, Michael P. Frank 62
  • 63. Module #22 - Graphs Binary Search Tree Format • Items are stored at Example: individual tree nodes. 7 • We arrange for the tree to always obey 3 12 this invariant: – For every item x, • Every node in x’s left 1 5 9 15 subtree is less than x. • Every node in x’s right subtree is greater than x. 0 2 8 11 11/06/12 (c)2001-2003, Michael P. Frank 63
  • 64. Module #22 - Graphs Recursive Binary Tree Insert procedure insert(T: binary tree, x: item) v := root[T] if v = null then begin root[T] := x; return “Done” end else if v = x return “Already present” else if x < v then return insert(leftSubtree[T], x) else {must be x > v} return insert(rightSubtree[T], x) 11/06/12 (c)2001-2003, Michael P. Frank 64
  • 65. Module #22 - Graphs Decision Trees (pp. 646-649) • A decision tree represents a decision-making process. – Each possible “decision point” or situation is represented by a node. – Each possible choice that could be made at that decision point is represented by an edge to a child node. • In the extended decision trees used in decision analysis, we also include nodes that represent random events and their outcomes. 11/06/12 (c)2001-2003, Michael P. Frank 65
  • 66. Module #22 - Graphs Coin-Weighing Problem • Imagine you have 8 coins, one of which is a lighter counterfeit, and a free-beam balance. – No scale of weight markings is required for this problem! • How many weighings are ? needed to guarantee that the counterfeit coin will be found? 11/06/12 (c)2001-2003, Michael P. Frank 66
  • 67. Module #22 - Graphs As a Decision-Tree Problem • In each situation, we pick two disjoint and equal- size subsets of coins to put on the scale. The balance then A given sequence of “decides” whether weighings thus yields to tip left, tip right, a decision tree with or stay balanced. branching factor 3. 11/06/12 (c)2001-2003, Michael P. Frank 67
  • 68. Module #22 - Graphs Applying the Tree Height Theorem • The decision tree must have at least 8 leaf nodes, since there are 8 possible outcomes. – In terms of which coin is the counterfeit one. • Recall the tree-height theorem, h≥logm. – Thus the decision tree must have height h ≥ log38 = 1.893… = 2. • Let’s see if we solve the problem with only 2 weighings… 11/06/12 (c)2001-2003, Michael P. Frank 68
  • 69. Module #22 - Graphs General Solution Strategy • The problem is an example of searching for 1 unique particular item, from among a list of n otherwise identical items. – Somewhat analogous to the adage of “searching for a needle in haystack.” • Armed with our balance, we can attack the problem using a divide- and-conquer strategy, like what’s done in binary search. – We want to narrow down the set of possible locations where the desired item (coin) could be found down from n to just 1, in a logarithmic fashion. • Each weighing has 3 possible outcomes. – Thus, we should use it to partition the search space into 3 pieces that are as close to equal-sized as possible. • This strategy will lead to the minimum possible worst-case number of weighings required. 11/06/12 (c)2001-2003, Michael P. Frank 69
  • 70. Module #22 - Graphs General Balance Strategy • On each step, put n/3 of the n coins to be searched on each side of the scale. – If the scale tips to the left, then: • The lightweight fake is in the right set of n/3 ≈ n/3 coins. – If the scale tips to the right, then: • The lightweight fake is in the left set of n/3 ≈ n/3 coins. – If the scale stays balanced, then: • The fake is in the remaining set of n − 2n/3 ≈ n/3 coins that were not weighed! • Except if n mod 3 = 1 then we can do a little better by weighing n/3 of the coins on each side. You can prove that this strategy always leads to a balanced 3-ary tree. 11/06/12 (c)2001-2003, Michael P. Frank 70
  • 71. Module #22 - Graphs Coin Balancing Decision Tree • Here’s what the tree looks like in our case: left: 123 vs 456 123 balanced: right: 78 456 1 vs. 2 4 vs. 5 7 vs. 8 L:1 R:2 B:3 L:4 R:5 B:6 L:7 R:8 11/06/12 (c)2001-2003, Michael P. Frank 71
  • 72. Module #22 - Graphs §9.3: Tree Traversal • Universal address systems • Traversal algorithms – Depth-first traversal: • Preorder traversal • Inorder traversal • Postorder traversal – Breadth-first traversal • Infix/prefix/postfix notation 11/06/12 (c)2001-2003, Michael P. Frank 72

Hinweis der Redaktion

  1. A word about organization: Since different courses have different lengths of lecture periods, and different instructors go at different paces, rather than dividing the material up into fixed-length lectures, we will divide it up into “modules” which correspond to major topic areas and will generally take 1-3 lectures to cover. Within modules, we have smaller “topics”. Within topics are individual slides.