Top Banner
Discrete Structures Lecture 13: Trees Ji Yanyan United International College Thanks to Professor Michael Hvidsten
33

Discrete Structures Lecture 13: Trees Ji Yanyan United International College Thanks to Professor Michael Hvidsten.

Dec 16, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Discrete Structures Lecture 13: Trees Ji Yanyan United International College Thanks to Professor Michael Hvidsten.

Discrete Structures

Lecture 13: TreesJi Yanyan

United International College

Thanks to Professor Michael Hvidsten

Page 2: Discrete Structures Lecture 13: Trees Ji Yanyan United International College Thanks to Professor Michael Hvidsten.

Game Trees

• Definition: A Game Tree models the different outcomes possible in a game.

Vertices: positions in a game

Edges: legal moves from one position to another

Leaves: Final positions of a game

Page 3: Discrete Structures Lecture 13: Trees Ji Yanyan United International College Thanks to Professor Michael Hvidsten.

Game Trees

• Example: Tic-Tac-Toe

Page 4: Discrete Structures Lecture 13: Trees Ji Yanyan United International College Thanks to Professor Michael Hvidsten.

10.3 Tree Traversal

• Rooted trees are used to store information. We often need to do some operation on the vertices in such a tree. A tree traversal is an algorithm designed to “visit” each node in the tree.

• Traversal Algorithms: – Preorder traversal– Inorder traversal– Postorder traversal

• These types correspond to Prefix/Infix/Postfix notation.

Page 5: Discrete Structures Lecture 13: Trees Ji Yanyan United International College Thanks to Professor Michael Hvidsten.

10.3 Tree Traversal

• Definition: Let T be a rooted tree with root r. If T consists only of r, then r is the preorder traversal of T. Otherwise, let T1 , T2 , … , Tn be the subtrees at r from left to right. The preorder traversal of T will begin by visiting r, then T1 (in preorder), then T2 (in preorder), etc, until Tn is traversed in preorder.

Page 6: Discrete Structures Lecture 13: Trees Ji Yanyan United International College Thanks to Professor Michael Hvidsten.

10.3 Tree Traversal

Page 7: Discrete Structures Lecture 13: Trees Ji Yanyan United International College Thanks to Professor Michael Hvidsten.

10.3 Tree Traversal

• Preorder Traversal = (a,b,e,j,k,n,o,p, f,c,d,g,l,m,h,i)

Page 8: Discrete Structures Lecture 13: Trees Ji Yanyan United International College Thanks to Professor Michael Hvidsten.

10.3 Tree Traversal

• Definition: Let T be a rooted tree with root r. If T consists only of r, then r is the inorder traversal of T. Otherwise, let T1 , T2 , … , Tn be the subtrees at r from left to right. The inorder traversal of T will begin by visiting T1 (inorder), then the root r, then T2 (inorder), etc, until Tn is traversed inorder.

Page 9: Discrete Structures Lecture 13: Trees Ji Yanyan United International College Thanks to Professor Michael Hvidsten.

10.3 Tree Traversal

Page 10: Discrete Structures Lecture 13: Trees Ji Yanyan United International College Thanks to Professor Michael Hvidsten.

10.3 Tree Traversal

• Inorder Traversal = (j,e,n,k,o,p,b,f,a, c,l,g,m,d,h,i)

Page 11: Discrete Structures Lecture 13: Trees Ji Yanyan United International College Thanks to Professor Michael Hvidsten.

10.3 Tree Traversal

• Definition: Let T be a rooted tree with root r. If T consists only of r, then r is the postorder traversal of T. Otherwise, let T1 , T2 , … , Tn be the subtrees at r from left to right. The postorder traversal of T will begin by visiting T1 (in postorder), then T2 (in postorder), etc, then Tn (in postorder) and finally the root r.

Page 12: Discrete Structures Lecture 13: Trees Ji Yanyan United International College Thanks to Professor Michael Hvidsten.

10.3 Tree Traversal

Page 13: Discrete Structures Lecture 13: Trees Ji Yanyan United International College Thanks to Professor Michael Hvidsten.

10.3 Tree Traversal

• Postorder Traversal = (j,n,o,p,k,e,f,b, c,l,m,g,h,i,d,a)

Page 14: Discrete Structures Lecture 13: Trees Ji Yanyan United International College Thanks to Professor Michael Hvidsten.

Expression Trees

• The expression tree for an arithmetic expression consists of – Vertices: numbers, +, -, *, /, ↑

• (↑ represents the power function) – Edges: linking parts of an expression– Internal vertices represent operations– Leaves represent the variables or numbers

Page 15: Discrete Structures Lecture 13: Trees Ji Yanyan United International College Thanks to Professor Michael Hvidsten.

Expression Trees

• Example: We build the expression tree for ((x+y)↑2)+ ((x-4)/3) from the “bottom” up

Page 16: Discrete Structures Lecture 13: Trees Ji Yanyan United International College Thanks to Professor Michael Hvidsten.

Expression Trees

• Infix form of the expression = inorder traversal

• Get: x+y↑2+x-4/3• Add parentheses for groups:• ((x+y)↑2)+((x-4)/3)

Page 17: Discrete Structures Lecture 13: Trees Ji Yanyan United International College Thanks to Professor Michael Hvidsten.

