COMP108 Algorithmic Foundations

Similar documents
Lesson 4.4. Euler Circuits and Paths. Explore This

Lecture 13: Graphs I: Breadth First Search

12/9/14. CS151 Fall 20124Lecture (almost there) 12/6. Graphs. Seven Bridges of Königsberg. Leonard Euler

Lecture 8: Graph-theoretic problems (again)

Adjacency. Adjacency Two vertices u and v are adjacent if there is an edge connecting them. This is sometimes written as u v.

V = set of vertices (vertex / node) E = set of edges (v, w) (v, w in V)

10.2 Graph Terminology and Special Types of Graphs

Chapter 9. Greedy Technique. Copyright 2007 Pearson Addison-Wesley. All rights reserved.

CS311H: Discrete Mathematics. Graph Theory IV. A Non-planar Graph. Regions of a Planar Graph. Euler s Formula. Instructor: Işıl Dillig

Ma/CS 6b Class 1: Graph Recap

Paradigm 5. Data Structure. Suffix trees. What is a suffix tree? Suffix tree. Simple applications. Simple applications. Algorithms

Problem Final Exam Set 2 Solutions

CS 241 Week 4 Tutorial Solutions

CMPUT101 Introduction to Computing - Summer 2002

Greedy Algorithm. Algorithm Fall Semester

Ma/CS 6b Class 1: Graph Recap

What are suffix trees?

Lecture 12 : Topological Spaces

Solving Problems by Searching. CS 486/686: Introduction to Artificial Intelligence Winter 2016

2 Computing all Intersections of a Set of Segments Line Segment Intersection

10.5 Graphing Quadratic Functions

If you are at the university, either physically or via the VPN, you can download the chapters of this book as PDFs.

Final Exam Review F 06 M 236 Be sure to look over all of your tests, as well as over the activities you did in the activity book

Lesson6: Modeling the Web as a graph Unit5: Linear Algebra for graphs

ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών

Convex Hull Algorithms. Convex hull: basic facts

Containers: Queue and List

Solving Problems by Searching. CS 486/686: Introduction to Artificial Intelligence

Graph theory Route problems

CS 551 Computer Graphics. Hidden Surface Elimination. Z-Buffering. Basic idea: Hidden Surface Removal

Tries. Yufei Tao KAIST. April 9, Y. Tao, April 9, 2013 Tries

Honors Thesis: Investigating the Algebraic Properties of Cayley Digraphs

COMP 423 lecture 11 Jan. 28, 2008

Introduction to Algebra

Chapter 4 Fuzzy Graph and Relation

Outline. CS38 Introduction to Algorithms. Graphs. Graphs. Graphs. Graph traversals

Suffix trees, suffix arrays, BWT

Pattern Matching. Pattern Matching. Pattern Matching. Review of Regular Expressions

s 1 t 4 s 2 4 t 2 a b r 2 r 8 r10 g 4

MITSUBISHI ELECTRIC RESEARCH LABORATORIES Cambridge, Massachusetts. Introduction to Matroids and Applications. Srikumar Ramalingam

Can Pythagoras Swim?

Today. CS 188: Artificial Intelligence Fall Recap: Search. Example: Pancake Problem. Example: Pancake Problem. General Tree Search.

Internet Routing. IP Packet Format. IP Fragmentation & Reassembly. Principles of Internet Routing. Computer Networks 9/29/2014.

Presentation Martin Randers

Line The set of points extending in two directions without end uniquely determined by two points. The set of points on a line between two points

binary trees, expression trees

CS553 Lecture Introduction to Data-flow Analysis 1

6.045J/18.400J: Automata, Computability and Complexity. Quiz 2: Solutions. Please write your name in the upper corner of each page.

LINX MATRIX SWITCHERS FIRMWARE UPDATE INSTRUCTIONS FIRMWARE VERSION

Information Retrieval and Organisation

Distance vector protocol

ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών. Lecture 3b Lexical Analysis Elias Athanasopoulos

