24
Note about Exer II.2:9c iii) 7b) Chapter III Trees and Searching A tree is a connected graph with no circuits or equivalently, A tree is a graph with a designated vertex called the root; there is a unique path from the root to any other vertex. Note that any vertex can be the root of an undirected tree.

tucker/AMS301-III.docx  · Web viewA tree is called m-ary if every parent has exactly m children, e.g., binary tree. We also call parent vertices . internal vertices. Non-parents

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Page 1: tucker/AMS301-III.docx  · Web viewA tree is called m-ary if every parent has exactly m children, e.g., binary tree. We also call parent vertices . internal vertices. Non-parents

Note about Exer II.2:9c iii) 7b)Chapter III Trees and Searching

A tree is a connected graph with no circuits or equivalently,

A tree is a graph with a designated vertex called the root; there is a unique path from the root to any other vertex.

Note that any vertex can be the root of an undirected tree.

Page 2: tucker/AMS301-III.docx  · Web viewA tree is called m-ary if every parent has exactly m children, e.g., binary tree. We also call parent vertices . internal vertices. Non-parents

With rooted tree structure, we get parents, children, siblings, ancestors, and descendants.

The level number of a vertex x in a tree is the length (number of edges) in the unique path from the root to x.

Theorem 2: A tree with n vertices has n-1 edges. Proof: Let the tree be rooted. Each edge is directed downwards towards a unique vertex. Note that the root has no edge directed into it. So |edges| = |no. of vertices excluding root|

Page 3: tucker/AMS301-III.docx  · Web viewA tree is called m-ary if every parent has exactly m children, e.g., binary tree. We also call parent vertices . internal vertices. Non-parents

A tree is called m-ary if every parent has exactly m children, e.g., binary tree

We also call parent vertices internal vertices. Non-parents are called leaves.

Theorem 3: Let T be an m-ary tree with n vertices, of which i vertices are internal. Then n = mi + 1. Proof: Write as mi = n-1. We use a similar argument as before but now count edges from their upper endvertex. They come from internal vertices. Each internal vertex sends down i edges. So |edges| = mi. Corollary: If T is an m-ary tree with n-vertices—i internal vertices and l leaves (i + l = n)- thena) given i, n = mi + 1 and l = (m-1)i + 1b) given l, then i = (l-1)/(m-1) and n = (ml-1)/(m-1)c) given n, then i = (n-1)/m and l = [(m-1)n+1]/m

Obtained by combining mi + 1=n and i+l = n. For example, mi + 1=n = i+l --> l = mi – i + 1= (m-1)i + 1.

Page 4: tucker/AMS301-III.docx  · Web viewA tree is called m-ary if every parent has exactly m children, e.g., binary tree. We also call parent vertices . internal vertices. Non-parents

Example 1: A tennis tournament has 56 entrants. How many matches will be played? Entrants = leaves and matches = internal vert. By Corol. b), i=(l-1)/(m-1)= (56-1)/(2-1) = 55

Example 2: In a telephone chain among 100 parents to warn of a school closing, a root parent calls three other parents who in turn call three other parents, etc. How many parents will make calls? Parents = total vertices (n) and callers = internal ver.By c), i = (n-1)/m = (100-1)/3=33.

Page 5: tucker/AMS301-III.docx  · Web viewA tree is called m-ary if every parent has exactly m children, e.g., binary tree. We also call parent vertices . internal vertices. Non-parents

The height of a rooted tree is the largest level number (longest path from root to some vertex).

If h is the height of rooted tree T, then T is balanced if every leaf has level number h or h-1. E.g., tennis tournaments are designed to be balanced trees.

Theorem 4: Let T be a rooted m-ary tree of height h with l leaves.

a) l ≤ mh ; if all leaves are at height h, then l = mh.b) logml ≤ h; if the tree is balanced, h = logml. As the above binary tree illustrates, when a tree is given an extra level, all the current leaves can become parents with m children each. This multiplies the number of leaves by m. After h levels of this process starting from the root, we have (at

most) mh leaves.

Taking log base m on both sides of l ≤ mh we have logml ≤

logm(mh) = h. Rounding up to the next integer (since h is an integer), logml ≤ h.

Page 6: tucker/AMS301-III.docx  · Web viewA tree is called m-ary if every parent has exactly m children, e.g., binary tree. We also call parent vertices . internal vertices. Non-parents

Example 3: We want to identify an unknown word X by looking it up in a dictionary. Suppose we are dealing with the first 14 letters of the alphabet. If we use a three-way test (less than, equal to, greater than) , then a search tree might look like

To minimize the number of tests to recognize any X, that is, to minimize the height of the search tree, we should make the tree balanced.

Page 7: tucker/AMS301-III.docx  · Web viewA tree is called m-ary if every parent has exactly m children, e.g., binary tree. We also call parent vertices . internal vertices. Non-parents

