# Discrete Mathematics I Lectures Chapter 10

## Presentation on theme: "Discrete Mathematics I Lectures Chapter 10"— Presentation transcript:

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
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 eE, 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 uV Duplicate edges: A graph is called a multi-graph if there is at least one duplicate edge.

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 eE, e = (u, v) where u, v  V. An edge e is a loop if e = (u, u) for some uV. A simple graph is just like a directed graph, but with no specified direction of its edges.

Graph Models Example 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übeck Toronto Hamburg

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. Bruins Maple Leafs Penguins Lübeck Giants

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

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}.

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

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.

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? a b c d f h g j e 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.

Graph Terminology Determine the number of its edges and the sum of the degrees of all its vertices: a b c d f h g j 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.

Graph Terminology The Handshaking Theorem: Let G = (V, E) be an undirected graph with e edges. Then 2e = vV 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 610 = 60. According to the Handshaking Theorem, it follows that 2e = 60, so there are 30 edges.

2|E| = vV deg(v) = vV1 deg(v) + vV2 deg(v)
Graph Terminology Theorem: 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| = vV deg(v) = vV1 deg(v) + vV2 deg(v) Since both 2|E| and vV1 deg(v) are even, vV2 deg(v) must be even. Since deg(v) is odd for all vV2, |V2| must be even. QED

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

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?

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.

Graph Terminology Definition: 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.

Graph Terminology Example: What are the in-degrees and out-degrees of the vertices a, b, c, d in this graph: deg-(a) = 1 deg+(a) = 2 deg-(b) = 4 deg+(b) = 2 a b c d deg-(d) = 2 deg+(d) = 1 deg-(c) = 0 deg+(c) = 2

Graph Terminology Theorem: Let G = (V, E) be a graph with directed edges. Then: vV deg-(v) = vV 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.

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

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

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?

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

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

Special Graphs Definition: The n-cube, denoted by Qn, is the graph that has vertices representing the 2n bit strings of length n. Two vertices are adjacent if and only if the bit strings that they represent differ in exactly one bit position. 000 001 101 100 010 011 111 110 00 01 11 10 1 Q1 Q2 Q3

Special Graphs Definition: 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).

Special Graphs Example I: Is C3 bipartite?
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. v1 v2 v3 Example II: Is C6 bipartite? v5 v1 v2 v3 v4 v6 v1 v6 v2 v5 v3 v4 Yes, because we can display C6 like this:

Special Graphs Definition: 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

Exercise 6 Draw the complete bipartite graphs for K2,2, K2,3, and K3,4

Exercise 7 Determine a formula for the number of edges in a complete bipartite graph.

Exercise 8 What is the degree of each vertex in the complete bipartite graph K4,5?

Operations on Graphs Definition: A subgraph of a graph G = (V, E) is a graph H = (W, F) where WV and FE. 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

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

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

Section 10.2 Representing Graphs, Walks, Paths and Circuits, Connectedness

Representing Graphs a b c d a b c d a, d b c a, b, c d b, c, d a
Adjacent Vertices Vertex a b c a, b, c d Terminal Vertices Initial Vertex

Representing Graphs Definition: 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 nn 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.

Representing Graphs Example: What is the adjacency matrix AG for the following graph G based on the order of vertices a, b, c, d ? a b c d Solution: Note: Adjacency matrices of undirected graphs are always symmetric.

Graph Walks Definition: 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.

Paths and Circuits A 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 walk A trail is a walk with no repeated edges A path is a walk with no repeated vertices A circuit is a closed walk with no repeated edges A 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

Exercise 1 In the given graph, determine whether each of the following is a path, simple path, circuit, or simple circuit abcfb abcf fabfcdf fabcdf abfb cfbbc bb e A F E B C D

Connectivity Let 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.

Exercise 2: Connected or not?
a b c e d a b c e Yes. No. d a b c e d a b c e f No. Yes.

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 connected H is not a proper subgraph of any connected subgraph of G It follows that a graph is connected  G has only one connected component

