Trees Algorhyme by Radia Perlman

Similar documents
Binary Trees

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

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

March 20/2003 Jayakanth Srinivasan,

CS204 Discrete Mathematics. 11 Trees. 11 Trees

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

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.

Foundations of Discrete Mathematics

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


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

CS24 Week 8 Lecture 1

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. Q: Why study trees? A: Many advance ADTs are implemented using tree-based data structures.

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

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

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

Binary Trees, Binary Search Trees

CE 221 Data Structures and Algorithms

Trees. Truong Tuan Anh CSE-HCMUT

Introduction to Computers and Programming. Concept Question

Programming II (CS300)

Graph Algorithms Using Depth First Search

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

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

Trees. CSE 373 Data Structures

Programming II (CS300)

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

Graph. Vertex. edge. Directed Graph. Undirected Graph

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

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

Undirected Graphs. Hwansoo Han

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

CS 441 Discrete Mathematics for CS Lecture 26. Graphs. CS 441 Discrete mathematics for CS. Final exam

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

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

Course Review for Finals. Cpt S 223 Fall 2008

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

Topic 18 Binary Trees "A tree may grow a thousand feet tall, but its leaves will return to its roots." -Chinese Proverb

Analysis of Algorithms

Trees. Arash Rafiey. 20 October, 2015

Algorithms and Data Structures

Why Do We Need Trees?

CS 220: Discrete Structures and their Applications. graphs zybooks chapter 10

Binary Trees. College of Computing & Information Technology King Abdulaziz University. CPCS-204 Data Structures I

Outlines: Graphs Part-2

Trees. Tree Structure Binary Tree Tree Traversals

Data Structures Question Bank Multiple Choice

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

CS521 \ Notes for the Final Exam

Name CPTR246 Spring '17 (100 total points) Exam 3

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

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

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

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

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

Graph Algorithms. Chapter 22. CPTR 430 Algorithms Graph Algorithms 1

Lecture Notes. char myarray [ ] = {0, 0, 0, 0, 0 } ; The memory diagram associated with the array can be drawn like this

CS 125 Section #6 Graph Traversal and Linear Programs 10/13/14

Minimum Spanning Trees My T. UF

OUTLINE. General Trees (Ch. 7.1) Binary Trees (Ch. 7.3) Tree Traversals (Ch. 7.2)

Chapter 20: Binary Trees

Recursive Data Structures and Grammars

Trees. Trees. CSE 2011 Winter 2007

Trees. Eric McCreath

Data Structures Brett Bernstein

A6-R3: DATA STRUCTURE THROUGH C LANGUAGE

CS 280 Problem Set 10 Solutions Due May 3, Part A

COSC 2011 Section N. Trees: Terminology and Basic Properties

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

Graphs. Part I: Basic algorithms. Laura Toma Algorithms (csci2200), Bowdoin College

Info 2950, Lecture 16

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

Copyright 1998 by Addison-Wesley Publishing Company 147. Chapter 15. Stacks and Queues

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

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

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

TREES. Trees - Introduction

Minimum Spanning Trees

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

1 Binary trees. 1 Binary search trees. 1 Traversal. 1 Insertion. 1 An empty structure is an empty tree.

CMSC th Lecture: Graph Theory: Trees.

NANYANG TECHNOLOGICAL UNIVERSITY SEMESTER II EXAMINATION MH 1301 DISCRETE MATHEMATICS TIME ALLOWED: 2 HOURS

Konigsberg Bridge Problem

Undirected Graphs. DSA - lecture 6 - T.U.Cluj-Napoca - M. Joldos 1

( ) ( ) C. " 1 n. ( ) $ f n. ( ) B. " log( n! ) ( ) and that you already know ( ) ( ) " % g( n) ( ) " #&

Data Structure Advanced

Topic 14. The BinaryTree ADT

CSE 100 Minimum Spanning Trees Prim s and Kruskal

tree nonlinear Examples

Todays Lecture. Assignment 2 deadline: You have 5 Calendar days to complete.

Chapter 14. Graphs Pearson Addison-Wesley. All rights reserved 14 A-1

CSI33 Data Structures

