11
2/10/03 Tucker, Sec. 3.2 1 Tucker, Applied Combinatorics, Sec. 3.2, Important Definitions Enumeration: Finding all of the possible paths in a rooted tree that begin at the root, or the solutions that that path represents. This means finding the unique path from the root to each internal vertex and leaf. Here the paths would be: (A-B), (A-B-D), (A-B-E), (A-C) and (A-C-F) A B C D E F

2/10/03Tucker, Sec. 3.21 Tucker, Applied Combinatorics, Sec. 3.2, Important Definitions Enumeration: Finding all of the possible paths in a rooted tree

  • View
    219

  • Download
    0

Embed Size (px)

Citation preview

2/10/03 Tucker, Sec. 3.2 1

Tucker, Applied Combinatorics, Sec. 3.2,

Important Definitions

Enumeration: Finding all of the possible paths in a rooted tree that begin at the root, or the solutions that that path represents. This means finding the unique path from the root to each internal vertex and leaf.

Here the paths would be:

(A-B), (A-B-D), (A-B-E), (A-C)

and (A-C-F)

A

B C

D E F

2/10/03 Tucker, Sec. 3.2 2

Depth-First Search, or Backtracking: This method of enumerating a tree builds a path from the root as far down as possible in the tree.

•Each new edge must lead to a new vertex that has not already been visited.

•The path building stops when you reach a vertex that has no edge connecting it to another new vertex or, in other words, is a leaf.

•You then backtrack up to this leaf’s parent and try to go down a sibling edge and follow it down as far as possible.

•If all of the sibling have already been tried, backtrack up another level and continue with the process. You will eventually hit all of the vertices in the tree.

2/10/03 Tucker, Sec. 3.2 3

A

B C

D EF

2/10/03 Tucker, Sec. 3.2 4

Preorder Traversal: A depth-first search of a tree in which a vertex is examined when it is first encountered. In this search the root is examined first

Postorder Traversal: A depth-first search of a tree in which a vertex is examined when it is last encountered, that is, when you finally move up a level from that vertex. In this search the root is examined last.

Inorder Traversal: A depth-first search of a binary tree in which a vertex is examined before its right subtree is traversed, but after its left subtree is traversed. This assumes that in the depth-first search, the left child of a vertex is traversed, then its right child

A

B C

D E F

A

B C

D E F

Pre: A, B, D, E, C, F Post: D, E, B, F, C, A

A

B C

D E F

Inorder: D, B, E, A, C, F

2/10/03 Tucker, Sec. 3.2 5

Breadth-First Searching: This method of enumerating a tree consists of examining all the vertices adjacent to the root, then all the children of those vertices, and so on.

A

B C

D E F

Breadth-First: A, B, C, D, E, F

2/10/03 Tucker, Sec. 3.2 6

Example #1

Suppose we are given three pitchers of water, of sizes 10 quarts, 7 quarts, and 4 quarts.

Initially the 10 quart pitcher is full and the other two empty.

We can pour water from one pitcher to another, pouring until the receiving pitcher is full or the pouring pitcher is empty.

Is there a way to pour among pitchers to obtain exactly 2 quarts in the 7- or 4-quart pitcher?

If so, what is the most efficient way?

2/10/03 Tucker, Sec. 3.2 7

(6,0,4) (3,7,0)  

(6,4,0) (0,6,4) (0,7,3) (3,3,4)  

(2,4,4) (4,6,0) (7,0,3) (7,3,0)  

(2,7,1) (4,2,4)

(10,0,0)10 7 4

2/10/03 Tucker, Sec. 3.2 8

Example #2

Put the following arithmetic expression into a form easily programmed into and executed by a computer.

a b c q d e f g

2/10/03 Tucker, Sec. 3.2 9

a b

c

d e

q

f

g

a b c q d e f g This tree allow s a computer to see and compute the entire expression without being able to parse the whole thing, like a human can do. In a postorder search of this tree, a computer would register the values of a and b, then the plus sign above them, and then perform the operation. In this way it would execute all the operations in the expression in the correct order, and without running into an operator when it did not know which two values were being operated on.

2/10/03 Tucker, Sec. 3.2 10

Class Exercise

Construct a rooted tree, as in the previous example, for the following expression:

kjlihabfedcg 1

2/10/03 Tucker, Sec. 3.2 11

a

b

c d e f

g

kjlihabfedcg 1

Solution:

kjli

h

1