CS204 Discrete Mathematics. 11 Trees. 11 Trees

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

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

Trees Algorhyme by Radia Perlman

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

March 20/2003 Jayakanth Srinivasan,

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

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

TREES. Trees - Introduction

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

6-TREE. Tree: Directed Tree: A directed tree is an acyclic digraph which has one node called the root node

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

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

An undirected graph is a tree if and only of there is a unique simple path between any 2 of its vertices.


Binary Trees, Binary Search 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

Foundations of Discrete Mathematics

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

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

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

Introduction to Computers and Programming. Concept Question

Algorithms and Data Structures

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

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

Definition Example Solution

CE 221 Data Structures and Algorithms

Graph Algorithms Using Depth First Search

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

Binary Trees

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

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

CMSC th Lecture: Graph Theory: Trees.

CS24 Week 8 Lecture 1

8. Write an example for expression tree. [A/M 10] (A+B)*((C-D)/(E^F))

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.

EE 368. Weeks 5 (Notes)

A6-R3: DATA STRUCTURE THROUGH C LANGUAGE

Trees Rooted Trees Spanning trees and Shortest Paths. 12. Graphs and Trees 2. Aaron Tan November 2017

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

Data Structures and Algorithms for Engineers

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

Programming II (CS300)

Recursive Data Structures and Grammars

Garbage Collection: recycling unused memory

Trees. T.U. Cluj-Napoca -DSA Lecture 2 - M. Joldos 1

Undirected Graphs. Hwansoo Han

Trees. Truong Tuan Anh CSE-HCMUT

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

Data Structures Question Bank Multiple Choice

Trees. Trees. CSE 2011 Winter 2007

Trees. CSE 373 Data Structures

Discrete Mathematics. Chapter 7. trees Sanguk Noh

CSI33 Data Structures

E.G.S. PILLAY ENGINEERING COLLEGE (An Autonomous Institution, Affiliated to Anna University, Chennai) Nagore Post, Nagapattinam , Tamilnadu.

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

Graph and Digraph Glossary

CSCI-401 Examlet #5. Name: Class: Date: True/False Indicate whether the sentence or statement is true or false.

ROOT: A node which doesn't have a parent. In the above tree. The Root is A.

Friday Four Square! 4:15PM, Outside Gates

Stacks, Queues and Hierarchical Collections

Stacks, Queues and Hierarchical Collections. 2501ICT Logan

12 Abstract Data Types

COMP 250 Fall binary trees Oct. 27, 2017

Trees. Tree Structure Binary Tree Tree Traversals

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

CSCI2100B Data Structures Trees

Bioinformatics Programming. EE, NCKU Tien-Hao Chang (Darby Chang)

INF2220: algorithms and data structures Series 1

Trees and Graphs Shabsi Walfish NYU - Fundamental Algorithms Summer 2006

MAT 7003 : Mathematical Foundations. (for Software Engineering) J Paul Gibson, A207.

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

Data Structures Brett Bernstein

Binary Trees and Huffman Encoding Binary Search Trees

CSC Intro to Intelligent Robotics, Spring Graphs

R13. II B. Tech I Semester Supplementary Examinations, May/June DATA STRUCTURES (Com. to ECE, CSE, EIE, IT, ECC)

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

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

CS521 \ Notes for the Final Exam

Elements of Graph Theory

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

Round 3: Trees. Tommi Junttila. Aalto University School of Science Department of Computer Science. CS-A1140 Data Structures and Algorithms Autumn 2017

Lecture 10. Elementary Graph Algorithm Minimum Spanning Trees

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

CSE 431/531: Algorithm Analysis and Design (Spring 2018) Greedy Algorithms. Lecturer: Shi Li

CSE 230 Intermediate Programming in C and C++ Binary Tree

CS 151. Binary Trees. Friday, October 5, 12

Discrete mathematics

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

CSI33 Data Structures

Uses for Trees About Trees Binary Trees. Trees. Seth Long. January 31, 2010

CSE 373 APRIL 17 TH TREE BALANCE AND AVL

Why Do We Need Trees?

Formal Languages and Automata Theory, SS Project (due Week 14)