n 2 C. Θ n ( ) Ο f ( n) B. n 2 Ω( n logn)

CS 231 Data Structures and Algorithms Fall Recursion and Binary Trees Lecture 21 October 24, Prof. Zadia Codabux

Spanning Trees, greedy algorithms. Lecture 22 CS2110 Fall 2017

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

Data Structures And Algorithms

R10 SET - 1. Code No: R II B. Tech I Semester, Supplementary Examinations, May

Transcription:

Algorhyme by Radia Perlman I think that I shall never see A graph more lovely than a tree. A tree whose crucial property Is loop-free connectivity. A tree which must be sure to span. So packets can reach every LAN. First the Root must be selected By ID it is elected. Least cost paths from Root are traced In the tree these paths are placed. A mesh is made by folks like me Then bridges find a spanning tree. CS 2233 Discrete Mathematical Structures 1

Rooted 1 Rooted 2 Rooted 3 Binary Search Expression More Exp. A tree is a connected undirected graph with no simple circuits. EP A SA CC A forest is a undirected graph with no simple circuits. A forest is a set of trees. H H H O O O O O N N C CS 2233 Discrete Mathematical Structures 2

Rooted 1 Rooted 1 Rooted 2 Rooted 3 Binary Search Expression More Exp. In a rooted tree, one vertex is the root, and all edges are directed away from the root. bin / etc usr cd ls rm X11 rc.d bin local sbin CS 2233 Discrete Mathematical Structures 3

Rooted 2 Rooted 1 Rooted 2 Rooted 3 Binary Search Expression More Exp. If there is an edge (u, v) in a rooted tree, u is the parent of v, and v is a child of u. If u has children, then u is internal. If u has no children, then v is a leaf. u and v are siblings if they have same parent. The height of a tree is the length of the longest path from the root to a vertex. The level of a vertex is the path length from the root. The root is on level 0. CS 2233 Discrete Mathematical Structures 4

Rooted 3 Rooted 1 Rooted 2 Rooted 3 Binary Search Expression More Exp. If there is a path from u to v in a rooted tree, u is an ancestor of v, v is a descendent of v, and v is in u s subtree. A rooted tree is an m-ary tree if every internal vertex has m children. It is a full m-ary tree if every internal vertex has exactly m children. A binary tree is an m-ary tree with m = 2. In an ordered rooted tree, siblings are ordered. In an ordered binary tree, an internal vertex has a left child and/or a right child. CS 2233 Discrete Mathematical Structures 5

Properties of 1 Rooted 1 Rooted 2 Rooted 3 Binary Search Expression More Exp. A tree with n vertices has n 1 edges. Proof: Every vertex except the root has an edge from its parent. An m-ary tree with i internal vertices has at most mi + 1 vertices. Proof: Each internal vertex can at most m edges to its children. mi edges imply mi + 1 vertices. A binary tree of height h has at most 2 h leaves. Proof later. CS 2233 Discrete Mathematical Structures 6

Properties of 2 Rooted 1 Rooted 2 Rooted 3 Binary Search Expression More Exp. A binary tree of height h has at most 2 h+1 1 vertices. Adapt proof of previous property. The minimum height of a binary tree with l leaves is h = log 2 l. Proof: Minimum height h must satisfy 2 h 1 < l 2 h. This is equivalent to h 1 < log 2 l h. It follows that log 2 l = h The minimum height of a binary tree with n vertices is log 2 n. Adapt previous proof. CS 2233 Discrete Mathematical Structures 7

Number of Leaves in a Binary Tree Rooted 1 Rooted 2 Rooted 3 Binary Search Expression More Exp. A binary tree of height h has at most 2 h leaves. Basis: A binary tree of height 0 has 2 0 = 1 leaf. Induction: Assume k 0 and a binary tree of height k has at most 2 k leaves. Show a binary tree of height k + 1 has at most 2 k+1 leaves. Proof: The root of a binary tree of height k + 1 can have 2 subtrees of height k (or less). By the inductive assumption, a binary tree of height k has at most 2 k leaves. 2 subtrees of height k have at most 2(2 k ) = 2 k+1 leaves. CS 2233 Discrete Mathematical Structures 8

