discrete mathematics i lectures chapter 10

99
DISCRETE MATHEMATICS I LECTURES CHAPTER 10 Dr. Adam Anthony Spring 2011 Some material adapted from lecture notes provided by Dr. Chungsim Han and Dr. Sam Lomonaco

Upload: kalyca

Post on 24-Feb-2016

84 views

Category:

Documents


6 download

DESCRIPTION

Discrete Mathematics I Lectures Chapter 10. Some material adapted from lecture notes provided by Dr. Chungsim Han and Dr. Sam Lomonaco. Dr. Adam Anthony Spring 2011. Section 10.1. Introduction to Graphs. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Discrete Mathematics I Lectures Chapter 10

DISCRETE MATHEMATICS ILECTURES CHAPTER 10Dr. Adam AnthonySpring 2011

Some material adapted from lecture notes provided by Dr. Chungsim Han and Dr. Sam Lomonaco

Page 2: Discrete Mathematics I Lectures Chapter 10

2 Section 10.1

Page 3: Discrete Mathematics I Lectures Chapter 10

3

Introduction to Graphs

Definition: A simple graph G = (V, E) consists of V, a nonempty set of vertices, and E, a set of unordered pairs of distinct elements of V called edges.For each eE, e = {u, v} where u, v V.An undirected graph (not simple) may contain:

Loops: An edge e is a loop if e = {u, u} for some uVDuplicate edges: A graph is called a multi-graph if there is at least one duplicate edge.

Page 4: Discrete Mathematics I Lectures Chapter 10

4

Introduction to Graphs

Definition: A directed graph G = (V, E) consists of a set V of vertices and a set E of edges that are ordered pairs of elements in V.For each eE, e = (u, v) where u, v V.An edge e is a loop if e = (u, u) for some uV.A simple graph is just like a directed graph, but with no specified direction of its edges.

Page 5: Discrete Mathematics I Lectures Chapter 10

5

Graph ModelsExample I: How can we represent a network of (bi-directional) railways connecting a set of cities?We should use a simple graph with an edge {a, b} indicating a direct train connection between cities a and b.

New York

Boston

Washington

LübeckToronto

Hamburg

Page 6: Discrete Mathematics I Lectures Chapter 10

6

Graph Models

Example II: In a round-robin tournament, each team plays against each other team exactly once. How can we represent the results of the tournament (which team beats which other team)?We should use a directed graph with an edge (a, b) indicating that team a beats team b.

Penguins

Bruins

Lübeck Giants

Maple Leafs

Page 7: Discrete Mathematics I Lectures Chapter 10

7

Exercise 1 What might the nodes/edges be if we

modeled the following data? Would the graph be best undirected or directed? Acquaintance Graph: Identifying people are

mutually acquainted Influence Graph: Identifying data where one

person has influence over another. Computer network Road Map The World Wide Web

Page 8: Discrete Mathematics I Lectures Chapter 10

8

Graph Terminology

Definition: Two vertices u and v in an undirected graph G are called adjacent (or neighbors) in G if {u, v} is an edge in G.If e = {u, v}, the edge e is called incident with the vertices u and v. The edge e is also said to connect u and v.The vertices u and v are called endpoints of the edge {u, v}.

Page 9: Discrete Mathematics I Lectures Chapter 10

9

Graph Terminology

Definition: The degree of a vertex in an undirected graph is the number of edges incident with it, except that a loop at a vertex contributes twice to the degree of that vertex.In other words, you can determine the degree of a vertex in a displayed graph by counting the lines that touch it.The degree of the vertex v is denoted by deg(v).

Page 10: Discrete Mathematics I Lectures Chapter 10

10

Graph Terminology

A vertex of degree 0 is called isolated, since it is not adjacent to any vertex. Note: A vertex with a loop at it has at least degree 2 and, by definition, is not isolated, even if it is not adjacent to any other vertex.A vertex of degree 1 is called pendant. It is adjacent to exactly one other vertex.

Page 11: Discrete Mathematics I Lectures Chapter 10

11

Graph Terminology