6. Finding Efficient Compressions; Huffman and Hu-Tucker

Direct Addressing Hash table: Collision resolution how handle collisions Hash Functions:

Binary Trees. Height 1

Info 2950, Lecture 16

INF2220: algorithms and data structures Series 1

A6-R3: DATA STRUCTURE THROUGH C LANGUAGE

Minimum Spanning Trees My T. UF

Transcription:

11.1 Introduction to Trees Def. 1 A tree T = (V, E) is a connected undirected graph with no simple circuits. acyclic connected graph. forest a set of trees. A vertex of degree one is called leaf. Thm. 1 An undirected graph is a tree, if and only if, there is a unique simple path between any two of its vertices. proof Assume T is a tree. Since, connected, u, v V, path(u, v). Assume another path(u, v). Then cycle(u, v). unique path(u, v). Assume unique path(u, v). T is connected and no simple circuit. unique path between every two vertices is a tree.(another Def. of tree) 5/20/16 Kwang-Moo Choe 1

Def. 2 A rooted tree T r = (V, E, r) is a tree where one vertex has designated as the root(r V) and every edge is directed away from the root. parent, children, siblings ancestor, decendents leaves, internal vertices subtree Recursive definition of rooted tree(see 5.3 Def. 3) in digraph Def. 3 A rooted tree is called m-ary tree, if every (internal) vertex has no more than m children. The tree is called full m-ary tree, if every internal(non-leaf) vertex has exactly n children. A 2-ary tree is call binary tree. An orderd rooted tree children are ordered from left to right left/right child left/right subtree 5/20/16 Kwang-Moo Choe 2

Tree as Models Computer file system Hierarchical structure Family tree 5/20/16 Kwang-Moo Choe 3

Properties of Trees Thm. 2 A tree with n vertices has n 1 edges. e = n 1. minimal connections of vertices Thm. 3 A full m-ary tree with i internal vertices has n = mi+1 vertices. and l = (m 1)i + 1 leaves. proof Every vertices except root is a child of internal vertices. n = mi + 1. Since n = i + l(leaves), i + l = mi + 1. l = (m 1)i + 1 leaves. Thm. 4 A full m-ary tree with e, i, n, l. e = n 1 n = i + l n = mi + 1. (1) n vertices has i = (n-1)/m internal vertices and l = [(m-1)n+1]/m leaves. (2) i intertinal vertices has n = mi+1 vertices and l = (m 1)i + 1 leaves. (3) l leaves has n = (ml 1)/(m-1) vertices and i = (l 1)/(m 1) interior vertices. 5/20/16 Kwang-Moo Choe 4

The level of a node is the length of the path from root to the node. The height of a tree is maximum node level. The m-ary tree with height h is called balanced, if every leaves are level h or h 1. Thm. 5 There are at most m h leaves in m-ary tree of height h. proof Consider a full, balanced m-ary tree and every leaves are of level h. It has m h leaves. Col. 1 If an m-ary tree with l leaves has hight h, then h log m l. If the tree is full and balanced h = log m l. 5/20/16 Kwang-Moo Choe 5

Thm. 6 Every tree has following properties: 1. Any connected subgraph is a tree. 2. There is a unique (simple) path between every pair of vetices. 3. Adding an edge between two vertices create a (simple) cycle. 4. Removing any edge disconnects the tree. 5. If tree has at least two vertices, then it has at least two leaves. 6. The number of vertices is one larger than that of edges. proof 1. Any subgraph of acyclic graph subgraph is also acyclic. 2. There is at least one (simple) path, since connected and acyclic. Assume two different simple paths from u to v. Assume x be the first vertex where the path diverge, y be the next vertex they share. There is a cycle from x to y and then y to x. 3. Additional edge {u, v} (simple) path from u to v = (simple) cycle 5/20/16 Kwang-Moo Choe 6