1.1. Interval Notation and Set Notation Essential Question When is it convenient to use set-builder notation to represent a set of numbers?

CS 221: Artificial Intelligence Fall 2011

Representation of Numbers. Number Representation. Representation of Numbers. 32-bit Unsigned Integers 3/24/2014. Fixed point Integer Representation

Outline. Motivation Background ARCH. Experiment Additional usages for Input-Depth. Regular Expression Matching DPI over Compressed HTTP

Today. Search Problems. Uninformed Search Methods. Depth-First Search Breadth-First Search Uniform-Cost Search

CS201 Discussion 10 DRAWTREE + TRIES

Premaster Course Algorithms 1 Chapter 6: Shortest Paths. Christian Scheideler SS 2018

Computational geometry

Fig.25: the Role of LEX

4.3 Balanced Trees. let us assume that we can manipulate them conveniently and see how they can be put together to form trees.

Type Checking. Roadmap (Where are we?) Last lecture Context-sensitive analysis. This lecture Type checking. Symbol tables

A Tautology Checker loosely related to Stålmarck s Algorithm by Martin Richards

CSCI 446: Artificial Intelligence

Announcements. CS 188: Artificial Intelligence Fall Recap: Search. Today. Example: Pancake Problem. Example: Pancake Problem

CSCI1950 Z Computa4onal Methods for Biology Lecture 2. Ben Raphael January 26, hhp://cs.brown.edu/courses/csci1950 z/ Outline

Definition of Regular Expression

COMMON FRACTIONS. or a / b = a b. , a is called the numerator, and b is called the denominator.

CS 340, Fall 2016 Sep 29th Exam 1 Note: in all questions, the special symbol ɛ (epsilon) is used to indicate the empty string.

c s ha2 c s Half Adder Figure 2: Full Adder Block Diagram

Error Numbers of the Standard Function Block

Duality in linear interval equations

Doubts about how to use azimuth values from a Coordinate Object. Juan Antonio Breña Moral

Orthogonal line segment intersection

6.3 Volumes. Just as area is always positive, so is volume and our attitudes towards finding it.

String comparison by transposition networks

Tight triangulations: a link between combinatorics and topology

AI Adjacent Fields. This slide deck courtesy of Dan Klein at UC Berkeley

Introduction. Example

[SYLWAN., 158(6)]. ISI

Width and Bounding Box of Imprecise Points

Parallelization Optimization of System-Level Specification

CSEP 573 Artificial Intelligence Winter 2016

Introducing fractions

Suffix Tries. Slides adapted from the course by Ben Langmead

Minimal Memory Abstractions

Algorithm Design (5) Text Search

COMBINATORIAL PATTERN MATCHING

The Network Layer: Routing in the Internet. The Network Layer: Routing & Addressing Outline

Internet Routing. Reminder: Routing. CPSC Network Programming

From Dependencies to Evaluation Strategies

Dr. D.M. Akbar Hussain

Journal of Combinatorial Theory, Series A

Balanced Trees. 2-3 trees red-black trees B-trees. 2-3 trees red-black trees B-trees smaller than. 2-node. 3-node E J S X A C.

Distributed Systems Principles and Paradigms

Right Angled Trigonometry. Objective: To know and be able to use trigonometric ratios in rightangled

Calculus Differentiation

ITEC2620 Introduction to Data Structures

Intermediate Information Structures

Transcription:

Grph Theory Prudene Wong http://www.s.liv..uk/~pwong/tehing/omp108/201617

How to Mesure 4L? 3L 5L 3L ontiner & 5L ontiner (without mrk) infinite supply of wter You n pour wter from one ontiner to nother How to mesure 4L of wter?

Lerning outomes Ale to tell wht n undireted grph is nd wht direted grph is Know how to represent grph using mtrix nd list Understnd wht Euler iruit is nd le to determine whether suh iruit exists in n undireted grph Ale to pply BFS nd DFS to trverse grph Ale to tell wht tree is 3

Grph

