Download presentation

Presentation is loading. Please wait.

Published byTamsyn Carpenter Modified over 4 years ago

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

2
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

3
Game Trees Example: Tic-Tac-Toe

4
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.

5
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 T 1, T 2, …, T n be the subtrees at r from left to right. The preorder traversal of T will begin by visiting r, then T 1 (in preorder), then T 2 (in preorder), etc, until T n is traversed in preorder.

6
10.3 Tree Traversal

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

8
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 T 1, T 2, …, T n be the subtrees at r from left to right. The inorder traversal of T will begin by visiting T 1 (inorder), then the root r, then T 2 (inorder), etc, until T n is traversed inorder.

9
10.3 Tree Traversal

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

11
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 T 1, T 2, …, T n be the subtrees at r from left to right. The postorder traversal of T will begin by visiting T 1 (in postorder), then T 2 (in postorder), etc, then T n (in postorder) and finally the root r.

12
10.3 Tree Traversal

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

14
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

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

16
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)

17
Expression Trees Prefix form of the expression = preorder traversal Get: + ↑ + x y 2 / - x 4 3

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

19
Expression Trees Postfix form of the expression = postorder traversal Get: x y + 2 ↑ x 4 – 3 / +

20
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

21
10.4 Spanning Trees

22
There are two algorithms for constructing spanning trees: Depth-First Search (Back-tracking) Breadth-First Search

23
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

24
Depth-First Search Start at f:

25
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

26
Breadth-First Search Start at e

27
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

28
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

29
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

30
Prim’s Algorithm

31
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.

32
Kruskal’s Algorithm

33
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)

Similar presentations

© 2019 SlidePlayer.com Inc.

All rights reserved.

To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie policy.

Ads by Google