Chapter 7 - Trees

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

Just before vertex b is removed from the list, the contents of the list are: b, e, f. The tree consists of edges {a, b}, {a, e}, and {a, f} and vertices a, b, e, and f. When b is removed from the list, there are no neighbors of b that are not already in the tree. Therefore, no new edges incident to b are added and vertex b has no children.

Consider breadth-first search run on the graph below starting from vertex a. In the resulting BFS tree rooted at vertex a, vertex v is a child of vertex u if edge {u, v} and vertex v are added when vertex u is removed from the list. 2) How many children does vertex b have in the resulting BFS tree?

k+1 The set of vertices are the k leaves plus the vertex in the center. Thus, the total number of vertices is k + 1.

How many vertices are there in Sk? Express your answer as a function of k.

Sk is a connected graph with no cycles. Therefore, Sk is a tree.

Is Sk a tree?

11110101 b = 1111. a = 0. t = 101. Therefore, bat = 11110101.

Use the prefix tree above to encode the word "bat".

- m - 4 - e or d

Which vertex is the root? What is the height of the tree? Name a level 3 vertex

graph traversal

starts at some vertex v and visits all vertices on the paths that start at v the process of visiting each vertex in a graph

Any free tree with at least two vertices has at least how many leaves.

two

siblings

two vertices that have the same parent

Find a graph with the given set of properties or explain why no such graph can exist. (e) Tree, all vertices have degree 2.

If a vertex has degree two, then the vertex has two neighbors. If there is a vertex in a graph of degree two, then the graph must have at least three vertices (the vertex and its two neighbors). Any tree with at least two vertices has at least two leaves. A leaf is a vertex of degree one. Therefore, it is impossible to have a tree in which every vertex has degree two.

A configuration corresponding to a tie game is a leaf in the game tree. What is the level of a tie configuration?

9 - If the game is a tie, the grid is all filled up. It takes 9 moves to fill up the grid.

traversal

A common procedure performed on a tree to process the information stored in the vertices by systematically visiting each vertex. As each vertex is visited, a processing step is performed which might entail a computation or outputting some data. For example, a tree could be used to hold the hierarchical structure of the sections of a book. In order to print out the table of contents, as each vertex is visited, the title of the corresponding portion of the book is printed out.

free tree

A connected undirected graph with no cycles that has no particular organization of the vertices and edges

We know that if a graph is a tree with n vertices and m edges, then m = n - 1. Is the converse true? That is if a graph has n vertices and n - 1 edges can you conclude that it is a tree? Justify your answer.

A cycle with n - 1 vertices plus a single isolated vertex together form a graph with n vertices and n - 1 edges that is not a tree. Note that the question does not specify that the graph must be connected. The diagram below shows an example with n = 4.

Find a graph with the given set of properties or explain why no such graph can exist. (b) Connected, six vertices, six edges.

A cycle with six vertices (C₆) is a connected graph with six edges and six vertices.

prefix

A string s is a prefix of another string t if all the characters in string s appear at the beginning of string t.

spanning tree

A subgraph of a connected graph which includes all the vertices of the graph and is also a tree.

The number of bits per character for the prefix code given in the tree is: a: 2 bits b: 2 bits c: 3 bits d: 3 bits e: 3 bits f: 3 bits The average number of bits per character is: (.05)·2 + (.05)·2 + (.1)·3 + (.15)·3 + (.25)·3 + (.4)·3 = 2.9 bits per character

A text file contains only characters from the set {a, b, c, d, e, f}. The frequency of each letter in the file is: a: 5% b: 5% c: 10% d: 15% e: 25% f: 40% What is the average number of bits per character used in encoding the file?

rooted tree

A tree in which one vertex has been designated as the root and every edge is directed away from the root and the vertices are organized according to their distance from the root

Breadth-first search (BFS)