Grphs introdued in the 18th entury Grph theory n old sujet with mny modern pplitions. An undireted grph G=(V,E) onsists of set of verties V nd set of edges E. Eh edge is n unordered pir of verties. (E.g., {,} & {,} refer to the sme edge.) A direted grph G=(V,E) onsists of Eh edge is n ordered pir of verties. (E.g., (,) refer to n edge from to.) d e f Modeling Feook & Twitter? d e f 5

Grphs represent set of interonneted ojets e e f f d d undireted grph direted grph 6

Applitions of grphs In omputer siene, grphs re often used to model omputer networks, preedene mong proesses, stte spe of plying hess (AI pplitions) resoure onflits, In other disiplines, grphs re lso used to model the struture of ojets. E.g., iology - evolutionry reltionship hemistry - struture of moleules 7

Undireted grphs Undireted grphs: simple grph: t most one edge etween two verties, no self loop (i.e., n edge from vertex to itself). multigrph: llows more thn one edge etween two verties. Reminder: An undireted grph G=(V,E) onsists of set of verties V nd set of edges E. Eh edge is n unordered pir of verties. d e f 8

Undireted grphs In n undireted grph G, suppose tht e = {u, v} is n edge of G u nd v re sid to e djent nd lled neighors of eh other. u nd v re lled endpoints of e. e is sid to e inident with u nd v. e is sid to onnet u nd v. u deg(v) = 2 e v w deg(u) = 1 The degree of vertex v, denoted y deg(v), is the numer of edges inident with it ( loop ontriutes twie to the degree); The degree of grph is the mximum degree over ll verties 9

Representtion (of undireted grphs) An undireted grph n e represented y djeny mtrix, djeny list, inidene mtrix or inidene list. Adjeny mtrix nd djeny list reord the reltionship etween vertex djeny, i.e., vertex is djent to whih other verties Inidene mtrix nd inidene list reord the reltionship etween edge inidene, i.e., n edge is inident with whih two verties 10

Dt Struture - Mtrix Retngulr / 2-dimensionl rry m-y-n mtrix m rows n olumns i,j m-y-n mtrix n olumns 1,1 1,2 1,3 1,n 2,1 2,2 2,3 2,n i,j row i, olumn j m rows 3,1 3,2 3,3 3,n m,1 m,2 m,3 m,n 11

Dt Struture - Linked List List of elements (nodes) onneted together like hin Eh node ontins two fields: "dt" field: stores whtever type of elements "next" field: pointer to link this node to the next node in the list Hed / Til hed 10 30 20 pointer to the eginning & end of list dt next til 12

Dt Struture - Linked List Queue (FIFO: first-in-first-out) Insert element (enqueue) to til Remove element (dequeue) from hed hed 10 30 20 til Insert 40 hed 10 30 20 40 til rete newnode of 40; til.next = newnode; til = til.next Remove 10 hed 30 20 40 til return whtever hed points to; hed = hed.next 13

Adjeny mtrix / list Adjeny mtrix M for simple undireted grph with n verties is n nxn mtrix M(i, j) = 1 if vertex i nd vertex j re djent M(i, j) = 0 otherwise Adjeny list: eh vertex hs list of verties to whih it is djent d e d e 0 0 1 1 0 0 0 1 1 0 1 1 0 1 1 d 1 1 1 0 1 e 0 0 1 1 0 d e d d d d e e 14

Representtion (of undireted grphs) An undireted grph n e represented y djeny mtrix, djeny list, inidene mtrix or inidene list. Adjeny mtrix nd djeny list reord the reltionship etween vertex djeny, i.e., vertex is djent to whih other verties Inidene mtrix nd inidene list reord the reltionship etween edge inidene, i.e., n edge is inident with whih two verties 15

Inidene mtrix / list Inidene mtrix M for simple undireted grph with n verties nd m edges is n mxn mtrix M(i, j) = 1 if edge i nd vertex j re inidene M(i, j) = 0 otherwise Inidene list: eh edge hs list of verties to whih it is inident with 1 2 3 4 7 5 d 6 e lels of edge re edge numer d e 1 1 0 1 0 0 2 1 0 0 1 0 3 0 1 1 0 0 4 0 1 0 1 0 5 0 0 1 1 0 6 0 0 0 1 1 7 0 0 1 0 1 1 2 d 3 4 d 5 d 6 d e 7 e 16

