CMSC th Lecture: Graph Theory: Trees.

Similar documents
Section Summary. Introduction to Trees Rooted Trees Trees as Models Properties of Trees

Binary Trees

Section 5.5. Left subtree The left subtree of a vertex V on a binary tree is the graph formed by the left child L of V, the descendents

Binary Trees, Binary Search Trees

Friday, March 30. Last time we were talking about traversal of a rooted ordered tree, having defined preorder traversal. We will continue from there.

Chapter Summary. Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees

March 20/2003 Jayakanth Srinivasan,

7.1 Introduction. A (free) tree T is A simple graph such that for every pair of vertices v and w there is a unique path from v to w

Graph Algorithms Using Depth First Search

Introduction to Computers and Programming. Concept Question

Topics. Trees Vojislav Kecman. Which graphs are trees? Terminology. Terminology Trees as Models Some Tree Theorems Applications of Trees CMSC 302

Chapter 4 Trees. Theorem A graph G has a spanning tree if and only if G is connected.

Graphs V={A,B,C,D,E} E={ (A,D),(A,E),(B,D), (B,E),(C,D),(C,E)}

License. Discrete Mathematics. Tree. Topics. Definition tree: connected graph with no cycle. examples. c T. Uyar, A. Yayımlı, E.

Data Structure Lecture#10: Binary Trees (Chapter 5) U Kang Seoul National University

Trees : Part 1. Section 4.1. Theory and Terminology. A Tree? A Tree? Theory and Terminology. Theory and Terminology

Chapter 10: Trees. A tree is a connected simple undirected graph with no simple circuits.

Trees. Introduction & Terminology. February 05, 2018 Cinda Heeren / Geoffrey Tien 1

Trees. Q: Why study trees? A: Many advance ADTs are implemented using tree-based data structures.

Lecture Notes 16 - Trees CSS 501 Data Structures and Object-Oriented Programming Professor Clark F. Olson

Binary search trees. Binary search trees are data structures based on binary trees that support operations on dynamic sets.

3. According to universal addressing, what is the address of vertex d? 4. According to universal addressing, what is the address of vertex f?


Why Do We Need Trees?

Trees. 3. (Minimally Connected) G is connected and deleting any of its edges gives rise to a disconnected graph.

4 Basics of Trees. Petr Hliněný, FI MU Brno 1 FI: MA010: Trees and Forests

CS204 Discrete Mathematics. 11 Trees. 11 Trees

Definition of Graphs and Trees. Representation of Trees.

Trees Algorhyme by Radia Perlman

BACKGROUND: A BRIEF INTRODUCTION TO GRAPH THEORY

CSC148 Week 6. Larry Zhang

Trees. CSE 373 Data Structures

Trees. (Trees) Data Structures and Programming Spring / 28

Data Structure. IBPS SO (IT- Officer) Exam 2017

Elements of Graph Theory

Jana Kosecka. Red-Black Trees Graph Algorithms. Many slides here are based on E. Demaine, D. Luebke slides

Lec 17 April 8. Topics: binary Trees expression trees. (Chapter 5 of text)

CSCI2100B Data Structures Trees

Lecture 2 - Graph Theory Fundamentals - Reachability and Exploration 1

CE 221 Data Structures and Algorithms

Binary search trees 3. Binary search trees. Binary search trees 2. Reading: Cormen et al, Sections 12.1 to 12.3

Hamilton paths & circuits. Gray codes. Hamilton Circuits. Planar Graphs. Hamilton circuits. 10 Nov 2015

COMP 250 Fall binary trees Oct. 27, 2017

Tree. A path is a connected sequence of edges. A tree topology is acyclic there is no loop.

Trees. Truong Tuan Anh CSE-HCMUT

Data Structure - Binary Tree 1 -

Garbage Collection: recycling unused memory

This course is intended for 3rd and/or 4th year undergraduate majors in Computer Science.

Programming II (CS300)

Chapter 20: Binary Trees

Upcoming ACM Events Linux Crash Course Date: Time: Location: Weekly Crack the Coding Interview Date:

COSC 2007 Data Structures II Final Exam. Part 1: multiple choice (1 mark each, total 30 marks, circle the correct answer)

There are many other applications like constructing the expression tree from the postorder expression. I leave you with an idea as how to do it.

Warm Up. Use Kruskal s algorithm to find the minimum spanning tree and it s weight.

BBM 201 Data structures

implementing the breadth-first search algorithm implementing the depth-first search algorithm

Abstract Data Structures IB Computer Science. Content developed by Dartford Grammar School Computer Science Department

Discrete mathematics

Algorithms and Data Structures (INF1) Lecture 8/15 Hua Lu

Trees and Graphs Shabsi Walfish NYU - Fundamental Algorithms Summer 2006

Binary Trees. Height 1

Trees and Tree Traversals. Binary Trees. COMP 210: Object-Oriented Programming Lecture Notes 8. Based on notes by Logan Mayfield

Search Trees. Undirected graph Directed graph Tree Binary search tree

Why Use Binary Trees? Data Structures - Binary Trees 1. Trees (Contd.) Trees

Data Structures - Binary Trees 1

Trees! Ellen Walker! CPSC 201 Data Structures! Hiram College!

Tree Structures. A hierarchical data structure whose point of entry is the root node

Lecture 3: Graphs and flows

CSI33 Data Structures

Binary Tree. Preview. Binary Tree. Binary Tree. Binary Search Tree 10/2/2017. Binary Tree

12/5/17. trees. CS 220: Discrete Structures and their Applications. Trees Chapter 11 in zybooks. rooted trees. rooted trees

12 Abstract Data Types

Algorithms and Data Structures

CSI 604 Elementary Graph Algorithms

CSCE f(n) = Θ(g(n)), if f(n) = O(g(n)) and f(n) = Ω(g(n)).

Data Structures Question Bank Multiple Choice

4 Fractional Dimension of Posets from Trees

Chapter 3 Trees. Theorem A graph T is a tree if, and only if, every two distinct vertices of T are joined by a unique path.

CS61B Lecture #20: Trees. Last modified: Mon Oct 8 21:21: CS61B: Lecture #20 1

Computer Science 210 Data Structures Siena College Fall Topic Notes: Trees

Outline. Preliminaries. Binary Trees Binary Search Trees. What is Tree? Implementation of Trees using C++ Tree traversals and applications

CHAPTER 11 Trees. 294 Chapter 11 Trees. f) This is a tree since it is connected and has no simple circuits.

Binary Trees. BSTs. For example: Jargon: Data Structures & Algorithms. root node. level: internal node. edge.

Binary Tree. Binary tree terminology. Binary tree terminology Definition and Applications of Binary Trees

TREES. Trees - Introduction

Data Structures. Trees. By Dr. Mohammad Ali H. Eljinini. M.A. Eljinini, PhD

Course Review for. Cpt S 223 Fall Cpt S 223. School of EECS, WSU

We will show that the height of a RB tree on n vertices is approximately 2*log n. In class I presented a simple structural proof of this claim:

v V Question: How many edges are there in a graph with 10 vertices each of degree 6?

Binary Trees. Examples:

Figure 4.1: The evolution of a rooted tree.

Trees and Binary Trees

CS 234. Module 5. October 18, CS 234 Module 5 ADTS with items related by structure 1 / 25

Prelim 2. CS 2110, November 20, 2014, 7:30 PM Extra Total Question True/False Short Answer

Binary Heaps in Dynamic Arrays

Announcements. HW3 is graded. Average is 81%

CSE 417: Algorithms and Computational Complexity. 3.1 Basic Definitions and Applications. Goals. Chapter 3. Winter 2012 Graphs and Graph Algorithms

Foundations of Discrete Mathematics

Foundations of Computer Science Spring Mathematical Preliminaries

Lecture 32. No computer use today. Reminders: Homework 11 is due today. Project 6 is due next Friday. Questions?

Transcription:

CMSC 27100 26th Lecture: Graph Theory: Trees. Lecturer: Janos Simon December 2, 2018 1 Trees Definition 1. A tree is an acyclic connected graph. Trees have many nice properties. Theorem 2. The following are equivalent for n-vertex graphs G = (V, E): (i) G is a tree. (ii) G is acyclic and has n 1 edges (iii) G is connected and has n 1 edges (iv) G is connected but e E G {e} is not connected (v) x, y V! path from x to y (! means there is a unique ) Proof. These are relatively easy proofs, and there is a strong intuition behind them. For example, (v) implies that deleting any edge will separate the graph into 2 connected components, since if we delete an edge incident on x it will disconnect it from y as there is a unique path from x to y. The trick is to make all these implications formal. They were assigned as homework... (Hint: make the implications a directed cycle (5 proofs) as opposed to a complete graph (at least 10 proofs)!) 1

Trees are important because they capture the connectivity in the simplest possible way. They have the smallest number of edges that still make the graph connected. A spanning tree of a graph has the same set of vertices as the graph (it is a spanning subgraph) and the same connectivity. (It is an easy observation that a graph is connected iff it has a spanning tree. A (bad) proof of this is the following inefficient algorithm: while the graph remains connected we keep throwing out edges that do not disconnect the graph. When we cannot do this any more, the remaining graph is a spanning tree.) If we want to connect a bunch of computers, and have costs for buying pairwise connections, the cheapest solution will be a spanning tree. We will study efficient algorithms for such problems in Algorithms. 2 Rooted Trees These are the trees you may be familiar with from Computer Science... Rosen uses the following definition: Consider a tree T, and choose a vertex a of T. It will be the root of the rooted tree. Now orient all edges of T so that all paths lead away from the root. This is a rooted tree with root a. The inductive definition below is more elegant, and much more useful for proving properties of rooted trees. Definition 3. (Rooted Tree) A vertex r is a rooted tree with root r. It has depth 0. Let T 1, T 2, be rooted trees with roots r 1, r 2, r k and depths d 1, d 2 d k respectively, and let d = max i d i. Let r be a new vertex, and add directed edges (r, r 1 ), (r, r 2 ), (r, r k ). The resulting directed tree is a rooted tree T with root r and depth d + 1. There are natural definitions of child, parent, descendant and ancestor. A leaf is a vertex with outdegree 0. Non-leaf vertices are called internal vertices. A subtree rooted at an internal vertex a is the rooted tree consisting of the vertices reachable by directed paths from a to a leaf in T. Rooted trees are very useful representation of hierarchies with each element having a single boss. Examples: 2

Hierarchies in an organization Linnæus Classification of Organisms File Systems The Library of Congress book classification system Algebraic Formulas US Code: Title 26 (IRS rules) Definition 4. An m-ary tree is one where every vertex has outdegree at most m. It is full if the number of children is m if the vertex is not a leaf, and complete if all leaves are at the same distance (number of edges traversed) from the root. It is called a binary tree if m = 2. Unless specifically defined otherwise, the children are ordered: in binary trees there is a LEFT child and a RIGHT child. An easy induction shows that the number of leaves of a complete m-ary tree of depth d is m d, in particular for binary trees this is 2 d. This implies that the number of internal vertices is 2 d 1 by noting that there are 2 i vertices at depth i (by the argument that yielded 2 d leaves at level d), and remembering that d 1 i=0 = 2d 1. For trees that are not complete, m d is only an upper bound. Exercise: Draw a binary tree of depth d with O(d) leaves. 3 Traversing Binary Trees In many algorithms one needs to visit all vertices of a binary tree. By visit we mean doing something at each node. For concrete examples, in a file system, checking that all file permissions include a given group,; in a library catalog, collecting statistics about the fraction of books printed before the year 1700 that are on the stacks in the library (one would need to follow pointers from each record to get this information.) We can think of a methodical way to traverse the tree by noting that a natural way to go through the tree will use recursion: when we arrive at a vertex v we will explore the left subtree of v, upon returning we will explore the right subtree, then return to the call that generated exploring 3