Example: Which vertices in the following graph are isolated, which are pendant, and what is the maximum degree? What type of graph is it?

Solution: Vertex f is isolated, and vertices a, d and j are pendant. The maximum degree is deg(g) = 5. This graph is a non-simple undirected graph.

a

b c

df h

gjf

e

Page 12: Discrete Mathematics I Lectures Chapter 10

12

Graph Terminology

Determine the number of its edges and the sum of the degrees of all its vertices:

a

b c

df h

gjf

e

Result: There are 9 edges, and the sum of all degrees is 18. This is easy to explain: Each new edge increases the sum of degrees by exactly two.

Page 13: Discrete Mathematics I Lectures Chapter 10

13

Graph TerminologyThe Handshaking Theorem: Let G = (V, E) be an undirected graph with e edges. Then2e = vV deg(v)

Corrolary: The total degree of any undirected graph is always even!

Example: How many edges are there in a graph with 10 vertices, each of degree 6?Solution: The sum of the degrees of the vertices is 610 = 60. According to the Handshaking Theorem, it follows that 2e = 60, so there are 30 edges.

Page 14: Discrete Mathematics I Lectures Chapter 10

14

Graph TerminologyTheorem: An undirected graph has an even number of vertices of odd degree. Proof: Let V1 and V2 be the set of vertices of even and odd degrees, respectively (Thus V1 V2 = , and V1 V2 = V). Then by Handshaking theorem

2|E| = vV deg(v) = vV1 deg(v) + vV2 deg(v) Since both 2|E| and vV1 deg(v) are even, vV2 deg(v) must be even. Since deg(v) is odd for all vV2, |V2| must be even.

QED

Page 15: Discrete Mathematics I Lectures Chapter 10

15

Exercise 2 Draw a graph with the specified

properties or show that no such graph exists: A graph with 6 vertices with the following

degrees: 1,1,2,2,3,4 A graph with 4 vertices of degrees 1,2,3,4 A simple graph with 4 vertices of degrees

1,2,3,4

Page 16: Discrete Mathematics I Lectures Chapter 10

16

Exercise 3 A graph has 5 vertices of degrees

1,1,4,4, and 6. How many edges does the graph have?

Is it possible in a group of 13 people for each to shake hands with exactly 7 others?

Is it possible to have a graph with 15 edges where each vertex has degree 4?

Is it possible to have a simple graph with 10 edges where each vertex has degree 4?

Page 17: Discrete Mathematics I Lectures Chapter 10

17

Graph Terminology

Definition: When (u, v) is an edge of the graph G with directed edges, u is said to be adjacent to v, and v is said to be adjacent from u. The vertex u is called the initial vertex (or source) of (u, v), and v is called the terminal vertex (or target) of (u, v).The initial vertex and terminal vertex of a loop are the same.

Page 18: Discrete Mathematics I Lectures Chapter 10

18

Graph TerminologyDefinition: In a graph with directed edges, the in-degree of a vertex v, denoted by deg-(v), is the number of edges with v as their terminal vertex.The out-degree of v, denoted by deg+(v), is the number of edges with v as their initial vertex.Question: How does adding a loop to a vertex change the in-degree and out-degree of that vertex?Answer: It increases both the in-degree and the out-degree by one.

Page 19: Discrete Mathematics I Lectures Chapter 10

19

Graph Terminology

Example: What are the in-degrees and out-degrees of the vertices a, b, c, d in this graph: a b

cd

deg-(a) = 1deg+(a) = 2

deg-(b) = 4deg+(b) = 2

deg-(d) = 2deg+(d) = 1

deg-(c) = 0deg+(c) = 2

Page 20: Discrete Mathematics I Lectures Chapter 10

20

Graph Terminology

Theorem: Let G = (V, E) be a graph with directed edges. Then:vV deg-(v) = vV deg+(v) = |E|

This is easy to see, because every new edge increases both the sum of in-degrees and the sum of out-degrees by one.

Page 21: Discrete Mathematics I Lectures Chapter 10

21

Exercise 4 What is the maximum number of edges

possible in a simple graph on n vertices?

What is the maximum number of edges possible in a directed graph on n vertices (loops included)?