4. Remove {u, v}. Unique simple path was (u, v). Not connected 5. Let (v 1,, v m ) be the longest simple path in the tree. Then m 2. 2 i m, {v 1, v i } E, since (v 1,, v i, v 1 ) is a cycle. {u, v 1 } E where u is not in the path, since (v 1,, v m ) is the longest. v 1 is a leaf. By symmetric argumant v m is a second leaf. 6. Induction on number of vertices. n=1, no edge. 0 + 1 = 1. O.K. Consider (n+1)-vertex tree T and let v be a leaf of T. Deleting v and its incident edge gives a smaller tree. ( E 1) = ( V 1) + 1 Adding v and its incident edge gives a larger tree. E = V + 1. 5/20/16 Kwang-Moo Choe 7

11.2 Application of Trees Binary Search Tree procedure search&insert(t = (V, E, r): binary Tree, x: item) if r null v := r; do v null label(v) x if x label(v) if left child(v) null v := left child(v) left child(v) = null add new vertex v(label(x)) as a left child fi x label(v) if right child(v) null v := right child(v) right child(v)=null add new vertex v(label(x))as a right child od fi fi r = null add new vertex v(label(x)) as root of the tree fi label(v) = x. 5/20/16 Kwang-Moo Choe 8

procedure search&insert(t = (V, E, r): binary Tree, x: item) v := r; if v null if x label(v) return search&insert(leftsubtree(t), x) x label(v) return search&insert(rightsubtree(t), x) x = label(v) return arleady present fi v = null r := add a new vertex with label x; return Not present and inserted fi 5/20/16 Kwang-Moo Choe 9

Decision Trees One counterfeit( 함량불량 ) coin in n coins balance Three groups of n/3 coins if first group second group counterfeit in the first group first group = second group counterfeit in the third group first group second group counterfeit in the second group fi log 3 n comparisons. Ex 3. Fig. 3 One counterfeit coin and 7 normal coins.(p730) log 3 8 = 2 comparisons! 5/20/16 Kwang-Moo Choe 10

The complexity of sorting algorithms n elements n! permutations binary comparisions n! leaves in the binary decision tree Height of the decision tree is log n! O(n log 2 n) is optimal The lower bound for sorting based on binary search is (n log 2 n) The average bound for sorting based on binary search is (n log 2 n) 5/20/16 Kwang-Moo Choe 11

Prefix code 26 leters 5 bits(2 4 26 2 5 ) But more frequent letters have shorter sequence. e 00 t 01 w 0001 0001 w or et e is a prefix of w A set of sequences(codes) is a prefix code, if no sequence in the set is the prefix of other sequences in the set. Full binary tree prefix code 5/20/16 Kwang-Moo Choe 12

Algorithm 2 Huffman coding procedure Huffman(V: set of symbols, w: V R + is a weigh of a symbol) F := for a V do make a tree with symbol a and weight w(a) od do F is not a tree Find two minimun weighted trees T 1 and T 2 ; Make a new tree T with weight w(t) = w(t 1 ) + w(t 2 ) and T 1 and T 2 as subtrees. od internal vertices leaf weight of the subtree symbol See Fig. 6 Huffman coding in p 734. Is the Huffman coding optimal? 5/20/16 Kwang-Moo Choe 13

11.3 Tree Traversal Universal Address System in a Rooted Ordered Tree Label root as 0. If Subtree T is labeled as x and n children then n children of x are labeled as x.1, x.2,, x.n. We use x instead of 0.x(omit 0.). Ex 1 rooted tree (Fig.1 p741) 0 1 1.1 1.2 1.3 2 3 3.1 3.1.1 3.1.2 3.1.2.1 3.1.2.2 3.1.2.3 3.1.2.4 3.1.3 3.2 4 4.1 5 5.1 5.1.1 5.2 5.3 Prefix code Lexicographic ordering x 1.x 2..x n y 1.y 2..y m, if i, 1 i n, 1 j i, x j = y j, x i y i or n m, 1 j n, x j = y j. 5/20/16 Kwang-Moo Choe 14