the subtree rooted at v (if a subtree is empty, the recursive call is not made, and when returning from the root of the tree we are done.) So we arrive at each vertex 3 times: we have a choice of which of these times we visit the vertex. If we choose to visit before exploring the left subtree the algorithm is a preorder traversal, if we do it after returning from the left subtree it is an inorder traversal, and if we do it after returning from the right subtree it is a postorder traversal. The respective algorithms, PRE(), IN() and POST() are given below. PRE(r) Visit(r) PRE(LEFTCHILD) PRE(RIGHTCHILD) return IN(r) IN(LEFTCHILD) Visit(r) IN(RIGHTCHILD) return POST(r) POST(LEFTCHILD) POST(RIGHTCHILD) Visit(r) return Exercise: Draw a binary tree. Assign distinct letters to all vertices. In the programs above, consider visit(r) to mean print the letter at node r. List the orderings of letters you get from each traversal. 3.1 Applications of Postorder traversal Consider the formula (((x + y) (z u)) + 7) where we used a parenthesis to enclose every binary operation. There is a natural binary tree associated with the formula, where the internal nodes are the binary operations and the leaves are variables or constants. Compilers 4

transform expressions into their trees, which can be evaluated by the simple recursive program get the value of the left subtree get the value of the right subtree perform the operation Clearly, this corresponds to a postorder traversal of the tree. If we perform the Exercise proposed above on the tree, we will get the postorder listing xy + zu 7+ It should be clear that the notation above (expressions of the form operand1 operand2 operation ) is unambiguous and needs no parenthesis. It was invented by the Polish mathematician Lukasiewicz, and is known as Polish Notation (cf. Chinese Remainder Theorem ). It is an easy programming exercise to evaluate an expression given in Polish notation using a stack (Do it!). Nerdy comment: if we were taught Polish notation from elementary school, we would not need parenthesis... 3.2 More about binary tree traversal When traversing (rooted) binary trees, we need three pieces of information at each node: the LEFTCHILD pointer, the RIGHTCHILD pointer, and the PARENT pointer. Many algorithms do not need all three pointers. For example, if all we need is to go to the root given a leaf, we only need the PARENT pointer. In the traversal algorithms above, we avoided having explicitly the PAR- ENT pointer by using recursion (usually implemented by an additional stack.) Can we avoid using a stack? (of course we need to store this information somewhere, but we may be able to reuse to LEFTCHILD and RIGHTCHILD fields to hide the stack there... Challenging Hacking Question Using an extra bit per vertex, and having the ability to change the LEFTCHILD and RIGHTCHILD fields, write an algorithm that implements the postorder traversal algorithm POST() using only a constant number of registers. Even more challeging Implement an algorithm that will visit every vertex and return to the root. It may visit a vertex more than once, but it doesn t 5

use any extra bits at the vertices only the LEFTCHILD and RIGHTCHILD fields, and a constant number of external registers. 4 Representing Graphs in a Computer Adjacency Matrix Let G = (V, E). let V = v 1, v 2 v n. The adjacency matrix of G is an n n Boolean matrix A with A[i, j] = 1 iff (v i, v j ) E (for directed graphs for graphs A[i, j] = 1 iff {v i, v j } E.) Incidence Matrix Let G = (V, E). let V = v 1, v 2 v n and let E = e 1, e 2, e m. The incidence matrix I of G is an n m Boolean matrix with I[i, j] = 1 iff v i is incident with e j. For sparse graphs it is convenient to use an adjacency list: for every vertex v i we have a list of the vertices v k that v i is connected to. Adjacency lists are convenient because many graphs have o(n 2 ) edges, and an algorithm that uses an adjacency matrix must spend Ω(n 2 ) time to set up the adjacency matrix (A has n 2 entries.) Trees, planar graphs (graphs that one can draw on the plane without edges crossing), and many other interesting classes of graphs have a number of edges that is linear in the size of the graph, i.e. V + E which is Θ(n). There are many interesting linear (in n ) time algorithms for such graphs. 6