Page 22: Discrete Mathematics I Lectures Chapter 10

22

Special Graphs

Definition: The complete graph on n vertices, denoted by Kn, is the simple graph that contains exactly one edge between each pair of distinct vertices.

K1 K2 K3 K4 K5

Page 23: Discrete Mathematics I Lectures Chapter 10

23

Exercise 5 What is the degree of each vertex in the

complete graph K9? What is the total degree of K9? How many edges are there in K9? How many edges are there in Kn? What is the degree of a vertex in Kn? What is the total degree of Kn?

Page 24: Discrete Mathematics I Lectures Chapter 10

24

Special Graphs

Definition: The cycle Cn, n 3, consists of n vertices v1, v2, …, vn and edges {v1, v2}, {v2, v3}, …, {vn-1, vn}, {vn, v1}.

C3 C4 C5 C6

Page 25: Discrete Mathematics I Lectures Chapter 10

25

Special Graphs

Definition: We obtain the wheel Wn when we add an additional vertex to the cycle Cn, for n 3, and connect this new vertex to each of the n vertices in Cn by adding new edges.

W3 W4 W5 W6

Page 26: Discrete Mathematics I Lectures Chapter 10

27

Special GraphsDefinition: A simple graph is called bipartite if its vertex set V can be partitioned into two disjoint nonempty sets V1 and V2 such that every edge in the graph connects a vertex in V1 with a vertex in V2 (so that no edge in G connects either two vertices in V1 or two vertices in V2).For example, consider a graph that represents each person in a village by a vertex and each marriage by an edge.This graph is bipartite, because each edge connects a vertex in the subset of males with a vertex in the subset of females (if we think of traditional marriages).

Page 27: Discrete Mathematics I Lectures Chapter 10

Special Graphs28

Example I: Is C3 bipartite?v1

v2 v3

No, because there is no way to partition the vertices into two sets so that there are no edges with both endpoints in the same set.Example II: Is C6 bipartite?

v5

v1

v2

v3 v4

v6 v1 v6

v2v5

v3 v4

Yes, because we can display C6 like this:

Page 28: Discrete Mathematics I Lectures Chapter 10

29

Special GraphsDefinition: The complete bipartite graph Km,n is the graph that has its vertex set partitioned into two subsets of m and n vertices, respectively. Two vertices are connected if and only if they are in different subsets.

K3,2 K3,4

Page 29: Discrete Mathematics I Lectures Chapter 10

30

Exercise 6 Draw the complete bipartite graphs for

K2,2, K2,3, and K3,4

Page 30: Discrete Mathematics I Lectures Chapter 10

32

Exercise 8 What is the degree of each vertex in the

complete bipartite graph K4,5?

Page 31: Discrete Mathematics I Lectures Chapter 10

33

Operations on Graphs

Definition: A subgraph of a graph G = (V, E) is a graph H = (W, F) where WV and FE.Note: Of course, H is a valid graph, so we cannot remove any endpoints of remaining edges when creating H.Example:

K5 subgraph of K5

Page 32: Discrete Mathematics I Lectures Chapter 10

34

Operations on Graphs

Definition: The union of two simple graphs G1 = (V1, E1) and G2 = (V2, E2) is the simple graph with vertex set V1 V2 and edge set E1 E2. The union of G1 and G2 is denoted by G1 G2.

G1 G2 G1 G2 = K5

Page 33: Discrete Mathematics I Lectures Chapter 10

35

Exercise 9 Let G be a simple graph with V =

{a,b,c,d,e} and E = {{a,a},{a,b},{a,c},{b,c},{c,d}}. Is H = (VH,EH) with VH={a,b,c,d} and

EH={{a,c},{b,c},{c,d}} a subgraph of G? If so, find a second subgraph L such that H

L = G

Page 34: Discrete Mathematics I Lectures Chapter 10

36

Representing Graphs, Walks, Paths and Circuits, Connectedness

Section 10.2

Page 35: Discrete Mathematics I Lectures Chapter 10

37

Representing Graphs

ab

cd

ab

c

d

a, dba, dc