An algorithm for searching a tree data structure for a node that satisfies a given property. It starts at the tree root and explores all nodes at the present depth prior to moving on to the nodes at the next depth level. Extra memory, usually a queue, is needed to keep track of the child nodes that were encountered but not yet explored.

Under what circumstances does an edge in a connected graph belong to every spanning tree?

An edge e belongs to every spanning tree if and only if removing e from the graph disconnects the graph into one or more connected components. Suppose that removing an edge e disconnects the graph into more than one connected component. Let v be a vertex in one component and w be a vertex in another component after e is removed. Every path in the original graph between vertex v and vertex w uses edge e. Otherwise, there would still be a path between w and v after e is removed from the graph. Every spanning tree T of the original graph contains a path between v and w. Therefore T must contain the edge e. Suppose that removing edge e from the graph does not disconnect the graph. Let G be the graph before edge e is removed and G' be the graph after edge e is removed. Since G' is connected, G' has a spanning tree. The spanning tree of G' is also a spanning tree of G and does not contain the edge e.

Yes - If the X player places an X in the upper left corner or the middle of the third row, he wins the game. The corresponding configuration is a leaf in the game tree.

Does the shown configuration have a child that is a leaf in the game tree? (X plays next).

(a) f, i, h, e, b, g, c, a, d (b) d, f, b, i, h, e, a, c, g

(a) Give the order in which the vertices of the tree are visited in a post-order traversal. (b) Give the order in which the vertices of the tree are visited in a pre-order traversal.

1. The current vertex is a. None of a's neighbors have been visited. The neighbors of a are considered in alphabetical order, so vertex b is next. {a, b} 2. The current vertex is b. Vertex a is the only neighbor of b that has been visited. The neighbors of b that have not been visited are e and f, so vertex e is next. {b, e} 3. The current vertex is e. Both of e's neighbors have been visited, so the current vertex backtracks to b. The only neighbor of b that has not been visited is vertex f. {b, f} 4. The current vertex is f. Vertices a and b have been visited, so the next neighbor of f that has not been visited is vertex c. {f, c} 5. The current vertex is c. Vertex d is the only neighbor of c that has not been visited, so vertex d is next. {c, d}

Consider an execution of depth-first search that starts at vertex a. Select the edges that are included in the depth-first search tree in the order that they are added to the tree.

When vertex a is removed from the list, none of its neighbors are in the tree. Vertices b, e, and f and edges {a, b}, {a, e}, and {a, f} are added to the tree. Therefore vertex a has three children in the BFS tree: b, e, and f.

Consider breadth-first search run on the graph below starting from vertex a. In the resulting BFS tree rooted at vertex a, vertex v is a child of vertex u if edge {u, v} and vertex v are added when vertex u is removed from the list. 1) How many children does vertex a have in the resulting BFS tree?

Just before vertex e is removed from the list, the contents of the list are: e, f. The tree consists of edges {a, b}, {a, e}, and {a, f} and vertices a, b, e, and f. When e is removed from the list, there are no neighbors of e that are not already in the tree. Therefore, no new edges incident to e are added and vertex e has no children.

Consider breadth-first search run on the graph below starting from vertex a. In the resulting BFS tree rooted at vertex a, vertex v is a child of vertex u if edge {u, v} and vertex v are added when vertex u is removed from the list. 3) How many children does vertex e have in the resulting BFS tree?

When vertex f is removed from the list, the tree consists of edges {a, b}, {a, e}, and {a, f} and vertices a, b, e, and f. Neither c nor d have been added to the tree, but vertices a and b have already been added. Thus, edges {f,c} and {f,d} are added to T when f is removed from the list. The children of vertex f are vertices c and d.

Consider breadth-first search run on the graph below starting from vertex a. In the resulting BFS tree rooted at vertex a, vertex v is a child of vertex u if edge {u, v} and vertex v are added when vertex u is removed from the list. 4) How many children does vertex f have in the resulting BFS tree?

What is the level of a vertex?

It's distance from the root