Exerise Give the djeny mtrix nd inidene mtrix of the following grph d e f 1 2 3 d e f lels of edge re edge numer 4 5 d e f 1 2 3 4 5 d e f 17

Direted grph

Direted grph Given direted grph G, vertex is sid to e onneted to vertex if there is pth from to. E.g., G represents the routes provided y ertin irline. Tht mens, vertex represents ity nd n edge represents flight from ity to nother ity. Then we my sk question like: Cn we fly from one ity to nother? Reminder: A direted grph G=(V,E) onsists of set of verties V nd set of edges E. Eh edge is n ordered pir of verties. d e E = { (,), (,d), (,e), (,), (,e), (d,e) } N.B. (,) is in E, ut (,) is NOT 19

In/Out degree (in direted grphs) The in-degree of vertex v is the numer of edges leding to the vertex v. The out-degree of vertex v is the numer of edges leding wy from the vertex v. d e v in-deg(v) out-deg(v) 0 1 2 2 0 2 d 1 1 e 3 0 sum: 6 6 Alwys equl? 20

Representtion (of direted grphs) Similr to undireted grph, direted grph n e represented y djeny mtrix, djeny list, inidene mtrix or inidene list. 21

Adjeny mtrix / list Adjeny mtrix M for direted grph with n verties is n nxn mtrix M(i, j) = 1 if (i,j) is n edge M(i, j) = 0 otherwise Adjeny list: eh vertex u hs list of verties pointed to y n edge leding wy from u d e d e 0 1 0 0 0 0 0 0 1 1 0 1 0 0 1 d 0 0 0 0 1 e 0 0 0 0 0 d e d e e e 22

Inidene mtrix / list Inidene mtrix M for direted grph with n verties nd m edges is n mxn mtrix M(i, j) = 1 if edge i is leding wy from vertex j M(i, j) = -1 if edge i is leding to vertex j Inidene list: eh edge hs list of two verties (leding wy is 1st nd leding to is 2nd) 1 2 3 6 d 5 4 e d e 1 1-1 0 0 0 2 0-1 1 0 0 3 0 1 0-1 0 4 0 1 0 0-1 5 0 0 0 1-1 6 0 0 1 0-1 1 2 3 4 d e 5 d e 6 e 23

Exerise Give the djeny mtrix nd inidene mtrix of the following grph d e f 1 2 3 4 d e f lels of edge re edge numer 5 6 d e f 1 2 3 4 5 6 d e f 24

Euler iruit

Pths, iruits (in undireted grphs) In n undireted grph, pth from vertex u to vertex v is sequene of edges e 1 = {u, x 1 }, e 2 = {x 1, x 2 }, e n = {x n-1, v}, where n 1. The length of this pth is n. Note tht pth from u to v implies pth from v to u. If u = v, this pth is lled iruit (yle). u e 1 e 2 e n v 26

Euler iruit A simple iruit visits n edge t most one. An Euler iruit in grph G is iruit visiting every edge of G extly one. (NB. A vertex n e repeted.) Does every grph hs n Euler iruit? d e d e d e d no Euler iruit 27

History: In Konigserg, Germny, river rn through the ity nd seven ridges were uilt. The people wondered whether or not one ould go round the ity in wy tht would involve rossing eh ridge extly one. river nks 3 1 4 2 5 7 d 6 ridges 1 2 3 4 7 5 6 d no Euler iruit 28

How to determine whether there is n Euler iruit in grph? 29

A trivil ondition An undireted grph G is sid to e onneted if there is pth etween every pir of verties. If G is not onneted, there is no single iruit to visit ll edges or verties. d e f Even if the grph is onneted, there my e no Euler iruit either. d d e d e 30