a, b, cd

b, c, da

Adjacent VerticesVertex

abc

a, b, cd

ca

Terminal Vertices

Initial Vertex

Page 36: Discrete Mathematics I Lectures Chapter 10

38

Representing GraphsDefinition: Let G = (V, E) be a simple graph with |V| = n. Suppose that the vertices of G are listed in arbitrary order as v1, v2, …, vn. The adjacency matrix A (or AG) of G, with respect to this listing of the vertices, is the nn zero-one matrix with 1 as its (i, j)th entry when vi and vj are adjacent, and 0 otherwise.In other words, for an adjacency matrix A = [aij], aij = 1 if {vi, vj} is an edge of G,aij = 0 otherwise.

Page 37: Discrete Mathematics I Lectures Chapter 10

39

Representing Graphs

Example: What is the adjacency matrix AG for the following graph G based on the order of vertices a, b, c, d ?

ab

cdSolution:

Note: Adjacency matrices of undirected graphs are always symmetric.

0111100110011110

GA

Page 38: Discrete Mathematics I Lectures Chapter 10

40

Graph WalksDefinition: A walk of length n from u to v, where n is a positive integer, in an undirected graph is a sequence of edges e1, e2, …, en of the graph such that

e1 = {x0, x1}, e2 = {x1, x2}, …, en = {xn-1, xn}, where x0 = u and xn = v.When the graph is simple, we denote this path by its vertex sequence x0, x1, …, xn, since it uniquely determines the path.The path or circuit is said to pass through or traverse x1, x2, …, xn-1.

Page 39: Discrete Mathematics I Lectures Chapter 10

41

Paths and CircuitsA trivial walk from v to v consists of the single vertex v, and no edges. The path is a closed-walk if it begins and ends at the same vertex, that is, if u = v. The length of a walk is the number of edges in the walkA trail is a walk with no repeated edgesA path is a walk with no repeated verticesA circuit is a closed walk with no repeated edgesA path or circuit is simple if it has no repeated vertices (except the first and last in a circuit). A non-trivial simple circuit is called a cycle

Page 40: Discrete Mathematics I Lectures Chapter 10

42

Exercise 1 In the given graph, determine whether each of the

following is a path, simple path, circuit, or simple circuit

1. abcfb2. abcf3. fabfcdf4. fabcdf 5. abfb6. cfbbc7. bb8. e

A

B C D

F E

Page 41: Discrete Mathematics I Lectures Chapter 10

43

ConnectivityLet us now look at something new:Definition: An undirected graph is called connected if there is a walk (or a path, or a simple path) between every pair of distinct vertices in the graph. For example, any two computers in a network can communicate if and only if the graph of this network is connected.Note: A graph consisting of only one vertex is always connected, because it does not contain any pair of distinct vertices.

Page 42: Discrete Mathematics I Lectures Chapter 10

44

Exercise 2: Connected or not?

d

ab

c

e

Yes.d

ab

ce

No.

d

ab

c

e

Yes.

d

ab

ce

fNo.

Page 43: Discrete Mathematics I Lectures Chapter 10

45

Connectivity

A graph that is not connected is the union of two or more connected subgraphs, each pair of which has no vertex in common. A subgraph H is a connected component of a graph G if:

H is connectedH is not a proper subgraph of any connected subgraph of G

It follows that a graph is connected G has only one connected component

Page 44: Discrete Mathematics I Lectures Chapter 10

46

Exercise 3

Example: What are the connected components in the following graph?

a

b c

dg h

ijf

e

Solution: The connected components are the graphs with vertices {a, b, c, d}, {e}, {f}, {g, h, i, j}.

Page 45: Discrete Mathematics I Lectures Chapter 10

47

Exercise 4 What is the minimum number of edges

possible in a connected graph on 4 vertices?

Page 46: Discrete Mathematics I Lectures Chapter 10

48

Mathematical Induction Let p(n) be a statement in an integer

variable n. Suppose:

i. p(1) is true [base step]ii. For all integers k 2, if p(k – 1) is true

(induction hypothesis) To be shown: Given i and ii above,

prove that p(k) must also be true