Example (from section III.4): What is the fewest number of comparisons needed to arrange n different numbers a1, a2, a3, a4, . . . an into ascending order? The sequences of possible comparisons in such a sorting algorithm produce a binary tree, where the internal vertices are the binary comparisons; for example, the algorithm might start with a root (starting) comparison a1< a2. The leaves are the n! different arrangements of the n numbers. The maximum length of a sequence of comparisons to find a correct arrangement will be the height h of this sorting tree From Theorem 4, we have h ≥ log2(n!). Mathematical analysis shows that n! ≈ (n/e)n where e is Euler’s constant. So h ≤ log2[(n/e)n] ≈ n log2(n).

Page 8: tucker/AMS301-III.docx  · Web viewA tree is called m-ary if every parent has exactly m children, e.g., binary tree. We also call parent vertices . internal vertices. Non-parents

Example 4: A logical puzzle involves 8 coins with exactly one coin counterfeit—too light. Given a balance with two pans, what is the minimum number of weighings that are needed to find the counterfeit coin. The balance has three states: the left pan raised, the right pan raised or balanced. So a sequence of weighings yields a ternary tree, where the internal vertices are weighings and the leaves are which coin is counterfeit:

Since there are 8 coins, that is, 8 leaves, then two weighings should be enough: 8 ≤ 32.

Page 9: tucker/AMS301-III.docx  · Web viewA tree is called m-ary if every parent has exactly m children, e.g., binary tree. We also call parent vertices . internal vertices. Non-parents

III.2 Search Trees

Trees provide a natural framework for finding solutions to problems that involve a sequence of choices, such as chess programs or getting out of a maze.

In graph searching, it is natural to use a spanning tree of a graph, a tree that includes all the graph’s vertices.

Two standard ways to build a spanning tree:a) depth-first search: initially building a path going down

as many levels until forced to backup and down starting another path.

b) breadth-first search: at level 1, add all vertices adjacent to the root; at level 2, add all vertices adjacent to a vertex at level 1, etc.

Page 10: tucker/AMS301-III.docx  · Web viewA tree is called m-ary if every parent has exactly m children, e.g., binary tree. We also call parent vertices . internal vertices. Non-parents

Example 1: Testing for Connectedness Is the graph with this adjacency matrix connected?

Test with a depth-first and a breadth-first search to try to find paths from x1 to all other vertices.

Page 11: tucker/AMS301-III.docx  · Web viewA tree is called m-ary if every parent has exactly m children, e.g., binary tree. We also call parent vertices . internal vertices. Non-parents

Example 2: Traversing a MazeUse a depth-first search to get from S (start) to exit(E). A breadth-first search would be tedious. A useful heuristic is to keep one hand on the right wall.

Page 12: tucker/AMS301-III.docx  · Web viewA tree is called m-ary if every parent has exactly m children, e.g., binary tree. We also call parent vertices . internal vertices. Non-parents

Example 3: Pitcher-Pouring PuzzleGiven pitchers of sizes 10 quarts, 7 quarts, 4 quarts. Initially 10-quart pitcher is full. By pouring among pitchers, is there a way to get exactly 2 quarts in the 7-quart or 4-quart pitcher?Three options: pour between 10-qt and 7 qt pour between 10-qt and 4 qt pour between 7-qt and 4 qtDisplay on coordinates of 7-qt and 4-qt amounts

Page 13: tucker/AMS301-III.docx  · Web viewA tree is called m-ary if every parent has exactly m children, e.g., binary tree. We also call parent vertices . internal vertices. Non-parents

Example 4: Missionaries and Cannibals PuzzleThree missionaries and 3 cannibals must cross a river in a 2-person boat. Show how to do this so that at no time do the cannibals outnumber the missionaries on either shore (unless there are no missionaries on a shore).

Page 14: tucker/AMS301-III.docx  · Web viewA tree is called m-ary if every parent has exactly m children, e.g., binary tree. We also call parent vertices . internal vertices. Non-parents

III.3 Traveling Salesperson Problem. Find the shortest tour (Hamilton circuit) of a set of cities. Will use branch-and-bound strategy. Infinities block forbidden subcircuits.

1.) Subtract smallest (non-zero) entry from each row. Then smallest entry from each column to get an initial lower bound. 0 is a lower bound in the reduced matrix. So amount subtracted is lower bound on original matrix.

2.) Branch on 0-entry which if NOT used, raises the lower bound substantially. For example (1,2)

Page 15: tucker/AMS301-III.docx  · Web viewA tree is called m-ary if every parent has exactly m children, e.g., binary tree. We also call parent vertices . internal vertices. Non-parents

If we do use (1,2), then we cannot use another entry in row 1 or column 2- deleted them

In resulting array, we must place ∞ in (2,1) to block subcircuit. Now row 2 has min entry of 1, subtract 1

Now pick entry (2,4). Not using increases cost by 2. Delete row 2 and column 4. We get

Page 16: tucker/AMS301-III.docx  · Web viewA tree is called m-ary if every parent has exactly m children, e.g., binary tree. We also call parent vertices . internal vertices. Non-parents

Now use (4,3) and (3,1) to complete the tour. Because the cost of this tour 1-2-4-3-1 is 17, we do not need to consider any of

the branches that we did not use in our decision tree for finding the minimum solution.

Page 17: tucker/AMS301-III.docx  · Web viewA tree is called m-ary if every parent has exactly m children, e.g., binary tree. We also call parent vertices . internal vertices. Non-parents