Presentation is loading. Please wait.

Presentation is loading. Please wait.

Applied Combinatorics, 4th Ed. Alan Tucker

Similar presentations


Presentation on theme: "Applied Combinatorics, 4th Ed. Alan Tucker"— Presentation transcript:

1 Applied Combinatorics, 4th Ed. Alan Tucker
Section 3.2 Search Trees and Spanning Trees Prepared by Jessica Scheld 9/22/2018 Tucker, Sec. 3.2

2 Trees Enumeration: Finding all of the possible paths in a rooted tree that begin at the root, or the solutions that path represents; finding the unique path from the root to each internal vertex and leaf Example: A The paths are: (A-B) (A-B-D) B C (A-B-E) (A-C) (A-C-F) D E F 9/22/2018 Tucker, Sec. 3.2

3 Depth-First Search Tree
(A-B-D-E-C-F) B C D E F 9/22/2018 Tucker, Sec. 3.2

4 Breadth-First Search Tree
A-B-C-D-E-F B C D E F 9/22/2018 Tucker, Sec. 3.2

5 Spanning Tree Spanning tree: a subgraph of a graph G that is a tree containing all vertices of G can be constructed in 2 ways Depth-first (backtrack) search Breadth-first search If a graph is not connected, no spanning tree exists. 9/22/2018 Tucker, Sec. 3.2

6 Depth-First Search for a Spanning Tree
Pick a vertex as the root and begin building a path from the root composed of edges of the graph. Continue this path until it cannot go any further without repeating a vertex already in the tree. Vertex where path must stop is a leaf of the spanning tree. Now, backtrack to the parent of the leaf and try to build a path from the parent in another direction. Exhaust all paths from parent y, then backtrack to parent of y and repeat the process until forced to return to the root. 9/22/2018 Tucker, Sec. 3.2

7 Finding a Spanning Tree
For a graph G: Search Tree - Depth-First Search A B A F C B C E D D E Note: There are other ways to find a spanning tree by doing a depth-first search. F 9/22/2018 Tucker, Sec. 3.2

8 Finding a Spanning Tree
For a graph G: Search Tree – Depth-First Search A B A F C D E E D B C F 9/22/2018 Tucker, Sec. 3.2

9 Breadth-First Search for a Spanning Tree
Pick some vertex x as the root and put all edges leaving x (along with the vertices at the ends of these edges) in the tree. Successively add to the tree the edges leaving the vertices adjacent from x, unless such an edge goes to a vertex already in the tree. Continue in a level-by-level fashion. 9/22/2018 Tucker, Sec. 3.2

10 Finding a Spanning Tree
Given a graph G: Find spanning tree using the breadth-first method: A B A F C B F D E D C E 9/22/2018 Tucker, Sec. 3.2

11 Spanning Trees from Adjacency Matrices
This adjacency matrix shows that A is connected to B and C, B is connected to A, C and D, C is connected to A and B, and D is connected to B. The corresponding graph would look like this: a b c d 1 A B C D 9/22/2018 Tucker, Sec. 3.2

12 Adjacency Matrices A B C D E F G H 1
1 This graph G is represented in the adjacency matrix to the left. We want to determine a spanning tree given this information. A B C D F E G H 9/22/2018 Tucker, Sec. 3.2

13 Finding Spanning Trees
Depth-first search spanning tree A B C D E F G H 1 f b 1 1 e 1 1 c g h d 1 A F B 1 1 E D C G H 9/22/2018 Tucker, Sec. 3.2

14 Finding Spanning Trees
B C D E F G H 1 Breadth-first search spanning tree 1 1 1 1 a 1 1 f b d c g e h A F B 1 E D C G H 9/22/2018 Tucker, Sec. 3.2

15 The problem: You have 3 pitchers: sizes 10 quarts, 7 quarts, and 4 quarts. Begin with 10 quarts of water in the largest pitcher, other two pitchers are empty. You can pour until the receiving pitcher is full or the pouring pitcher is empty. Problem: Is there a way to have exactly 2 quarts in the 7- or 4-quart pitcher? If so, what is the minimum sequence of pourings to get 2 quarts? 9/22/2018 Tucker, Sec. 3.2