Page 47: Discrete Mathematics I Lectures Chapter 10

49

Exercise 5 Use induction on n to prove that a

connected graph on n vertices has at least n – 1 edges.

Page 48: Discrete Mathematics I Lectures Chapter 10

50

Trees

Section 10.5

Page 49: Discrete Mathematics I Lectures Chapter 10

51

What is a Tree? A graph is called acyclic if it has no non-

trivial circuits A tree is an acyclic, connected graph A trivial tree is a graph with a single

vertex A forest is an acyclic, disconnected graph

A tree is a special kind of simple graph

Page 50: Discrete Mathematics I Lectures Chapter 10

52

Exercise 1 Which of the following is a tree?

Page 51: Discrete Mathematics I Lectures Chapter 10

53

Tree Example—Decision Tree

Page 52: Discrete Mathematics I Lectures Chapter 10

54

Tree Example: Directory Structures

C:\

WINDOWS\

Documents and Settings\

Program Files\

apanthon\

katchins\

My Documents\

Desktop\

Page 53: Discrete Mathematics I Lectures Chapter 10

Tree Example: Parse Tree55

“The young man caught the ball”Sentence

Verb Phrase

Noun Phrase

verb

Noun Phrase

caught

the

ball

Article

Adjective

noun

The

young

man

Page 54: Discrete Mathematics I Lectures Chapter 10

56

Every Graph Contains A Tree Let G be any graph

If G has no cycles, then it is a tree! If G does have cycles, for each cycle

Remove one edge from the cycle G will still be connected. Why?

The resulting graph G’ will be an acyclic tree

Page 55: Discrete Mathematics I Lectures Chapter 10

57

Cyclical Proofs How do we prove a b?

Prove a b and b a

a b means “a and b are equivalent” What if we say “a, b, and c are

equivalent? a b c

a b

a b ca b

c

Page 56: Discrete Mathematics I Lectures Chapter 10

58

Tree Properties Let T = {V,E} be a graph. The following

are equivalent: a) T is connected and removing any edge

from T disconnects T into two subgraphs that are trees (subtrees)

b) There is a unique path between any two distinct vertices v and w in T

c) T is a tree Use a cyclical proof to show this is true

Page 57: Discrete Mathematics I Lectures Chapter 10

59

Proof: ab Prove that if T is connected and

removing any single edge results in two disconnected subgraphs, both of which are trees, then there is a unique path between any two vertices u and v

Proof by contraposition: Prove that if there exists at least one

alternative path between u and v, then either T is not connected or removing any edge will not result in two disconnected subgraphs.

Page 58: Discrete Mathematics I Lectures Chapter 10

60

Proof ab (continued) Suppose T is any graph, such that there are

two distinct paths between some vertices u and v. If there are two distinct paths, then there must be

a cycle in the graph, found by following the first path from u to v, then reversing the second path to get back from v to u.

We have already observed that removing an edge in a cycle will not disconnect a graph. Therefore, there is at least one edge which can be removed from this graph that will not disconnect T, which is what was to be shown.

Page 59: Discrete Mathematics I Lectures Chapter 10

61

Proof bc Prove that if there exists a unique path

between all pairs of vertices in T, then T must be a tree.

Proof by contraposition that if T is not a tree, then there is not a unique path between all pairs of vertices in T There are two cases:

T is not connected T has a cycle

Page 60: Discrete Mathematics I Lectures Chapter 10

62

Proof of bc (continued) Case 1: T is not connected

Then there must not be a path between at least two vertices u and v, which is what was to be shown.

Case 2: T has a cycle Pick any edge {u,v} on the cycle. This

represents a path from u to v. Now follow a path the ‘long way around’ the cycle, starting at v and ending at u. This path is distinct from the first, which is what was to be shown.

Page 61: Discrete Mathematics I Lectures Chapter 10

63

Proof of ca Prove that if T is a tree, then T is connected and

removing any edge results in two disconnected subgraphs that are themselves trees.

Suppose T is a graph that is a tree Then T is, by definition, connected and has no cycles Remove any one edge {u,v} from T. Since T has no cycles,

