cmps 2433 discrete structures chapter 5 - trees midwestern state university

25
CMPS 2433 Discrete Structures Chapter 5 - Trees MIDWESTERN STATE UNIVERSITY

Upload: jane-booth

Post on 17-Jan-2016

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CMPS 2433 Discrete Structures Chapter 5 - Trees MIDWESTERN STATE UNIVERSITY

CMPS 2433 Discrete StructuresChapter 5 - Trees MIDWESTERN STATE UNIVERSITY

Page 2: CMPS 2433 Discrete Structures Chapter 5 - Trees MIDWESTERN STATE UNIVERSITY

Basic TreeTree: Connected graph with no cyclesTheorem 5.1: Let U & V be vertices in a tree. Then there is exactly one simple path between U & V.• Since connected, there exists at least one path• If 2, then there must be a cycle (discussion pg. 231)

Page 3: CMPS 2433 Discrete Structures Chapter 5 - Trees MIDWESTERN STATE UNIVERSITY

More Tree Theorems• 5.2: In a tree with more than 1 vertex, there are at

least 2 vertices of degree 1.• 5.3: A tree with n vertices has exactly n-1 edges.

(Proof by induction – p. 232)• 5.4a: If an edge is removed from a tree, the

resulting graph is not connected (so not a tree)• 5.4b: If an edge is added to a tree, the result is a

cycle (so not a tree)

Page 4: CMPS 2433 Discrete Structures Chapter 5 - Trees MIDWESTERN STATE UNIVERSITY

Theorem 5.5:Following statements are equivalent for a graph T

a) T is a treeb) T is connected & V = E+1c) T has 0 cycles & V = E+1d) There is exactly 1 simple path between any 2 verticese) T is connected & removal of any edge results in graph that is not

connectedf) T has no cycles & addition of edge between 2 non-adjacent

vertices results in a cycle

Page 5: CMPS 2433 Discrete Structures Chapter 5 - Trees MIDWESTERN STATE UNIVERSITY

Homework• Section 5.1 – pages 234-236• Exercises 1 – 18, 27

Page 6: CMPS 2433 Discrete Structures Chapter 5 - Trees MIDWESTERN STATE UNIVERSITY

Example 5.2 – page 229• Build telephone network among a group towns

using fewest lines possible; connections do not have to be direct, just connected

• Consider 5 towns• Fully connected• Tree

• See also Example 5.6 – p. 238 – Oil Pipeline

Page 7: CMPS 2433 Discrete Structures Chapter 5 - Trees MIDWESTERN STATE UNIVERSITY

Spanning Tree• Spanning tree of a graph G is a tree (connected

graph with no cycles) containing all vertices of G• May not be unique• Note: A tree is a spanning tree.• How? Remove 1 edge from each cycle.• How do you find the cycles?

Page 8: CMPS 2433 Discrete Structures Chapter 5 - Trees MIDWESTERN STATE UNIVERSITY

Breadth First Search Algorithm

• Using from previous chapter• Edges connecting vertex to its predecessor forms a

tree• Can start at any vertex• Produces Shortest Paths Tree

Page 9: CMPS 2433 Discrete Structures Chapter 5 - Trees MIDWESTERN STATE UNIVERSITY

Theorem 5.6• A graph is connected iff it has a spanning tree.

Page 10: CMPS 2433 Discrete Structures Chapter 5 - Trees MIDWESTERN STATE UNIVERSITY

Minimal & Maximal Spanning Trees• Applies to Weighted Graphs• Minimal Spanning Tree • No other spanning tree of the graph has smaller weight• May be multiple with same weight• Figure 5.15 – pg. 242

Page 11: CMPS 2433 Discrete Structures Chapter 5 - Trees MIDWESTERN STATE UNIVERSITY

Minimal Spanning Tree - Prim’s Algorithm• Select any vertex, V1• Select smallest edge incident on V1• Now, some V2 is also included in selection• Select smallest edge with 1 end a selected vertex &