16 Example: Pitcher-Pouring Puzzle
4 2 1 (0,4) (10,0,0) 3 (6,0,4) (3,7,0) (7,0) Draw a grid. The boundaries are that b=7, c=4, and b+c=10. Pouring between 10 and 7 quart pitchers will be represented by horizontal lines, between 10 and 4 quart pitchers will be by vertical lines, and between 4 and 7 quart pitchers is a diagonal line. Start at (0,0) – the root of the tree You have two choices – pour all the water into the 7 quart pitcher or the 4 quart pitcher. 9/22/2018 Tucker, Sec. 3.2

17 Pitcher Problem Continued
4 3 2 1 (4,0) (3,4) (6,4) (7,3) (10,0,0) (6,0,4) (3,7,0) (6,4,0) (0,6,4) (0,7,3) (3,3,4) Note: We are using the breadth-first search method since it produces a spanning tree with the shortest paths from the root to every other vertex in the graph. 9/22/2018 Tucker, Sec. 3.2

18 Pitcher Problem Continued
(2,4) (4,4) 4 (10,0,0) (0,3) 3 2 (6,0,4) (3,7,0) 1 (7,1) (6,4,0) (0,6,4) (0,7,3) (3,3,4) (3,0) (6,0) (2,4,4) (4,6,0) (7,0,3) (7,3,0) The shortest path is from (0,0)-(0,4)-(6,4)-(6,0)-(2,4) (2,7,1) (4,2,4) 9/22/2018 Tucker, Sec. 3.2

19 Problem: Jealous Wives
We have 3 wives and their respective husbands who are on the left side of a river. They must all make it to the right side of the river via a little boat which seats at most 2 people. Problem: Find a sequence of boat trips which allows all 6 to get across without any husband being alone (without his wife) and with another husband’s wife. Let the wives be denoted: A,B,C. Let the husbands be denoted: a,b,c. 9/22/2018 Tucker, Sec. 3.2

20 Example 4: Jealous Wives Puzzle
This is one example of the path taken to get the 6 people across. This graph represents the process from one side to the other. ABCabc BCbc ABCbc ABC ABCa Aa ABab ab abc a ab END Aa A bc a BC Bb AB c bc b ab Vertices represent who is on the left side of the river and still needs to cross. Edges represent who is crossing. The arrows dictate the direction of movement (right arrow represents crossing the river, left arrow represents returning to the original side). 9/22/2018 Tucker, Sec. 3.2

21 Definitions Preorder traversal: depth-first search that examines an internal vertex when the vertex is first encountered in the search; when vertex precedes its children and all its other descendants. Postorder traversal: examines an internal vertex when last encountered (before the search backtracks away from the vertex and its subtree); when a vertex follows its children and all its other descendants. If a tree is binary, an inorder traversal checks an internal vertex in between the traversal of its left and right subtrees. 9/22/2018 Tucker, Sec. 3.2

22 Examples A D E F C B A D E F C B A Pre: A,B,D,E,C,F Post: D,E,B,F,C,A
Identify vertex last time you see it. Identify vertex first time you see it. B C In order: D,B,E,A,G,C,F Must be binary – identify vertex in between children. D E G F 9/22/2018 Tucker, Sec. 3.2

23 Arithmetic Tree × ÷ ÷ - + c g ÷ a f b q + e d 9/22/2018
Tucker, Sec. 3.2

24 Class Exercise: Pg. 113 #23 4 people have to cross a bridge that only holds 2 people at a time It is night, there is one flashlight. Any party to cross must have the flashlight. Person A takes 1 minute to cross Person B takes 2 minutes Person C takes 5 minutes Person D takes 10 minutes. You must go at the pace of the slowest walker. Find a way to get all 4 to the other side in 17 minutes. 9/22/2018 Tucker, Sec. 3.2

25 Bridge Crossing: Solution
First, let A and B cross the bridge 2 minutes A returns with the flashlight 1 minute C and D cross the bridge 10 minutes B returns with the flashlight A and B cross the bridge TOTAL TIME: 17 minutes! ABCD CD ACD AB A CD A AB Complete AB B 9/22/2018 Tucker, Sec. 3.2


Download ppt "Applied Combinatorics, 4th Ed. Alan Tucker"

Similar presentations


Ads by Google