then this edge represents a unique path from u to v because if there were another path, then {u,v} would complete a cycle

Therefore, removing {u,v} disconnects the graph into two distinct components.

The two subgraphs have no cycles, and since only one edge was removed, they are connected. Thus they are trees.

Page 62: Discrete Mathematics I Lectures Chapter 10

66

More tree properties! A tree T with n vertices has n-1 edges. Proof (by induction):

A tree with n = 1 vertex has n – 1 = 0 edges. For a graph with n 2 vertices, suppose that any graph with n

– 1 or fewer vertices has one less edge than it does vertices. Take a tree with n vertices and remove a single edge from the

tree. This results in two smaller subtrees T1 = (V1,E1) and T2 = (V2 E2).

n = |V| = |V1| + |V2| |E| = |E1| + |E2| + 1 By the induction hypothesis, |E1| = |V1| - 1 and |E2| = |V2| - 1 Then |E| = (|V1| - 1) + (|V2| - 1) + 1 = |V1| + |V2| - 1 = |V| - 1, which is what was to be shown

Page 63: Discrete Mathematics I Lectures Chapter 10

67

Exercise 2 Prove by contradiction that a finite tree

with more than 1 vertex has at least 1 vertex of degree 1

Page 64: Discrete Mathematics I Lectures Chapter 10

68

Exercise 3 Is a graph with 12 vertices and 12 edges a tree?

Is any graph with 5 vertices and 4 edges a tree?

Is any connected graph with 5 vertices and 4 edges a tree?

Page 65: Discrete Mathematics I Lectures Chapter 10

69

Exercise 4 Let T be a graph on n vertices. Prove

that the following are all equivalent:a) T is a treeb) T is connected and has n – 1 edgesc) T is acyclic and has n – 1 edges

Page 66: Discrete Mathematics I Lectures Chapter 10

70

Rooted Trees and Tree Traversals

Section 10.6

Page 67: Discrete Mathematics I Lectures Chapter 10

71

What is a rooted tree?

Root(level = 0)Internal Vertex(level = 1)Leaf(level = 2)

Parent

Child

Page 68: Discrete Mathematics I Lectures Chapter 10

72

Tree Definitions The root is any vertex in a tree that is selected to be

the root Level(v) = #edges it takes to reach v from the root height(T) = the maximum level of any vertices in T Children(v) all vertices adjacent to v whose level is

Level(v) + 1 Parent(v) The unique vertex that is adjacent to v

whose level is Level(v) – 1 A leaf is a node with no children. An ancestor v is any vertex w that lies on the path

from the root to v. v would be considered a descendant of all such vertices.

Page 69: Discrete Mathematics I Lectures Chapter 10

73

Exercise 1

e

v w

r

s

x

t

z

u

y

ih

dcba

gf

Find the: a) Level of eb) Height of the

treec) Children of td) Parent of te) Ancestors of gf) Descendants

of tg) Leaves of the

tree

Page 70: Discrete Mathematics I Lectures Chapter 10

74

Exercise 2 Design a tree to represent the table of

contents of a book: C1S1.1S1.2S1.3C2S2.1S2.2S2.2.1S2.2.2C3S3.1S3.2

Page 71: Discrete Mathematics I Lectures Chapter 10

75

Binary Trees A Binary Tree is a rooted tree in which each internal

vertex has at most two children Since there are only two, they get special names:

Left child Right child If there is only one, call it the left child

Subtrees get special names too! The left subtree of a vertex v is the subtree rooted at the

left child of v The right subtree of a vertex v is the subtree rooted at the

right child of v A full (or complete) binary tree is a binary tree in

which each internal vertex has exactly two children

Page 72: Discrete Mathematics I Lectures Chapter 10

76

Exercise 3 Use a complete binary tree to represent

the following mathematical expressions: a + b (a + b) ((c * d) – e)

Page 73: Discrete Mathematics I Lectures Chapter 10

77

M-ary Trees An m-ary tree is a rooted tree in which

each internal vertex has at most m children

A full m-ary tree is an m-ary tree in which each internal vertex has exactly m children