Show that if two distinct vertices in a rooted tree, u and v, have a common descendant, then vertex u is a descendant of v or vertex v is a descendant of u. You can assume that every vertex in a rooted tree has a unique path to the root.

Let a and b be two distinct vertices in a rooted tree. According to the definitions of descendants and ancestors: vertex a is a descendant of vertex b if and only if b is an ancestor of a. Vertex b is an ancestor of a if and only if the path from vertex a to the root contains b. Therefore vertex a is a descendant of vertex b if and only if the path from vertex a to the root contains vertex b. Let x be the common descendant of vertices u and v. By definition of a descendant, vertex u is on the path from x to the root and vertex v is on the path from x to the root. Since vertex x has a unique path to the root then vertices u and v are on the same path from x to the root. Suppose vertex u is encountered before v in traversing the path from x to the root. The portion of the path that starts at u and goes to the root contains vertex v. Since v is on a path from u to the root, vertex u is a descendant of vertex v. On the other hand, if vertex v is encountered before u in traversing the path from x to the root, then the portion of the path that starts at v and goes to the root contains vertex u. Since u is on a path from v to the root, vertex v is a descendant of vertex u. In either case, one of u or v is a descendant of the other.

Find a graph with the given set of properties or explain why no such graph can exist. (a) Tree, seven vertices, total degree = 14.

No such graph exists. The number of edges is equal to the total degree divided by two, so the number of edges in the graph is seven. However, a tree with seven vertices must have exactly six edges.

Is Pn a tree?

Pn is a connected graph with no cycles. Therefore, Pn is a tree.

Use the fact proven in the previous problem to show that if a graph has no cycles and has at least n - 1 edges then the graph must be connected.

Proof by contradiction. Suppose that there is a graph with no cycles and at least n - 1 edges but is not connected. A graph with no cycles is a forest and the number of edges in a forest is n - c, where c is the number of components. If a graph is not connected, then the number of connected components c is at least 2. Since c ≥ 2, the number of edges n - c ≤ n - 2. Therefore the graph can not have at least n - 1 edges.

(a) Give an example of a graph with five vertices along with a labeling of the vertices (a, b, c, d, e) so that the resulting BFS tree and DFS tree are the same and the edges are added to the tree in the same order. Assume that for both BFS and DFS, the neighbors of a vertex are considered in alphabetical order and both start at vertex a.

The edges of a BFS or DFS tree are just all the edges in the graph. The order in which the edges are added is: {a, b}, {b, c}, {c, d}, {d, e}.

All of them are correct Any of the four edges not included will create a spanning tree when included.

The graph shows a partially constructed spanning tree. The edges in red are included in the partial tree. Which edge, when added, will create a spanning tree? - {a,e} only - {a,f} only - {c,d} only

What is the height of a tree?

The highest level of an vertex

post-order traversal

The process of systematically visiting every node in a tree once, starting at the root and proceeding left down the tree, accessing the first node encountered at its "right" side, proceeding likewise along the tree, accessing each node as encountered at its "right" side. when a vertex is visited after its descendants

Pre-Order Traversal

The process of systematically visiting every node in a tree once, starting with the root node, proceeding to the left along the tree and accessing the node when the "left" side of the node is encountered.

Each sequence represents a pre-order traversal of a tree. Each letter is the label of a vertex. Internal vertices are labeled with lower-case letters and leaves are labeled with capital letters. Furthermore, the tree has the property that every internal vertex has exactly two children. Give a tree T such that a pre-order traversal of T visits the vertices in the order given. (c) If the number of children of an internal vertex can be one or two, does each sequence correspond to a unique tree? That is, is it possible to have two different trees whose pre-order traversals result in the same sequence? (Capital letters must be leaves and lower-case letters must be internal vertices.)

The tree is not necessarily unique. Both trees below result in the sequence a, b, A, B.

How many edges are there in a star graph with n vertices? Express your answer as a function of n.

There are n - 1 edges connecting the leaves to the center vertex.

