22
Trees MAT230 Discrete Mathematics Fall 2016 MAT230 (Discrete Math) Trees Fall 2016 1 / 22

Trees - Gordon College Department of Mathematics and ...math-cs.gordon.edu/courses/mat230/notes/trees.pdfB C D E F G H I J K Vertex C is the parent of vertices G, H, and I. Vertices

Embed Size (px)

Citation preview

Page 1: Trees - Gordon College Department of Mathematics and ...math-cs.gordon.edu/courses/mat230/notes/trees.pdfB C D E F G H I J K Vertex C is the parent of vertices G, H, and I. Vertices

Trees

MAT230

Discrete Mathematics

Fall 2016

MAT230 (Discrete Math) Trees Fall 2016 1 / 22

Page 2: Trees - Gordon College Department of Mathematics and ...math-cs.gordon.edu/courses/mat230/notes/trees.pdfB C D E F G H I J K Vertex C is the parent of vertices G, H, and I. Vertices

Outline

1 What is a Tree?

2 Spanning Trees

3 Rooted Trees

4 Binary Trees

MAT230 (Discrete Math) Trees Fall 2016 2 / 22

Page 3: Trees - Gordon College Department of Mathematics and ...math-cs.gordon.edu/courses/mat230/notes/trees.pdfB C D E F G H I J K Vertex C is the parent of vertices G, H, and I. Vertices

Definitions

Definition

A tree is a connected undirected graph that has no cycles or self-loops.

Some examples:

Trees Not a tree:

has a cycle

Not a tree:

disconnected

Definition

A forest is an undirected graph whose components are all trees.

MAT230 (Discrete Math) Trees Fall 2016 3 / 22

Page 4: Trees - Gordon College Department of Mathematics and ...math-cs.gordon.edu/courses/mat230/notes/trees.pdfB C D E F G H I J K Vertex C is the parent of vertices G, H, and I. Vertices

A Theorem About Trees

Theorem

Let T = (V ,E ) be an undirected graph with no self-loops and |V | = n.Then the following statements are equivalent:

1 T is a tree.

2 Any two vertices of T are connected by exactly one path.

3 T is connected and every edge is an isthmus (its removal disconnectsT).

4 T contains no cycles, but the addition of any new edge createsexactly one cycle.

5 T is connected and has n − 1 edges.

MAT230 (Discrete Math) Trees Fall 2016 4 / 22

Page 5: Trees - Gordon College Department of Mathematics and ...math-cs.gordon.edu/courses/mat230/notes/trees.pdfB C D E F G H I J K Vertex C is the parent of vertices G, H, and I. Vertices

A Theorem About Trees

Proof.

(1) ⇒ (2) Suppose T = (V ,E ) is a tree and let u and v be any twovertices in V . By definition T is connected so there is a pathbetween u and v . Suppose there are two paths between uand v . Then there must be two vertices (possibly u and vthemselves) common to both paths but with distinct pathsbetween them. These two paths would then form a cycle,something that is not possible in a tree. Thus, the pathbetween u and v must be unique.

(2) ⇒ (3) Consider any edge e = {u, v} in T . Removing e will removethe only path between u and v , disconnecting the graph.Thus, every edge in T is an isthmus.

(Continued next slide)

MAT230 (Discrete Math) Trees Fall 2016 5 / 22

Page 6: Trees - Gordon College Department of Mathematics and ...math-cs.gordon.edu/courses/mat230/notes/trees.pdfB C D E F G H I J K Vertex C is the parent of vertices G, H, and I. Vertices

A Theorem About Trees

Proof.

(Continued)

(3) ⇒ (4) Suppose T = (V ,E ) contains a cycle. Any two vertices inthis cycle will still be connected after removing any edge inthe cycle. By (3) we know this is not possible, so T cannotcontain a cycle. Now let u and v be any two vertices in V .There is a path between u and v since T is connected.Adding an edge connecting u and v creates a second pathbetween them, and hence creates a cycle containing u and v .