Neessry nd suffiient ondition Let G e onneted grph. Lemm: G ontins n Euler iruit if nd only if degree of every vertex is even. 31

Neessry nd suffiient ondition Let G e onneted grph. Lemm: G ontins n Euler iruit if nd only if degree of every vertex is even. d e f u ed d e f How to find it? u u' ed edfd d e f u u' d e f u'' ed edfd edfefd 32

Hmiltonin iruit Let G e n undireted grph. A Hmiltonin iruit is iruit ontining every vertex of G extly one. Note tht Hmiltonin iruit my NOT visit ll edges. Unlike the se of Euler iruits, determining whether grph ontins Hmiltonin iruit is very diffiult prolem. (NP-hrd) 33

Bredth First Serh BFS

Bredth First Serh (BFS) All verties t distne k from s re explored efore ny verties t distne k+1. The soure is. Order of explortion, d e f g h k 35

Bredth First Serh (BFS) All verties t distne k from s re explored efore ny verties t distne k+1. The soure is. Distne 1 from. Order of explortion,, e, d d e f g h k 36

Bredth First Serh (BFS) All verties t distne k from s re explored efore ny verties t distne k+1. The soure is. Order of explortion,, e, d,, f, h, g d e f Distne 2 from. g h k 37

Bredth First Serh (BFS) All verties t distne k from s re explored efore ny verties t distne k+1. The soure is. Order of explortion,, e, d,, f, h, g, k d e f g h k Distne 3 from. 38

In generl (BFS) Explore dist 0 frontier s distne 0 39

In generl (BFS) Explore dist 1 frontier s distne 0 distne 1 40

In generl (BFS) s distne 0 distne 1 Explore dist 2 frontier distne 2 41

Bredth First Serh (BFS) A simple lgorithm for serhing grph. Given G=(V, E), nd distinguished soure vertex s, BFS systemtilly explores the edges of G suh tht ll verties t distne k from s re explored efore ny verties t distne k+1. 42

Exerise BFS Apply BFS to the following grph strting from vertex nd list the order of explortion d e f 43

Exerise (2) BFS Apply BFS to the following grph strting from vertex nd list the order of explortion e d f g 44

BFS Pseudo ode unmrk ll verties hoose some strting vertex s mrk s nd insert s into til of list L while L is nonempty do egin remove vertex v from front of L visit v for eh unmrked neighor w of v do mrk w nd insert w into til of list L end 45

BFS using linked list hed til d e f hed e d til g h k hed e d f til,, e, d,, f, h, g, k hed d f til hed f h g til hed f h g til hed h g k til 46 & so on

Depth First Serh DFS

Depth First Serh (DFS) Edges re explored from the most reently disovered vertex, ktrks when finished The soure is. Order of explortion, d e f g h k DFS serhes "deeper" in the grph whenever possile 48

Depth First Serh (DFS) Edges re explored from the most reently disovered vertex, ktrks when finished The soure is. Order of explortion, serh spe d e f g h k DFS serhes "deeper" in the grph whenever possile 49

Depth First Serh (DFS) Edges re explored from the most reently disovered vertex, ktrks when finished The soure is. serh spe Order of explortion, d e f g h k DFS serhes "deeper" in the grph whenever possile 50

Depth First Serh (DFS) Edges re explored from the most reently disovered vertex, ktrks when finished The soure is. serh spe is empty Order of explortion,, d e f g h k DFS serhes "deeper" in the grph whenever possile 51

Depth First Serh (DFS) Edges re explored from the most reently disovered vertex, ktrks when finished The soure is. Order of explortion,, nowhere to go, ktrk d e f g h k serh spe DFS serhes "deeper" in the grph whenever possile 52

Depth First Serh (DFS) Edges re explored from the most reently disovered vertex, ktrks when finished The soure is. Order of explortion,,, f d e f g h k serh spe DFS serhes "deeper" in the grph whenever possile 53

Depth First Serh (DFS) Edges re explored from the most reently disovered vertex, ktrks when finished The soure is. Order of explortion,,, f, k d e f g h k serh spe is empty DFS serhes "deeper" in the grph whenever possile 54

