22
03/12/13 Trees and CFGs Discrete Structures (CS 173) Derek Hoiem, University 1

03/12/13

  • Upload
    mikasi

  • View
    29

  • Download
    0

Embed Size (px)

DESCRIPTION

Trees and CFGs. 03/12/13. Discrete Structures (CS 173) Derek Hoiem, University of Illinois. Last class: recursive functions.  base case  recursive formula Process for finding closed form Unroll for several steps Write in terms of or Substitute for value of that is base case - PowerPoint PPT Presentation

Citation preview

Page 1: 03/12/13

03/12/13

Trees and CFGs

Discrete Structures (CS 173)Derek Hoiem, University of Illinois 1

Page 2: 03/12/13

Last class: recursive functions

base case recursive formula

• Process for finding closed form1. Unroll for several steps2. Write in terms of or 3. Substitute for value of that is base case4. Substitute base case value(s) and solve

• Verifying closed form with induction

2

Page 3: 03/12/13

Today’s lecture: Trees and CFGs

• Trees– Examples of uses– Terminology– Induction on trees

• Context free grammars (CFGs)– What they are, how they work– Induction on CFG

3

Page 4: 03/12/13

Tree: special form of graph with “root” and no cycles

4

root

Page 5: 03/12/13

Tree terminologyNodes: root, internal, leaf, level, tree heightRelations: parent/child/sibling, ancestor/descendant

5overhead

Page 6: 03/12/13

Another example

6

64

7

5

2

3

1root

Page 7: 03/12/13

Another example

7

6

4

7

5

2

3

1root

64

7

5

2

3

1root

Page 8: 03/12/13

Trees for sorting

8

><

><

Page 9: 03/12/13

Decision trees

9

Page 10: 03/12/13

Hierarchical data structure

10

Page 11: 03/12/13

Trees for clustering• Goal: create a function that maps from to

such that nearby N-dimensional points are mapped to the same integer

11

b>5

xx

x

x

x

x

x

xx

xx

1

2 3

a

b

5

6

a>6

1

yes

no

no

yes

2 3

Page 12: 03/12/13

More terminologym-ary tree: each node can split into m subtreesfull: each node splits or timescomplete: all leaves have the same heightBalanced: all leaves are approximately the same height

How many nodes in a full m-ary tree with internal nodes?

What are the lower/upper bounds on the number of nodes of an m-ary tree with height ?

12overhead

Page 13: 03/12/13

Induction proof on treesClaim: In a binary tree of height , the number of nodes .

13overhead

Page 14: 03/12/13

Context-free GrammarsA context-free grammar (CFG) is a set of rules that defines a set of possible parse trees.

A CFG specifies a set of rules, valid start symbols, and valid terminals.

Example: Start symbol: Terminals:

14overhead

Page 15: 03/12/13

CFG ExampleExample:

Which of these strings can be generated by the grammar above?

15overhead

Page 16: 03/12/13

Examples of parse trees

16

Language

Fig: Johnson 2007

Page 17: 03/12/13

Examples of parse trees

17Figs: Zhu and Mumford 2007

Scene parse

Object Parse

Page 18: 03/12/13

Examples of parse trees

18

Stochastic CFG for blackjack actions

Page 19: 03/12/13

Induction proof on CFG Claim: For any string generated by the grammar above, the number of ’s will be equal to the number of ’s plus the number of ’s (

19overhead

Page 20: 03/12/13

CFG example

20

*

I like to eat apples and bananas

overhead

Page 21: 03/12/13

Things to remember

• Trees are a special graph with root and no cycles, with many uses– Sorting, clustering, finding similar values– Decision tree: machine learning, modeling choices– Parse trees: representing hierarchical structures

• Context free grammars: generate parse trees

• Proofs on trees: split at root, use inductive hypothesis on subtrees headed by the root’s children

21

Page 22: 03/12/13

Next class: more trees• Recursion trees and more proofs with trees

22