Binary Search Rooted 1 Rooted 2 Rooted 3 Binary Search Expression More Exp. In a binary search tree, each vertex is labeled with a key, the left tree of a vertex contains smaller keys, and its right tree contains larger keys. CS 2233 Discrete Mathematical Structures 9

Rooted 1 Rooted 2 Rooted 3 Binary Search Expression More Exp. procedure preorder(v) assert v is a vertex in an ordered rooted tree list v for each child c of v from left to right preorder(c) procedure postorder(v) assert v is a vertex in an ordered rooted tree for each child c of v from left to right postorder(c) list v CS 2233 Discrete Mathematical Structures 10

Rooted 1 Rooted 2 Rooted 3 Binary Search Expression More Exp. procedure inorder(v) assert v is a vertex in an ordered rooted tree for each child c of v from left to right inorder(c) if c is first child of v then list v if v has no children then list v v preorder postorder inorder T1 T2 v T1 T2 T1 T2 v T1 v T2 CS 2233 Discrete Mathematical Structures 11

Expression Rooted 1 Rooted 2 Rooted 3 Binary Search Expression More Exp. CS 2233 Discrete Mathematical Structures 12

More Expression Rooted 1 Rooted 2 Rooted 3 Binary Search Expression More Exp. CS 2233 Discrete Mathematical Structures 13

DFS BFS Minimum Spanning Prim s Algorithm Kruskal s Algorithm A spanning tree of a simple graph G is a subgraph that is a tree and contains every vertex. A simple graph is connected if and only if it has a spanning tree. Depth-first search and breadth-first search can be used to find spanning trees. Depth-first and breadth-first search can be implemented in O(v + e) time where v is the number of vertices and e is the number of edges. CS 2233 Discrete Mathematical Structures 14

Depth-First Search Again DFS BFS Minimum Spanning Prim s Algorithm Kruskal s Algorithm procedure DFS (G) assert G is weighted connected undirected graph stack := arbitrary vertex in G T := empty tree while stack is not empty assert x T or x reachable from some y stack u := top of stack if there is an edge {u, v} s.t. v T then add {u, v} to T and push v on stack else pop stack end while assert all vertices are in T return T CS 2233 Discrete Mathematical Structures 15

Breadth-First Search DFS BFS Minimum Spanning Prim s Algorithm Kruskal s Algorithm procedure BFS (G) assert G is weighted connected undirected graph queue := arbitrary vertex in G T := empty tree while queue is not empty assert x T or x reachable from some y queue u := beginning of queue if there is an edge {u, v} s.t. v T then add {u, v} to T and enqueue v on queue else dequeue queue end while assert all vertices are in T return T CS 2233 Discrete Mathematical Structures 16

Minimum DFS BFS Minimum Prim s Algorithm Kruskal s Algorithm A minimum spanning tree of a weighted simple graph G is a spanning tree with the minimum sum of weights. Prim s algorithm and Kruskal s algorithm can be used to find minimal spanning trees. Prim s algorithm and Kruskal s algorithm can be implemented in O(e log e) time where e is the number of edges. Prim: Find min. edge from tree so far to new vertex. Kruskal: Find minimum edge avoiding cycle. CS 2233 Discrete Mathematical Structures 17

Prim s Algorithm DFS BFS Minimum Spanning Prim s Algorithm Kruskal s Algorithm procedure Prim (G) assert G is weighted connected undirected graph T := smallest edge in G and the edge s vertices n := number of vertices in G for i := 1 to n 2 assert T is a tree with i edges e := smallest edge in G with one vertex in T and the other vertex not in T add e and the new vertex to T end for assert T is a tree with n 1 edges return T CS 2233 Discrete Mathematical Structures 18

Kruskal s Algorithm DFS BFS Minimum Spanning Prim s Algorithm Kruskal s Algorithm procedure Kruskal (G: weighted connected simple graph) assert G is weighted connected undirected graph T := vertices of G and smallest edge in G n := number of vertices in G for i := 1 to n 2 assert T is a forest with n i components e := smallest edge in G connecting two components of T add e to T end for assert T is a forest with 1 component return T CS 2233 Discrete Mathematical Structures 19