procedure preorder(t with root r) visit r; preorder(child 1 (r)); preorder(child 2 (r)); ; preorder(child n (r)) end preorder; 0 1 1.1 1.2 1.3 2 3 3.1 3.1.1 3.1.2 3.1.2.1 3.1.2.2 3.1.2.3 3.1.2.4 3.1.3 3.2 4 4.1 5 5.1 5.1.1 5.2 5.3 Same as lexicographic order procedure inorder(t with root r) inorder(child 1 (r)) visit r; inorder(child 2 (r)); inorder(child n (r)) end inorder; 1.1 1 1.2 1.3 0 2 3.1.1 3.1 3.1.2.1 3.1.2 3.1.2.2 3.1.2.3 3.1.2.4 3.1.3 3 3.2 4.1 4 5.1 5.1.1 5 5.2 5.3 procedure postorder(t with root r) postorder(child 1 (r)); posteorder(child 2 (r)); postorder(child n (r)); visit r end postorder 1.1 1.2 1.3 1 2 3.1.1 3.1.2.1 3.1.2.2 3.1.2.3 3.1.2.4 3.1.2 3.1.3 3.1 3.2 3 4.1 4 5.1.1 5.1 5.2 5.3 5 0 5/20/16 Kwang-Moo Choe 15

Infix Notation x y binary operation binary expression tree inorder traversal of the expression tree Ambiguous parentheses () precedence between operators x y z prec( ) prec( ) = prec( ) prec(+) = prec( ) u x y z = ( u x ) ( y z ) ) associativity of operands x y z left associative, + x y z = (x y) z right associative,, x y z = x (y z) Prefix(Polish) Notation n op 1 op 2 op n n-ary opertaion n-ary expression tree preorder traversal of the expression tree Postfix(reversed Polish) Notation op 1 op 2 op n n operational stack 5/20/16 Kwang-Moo Choe 16

Ambiguous but simple context-free grammar for binary infix expressions E E + E E E E E E / E E E a z ( E ) Unambiguous context-free grammar (without parentheses) E E + T T E T F F / T X F a z ( E ) T T F F / T X F a z ( E ) F X F a z ( E ) X a z ( E ) Consider precedence between operators: for example u x y z. E T E X F E u F E u x E u x T F u x y F u x y z. Consider associativity of operators: for example x y z. E X F x F x X F x y F x y z. Compare grammar tree and expression tree 5/20/16 Kwang-Moo Choe 17

11.4 Spanning Trees Def. 1 Let G be a ugraph. A spanning tree of G is a subgraph of G that is tree containing every vertex of G. Thm. 1 Every connected graph has spanning tree. proof Let T be a connected spanning subgraph of G with the smallest number of edges. Suppose T has a cycle (v 0, v 1,, v n, v 0 ). Suppose we remove the edge {v n, v 0 }. If arbitrary vertices x and y has a path not containing the edge (v n, v 0 ), x and y has a path containing that path. If arbitrary vertices x and y has a path containing the edge (v n, v 0 ), x and y has a path containing the path (v 0, v 1,, v n ). This is a contradiction that T has the smallest number of edges and connected. T is acyclic. T is a tree. 5/20/16 Kwang-Moo Choe 18

Alg. 1 Depth-First Search procedure DFS(G = (V, E): connected graph) T := Tree with vertex v V only; visit(v); end DFS; procedure visit(v V) for (v, w) E w T do add w and (v, w) to T; visit(w) od end visit Alg. 2 Breadth-First Search procedure BFS(G = (V, E): connected graph) T := Tree with vertex v V only; L := list with vertex v V; do L is not empty remove the first vertex, v, from L; for (v, w) E where w T w L do add w to the end of L; add w and (v, w) to T od od end BFS; 5/20/16 Kwang-Moo Choe 19

11.5 Minimum Spanning Trees Definition 1 A minimum panning tree in a connected weighted graph is a spanning tree that has the smallest sum of weight of its edges. Algorithm 1 Prim s Algorithm procedure Prim(G = (V, E): connected weighted graph) T := a minimum weighted edge; for i := 1 to V 2 do e := a minimum weighted edge incident to a vertex in T and not forming a simple circuit in T, if added to T; T := T with e added od end Prim 5/20/16 Kwang-Moo Choe 20

Algorithm 2 Kruskal s Algorithm procedure Kruskal(G = (V, E): connected weighted graph) T := empty graph; for i := 1 to V 1 do e := any edge in G with smallest weight that does not form a simple circuit in T, if added to T; T := T with e added od 5/20/16 Kwang-Moo Choe 21