No - The configurations differ in two places: the O in the middle row has moved from the right to the left. There is an additional X in the middle row. Therefore, one configuration can not be a child of the other.

Two configurations are shown. Is one a child of the other in the game tree?

k - The path from the root specified by the string 1110 leads to the letter k.

Use the prefix tree above to decode the string: 1110

kn - The path from the root specified by the string 1110 leads to the letter k. Then the path from the root specified by the string 100 leads to the letter n.

Use the prefix tree above to decode the string: 1110100

k = 1110. n = 100. o = 110. t = 101 "knot"

Use the prefix tree above to decode the string: 1110100110101

1111 - The path from the root to the letter b encounters four consecutive 1's.

Use the prefix tree above to encode the letter "b".

11110 - The path to the letter b encounters four consecutive 1's. Then the path from the root to the letter a encounters one 0.

Use the prefix tree above to encode the string "ba".

-Vertices m, h, l, p -Vertices f, g, e, d, a, b, c. -Vertices j, q, n, k, e, a, b, c, g. - level 3 - 5 - Vertices o, n, a, b, c - Vertices l and k

Which vertices are ancestors of vertex n? Which vertices are the descendants of vertex i? List the leaves in the tree. What is the level of vertex d? What is the height of the tree? List the level 4 vertices. Draw the subtree rooted at vertex h. What are the siblings of vertex i?

What is a game tree?

a graphical representation of the consequences of different actions in a strategic setting shows all possible playing strategies of both players in a game each vertex in the tree represents a configuration of the game as well as an indication of whose turn it is to play next

Depth-First Search

a search in which children of a node are considered (recursively) before siblings are considered. Visits the child vertices before visiting the sibling vertices A stack is usually implemented

internal vertex

a vertex that has degree of at least two

leaf

a vertex with no children

descendants

all the vertices along a path away from the root

ancestors

all the vertices between the vertex and the root

tree

an undirected graph that is connected and has no cycles

subtree

at a vertex consists of all of that vertex's descendants

Suppose that there is a forest with n vertices and c connected components. Give a conjecture for the number of edges in the forest as a function of n and c.

n - c

How many edges are there in Pn? Express your answer as a function of n.

n-1 There is an edge between every pair of consecutive vertices. Therefore, the number of edges is n - 1.

What is the vertex degree of a leaf?

one - an exception occurs when the free tree has only one vertex, then that single vertex is a leaf

child

the next vertex encountered travelling away from the root

variable length codes

the number of bits for each character can vary

parent

the parent of a vertex is the first vertex encountered while traveling towards the root

The two sequences of nodes given below denote two distinct paths in a graph between vertices j and d. ⟨j,h,b,i,g,f,c,d⟩ ⟨j,h,b,a,f,e,c,d⟩ Which of the following answers describes a cycle that you can deduce is in G? ⟨b,a,g,i,b⟩ ⟨b,a,f,g,i,b⟩ ⟨b,a,f,e,c,f,g,i,b⟩

⟨b,a,f,g,i,b⟩ The paths diverge at b, then converge at f. Follow b, a, f along the bottom path, then f, g, i, b along the top path in reverse, thus leading from b back to b— a cycle.

How is distance determined between two vertices in an undirected graph?

determining the number of edges in the shortest path between the two vertices

Prove that a forest with c components and n vertices has n - c edges. Assume n ≥ c. (Hint: the base case will have n = c.)

Base case: n = c. Each component must have at least one vertex. Since there are n vertices and n = c components, each component consists of a single vertex. A component with a single vertex does not have any edges, so m = 0 = n - c. Inductive step: Assume for n > c, that a forest with n - 1 vertices and c components has n - 1 - c edges. We will show that a forest with n vertices and c components has n - c edges. If the number of vertices is at least c + 1, then at least one component must have at least two vertices. Select a component with at least two vertices. That component is a tree with at least 2 vertices. Therefore the component has at least two leaves. Select a leaf v and let e be the edge incident to v. Remove v and e from the graph. The result is a forest with n - 1 vertices and c components. By induction, the number of edges in the forest with v and e removed is n - 1 - c. Since one edge was removed to get the forest with n - 1 vertices, the original forest has (n - 1 - c) + 1 = n - c edges.