m = 2 (binary tree), m = 3 (ternary tree)

Page 74: Discrete Mathematics I Lectures Chapter 10

78

Theorem 1 Let T be a full m-ary tree with n vertices,

i internal vertices, and L leaves. Then each of the following is true:

mn

mLi

miLmin

111 c)

1)1( b)1 a)

Page 75: Discrete Mathematics I Lectures Chapter 10

79

Corollary to Theorem 1 Let T be a full binary tree with n vertices,

i internal vertices, and L leaves. Then each of the following is true:

211 c)

211 b)

12 a)

nLi

niL

in

Page 76: Discrete Mathematics I Lectures Chapter 10

80

Exercise 4 How many vertices does a full ternary tree

with 11 leaves have?

Is there a full binary tree with 12 vertices?

How many edges does a full 5-ary tree with 100 internal vertices have?

Is there a full binary tree that has 10 internal vertices and 13 leaves?

Page 77: Discrete Mathematics I Lectures Chapter 10

81

Exercise 5 The Wimbledon tennis championship is a

single-elimination tournament in which a player is eliminated after a single loss. If 31 women compete in the championship, how many matches must be played to determine the champion?

Page 78: Discrete Mathematics I Lectures Chapter 10

82

Exercise 6 Suppose someone starts a chain letter.

Each person who receives the letter is asked to send it to 5 other people. If everyone who receives the letter follows the

instructions, how many people can be reached in a tree of height 2?

If 125 people received the letter, but did not send it, determine the following: How many people sent the letter? How many people in total have seen the letter,

including the person who started it?

Page 79: Discrete Mathematics I Lectures Chapter 10

83

Exercise 7 A computer lab has a single wall socket

with 6 outlets in it. Using power strips with 6 connections each, how many extension cords do we need to power 46 all-in-one computers?

Page 80: Discrete Mathematics I Lectures Chapter 10

84

Balanced Trees An m-ary tree of height h is balanced if

every leaf is at level h or h – 1. Which of the following trees are

balanced?

Page 81: Discrete Mathematics I Lectures Chapter 10

85

Number of Vertices in a Binary Tree Based on Height

If we have binary a tree of height h, what is the maximum number of vertices n we can have in that tree? Hint: it’s when every internal vertex has as

many children as possible! Given the solution to the above, how

many leaves are in the tree, given the height?

Page 82: Discrete Mathematics I Lectures Chapter 10

86

Minimum Binary Tree Height Based on Number of Vertices

If a binary tree has n vertices, what is the maximum height?

More interesting, what is the minimum height? 20 n < 21, h 0 21 n < 22, h 1 22 n < 23, h 2 23 n < 24, h 3 2k n < 2k+1, h k (Now let’s solve

this for k!) h log(n)

Page 83: Discrete Mathematics I Lectures Chapter 10

87

M-Ary Tree Version In an m-ary tree of height h, what is the

maximum number of vertices n? #V at Level 0: m0, n 1 #V at Level 1: m1, n m + 1 = (m2 – 1)/(m – 1) #V at Level 2: m2, n m2 + m + 1 = (m3 – 1)/(m – 1) #V at Level 3: m3, n m3 + m2 + m + 1 = (m4 –

1)/(m – 1) #V at Level k: mk, n mk + … + m2 + m + 1 = (mk

– 1)/(m – 1) Back to the chain-letter example, now how

many vertices would there be if the letter was sent by everybody for 10 levels?

Page 84: Discrete Mathematics I Lectures Chapter 10

88

Theorem Prove by induction that if T is an m-ary

tree of height h, then the number of leaves l mh

Then prove the corrolary that if T is an m-ary tree with l leaves, then logm l h

Page 85: Discrete Mathematics I Lectures Chapter 10

89

Exercise 8 A full binary tree has 500 leaves

What is the minimum height of the tree?

Is there a ternary tree of height 4 with 100 leaves?

Page 86: Discrete Mathematics I Lectures Chapter 10

90

YES THIS MATERIAL WILL BE ON THE EXAM!!!

Binary Tree Traversals

Page 87: Discrete Mathematics I Lectures Chapter 10