Expression Trees

• Prefix form of the expression = preorder traversal

• Get: +↑+ x y 2 / - x 4 3

Page 18: Discrete Structures Lecture 13: Trees Ji Yanyan United International College Thanks to Professor Michael Hvidsten.

Expression Trees

• What is expression with prefix form + - * 2 3 5 /↑2 3 4 ?• Answer: ((2*3)-5)+((2↑3)/4)

Page 19: Discrete Structures Lecture 13: Trees Ji Yanyan United International College Thanks to Professor Michael Hvidsten.

Expression Trees

• Postfix form of the expression = postorder traversal

• Get: x y + 2 ↑ x 4 – 3 / +

Page 20: Discrete Structures Lecture 13: Trees Ji Yanyan United International College Thanks to Professor Michael Hvidsten.

10.4 Spanning Trees

• Recall: A tree is an undirected connected graph without cycles

• Definition: A spanning tree of a connected undirected graph G is a subgraph of G that contains all of G’s vertices and enough of its edges to form a tree

• To obtain a spanning tree from a connected undirected graph with cycles– Remove edges until there are no cycles

Page 21: Discrete Structures Lecture 13: Trees Ji Yanyan United International College Thanks to Professor Michael Hvidsten.

10.4 Spanning Trees

Page 22: Discrete Structures Lecture 13: Trees Ji Yanyan United International College Thanks to Professor Michael Hvidsten.

10.4 Spanning Trees

• There are two algorithms for constructing spanning trees:

• Depth-First Search (Back-tracking)• Breadth-First Search

Page 23: Discrete Structures Lecture 13: Trees Ji Yanyan United International College Thanks to Professor Michael Hvidsten.

Depth-First Search

• Depth-First Search (DFS) proceeds along a path from a vertex v as deeply into the graph as possible before backing up (back-tracking)

• To create a depth-first search (DFS) spanning tree– Traverse the graph using a depth-first search and

mark the edges that you follow– After the traversal is complete, the graph’s vertices

and marked edges form the spanning tree

Page 24: Discrete Structures Lecture 13: Trees Ji Yanyan United International College Thanks to Professor Michael Hvidsten.

Depth-First Search

Start at f:

Page 25: Discrete Structures Lecture 13: Trees Ji Yanyan United International College Thanks to Professor Michael Hvidsten.

Breadth-First Search

• Breadth-First Search (BFS) visits every vertex adjacent to a vertex v that it can before visiting any other vertex

• To create a breath-first search (BFS) spanning tree– Traverse the graph using a bread-first search and

mark the edges that you follow– When the traversal is complete, the graph’s

vertices and marked edges form the spanning tree

Page 26: Discrete Structures Lecture 13: Trees Ji Yanyan United International College Thanks to Professor Michael Hvidsten.

Breadth-First Search

Start at e

Page 27: Discrete Structures Lecture 13: Trees Ji Yanyan United International College Thanks to Professor Michael Hvidsten.

Minimum Spanning Trees

• Definition: A minimum spanning tree in a connected weighted graph is a spanning tree that has the smallest possible sum of weights of edges. – There may be several minimum spanning trees for

a particular graph

Page 28: Discrete Structures Lecture 13: Trees Ji Yanyan United International College Thanks to Professor Michael Hvidsten.

Prim’s Algorithm

• Prim’s Algorithm finds a minimal spanning tree that begins at any vertex– Find the least-cost edge (v, u) from a visited vertex

v to some unvisited vertex u– Mark u as visited– Add the vertex u and the edge (v, u) to the

minimum spanning tree– Repeat the above steps until there are no more

unvisited vertices

Page 29: Discrete Structures Lecture 13: Trees Ji Yanyan United International College Thanks to Professor Michael Hvidsten.

Prim’s Algorithm

• Prim’s Algorithm finds a minimal spanning tree that begins at any vertex– Find the least-cost edge (v, u) from a visited vertex

v to some unvisited vertex u– Mark u as visited– Add the vertex u and the edge (v, u) to the

minimum spanning tree– Repeat the above steps until there are no more

unvisited vertices

Page 30: Discrete Structures Lecture 13: Trees Ji Yanyan United International College Thanks to Professor Michael Hvidsten.

Prim’s Algorithm

Page 31: Discrete Structures Lecture 13: Trees Ji Yanyan United International College Thanks to Professor Michael Hvidsten.

Kruskal’s Algorithm

• Kruskal’s Algorithm finds a minimal spanning tree that begins at any vertex– Choose a least-cost edge (v, u) as the root– Successively add edges with minimum weight that

do not from a circuit with already chosen edges. – Stop after n-1 edges are chosen.

Page 32: Discrete Structures Lecture 13: Trees Ji Yanyan United International College Thanks to Professor Michael Hvidsten.

Kruskal’s Algorithm

Page 33: Discrete Structures Lecture 13: Trees Ji Yanyan United International College Thanks to Professor Michael Hvidsten.

Ass 14

• P723 17(b)(c)(d)• Use breadth-first and depth-first search to produce a

spanning tree for the simple graphs in Exercises 13 of page 735. Choose e as the root of each spanning tree.( Using the alphabet order)

Bonus: P723 23(a)(c)