(Continued next slide)

MAT230 (Discrete Math) Trees Fall 2016 6 / 22

Page 7: Trees - Gordon College Department of Mathematics and ...math-cs.gordon.edu/courses/mat230/notes/trees.pdfB C D E F G H I J K Vertex C is the parent of vertices G, H, and I. Vertices

A Theorem About Trees

Proof.

(Continued)

(4) ⇒ (5) Let T = (V ,E ) be a graph with no cycles such that theaddition of any new edge creates a cycle. This implies thatthe vertices the new edge would be incident to are alreadyconnected, which in turn means that T is connected.

Now select any vertex r ∈ V . Consider any two adjacentvertices u, v in T with u being the one with the shorter pathto r . We will call u the parent of v and call v the child of u.The vertex r can have children but no parent. There aren − 1 vertices in V that are not r , and each one has a singleedge connecting it to its parent, and this will account for alledges in the graph. Thus, there are n − 1 edges in T .

(Continued next slide)

MAT230 (Discrete Math) Trees Fall 2016 7 / 22

Page 8: Trees - Gordon College Department of Mathematics and ...math-cs.gordon.edu/courses/mat230/notes/trees.pdfB C D E F G H I J K Vertex C is the parent of vertices G, H, and I. Vertices

A Theorem About Trees

Proof.

(Continued)

(5) ⇒ (1) Left as a potential exercise or exam problem.

MAT230 (Discrete Math) Trees Fall 2016 8 / 22

Page 9: Trees - Gordon College Department of Mathematics and ...math-cs.gordon.edu/courses/mat230/notes/trees.pdfB C D E F G H I J K Vertex C is the parent of vertices G, H, and I. Vertices

Spanning Trees and Minimum Spanning Trees

Suppose the following graph represents distance in miles between towns.The towns are to be connected by high-speed network cable. Assumingthe cost of cables is proportional to their length and bandwidth is not alimiting factor, the most cost effective network will be a tree that spansthe graph.

13

44

35

23

2529

30

22

20

29

2045

1532

MAT230 (Discrete Math) Trees Fall 2016 9 / 22

Page 10: Trees - Gordon College Department of Mathematics and ...math-cs.gordon.edu/courses/mat230/notes/trees.pdfB C D E F G H I J K Vertex C is the parent of vertices G, H, and I. Vertices

Definitions

Definition

Let G = (V ,E ) be a connected undirected graph. A spanning set for Gis a subset E ′ of E such that (V ,E ′) is connected.

Definition

Let G be a connected undirected graph. The subgraph T is a spanningtree for G if T is a tree and every node in G is a node in T .

Definition

If G is a weighted graph, then T is a minimal spanning tree of G if it isa spanning tree and no other spanning tree of G has smaller total weight.

MAT230 (Discrete Math) Trees Fall 2016 10 / 22

Page 11: Trees - Gordon College Department of Mathematics and ...math-cs.gordon.edu/courses/mat230/notes/trees.pdfB C D E F G H I J K Vertex C is the parent of vertices G, H, and I. Vertices

Minimal Spanning Trees (MST)

Suppose G = (V ,E ,w) is a weighted connected undirected graph. Theminimal spanning tree problem is to find a spanning tree T = (V ,E ′)

for G such that∑e∈E ′

w(e) is as small as possible.

Unlike the Traveling Salesman Problem, solving the MST problem isrelatively easy. We consider two algorithms.

MAT230 (Discrete Math) Trees Fall 2016 11 / 22

Page 12: Trees - Gordon College Department of Mathematics and ...math-cs.gordon.edu/courses/mat230/notes/trees.pdfB C D E F G H I J K Vertex C is the parent of vertices G, H, and I. Vertices

Prim’s Algorithm

Let G = (V ,E ,w) be a weighted connected undirected graph.

1 Pick any vertex v ∈ V .

2 V ′ = {v}3 V0 = V − {v}4 E ′ = ∅5 While V ′ 6= V do:

a Find u ∈ V ′ and v ∈ V0 such that edge e = {u, v} has minimumweight

b E ′ = E ′ ∪ ec V ′ = V ′ ∪ {v}d V0 = V0 − {v}

Upon termination, T = (V ,E ′) is a minimum spanning tree for G .

MAT230 (Discrete Math) Trees Fall 2016 12 / 22

Page 13: Trees - Gordon College Department of Mathematics and ...math-cs.gordon.edu/courses/mat230/notes/trees.pdfB C D E F G H I J K Vertex C is the parent of vertices G, H, and I. Vertices

Kruskal’s Algorithm

Let G = (V ,E ,w) be a weighted connected undirected graph.

1 Find edge e ∈ E with minimum weight

2 E ′ = {e}3 E0 = E − {e}4 V ′ = {v : v is a vertex for which e is an incident edge}5 While V ′ 6= V or (V ′,E ′) not connected do:

a Find edge e ∈ E0 with minimum weight that will not complete a cyclein (V ′,E ′).

b E0 = E0 − ec E ′ = E ′ ∪ ed V ′ = V ′ ∪ {v : v is a vertex for which e is an incident edge}

Upon termination, T = (V ,E ′) is a minimum spanning tree for G .

MAT230 (Discrete Math) Trees Fall 2016 13 / 22

Page 14: Trees - Gordon College Department of Mathematics and ...math-cs.gordon.edu/courses/mat230/notes/trees.pdfB C D E F G H I J K Vertex C is the parent of vertices G, H, and I. Vertices

Rooted Trees

Definition

Every nonempty tree can have a particular vertex called a root. In arooted tree, the root is at level 0. The level of all other vertices is onegreater than the number of edges in the walk from the root to the vertex.The height of a tree is the number of levels in the tree.

Definition

A vertex u in a rooted tree is a parent of a vertex v if v is adjacent to uand the level of v is one greater than the level of u. In this case v is achild of u. Two or more vertices are siblings if they have the same parent.

Definition

Nonroot vertices of degree 1 in a tree are called the leaves of the tree. Allother vertices are called internal vertices.

MAT230 (Discrete Math) Trees Fall 2016 14 / 22

Page 15: Trees - Gordon College Department of Mathematics and ...math-cs.gordon.edu/courses/mat230/notes/trees.pdfB C D E F G H I J K Vertex C is the parent of vertices G, H, and I. Vertices

Rooted Trees

On the left is a typical tree. On the right is the same tree redrawn withvertex A identified as the root.

A

B

C

DE F

G

H I

J

K

A

B C D

E F G H I J

K

Vertex C is the parent of vertices G, H, and I. Vertices E and F arechildren of vertex B and so are siblings. Vertices E, F, G, H, I, and J areall at level 2. Vertices E, F, G, H, I, and K are leaves while A, B, C, D,and J are internal vertices.

MAT230 (Discrete Math) Trees Fall 2016 15 / 22

Page 16: Trees - Gordon College Department of Mathematics and ...math-cs.gordon.edu/courses/mat230/notes/trees.pdfB C D E F G H I J K Vertex C is the parent of vertices G, H, and I. Vertices

Definition

A subtree is connected component of a rooted tree T that does notinclude the root of T . Every subtree is itself a rooted tree.

This tree has subtrees rooted atB, C, and D.

There is also a subtree rooted atJ.

Finally, each leaf is a subtree.

A

B C D

E F G H I J

K

MAT230 (Discrete Math) Trees Fall 2016 16 / 22

Page 17: Trees - Gordon College Department of Mathematics and ...math-cs.gordon.edu/courses/mat230/notes/trees.pdfB C D E F G H I J K Vertex C is the parent of vertices G, H, and I. Vertices

Kruskal’s Algorithm (Version 2)

Working with rooted trees makes Kruskal’s algorithm easier to describe.

Suppose there is an edge e between vertices u and v .