91

Tree Traversals In mathematics, we’re always studying tree

properties, and they are useful In computing, we’re not just studying trees, we

are storing and processing trees A tree traversal is a method for efficiently

retrieving information from a tree There are three different traversals that have

different applications: Pre-order In-order Post-order

Page 88: Discrete Mathematics I Lectures Chapter 10

92

Preorder Traversal of a Binary Tree Let T be a rooted binary tree with root R,

and left subtree TL and right subtree TR. The Preorder traversal of T is as follows:

‘Visit’ R (print value, perform computation, etc.)

Perform a preorder traversal on TL Perform a preorder traversal on TRA

CBA B C

Page 89: Discrete Mathematics I Lectures Chapter 10

93

About Recursion Recursion is weird…but really cool! The pre-order procedure is very easy to

define because it uses itself as part of the definition! ‘Visit’ R (print value, perform computation,

etc.) Perform a preorder traversal on TL Perform a preorder traversal on TR

If we follow this precisely, then it will gradually take us throughout the entire tree!

Page 90: Discrete Mathematics I Lectures Chapter 10

94

Result:

Exercise 1 Find the pre-order traversal of the following tree

(ROOT-L-R): r

adc

be

ihgf

j

on

k l m

p sq

Your Turn! Finish the traversal by performing a pre-order traversal of the right subtree.

Page 91: Discrete Mathematics I Lectures Chapter 10

95

Post-Order and In-Order Traversals As before, let T be a rooted binary tree with root

R, and left subtree TL and right subtree TR. The post-order traversal of T is:

Perform a post-order traversal of TL Perform a post-order traversal of TR ‘Visit’ R

The in-order traversal of T is: Perform a post-order traversal of TL ‘Visit’ R Perform a post-order traversal of TR

ACB

B C A

ACB

B A C

Page 92: Discrete Mathematics I Lectures Chapter 10

96

Exercise 2 Find the Post-Order (L-R-Root) traversal:

r

adc

be

ihgf

j

on

k l m

p sq

Page 93: Discrete Mathematics I Lectures Chapter 10

97

Exercise 3 Find the in-order (L-Root-R) traversal:

r

adc

be

ihgf

j

on

k l m

p sq

Page 94: Discrete Mathematics I Lectures Chapter 10

98

Arithmetic Trees, Revisited Consider the tree for ((a + b) * (c – d)):

Perform an in-order (L-Root-R) traversal of the tree

How can we get the parentheses back?

*

+ba

-dc

Page 95: Discrete Mathematics I Lectures Chapter 10

99

Arithmetic Trees, Revisited Consider the tree for ((a + b) * (c – d)):

Perform a pre-order traversal

Such an expression is called the prefix form of an arithmetic computation

*

+ba

-dc

Page 96: Discrete Mathematics I Lectures Chapter 10

100

Arithmetic Trees, Revisited Consider the tree for ((a + b) * (c – d)):

Perform a post-order traversal

Such an expression is called the postfix form of an arithmetic computation

*

+ba

-dc

Page 97: Discrete Mathematics I Lectures Chapter 10

101

Why pre-fix, post-fix matter You don’t need parentheses!!! Consider the prefix notation:

* + a b – c d Scan rightleft. When you reach an operator, apply it to the

two values to the right Then replace the three items with the single

answer Try it!

* + - 4 6 8 16 4

Page 98: Discrete Mathematics I Lectures Chapter 10

102

Computing Post-fix Expressions Similarly, compute post-fix expressions

from left-right: You’ll always have two values, then an

operand Apply the operand to the values, then replace

all three with the answer you get. Try again!

4 6 – 8 + 16 4 *

Page 99: Discrete Mathematics I Lectures Chapter 10

103

Exercise 4 Try some more!

Pre-fix: + 4 3 2 ^ 8 * Post-fix: 3 5 * 2 + 4 2 ^ 8 –

How about logic (works similarly)? Let a = TRUE, b = FALSE Prefix: a b a b Postfix: a b a ~ b

Thought exercise: postfix is easier for humans, but prefix is easier for computers. Why?