In a post-order traversal, each vertex is visited after its descendants are visited. The leftmost leaf is visited first. So vertex c is visited first. The subtree rooted at c has been visited, so the subtree rooted at d is next. Since e is a child of d, and a is the left child of e, a is visited next. Once a descendant of vertex e is visited, all the vertices of the subtree rooted at e must be visited before any other vertex. Since b is a child of g, b is visited next. All children of g have now been visited, so vertex g is visited next. All children of e have now been visited, so vertex e is visited next. All children of d have now been visited, so vertex d is visited next. The root, vertex f, is the last vertex to be visited. Therefore, the vertices are visited in the following order: c, a, b, g, e, d, f

Enter the labels of each vertex in the order visited in a post-order traversal.

1 The leftmost leaf is visited first in a post-order traversal. 2 Once a descendant of b is visited, all the vertices of the subtree rooted at b must be visited before any vertex outside the subtree rooted at b. The children of b are visited before b. 3 Once a descendant of b is visited, all the vertices of the subtree rooted at b must be visited before any vertex outside the subtree rooted at b. The children of b have already been visited, so vertex b is next. 4 After the entire subtree rooted at b has been visited, the next child of vertex a (vertex c) is visited next 5 The subtrees rooted at b and c have been visited, so the subtree rooted at d is next. Since g is a child of d, g is visited before d. 6 All the descendants of the root have been visited except d, so vertex d is visited next. 7 The root is the last vertex to be visited in a post-order traversal. e, f, b, c, g, d, a

Give the ordering that the vertices are visited according to a post-order traversal of the tree below.

1. The root is the first vertex to be visited in a pre-order traversal. 2. After vertex a is visited, the subtree at the leftmost child are visited next. The root of a subtree (in this case b) is visited before the descendants of b. 3. After b is visited, the descendants of b are visited before any vertex outside the subtree rooted at b. Vertex e is the leftmost child of b. 4. Since vertex e has no children, the next child of b (vertex f) is visited next. 5. After the entire subtree rooted at b has been visited, the next child of vertex a (vertex c) is visited next. 6. Since vertex c has no children, the next child of vertex a (vertex d) is visited next. 7. Vertex g is the only vertex left that has not yet been visited. a b e f c d g

Give the ordering that the vertices are visited according to a pre-order traversal of the tree below.

prefix code

Has the property that the code for one character can not be a prefix of the code for another character. The fact that the codes are organized as a tree in which characters are only stored at the leaves ensures the prefix property.

Depth-first search can be used to detect whether a graph has any cycles. How would you augment the pseudo-code for depth-first search, given below, to determine if the graph has a cycle? You can assume that the graph is connected. visit(v) For every neighbor w of v: If w is not already in T Add w and {w, v} to T. visit(w); End-if End-for

If an edge {v, w} is discovered that is not in the tree then the graph has a cycle. There is a unique path in T between v and w. Adding the edge {v, w} to the path between v and w makes a cycle. If a graph has a cycle, then it can not be the case that all the edges in the cycle are included in the DFS tree. A tree, by definition, does not have a cycle. Consider an edge {x, y} that is not in the tree. Suppose that among the pair x and y, x is added first to the DFS tree and y is added second. When vertex y is added to the tree, the cycle will be discovered upon the call to visit(y) because there is a neighbor of y that is already in the tree and the edge between them is not in the tree.

If T is a tree with n vertices, what is the most leaves that it can have? Your answer will be an expression involving the variable n. Explain your reasoning. Be sure to address small values for n (e.g., n = 1 or 2).