If u and v are both in a tree rootedat r then the edge e will create acycle.

r

u

v

However, if u is in a tree rooted at rand v is in a tree rooted at s, addingedge e does not create a cycle, butmerges the two trees.

r

u

s

v

In this case, we can choose either ror s to be the root of the mergedtree.

MAT230 (Discrete Math) Trees Fall 2016 17 / 22

Page 18: Trees - Gordon College Department of Mathematics and ...math-cs.gordon.edu/courses/mat230/notes/trees.pdfB C D E F G H I J K Vertex C is the parent of vertices G, H, and I. Vertices

Kruskal’s Algorithm (Version 2)

Let G = (V ,E ,w) be a weighted connected undirected graph and supposewe have function rootof(v) that returns the root of the tree containingvertex v .

1 Create sorted edge list EL (sort by increasing weight)

2 Initialize a list R of rooted trees with each vertex in the tree as theroot of its own tree.

3 E ′ = {}4 While R contains more than one entry and EL not empty do:

a remove edge e = {u, v} from EL

b if rootof(u) 6= rootof(v) thenF E ′ = E ′ ∪ {e}F Merge tree rooted at rootof(u) into tree rooted at rootof(v) (or

vice-versa). This reduces the number of entries in R by one.

Upon termination, T = (V ,E ′) is a minimum spanning tree for G .

MAT230 (Discrete Math) Trees Fall 2016 18 / 22

Page 19: Trees - Gordon College Department of Mathematics and ...math-cs.gordon.edu/courses/mat230/notes/trees.pdfB C D E F G H I J K Vertex C is the parent of vertices G, H, and I. Vertices

Binary Trees

Definition

A binary tree T is a tree that has zero or more nodes in which each nodehas at most two children. Each nonleaf node has left subtree and a rightsubtree, either of which may be empty.

Question 1: How many vertices can be found on level k of a binary tree?

Question 2: How many vertices can be found in a binary tree of height h?

MAT230 (Discrete Math) Trees Fall 2016 19 / 22

Page 20: Trees - Gordon College Department of Mathematics and ...math-cs.gordon.edu/courses/mat230/notes/trees.pdfB C D E F G H I J K Vertex C is the parent of vertices G, H, and I. Vertices

Binary Tree Traversal

A traversal of a tree visit each vertex of the tree in some order determinedby the connectivity within the tree. There are three common traversals ofbinary trees, each described by a recursive algorithm.

Preorder Traversal:

1 Visit the root of the tree2 Preorder traverse the left subtree3 Preorder traverse the right subtree

Inorder Traversal:

1 Inorder traverse the left subtree2 Visit the root of the tree3 Inorder traverse the right subtree

Postorder Traversal:

1 Postorder traverse the left subtree2 Postorder traverse the right subtree3 Visit the root of the tree

MAT230 (Discrete Math) Trees Fall 2016 20 / 22

Page 21: Trees - Gordon College Department of Mathematics and ...math-cs.gordon.edu/courses/mat230/notes/trees.pdfB C D E F G H I J K Vertex C is the parent of vertices G, H, and I. Vertices

Expression Trees

The arithmetic operations +, −, ×, and / are called binary operators sincethey each take two operands.

We can diagram them with trees. For example, trees for a + b and c/d are

+

a b

/

c d

MAT230 (Discrete Math) Trees Fall 2016 21 / 22

Page 22: Trees - Gordon College Department of Mathematics and ...math-cs.gordon.edu/courses/mat230/notes/trees.pdfB C D E F G H I J K Vertex C is the parent of vertices G, H, and I. Vertices

Expression Trees

Consider the arithmetic expression 3 + 4(5− 3) + (7 + 3)/5. A tree for thisexpression is

+

+

3 ×

4 −

5 3

/

+

7 3

5

This tree is not unique, other binary trees could be used to represent thesame expression.

Construct a preorder and postorder traversal of this tree.

MAT230 (Discrete Math) Trees Fall 2016 22 / 22