Depth First Serh (DFS) Edges re explored from the most reently disovered vertex, ktrks when finished The soure is. Order of explortion,,, f, k, e d e f serh spe g h k ktrk DFS serhes "deeper" in the grph whenever possile 55

Depth First Serh (DFS) Edges re explored from the most reently disovered vertex, ktrks when finished The soure is. Order of explortion,,, f, k, e ktrk serh spe d e f g h k DFS serhes "deeper" in the grph whenever possile 56

Depth First Serh (DFS) Edges re explored from the most reently disovered vertex, ktrks when finished The soure is. Order of explortion,,, f, k, e, d serh spe d e f g h k DFS serhes "deeper" in the grph whenever possile 57

Depth First Serh (DFS) Edges re explored from the most reently disovered vertex, ktrks when finished The soure is. Order of explortion,,, f, k, e, d, h d e f g h k serh spe is empty DFS serhes "deeper" in the grph whenever possile 58

Depth First Serh (DFS) Edges re explored from the most reently disovered vertex, ktrks when finished The soure is. Order of explortion,,, f, k, e, d, h d e f g h k serh spe ktrk DFS serhes "deeper" in the grph whenever possile 59

Depth First Serh (DFS) Edges re explored from the most reently disovered vertex, ktrks when finished The soure is. Order of explortion,,, f, k, e, d, h, g d e f g h k serh spe is empty DFS serhes "deeper" in the grph whenever possile 60

Depth First Serh (DFS) Edges re explored from the most reently disovered vertex, ktrks when finished The soure is. DONE! Order of explortion,,, f, k, e, d, h, g ktrk d e f g h k DFS serhes "deeper" in the grph whenever possile 61

Depth First Serh (DFS) Depth-first serh is nother strtegy for exploring grph; it serh "deeper" in the grph whenever possile. Edges re explored from the most reently disovered vertex v tht still hs unexplored edges leving it. When ll edges of v hve een explored, the serh "ktrks" to explore edges leving the vertex from whih v ws disovered. 62

Exerise DFS Apply DFS to the following grph strting from vertex nd list the order of explortion d e f, f, d,,, e?? 63

Exerise (2) DFS Apply DFS to the following grph strting from vertex nd list the order of explortion e d f g, e,,?,, f, d,,? 64

DFS Pseudo ode (reursive) Algorithm DFS(vertex v) visit v for eh unvisited neighor w of v do egin DFS(w) end 65

Dt Struture - Stk Dt orgnised in vertil mnner LIFO: lst-in-first-out Top: top of stk Opertions: push & pop top push: dds new element on top of stk pop: remove the element from top of stk 20 30 10 66

Dt Struture - Stk top 20 30 10 Push 40 top top++; stk[top] = newvlue 40 20 30 10 Pop return stk[top]; top-- top 30 10 Pop return stk[top]; top-- 20 30 10 top 67

DFS Pseudo ode (using stk) unmrk ll verties push strting vertex u onto top of stk S while S is nonempty do egin pop vertex v from top of S if (v is unmrked) then egin visit nd mrk v for eh unmrked neighor w of v do push w onto top of S end end 68

DFS using Stk d e f g h k top top e d top f e d top f e d top k e d,,, f, k, e, d, h, g top top g top h g top d top e d 69

Tree

Outline Wht is tree? Wht re sutrees How to trverse inry tree? Pre-order, In-order, Postorder Applition of tree trversl 71

Trees An undireted grph G=(V,E) is tree if G is onneted nd yli (i.e., ontins no yles) Other equivlent sttements: 1. There is extly one pth etween ny two verties in G (G is onneted nd yli) 2. G is onneted nd removl of one edge disonnets G (removl of n edge {u,v} disonnets t lest u nd v euse of [1]) 3. G is yli nd dding one edge retes yle (dding n edge {u,v} retes one more pth etween u nd v, yle is formed) 4. G is onneted nd m=n-1 (where V =n, E =m) 72

