CMPS 2433 Discrete Structures Chapter 5 - Trees R. HALVERSON – MIDWESTERN STATE UNIVERSITY Basic Tree Tree: Connected graph with no cycles Theorem 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) 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) Theorem 5.5: Following statements are equivalent for a graph T a) b) c) d) e) f) T is a tree T is connected & V = E+1 T has 0 cycles & V = E+1 There is exactly 1 simple path between any 2 vertices T is connected & removal of any edge results in graph that is not connected T has no cycles & addition of edge between 2 non-adjacent vertices results in a cycle Homework • Section 5.1 – pages 234-236 • Exercises 1 – 18, 27 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 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? 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 Theorem 5.6 • A graph is connected iff it has a spanning tree. 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 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 Prim’s Algorithm • Complexity O(n3) – n = number vertices • Maximal Spanning Tree?? How can we do this? 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 set How is this different from Prim’s? Homework • Section 5.2 – Pages 248 – 253 • Exercises 1 – 43 (except 12, 13, 36, 37) Section 5.3 – Depth-First Search • OMIT 5.4 Rooted Trees • Example: Family Tree • Rooted Tree: a directed graph T such that 1. If ignore direction of edges, graph is a tree 2. 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 Theorem 5.9 In a rooted tree: 1. V = E + 1 2. No Directed Cycles 3. Unique simple directed path from root to all other Vertices Tree Terminology (page 268) • • • • • • Parent Child Ancestor Descendant Terminal Vertex – Leaf Internal Vertex Section 5.4 Homework • Omit Pages 269 – 270 • Exercises Pages 271-274 • 1 – 12, 14, 20 – 25, 33 – 38 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 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. Insertion to a Binary Search Tree (p. 298) BST Traversals • Inorder Traversal (p.282) • Preorder Traversal (p. 277) • Postorder Traversal (p. 281) Searching a BST (p. 300) 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