other end not selected vertex• Repeat until all vertices are included

Page 12: CMPS 2433 Discrete Structures Chapter 5 - Trees MIDWESTERN STATE UNIVERSITY

Prim’s Algorithm• Complexity O(n3) – n = number vertices• Maximal Spanning Tree?? How can we do this?

Page 13: CMPS 2433 Discrete Structures Chapter 5 - Trees MIDWESTERN STATE UNIVERSITY

Kruskal’s Minimal Spanning Tree Alg. (p. 252)• Start with all vertices in separate sets• Select smallest edge; the 2 vertex endpoints have been

selected; Union the sets containing the 2 vertices• Select smallest edge for which 2 endpoints are NOT in

same set• Repeat until all vertices are in one setHow is this different from Prim’s?

Page 14: CMPS 2433 Discrete Structures Chapter 5 - Trees MIDWESTERN STATE UNIVERSITY

Homework• Section 5.2 – Pages 248 – 253• Exercises 1 – 43 (except 12, 13, 36, 37)

Page 15: CMPS 2433 Discrete Structures Chapter 5 - Trees MIDWESTERN STATE UNIVERSITY

Section 5.3 – Depth-First Search

• OMIT

Page 16: CMPS 2433 Discrete Structures Chapter 5 - Trees MIDWESTERN STATE UNIVERSITY

5.4 Rooted Trees• Example: Family Tree• Rooted Tree: a directed graph T such that

1. If ignore direction of edges, graph is a tree2. There exists unique vertex R with in-degree 0, in-degree

of all other V is 1 (R is called the Root)

• Usually draw with Root at top & other edges going down

Page 17: CMPS 2433 Discrete Structures Chapter 5 - Trees MIDWESTERN STATE UNIVERSITY

Theorem 5.9In a rooted tree:1. V = E + 12. No Directed Cycles3. Unique simple directed path from root to all other

Vertices

Page 18: CMPS 2433 Discrete Structures Chapter 5 - Trees MIDWESTERN STATE UNIVERSITY

Tree Terminology (page 268)

• Parent • Child• Ancestor • Descendant• Terminal Vertex – Leaf• Internal Vertex

Page 19: CMPS 2433 Discrete Structures Chapter 5 - Trees MIDWESTERN STATE UNIVERSITY

Section 5.4 Homework• Omit Pages 269 – 270• Exercises Pages 271-274• 1 – 12, 14, 20 – 25, 33 – 38

Page 20: CMPS 2433 Discrete Structures Chapter 5 - Trees MIDWESTERN STATE UNIVERSITY

5.5 Binary Trees & Traversals• A rooted tree in which each vertex has at most 2

children, denoted left child & right child• Left subtree of V: subtree rooted by left child of V• Right subtree

Page 21: CMPS 2433 Discrete Structures Chapter 5 - Trees MIDWESTERN STATE UNIVERSITY

5.6 Binary Search Tree (BST) (p.296)

Consider – each vertex has a value.BST: For every vertex, all vertices in left subtree have greater value that root and all vertices in right subtree have values less than root.

Page 22: CMPS 2433 Discrete Structures Chapter 5 - Trees MIDWESTERN STATE UNIVERSITY

Insertion to a Binary Search Tree (p. 298)

Page 23: CMPS 2433 Discrete Structures Chapter 5 - Trees MIDWESTERN STATE UNIVERSITY

BST Traversals• Inorder Traversal (p.282)• Preorder Traversal (p. 277)• Postorder Traversal (p. 281)

Page 24: CMPS 2433 Discrete Structures Chapter 5 - Trees MIDWESTERN STATE UNIVERSITY

Searching a BST (p. 300)

Page 25: CMPS 2433 Discrete Structures Chapter 5 - Trees MIDWESTERN STATE UNIVERSITY

Homework – 5.5 & 5.6• 5.5: pages 284+• Exercises 7 – 30, 55 – 64

• 5.6: pages 302+• Exercises 48 – 73, demonstrate 74 with 2 different BST’s