Exercise 3 Example: What are the connected components in the following graph? a b c d g h i j f e Solution: The connected components are the graphs with vertices {a, b, c, d}, {e}, {f}, {g, h, i, j}.

Exercise 4 What is the minimum number of edges possible in a connected graph on 4 vertices?

Mathematical Induction
Let p(n) be a statement in an integer variable n. Suppose: p(1) is true [base step] 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

Exercise 5 Use induction on n to prove that a connected graph on n vertices has at least n – 1 edges.

Section 10.5 Trees

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

Exercise 1 Which of the following is a tree?

Tree Example—Decision Tree

Tree Example: Directory Structures
Program Files\ WINDOWS\ Documents and Settings\ apanthon\ katchins\ My Documents\ Desktop\

Tree Example: Parse Tree
“The young man caught the ball” Sentence Noun Phrase Verb Phrase Article Adjective noun verb Noun Phrase The young man caught the ball

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

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 c a b c

Tree Properties Let T = {V,E} be a graph. The following are equivalent: T is connected and removing any edge from T disconnects T into two subgraphs that are trees (subtrees) There is a unique path between any two distinct vertices v and w in T T is a tree Use a cyclical proof to show this is true

Proof: ab 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.

Proof ab (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.

Proof bc 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

Proof of bc (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.

Proof of ca 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.

Proof: a  b (OLD ORDER) Prove that if T is a tree, then for any vertices u,v there is a unique path from u to, v By contradiction, suppose not, that T is a tree and there are two non-unique paths p1 and p2 from u to v. Since T is a simple graph, the paths can be reversed. Let p2rev be the reversal of p2. Following p1, then p2rev, we follow a conjoined path that starts at u, passes through v and ends at u. This is a cycle, contradicting the claim that T is a tree.

Proof: b  c (OLD ORDER) Prove that if every distinct pair of vertices u and v have a unique path, then removing one edge from T will result in two disconnected subgraphs that are trees Take any edge {x,y} in T. This edge identifies the one unique path between x and y. Removing this edge results in two disconnected subgraphs since there are no other paths connecting x and y. These subgraphs must be trees, since the presence of a cycle would imply that there is more than one path between two vertices (one that goes around the ‘top’ of the cycle, one around the ‘bottom’)

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

Exercise 2 Prove by contradiction that a finite tree with more than 1 vertex has at least 1 vertex of degree 1

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?

Exercise 4 Let T be a graph on n vertices. Prove that the following are all equivalent: T is a tree T is connected and has n – 1 edges T is acyclic and has n – 1 edges

Section 10.6 Rooted Trees and Tree Traversals

What is a rooted tree? Parent Root(level = 0) Internal Vertex
Child Leaf(level = 2)

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.

Exercise 1 e v w r s x t z u y i h d c b a g f Find the: Level of e
Height of the tree Children of t Parent of t Ancestors of g Descendants of t Leaves of the tree e v w r s x t z u y i h d c b a g f

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

Exercise 3 Use a complete binary tree to represent the following mathematical expressions: a + b (a + b)  ((c * d) – e)

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)

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:

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:

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?

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?

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?

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?

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?

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?

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

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?

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

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?

Binary Tree Traversals
YES THIS MATERIAL WILL BE ON THE EXAM!!!

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

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 TR A A B C B C

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!

Exercise 1 Find the pre-order traversal of the following tree (ROOT-L-R): Your Turn! Finish the traversal by performing a pre-order traversal of the right subtree. r a b c d e f g h i j k l m n o p q s Result:

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: A B C A B C A B A C B C

Exercise 2 Find the Post-Order (L-R-Root) traversal: r a b c d e f g h
j k l m n o p q s

Exercise 3 Find the in-order (L-Root-R) traversal: r a b c d e f g h i
j k l m n o p q s

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? * + - a b c d

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 * + - a b c d

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 * + - a b c d

Why pre-fix, post-fix matter
You don’t need parentheses!!! Consider the prefix notation: * + a b – c d Scan rightleft. 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! *  16 4

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 –  *

Exercise 4 Try some more! How about logic (works similarly)?
Pre-fix: ^ 8 * Post-fix: 3 5 * ^ 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?