Date post: | 16-Dec-2015 |
Category: |
Documents |
Upload: | tamsyn-carpenter |
View: | 216 times |
Download: | 0 times |
Discrete Structures
Lecture 13: TreesJi 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
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.
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.
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.
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.
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
Expression Trees
• Example: We build the expression tree for ((x+y)↑2)+ ((x-4)/3) from the “bottom” up
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)
Expression Trees
• What is expression with prefix form + - * 2 3 5 /↑2 3 4 ?• Answer: ((2*3)-5)+((2↑3)/4)
Expression Trees
• Postfix form of the expression = postorder traversal
• Get: x y + 2 ↑ x 4 – 3 / +
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
10.4 Spanning Trees
• There are two algorithms for constructing spanning trees:
• Depth-First Search (Back-tracking)• Breadth-First Search
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
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
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
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
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
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.