If n = 1, the single vertex is a leaf. If n = 2, then the tree is a single edge connected by an edge. The tree has 2 leaves. Therefore if n = 1 or 2, then a tree with n vertices has n leaves. For n > 2, the star graph on n vertices has n - 1 leaves, so a tree can have at least n -1 leaves. If n > 2, then a leaf can not be connected to another leaf. Two degree-1 vertices connected to each other can not be connected to any other vertices and would form their own connected component. Since a tree must be connected, a leaf must be connected to a non-leaf. Therefore if n > 2, then there is at least one non-leaf in the tree. The number of leaves can not be more than n - 1. Therefore, n - 1 is the maximum number of leaves a tree can have.

2 - The endpoints of the path are degree 1 vertices. Every other vertex in the path has degree 2.

If n ≥ 2, then how many leaves are there in Pn?

The ordering corresponds exactly to a post-order traversal. Each vertex is visited after all of its descendants. Once all of the descendants of a vertex are visited, that vertex should be the next vertex visited. c, e, g, b, d

In what order are the vertices visited in a post-order traversal of the tree below?

Let G be an undirected graph with n vertices and m edges. Prove that if m < n - 1, then G is not connected.

Proof by contradiction. Suppose there is a graph G that is connected and the number of edges m is strictly less than n - 1. Suppose that G has a cycle. Then an edge e = {v, w} can be removed from G and the graph will remain connected. (If e is part of a cycle, then there is a path between v and w that does not use the edge e. Any path connecting two vertices that used the edge e, can use the alternate path between v and w.) Therefore one can continue the process of removing an edge from a cycle as long as the graph has a cycle. The number of edges in the graph can only decrease and the graph will remain connected. The result is a new graph with m' ≤ m edges that has no cycles and is connected. A connected graph with no cycles is a tree. Since every tree has n - 1 edges, m' = n - 1. However by assumption m' ≤ m < n - 1 and therefore m' < n - 1 which is a contradiction. Therefore it is impossible to have a graph G that is connected and the number of edges m is strictly less than n - 1.

Consider a tree T resulting from running breadth-first search on an undirected graph. Is it possible to have an edge between a vertex v and a descendant of v that is not included in T? Why or why not?

Proof. Proof by contradiction. Suppose that vertex w is a descendant of vertex v in a BFS tree, {v, w} is an edge in the graph but {v, w} is not an edge in the BFS tree. Since {v, w} is not an edge in the tree, then v is not the parent of w. Let x be the parent of w. We have that x is a descendant of v, and w is a descendant of both v and x. Therefore, the order in which the vertices are removed from the list is v, then x, then w (with other vertices possibly coming in between). Since x is the parent of w, vertex w is added to the list at the time that x is removed from the list. However, when v is removed from the list, all of v's neighbors are considered. Since w is a neighbor of v, vertex w would have been added to the list at the time that v is removed which happens before x is removed. This leads to a contradiction. Therefore, if vertex w is a descendant of vertex v, it can not be the case that {v, w} is an edge unless {v, w} is also in the tree. ■

Neither traversal - For both pre-order and post-order traversals, every vertex in the left subtree of a given vertex is visited before any vertex in the right subtree. The children of vertex d are visited in left to right order. The left subtree of vertex d contains f, whereas the right subtree of vertex d contains c. Therefore c is visited after f in both a pre-order and post-order traversal.

Select all traversals in which vertex c comes before vertex f.

Draw a tree with eight vertices that has the most number of leaves possible.

Seven leaves is the most number of leaves a tree with eight vertices can have.


Kaugnay na mga set ng pag-aaral

Chapter 14: Accounts Payable and Other Liabilities

View Set

Module 8: Stereotypic Behavior Interventions

View Set

Prep u - Ch. 6 Values, Ethics, and Advocacy, Prep u - #2 - Chapter 6: Values, Ethics, and Advocacy, prep u - Taylor - Trans to RN Chapter 6

View Set

70-411 lesson 10 configuring VPN and Routing

View Set