Lemm: P(n): If tree T hs n verties nd m edges, then m=n-1. Proof: By indution on the numer of verties. Bse se: A tree with single vertex does not hve n edge. Indution step: P(n-1) P(n) for n > 1? Remove n edge from the tree T. By [2], T eomes disonneted. Two onneted omponents T 1 nd T 2 re otined, neither ontins yle (the yle is lso present in T otherwise). Therefore, oth T 1 nd T 2 re trees. Let n 1 nd n 2 e the numer of verties in T 1 nd T 2. [n 1 +n 2 = n] By the indution hypothesis, T 1 nd T 2 ontins n 1-1 nd n 2-1 edges. Hene, T ontins (n 1-1) + (n 2-1) + 1 = n-1 edges. 73

Rooted trees Tree with hierrhil struture, e.g., diretory struture of file system C:\ Progrm Files My Douments Mirosoft Offie Internet Explorer My Pitures My Musi 74

Terminologies root r r is prent of, & ;,, & re hildren of r deg-0: d, k, p, g, q, s (leves) d e f g h k p Topmost vertex is lled the root. q s deg-1:, e, f deg-2:,, h deg-3: r A vertex u my hve some hildren diretly elow it, u is lled the prent of its hildren. Degree of vertex is the no. of hildren it hs. (N.B. it is different from the degree in n unrooted tree.) Degree of tree is the mx. degree of ll verties. Wht is the degree of this tree? A vertex with no hild (degree-0) is lled lef. All others re lled internl verties. 75

More terminologies r r d e f g h T 1 T 2 T 3 T 1 k p T 2 T 3 q s three sutrees We n define tree reursively A single vertex is tree. If T 1, T 2,, T k re disjoint trees with roots r 1, r 2,, r k, the grph otined y tthing new vertex r to eh of r 1, r 2,, r k with single edge forms tree T with root r. T 1, T 2,, T k re lled sutrees of T. whih re the roots of the sutrees? 76

Binry tree tree of degree t most TWO the two sutrees re lled left sutree nd right sutree (my e empty) r d e f g h k left sutree right sutree There re three ommon wys to trverse inry tree: preorder trversl - vertex, left sutree, right sutree inorder trversl - left sutree, vertex, right sutree postorder trversl - left sutree, right sutree, vertex 77

Trversing inry tree r preorder trversl - vertex, left sutree, right sutree r -> -> -> d -> g -> -> e -> f -> h -> k d e f g h k r 1 2 6 3 4 d e f g 5 7 h 8 k 9 10 78

Trversing inry tree r preorder trversl - vertex, left sutree, right sutree r -> -> -> d -> g -> -> e -> f -> h -> k d e f g 2 r 5 h 7 k inorder trversl - left sutree, vertex, right sutree -> -> g -> d -> r -> e -> -> h -> f -> k 1 4 d e f g 3 6 h 9 k 8 10 79

Trversing inry tree r preorder trversl - vertex, left sutree, right sutree r -> -> -> d -> g -> -> e -> f -> h -> k d e f 1 g 4 r h 10 3 d e f g 2 5 h 9 k 8 k 6 7 inorder trversl - left sutree, vertex, right sutree -> -> g -> d -> r -> e -> -> h -> f -> k postorder trversl - left sutree, right sutree, vertex -> g -> d -> -> e -> h -> k -> f -> -> r 80

Exerise r Give the order of trversl of preorder, inorder, nd postorder trversl of the tree d e f g h k m n preorder: inorder: postorder: 81

Binry Serh Tree 60 for vertex with vlue X, left hild hs vlue X & right hild hs vlue > X 40 90 20 50 80 110 10 30 70 100 120 whih trversl gives numers in sending order? 82

Expression Tree 2 + 5 * * 3 4 (2+5*4)*3 postorder trversl gives 2 5 4 * + 3 * 1. push numers onto stk 2. when opertor is enountered, pop 2 numers, operte on them & push results k to stk 3. repet until the expression is exhusted 83