Download presentation
Presentation is loading. Please wait.
1
Discrete Mathematics and its Applications
9/17/2018 University of Aberdeen, Computing Science CS3511 Discrete Methods Kees van Deemter Slides adapted from Michael P. Frank’s Course Based on the Text Discrete Mathematics & Its Applications (5th Edition) by Kenneth H. Rosen 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. The instructor can bring several modules to each lecture with him, to make sure he has enough material to fill the lecture, or in case he wants to preview or review slides from upcoming or recent past lectures. 9/17/2018 (c) , Michael P. Frank (c) , Michael P. Frank
2
Rosen 5th ed., chs. 8-9 ~44 slides (more later), ~3 lectures
Module #22: Graph Theory Rosen 5th ed., chs. 8-9 ~44 slides (more later), ~3 lectures 9/17/2018 (c) , Michael P. Frank
3
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 graphical representation. 9/17/2018 (c) , Michael P. Frank
4
Applications of Graphs
(At least) anything that can be modelled using 1- or 2-place relations. Apps in networking, scheduling, flow optimization, circuit design, path planning, search; genealogy, any kind of taxonomy, … Challenge: name a topic that graphs cannot model 9/17/2018 (c) , Michael P. Frank
5
We shall introduce a number of different types of graphs, starting with undirected graphs:
“simple” graphs multigraphs pseudographs 9/17/2018 (c) , Michael P. Frank
6
Visual Representation of a Simple Graph
Simple Graphs Correspond to symmetric, irreflexive binary relations R. A simple graph G=(V,E) consists of: a set V of vertices or nodes (V corresponds to the universe of the relation R), a set E of edges: unordered pairs of distinct elements u,v V, such that uRv. Visual Representation of a Simple Graph 9/17/2018 (c) , Michael P. Frank
7
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} ={{FL,GA},{FL,AL},{FL,MS}, {FL,LA},{GA,AL},{AL,MS}, {MS,LA},{GA,SC},{GA,TN}, {SC,NC},{NC,TN},{MS,TN}, {MS,AL}} NC TN MS AL SC GA LA FL 9/17/2018 (c) , Michael P. Frank
8
Extensions All the main types of graphs can be extended to make them more expressive For example, edges may be labelled: Labelled graphs E.g., the edges in the previous example may be labelled with the type of border (e.g., patrolled/non-patrolled) 9/17/2018 (c) , Michael P. Frank
9
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 f:E{{u,v}|u,vV uv}. E.g., nodes are cities, edges are segments of major highways. Parallel edges 9/17/2018 (c) , Michael P. Frank
10
Multigraphs extended Extension: edges may be labelled with numbers of miles. How about this example? 110 A B 90 20 80 C 9/17/2018 (c) , Michael P. Frank
11
Discrete Mathematics and its Applications
9/17/2018 Multigraphs extended Can go from A to B via C in less than 110. The distances in the graph are not minimal, or something is wrong. 110 A B Exercise: formulate a constraint on labels that prevents this kind of mishap. 90 20 80 C 9/17/2018 (c) , Michael P. Frank (c) , Michael P. Frank
12
Pseudographs Like a multigraph, but edges connecting a node to itself are allowed. (R may even be reflexive.) A pseudograph G=(V, E, f ) where f:E{{u,v}|u,vV}. Edge eE 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. 9/17/2018 (c) , Michael P. Frank
13
Directed Graphs Correspond to arbitrary binary relations R, which need not be symmetric. A di(rected) 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} 9/17/2018 (c) , Michael P. Frank
14
Directed Multigraphs Like directed graphs, but there may be more than one edge 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:EVV. E.g., V=web pages, E=hyperlinks. The WWW as a directed multigraph... 9/17/2018 (c) , Michael P. Frank
15
Types of Graphs: Summary
Summary of the book’s definitions. This terminology is not fully standardized across different authors 9/17/2018 (c) , Michael P. Frank
16
§8.2: Graph Terminology Introducing the following terms:
Adjacent, connects, endpoints, degree, initial, terminal, in-degree, out-degree, complete, cycles, wheels, n-cubes, bipartite, subgraph, union. 9/17/2018 (c) , Michael P. Frank
17
Adjacency Let G be an undirected graph with edge set E. Let eE be (or map to) the pair {u,v}. Then we say: u, v are adjacent / 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. 9/17/2018 (c) , Michael P. Frank
18
Degree of a Vertex Let G be an undirected graph, vV 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. 9/17/2018 (c) , Michael P. Frank
19
Degree of a Vertex The degree of v, deg(v), is its number of incident edges. (Except that any self-loops are counted twice.) Exercise: construct a pseudograph with three vertices, all of which have different degrees. 9/17/2018 (c) , Michael P. Frank
20
Handshaking Theorem Let G be an undirected (simple, multi-, or pseudo-) graph with vertex set V and edge set E. Then Proof: every edge causes degree:= degree +2 9/17/2018 (c) , Michael P. Frank
21
Handshaking Theorem Let G be an undirected (simple, multi-, or pseudo-) graph with vertex set V and edge set E. Then Corollary: Any undirected graph has an even number of vertices of odd degree. 9/17/2018 (c) , Michael P. Frank
22
`Sociological’ example
(Like previous exercise, this is from Jonathan L.Gross’ coursenotes to Rosen) Suppose the students in this class are represented as vertices. An edge between a and b means that a and b were acquainted before the course began. [This is a simple graph.] How many students knew an odd number of other students? Could there be 3? 9/17/2018 (c) , Michael P. Frank
23
`Sociological’ example
(Like previous exercise, this is from Jonathan L.Gross’ coursenotes to Rosen) Suppose the students in this class are represented as vertices. An edge between a and b means that a and b were acquainted before the course began. [This is a simple graph.] [By corollary to handshaking theorem:] The number of students who knew an odd number of other students is even. 9/17/2018 (c) , Michael P. Frank
24
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 9/17/2018 (c) , Michael P. Frank
25
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. 9/17/2018 (c) , Michael P. Frank
26
Directed Handshaking Theorem
Let G be a directed (possibly multi-) graph with vertex set V and edge set E. Then: Note that the degree of a node is unchanged by whether we consider its edges to be directed or undirected. 9/17/2018 (c) , Michael P. Frank
27
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 9/17/2018 (c) , Michael P. Frank
28
Complete Graphs For any nN, 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,vV: uv{u,v}E. K1 K4 K2 K3 K5 K6 Note that Kn has edges. 9/17/2018 (c) , Michael P. Frank
29
Consider a complete graph G=(V,E)
Consider a complete graph G=(V,E). Can E contain any edges connecting a node in V to itself? 9/17/2018 (c) , Michael P. Frank
30
No: this would mean {u,v}E, where u=v hence u,vV: uv{u,v}E.
Consider a complete graph G=(V,E). Can E contain any edges connecting a node in V to itself? No: this would mean {u,v}E, where u=v hence u,vV: uv{u,v}E. 9/17/2018 (c) , Michael P. Frank
31
Can you think of a natural example where complete graphs might model the facts?
9/17/2018 (c) , Michael P. Frank
32
Can you think of a natural example where complete graphs might model the facts?
E.g., Graphs that model, for groups of n people, which people are seen by each member of the group 9/17/2018 (c) , Michael P. Frank
33
Discrete Mathematics and its Applications
9/17/2018 Cycles For any n3, a cycle on n vertices, Cn, is a simple graph where V={v1,v2,… ,vn} and E={{v1,v2},{v2,v3},…,{vn1,vn},{vn,v1}}. Number of edges equals n. C3 C4 C5 C6 C8 C7 How many edges are there in Cn? 9/17/2018 (c) , Michael P. Frank (c) , Michael P. Frank
34
Can you think of a natural example where cycles might model the facts?
E.g., Graphs that model, for groups of n people, which people are sitting next to each other (on a round table) 9/17/2018 (c) , Michael P. Frank
35
Can a cycle be a complete graph?
9/17/2018 (c) , Michael P. Frank
36
Discrete Mathematics and its Applications
9/17/2018 Can a cycle be a complete graph? Yes: every cycle with exactly 3 elements is a complete graph. No other cycle can be a complete graph. Can be proven by proving with induction that |Edges(C_n)|<|Edges(K_n)| for n>2. 9/17/2018 (c) , Michael P. Frank (c) , Michael P. Frank
37
Discrete Mathematics and its Applications
9/17/2018 Wheels For any n3, 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}}. The number is 2n W3 W4 W5 W6 W8 W7 How many edges are there in Wn? 9/17/2018 (c) , Michael P. Frank (c) , Michael P. Frank
38
Definition: A graph is regular iff every vertex has the same degree
Which of these are regular? (What degree?) Complete graphs? Cycle graphs? Wheel graphs? 9/17/2018 (c) , Michael P. Frank
39
A graph is regular iff every vertex has the same degree
Which of these are regular? (What degree?) Complete graphs? Yes: degree n-1 (for n nodes) Cycle graphs? Yes: degree 2 Wheel graphs? No, except when they have 3 nodes 9/17/2018 (c) , Michael P. Frank
40
Number of vertices: 2n. Number of edges:Exercise to try!
n-cubes (hypercubes) For any nN, 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! 9/17/2018 (c) , Michael P. Frank
41
n-cubes (hypercubes) For any nN, the hypercube Qn can be defined recursively as follows: Q0={{v0},} (one node and no edges) For any nN, if Qn=(V,E), where V={v1,…,va} and E={e1,…,eb}, then Qn+1 = … 9/17/2018 (c) , Michael P. Frank
42
n-cubes (hypercubes) For any nN, the hypercube Qn can be defined recursively as follows: Q0={{v0},} (one node and no edges) For any nN, 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´}. 9/17/2018 (c) , Michael P. Frank
43
Bipartite Graphs Def’n.: A graph G=(V,E) is bipartite (two-part) iff V = V1 V2 where V1∩V2= and eE: v1V1,v2V2: 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. 9/17/2018 (c) , Michael P. Frank
44
Bipartite graphs … are extremely common, for example when you’re modelling a domain that consists of two different kinds of entities Animals in a zoo, linked with their keepers Words, linked with numbers of letters in them Logical formulas, linked with English sentences that express their meaning 9/17/2018 (c) , Michael P. Frank
45
Some questions Can you think of a graph with two vertices that is not bipartite? Can you think of a simple graph with two vertices that is not bipartite? Can you think of a simple graph with three vertices and a nonempty set of edges that is bipartite? 9/17/2018 (c) , Michael P. Frank
46
Some questions Can you think of a graph with two vertices that is not bipartite? Yes: if there are self-loops Can you think of a simple graph with two vertices that is not bipartite? No: must be bipartite Can you think of a simple graph with three vertices and a nonempty set of edges that is bipartite? Yes: as long as it’s not complete 9/17/2018 (c) , Michael P. Frank
47
Given a (bipartite) graph, can there be more than 1 way of partitioning V into V1 and V2 ?
9/17/2018 (c) , Michael P. Frank
48
Bipartite Graphs Given a (bipartite) graph, can there be more than 1 way of partitioning V into V1 and V2 ? Yes: isolated vertices can be put in either part: V1 V2 9/17/2018 (c) , Michael P. Frank
49
Complete Bipartite Graphs
Discrete Mathematics and its Applications 9/17/2018 Complete Bipartite Graphs For m,nN, the complete bipartite graph Km,n is a bipartite graph where |V1| = m, |V2| = n, and E = {{v1,v2}|v1V1 v2V2}. That is, there are m nodes in the left part, n nodes in the right part, and every node in the left part is connected to every node in the right part. K4,3 Note: not every CBG is complete! (So “CBG” is a strange name) K_{m,n} has m+n nodes and n*m edges. Km,n has _____ nodes and _____ edges. 9/17/2018 (c) , Michael P. Frank (c) , Michael P. Frank
50
Subgraphs A subgraph of a graph G=(V,E) is a graph H=(W,F) where WV and FE. G H 9/17/2018 (c) , Michael P. Frank
51
A subgraph of a graph G=(V,E) is a graph H=(W,F) where WV and FE.
Note: since H is a graph, F can only involve nodes that are elements of W. 9/17/2018 (c) , Michael P. Frank
52
§8.3: Graph Representations & Isomorphism
Adjacency lists. Adjacency matrices. [Not covered this year] Incidence matrices. [Not covered this year] 9/17/2018 (c) , Michael P. Frank
53
Adjacency Lists A table with 1 row per vertex, listing its adjacent vertices. b a d c e f 9/17/2018 (c) , Michael P. Frank
54
An equivalence relation between graphs
Unlike ordinary pictures, we can say precisely when two graphs are similar Obviously, a given graph (V,E) may be drawn in different ways. But even (V,E) and (V’,E’) (where V<>V’ and E<>E’) may in some sense be equivalent: Graph isomorphism (informal): Two graphs are isomorphic iff they are identical except for their node names. 9/17/2018 (c) , Michael P. Frank
55
How would you define graph isomorphism formally?
Graphs that are isomorphic share all their `important’ properties, e.g., The number of nodes and edges The degrees of all their nodes Whether they are bipartite or not, etc. How would you define graph isomorphism formally? For simplicity: focus on simple graphs Hint: use the notion of a bijection 9/17/2018 (c) , Michael P. Frank
56
Graph Isomorphism Formal definition:
Simple graphs G1=(V1, E1) and G2=(V2, E2) are isomorphic iff a bijection f:V1V2 such that a,bV1, 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 be extended to other types of graphs. 9/17/2018 (c) , Michael P. Frank
57
Graph Isomorphism How can we tell whether two graphs are isomorphic?
The best algorithms that are known to solve this problem have exponential worst-case time complexity. (Faster solutions may be possible.) In practice, a few tests go a long way … 9/17/2018 (c) , Michael P. Frank
58
Graph Invariants under Isomorphism
Necessary but not sufficient conditions for G1=(V1, E1) to be isomorphic to G2=(V2, E2): |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. 9/17/2018 (c) , Michael P. Frank
59
Isomorphism Example If isomorphic, label the 2nd graph to show the isomorphism, else identify difference. d b a b a d c e e c f f 9/17/2018 (c) , Michael P. Frank
60
Are These Isomorphic? If isomorphic, label the 2nd graph to show the isomorphism, else identify difference. Same # of vertices a b Same # of edges Different # of verts of degree 2! (1 vs 3) d e c 9/17/2018 (c) , Michael P. Frank
61
We now jump to the area that gave raise to the invention of (a formal theory of) graphs
Questions like “Can I travel from a to b?” “Can I go travel a to b without going anywhere twice?” “What’s the quickest route from a to b?” 9/17/2018 (c) , Michael P. Frank
62
§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. 9/17/2018 (c) , Michael P. Frank
63
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. 9/17/2018 (c) , Michael P. Frank
64
( 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 ) 9/17/2018 (c) , Michael P. Frank
65
Paths & Isomorphism Note that connectedness, and the existence of a circuit or simple circuit of length k are graph invariants with respect to isomorphism. 9/17/2018 (c) , Michael P. Frank
66
§8.5: Euler & Hamilton Paths
We’ll show you the problem that prompted Euler to invent the theory of graphs: the bridges of Koenigsberg (town later called Kaliningrad) 9/17/2018 (c) , Michael P. Frank
67
Bridges of Königsberg Problem
Can we walk through town, crossing each bridge exactly once, and return to start? 9/17/2018 (c) , Michael P. Frank
68
Bridges of Königsberg Problem
Can we walk through town, crossing each bridge exactly once, and return to start? A Can you model the situation using a graph? D B C The original problem 9/17/2018 (c) , Michael P. Frank
69
Bridges of Königsberg Problem
Can we walk through town, crossing each bridge exactly once, and return to start? A D B C Equivalent multigraph The original problem 9/17/2018 (c) , Michael P. Frank
70
§8.5: Euler & Hamilton Paths
Terminology: An Euler circuit in a graph G is a simple circuit containing every edge of G. An Euler path in G is a simple path containing every edge of G. 9/17/2018 (c) , Michael P. Frank
71
Bridges of Koenigsberg
Bridges are edges. So the answer to the problem is YES iff its graph contains an Euler circuit. In fact, it does not … 9/17/2018 (c) , Michael P. Frank
72
Euler Path Theorems Theorem: A finite 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 Theorem: A connected multigraph has an Euler path iff it has exactly 2 vertices of odd degree. One is the start, the other is the end. 9/17/2018 (c) , Michael P. Frank
73
Not all edges have even degree
… so there is no Euler circuit. A D B C Equivalent multigraph The original problem 9/17/2018 (c) , Michael P. Frank
74
Euler Circuit theorem Sketch of proof that even degree implies existence of Euler circuit: Start with any arbitrary node. Construct simple path from it till you get back to start. (Graph is connected; every node has even degree, so you can leave any node that you have entered) Repeat for each remaining subgraph, splicing results back into original cycle. Finiteness of graph implies that this process must end. Note that the complete version of this proof provides an algorithm: it’s a constructive proof of an existential proposition 9/17/2018 (c) , Michael P. Frank
75
( Hamilton Paths An Euler circuit in a graph G is a simple circuit containing every edge of G. An Euler path in G is a simple 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. ) 9/17/2018 (c) , Michael P. Frank
76
( Hamiltonian Path Theorems
Dirac’s theorem: If (but not only if) G is connected, simple, has n3 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. ) 9/17/2018 (c) , Michael P. Frank
77
( 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. ) 9/17/2018 (c) , Michael P. Frank
78
§8.6: Shortest-Path Problems
Not covering this year. 9/17/2018 (c) , Michael P. Frank
79
§8.7: Planar Graphs Not covering this year. 9/17/2018
(c) , Michael P. Frank
80
§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 ≥ ___ ). 9/17/2018 (c) , Michael P. Frank
81
Tree and Forest Examples
Leaves in green, internal nodes in brown. A Tree: A Forest: 9/17/2018 (c) , Michael P. Frank
82
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. Concepts related to rooted trees: Parent, child, siblings, ancestors, descendents, leaf, internal node, subtree. 9/17/2018 (c) , Michael P. Frank
83
Same tree except for choice of root
Rooted Tree Examples Note that a given unrooted tree with n nodes yields n different rooted trees. Same tree except for choice of root root root 9/17/2018 (c) , Michael P. Frank
84
Rooted-Tree Terminology Exercise
Find the parent, children, siblings, ancestors, & descendants of node f. o n h r d m b root a c g e q i f l p j k 9/17/2018 (c) , Michael P. Frank
85
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. 9/17/2018 (c) , Michael P. Frank
86
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 ≤ ___. 9/17/2018 (c) , Michael P. Frank
87
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. 9/17/2018 (c) , Michael P. Frank
88
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? 9/17/2018 (c) , Michael P. Frank
89
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. 9/17/2018 (c) , Michael P. Frank
90
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. 9/17/2018 (c) , Michael P. Frank
91
§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 9/17/2018 (c) , Michael P. Frank
92
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. 9/17/2018 (c) , Michael P. Frank
93
Binary Search Tree Format
Items are stored at individual tree nodes. We arrange for the tree to always obey this invariant: For every item x, Every node in x’s left subtree is less than x. Every node in x’s right subtree is greater than x. Example: 7 3 12 1 5 9 15 2 8 11 9/17/2018 (c) , Michael P. Frank
94
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) 9/17/2018 (c) , Michael P. Frank
95
Decision Trees (pp ) 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. 9/17/2018 (c) , Michael P. Frank
96
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? ? 9/17/2018 (c) , Michael P. Frank
97
As a Decision-Tree Problem
In each situation, we pick two disjoint and equal-size subsets of coins to put on the scale. A given sequence of weighings thus yields a decision tree with branching factor 3. The balance then “decides” whether to tip left, tip right, or stay balanced. 9/17/2018 (c) , Michael P. Frank
98
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… 9/17/2018 (c) , Michael P. Frank
99
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. 9/17/2018 (c) , Michael P. Frank
100
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 − 2n/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. 9/17/2018 (c) , Michael P. Frank
101
Coin Balancing Decision Tree
Here’s what the tree looks like in our case: 123 vs 456 left: 123 balanced: 78 right: 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 9/17/2018 (c) , Michael P. Frank
102
Prefix Codes & Huffman Coding
pp 9/17/2018 (c) , Michael P. Frank
103
Game Trees pp 9/17/2018 (c) , Michael P. Frank
104
§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 9/17/2018 (c) , Michael P. Frank
105
§9.4: Spanning Trees Not covering this semester. 9/17/2018
(c) , Michael P. Frank
106
§9.5: Minimum Spanning Trees
Not covering this semester. 9/17/2018 (c) , Michael P. Frank
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.