CS 280 Problem Set 10 Solutions Due May 3, Part A

Similar documents
March 20/2003 Jayakanth Srinivasan,


7.1 Introduction. A (free) tree T is A simple graph such that for every pair of vertices v and w there is a unique path from v to w

Section Summary. Introduction to Trees Rooted Trees Trees as Models Properties of Trees

Trees : Part 1. Section 4.1. Theory and Terminology. A Tree? A Tree? Theory and Terminology. Theory and Terminology

12/5/17. trees. CS 220: Discrete Structures and their Applications. Trees Chapter 11 in zybooks. rooted trees. rooted trees

3. According to universal addressing, what is the address of vertex d? 4. According to universal addressing, what is the address of vertex f?

Chapter Summary. Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees

Section 5.5. Left subtree The left subtree of a vertex V on a binary tree is the graph formed by the left child L of V, the descendents

CS 125 Section #6 Graph Traversal and Linear Programs 10/13/14

Trees Algorhyme by Radia Perlman

Trees. Truong Tuan Anh CSE-HCMUT

Graphs Introduction and Depth first algorithm

Chapter 4 Trees. Theorem A graph G has a spanning tree if and only if G is connected.

Binary Trees

Computer Science 280 Fall 2002 Homework 10 Solutions

Copyright 2000, Kevin Wayne 1

Prelim 2. CS 2110, November 20, 2014, 7:30 PM Extra Total Question True/False Short Answer

(Refer Slide Time: 05:25)

Binary Trees, Binary Search Trees

γ(ɛ) (a, b) (a, d) (d, a) (a, b) (c, d) (d, d) (e, e) (e, a) (e, e) (a) Draw a picture of G.

Trees. Arash Rafiey. 20 October, 2015

Friday, March 30. Last time we were talking about traversal of a rooted ordered tree, having defined preorder traversal. We will continue from there.

Lecture 3: Graphs and flows

) $ f ( n) " %( g( n)

Depth-First Search Depth-first search (DFS) is another way to traverse the graph.

Searching in Graphs (cut points)

Prelim 2 Solutions. CS 2110, November 20, 2014, 7:30 PM Extra Total Question True/False Short Answer

Trees. (Trees) Data Structures and Programming Spring / 28

UNIT III TREES. A tree is a non-linear data structure that is used to represents hierarchical relationships between individual data items.

Hamilton paths & circuits. Gray codes. Hamilton Circuits. Planar Graphs. Hamilton circuits. 10 Nov 2015

CS 441 Discrete Mathematics for CS Lecture 26. Graphs. CS 441 Discrete mathematics for CS. Final exam

CS 206 Introduction to Computer Science II

Figure 4.1: The evolution of a rooted tree.

CHAPTER 11 Trees. 294 Chapter 11 Trees. f) This is a tree since it is connected and has no simple circuits.

CSC Intro to Intelligent Robotics, Spring Graphs

Trees. 3. (Minimally Connected) G is connected and deleting any of its edges gives rise to a disconnected graph.

Chapter 3 Trees. Theorem A graph T is a tree if, and only if, every two distinct vertices of T are joined by a unique path.

D. Θ nlogn ( ) D. Ο. ). Which of the following is not necessarily true? . Which of the following cannot be shown as an improvement? D.

Graph Algorithms Using Depth First Search

Homework Assignment #3 Graph

Definition Example Solution

n 2 C. Θ n ( ) Ο f ( n) B. n 2 Ω( n logn)

Graph Representation

Multiple Choice. Write your answer to the LEFT of each problem. 3 points each

Foundations of Discrete Mathematics

Data Structure Lecture#10: Binary Trees (Chapter 5) U Kang Seoul National University

Graph Algorithms. Chapter 22. CPTR 430 Algorithms Graph Algorithms 1

Topics. Trees Vojislav Kecman. Which graphs are trees? Terminology. Terminology Trees as Models Some Tree Theorems Applications of Trees CMSC 302

CSE 230 Intermediate Programming in C and C++ Binary Tree

Trees. Tree Structure Binary Tree Tree Traversals

COL351: Analysis and Design of Algorithms (CSE, IITD, Semester-I ) Name: Entry number:

CS 161 Lecture 11 BFS, Dijkstra s algorithm Jessica Su (some parts copied from CLRS) 1 Review

FINAL EXAM SOLUTIONS

CSE 331: Introduction to Algorithm Analysis and Design Graphs

Exercise set 2 Solutions

University of Palestine. Final Exam 2 nd semester 2014/2015 Total Grade: 50

Elements of Graph Theory

Binary Trees. BSTs. For example: Jargon: Data Structures & Algorithms. root node. level: internal node. edge.

( ) D. Θ ( ) ( ) Ο f ( n) ( ) Ω. C. T n C. Θ. B. n logn Ο

Undirected Graphs. V = { 1, 2, 3, 4, 5, 6, 7, 8 } E = { 1-2, 1-3, 2-3, 2-4, 2-5, 3-5, 3-7, 3-8, 4-5, 5-6 } n = 8 m = 11

Data Structures Brett Bernstein

Introduction to Computers and Programming. Concept Question

CS 220: Discrete Structures and their Applications. graphs zybooks chapter 10

Non-context-Free Languages. CS215, Lecture 5 c

Trees Rooted Trees Spanning trees and Shortest Paths. 12. Graphs and Trees 2. Aaron Tan November 2017

CS521 \ Notes for the Final Exam

Math 15 - Spring Homework 5.2 Solutions

n 2 ( ) ( ) Ο f ( n) ( ) Ω B. n logn Ο

CHAPTER 2. Graphs. 1. Introduction to Graphs and Graph Isomorphism

CE 221 Data Structures and Algorithms

Graph Algorithms. Andreas Klappenecker. [based on slides by Prof. Welch]

Data Structure. IBPS SO (IT- Officer) Exam 2017

Data Structures Question Bank Multiple Choice

Topic 18 Binary Trees "A tree may grow a thousand feet tall, but its leaves will return to its roots." -Chinese Proverb

( ) ( ) C. " 1 n. ( ) $ f n. ( ) B. " log( n! ) ( ) and that you already know ( ) ( ) " % g( n) ( ) " #&

Undirected Graphs. Hwansoo Han

The Structure of Bull-Free Perfect Graphs

Discrete Mathematics 2 Exam File Spring 2012

Chapter 20: Binary Trees

NANYANG TECHNOLOGICAL UNIVERSITY SEMESTER II EXAMINATION MH 1301 DISCRETE MATHEMATICS TIME ALLOWED: 2 HOURS

Binary search trees. Binary search trees are data structures based on binary trees that support operations on dynamic sets.

Graph Representations and Traversal

Module 11. Directed Graphs. Contents

Binary search trees 3. Binary search trees. Binary search trees 2. Reading: Cormen et al, Sections 12.1 to 12.3

& ( D. " mnp ' ( ) n 3. n 2. ( ) C. " n

Assignment 4 Trees and Heaps

COSC 2007 Data Structures II Final Exam. Part 1: multiple choice (1 mark each, total 30 marks, circle the correct answer)

Trees. Q: Why study trees? A: Many advance ADTs are implemented using tree-based data structures.

8.2 Paths and Cycles

CSCI2100B Data Structures Trees

1 Connected components in undirected graphs

Lecture 8: PATHS, CYCLES AND CONNECTEDNESS

CS4800: Algorithms & Data Jonathan Ullman

Depth-first search in undirected graphs What parts of the graph are reachable from a given vertex?

Info 2950, Lecture 16

Trees. Introduction & Terminology. February 05, 2018 Cinda Heeren / Geoffrey Tien 1

Algorithms (VII) Yijia Chen Shanghai Jiaotong University

CS204 Discrete Mathematics. 11 Trees. 11 Trees

logn D. Θ C. Θ n 2 ( ) ( ) f n B. nlogn Ο n2 n 2 D. Ο & % ( C. Θ # ( D. Θ n ( ) Ω f ( n)

Transcription:

CS 280 Problem Set 10 Due May 3, 2002 Part A (1) You are given a directed graph G with 1000 vertices. There is one weakly connected component of size 800 and several other weakly connected components of smaller size. There is one strongly connected component of size 500 and several other strongly connected components of smaller size. (a) Suppose a Breadth First Search starting at vertex u visits 600 vertices. Is this vertex in the large weakly connected component? Why? Is this vertex in the large strongly connected component? Why? (b) Suppose a Breadth First Search starting at vertex v visits 600 vertices when the search is run normally and visits 700 vertices when the search is run on the graph in which all directed edges are reversed. Is this vertex in the large weakly connected component? Why? Is this vertex in the large strongly connected component? Why? (a) Yes, the vertex is in the large weakly connected component. We can prove this by contradiction. Suppose that the vertex is not in the large weakly connected component. Then, it must belong to a component that consists of at most 1000 800 = 200 vertices. However, the Breadth First Search visits 600 vertices, and clearly, the search algorithm cannot visit vertices that are not even weakly connected to it (no path). Thus, the vertex must be in the large weakly connected component. The vertex does not necessarily have to be in the large strongly connected component. To elaborate, we shall give an instance of G in which the vertex does not lie in the strongly connected component, and another instance in which the vertex lies in the strongly connected component. Looking at the weakly connected component, suppose that the strongly connected component is a cycle of 500 vertices v 1, v 2, v 500, such that there is a directed edge from vertex v i to vertex v i+1, and from v 500 to v 1. These 500 vertices form a strongly connected component since it is possible to go from any of the 500 vertices to another vertex by going around the cycle. Suppose there is a directed edge from u to one of the 500 vertices in the cycle (say v 1 ). Suppose, also, that the remaining 299 vertices (call them u 1, u 2, u 299 in that order) in the weakly connected component forms a line of vertices, such that there is an edge from u to u 1, and from u i to u i+1 if i < 98, and from u i+1 to u i if i >98. Page 1

Starting from u, the Breadth First Search (BFS) algorithm will visit vertices v 1 to v 500, as well as u 1 to u 99, resulting in a total search tree of 600 vertices. However, u does not belong to the connected component {v 1, v 2,, v 500 }, since there is no way to get from any of the vertices in the component to u. Next, we consider another instance of G in which u is in the connected component. Suppose that we make a slight change in the previous graph discussed above by replacing u with u 0, and defining v 1 = u. We also switch the direction of the directed edge (u, v 1 ) to (v 1, u). The updated graph is shown below: Starting from u, the BFS algorithm will visit vertices v 1 to v 500, as well as u 1 to u 99, resulting in a total search tree of 600 vertices. Furthermore, since u = v 1 belongs to the cycle, it belongs to the connected component {v 1, v 2,, v 500 }. Page 2

(b) Yes, the vertex v is still in the weakly connected component, for the same reason given in (1)(a). Yes, the vertex v is in the large strongly connected component. We can prove this by contradiction. Suppose that the vertex is not in the large strongly connected component. Then, there must be a path leading from it to one of the vertex in the strongly connected component in G, otherwise, the search algorithm will not be able to visit any vertex in the strongly connected component, and thus, it will not visit more than 500 vertices. There must be no path leading from any vertex in the strongly connected component back to v, otherwise, v will belong to the strongly connected component, by definition. Then, if we reverse all the edges in G, the strongly connected component still remains, since there will still be a path from any two nodes v a and v b. (To get from v a to v b, follow the reverse v b to v a path in original G; to get from v b to v a, follow the reverse v a to v b path in original G). However, there will no longer be a path from v to the strongly connected component, since that path is now reversed. Therefore, a BFS starting from v will visit at most 500 vertices, contradicting the given fact that it visits 700 vertices. Therefore, v must be in the large strongly connected component. (2) Your relatives have decided that, as an expert in Computer Science, you should handle the difficult problem of planning seating assignments for the next meeting of your extended family. The difficulty is that several of your relatives are feuding and refuse to eat a meal in the same room with various other relatives. The event is being held in a banquet hall which has available many different dining rooms of many different sizes. The goal is to fit your relatives into as few rooms as possible without placing any feuding pair in the same room. Show how this problem can be solved using a graph coloring algorithm. In other words, assume you have an algorithm that colors a graph with as few colors as possible and show how to build a graph such that the graph-coloring solution or this graph easily leads to a solution for seating relatives. You should assume that the banquet hall has many rooms as you need and that, whatever size room you need, such a room is available. We can define the problem as follows: Let each relative r correspond to a node v V. For any pair of feuding relatives r i and r j, we include the undirected edge (v i, v j ) E. Then, for the graph G = (V, E), we ask: what is the minimum number of colors required to color G so that no two adjacent nodes are assigned the same color. Presumably, the algorithm provided will solve the problem. Page 3

Then we claim: (2.1) The graph can be colored with at most k colors if and only if there is a way of assigning all the relatives to at most k rooms such that no two feuding relative are assigned to the same room. Proof of claim: Suppose that the graph G can be colored with at most k colors. Then, we proceed to assign the relatives to the rooms as follows: if two nodes are assigned the same color, we allocate the corresponding relatives to the same room. Then, since there are at most k colors, the relatives are assigned to at most k rooms. Since any two adjacent nodes in the graph G are assigned different colors, we can also conclude that any two feuding relatives (corresponding to the adjacent nodes) are allocated different rooms as well. Conversely, suppose that the relatives can be satisfactorily allocated to at most k rooms (i.e. no two feuding relatives in same room). Then, we proceed to color the graph G as follows: if two relatives are allocated to the same room, we assign the two corresponding nodes the same color (corresponding to the room). Then, since there are at most k rooms, we color the graph G using at most k colors. Furthermore, since no two feuding relatives are in the same room, and feuding relatives have an edge between their corresponding nodes, we can also conclude that no two adjacent nodes are assigned the same color as well. Having proved (2.1), our construction is complete, and we can use this graph, and the graph-coloring algorithm to determine the minimum number of rooms required to seat all the relatives. Part B (3) The Emperor has gone insane and has declared that from now on, every road will be a one-way road. Further, the Emperor declares that if a traveler leaves a town then it should not be possible to return to it. As the Emperor s Chief Advisor, you have the task of picking a direction for each road in the Empire (you are not allowed to close or destroy roads). If you fail to complete the task the Emperor will be very unhappy and will probably order that something rather unpleasant be done to you. You would prefer to avoid this - the Emperor is insane after all. Is there a way to assign directions so that the Emperor s rules are satisfied? Explain. To simplify the problem, you can assume that each road starts and ends at a town and that roads only cross at towns. Yes, there is a way to assign directions so that the Emperor s rules are satisfied. We shall describe two ways of doing this. Page 4

Method 1: First, we construct a graph G in which the each vertex represents a town, and each road between two towns is represented as an edge between the two corresponding vertices. Then we assign the integers 1 to n to all vertices v 1 to v n so that each vertex gets a unique integer label. Finally, we require that any edge between two vertices v i and v j must go in the direction from the vertex with the smaller label to the vertex with the larger label. In other words, if v i and v j are connected, the edge will go from v i to v j if j > i, and from v j to v i if i < j. Since edges only go from vertices with smaller integer labels to vertices with larger integer labels, once we leave a vertex, we can only visit vertices with larger integer labels, and it is not possible to return. Therefore, the resulting graph satisfies the Emperor s rules. Method 2: Similarly, we construct a graph G in which the each vertex represents a town, and each road between two towns is represented as an edge between the two corresponding vertices. We choose a random vertex v in G, and declare that vertex to be the starting point. Similar to Dijkstra s algorithm, we require that the rest of the vertices be unexplored initially, except for the starting point v. Next, we perform a Breadth-First Search (BFS) with v as the root. Starting from v, we assign directions to edges with one end in v so that all roads point away from v. We then visit the next node specified by the BFS, label it explored, and assign directions so that all unassigned roads point away from this node. We proceed until all roads have been assigned directions. We claim that the resultant directed graph G` will satisfy the Emperor s rules. One important observation here is that at any instance in the execution of the algorithm, we cannot have roads leading from an unexplored node to an explored one, since we only assign road directions starting from explored nodes. Furthermore, we cannot have roads leading from an explored node to another explored one, since the nodes are explored in order, not simultaneously. Therefore, for any two adjacent nodes u, v G, one will be explored before the other; if u was explored in the BFS before v, the edge (u, v) will be directed from u to v, otherwise the edge will be directed from v to u. At any point in the execution of the algorithm, an edge can only be directed from an explored node to an unexplored one. Therefore, if u is visited before v, there will not be any path from v back to u, since u would have been explored, and edges can only lead to unexplored nodes. We have then proven that G` satisfy the rules set by the Emperor; if a traveler leaves a town, it is not possible to return to the town again. (4) A graph is self-complementary if it is isomorphic to its complement. G c, the complement of graph G, has the same vertex set as G, but has an edge between vertices u and v if and only if in G there is no edge between u and v. Give an example of a graph that is selfcomplementary. Page 5

An example of a self-complementary graph is shown below: G consist of the vertices V = {v 1, v 2, v 3, v 4 }, and edges E = {(v 1, v 3 ), (v 1, v 4 ), (v 2, v 4 )}. By definition, G c consists of V c = V, and E c = {(v 1, v 2 ), (v 2, v 3 ), (v 3, v 4 )} There is a one-to-one and onto function f from V to V c as follows: f(v 1 ) = v 2, f(v 2 ) = v 4, f(v 3 ) = v 1, and f(v 4 ) = v 3. Furthermore, the mapped vertices satisfy the property that v a and v b are adjacent in G if and only if f(v a ) and f(v b ) are adjacent in G c. Therefore, the graphs G and G c are isomorphic. Thus, G is a self-complementary graph. Note: This is not the only solution, even though this is a rather simple one. Part C (5) Consider the following tree: (a) In what order are the nodes processed in a preorder traversal? (b) In an inorder traversal? (c) In a postorder traversal? Page 6

(d) Give an example of an ordered binary tree with 4 nodes for which nodes are processed in the same order for both the preorder traversal and the inorder traversal. (a) In a preorder traversal, the root is traversed first, followed by the left subtree, and finally, the right subtree. Therefore, the order of traversal in this instance would be: a b d c e g j h f i (b) In an inorder traversal, the left subtree is traversed first, followed by the root, and finally, the right subtree. Therefore, the order of traversal in this instance would be: d b a g j e h c f i (c) In a postorder traversal, the left subtree is traversed first, followed by the right subtree, and finally, the root. Therefore, the order of traversal in this instance would be: d b j g h e i f c a (d) The following tree would give the same order of traversal for both preorder and inorder traversals. The order is a b c d. (6) (a) Give necessary and sufficient conditions on a graph G so that the depth-first tree and the breadth-first tree are the same. (b) How many nonisomorphic spanning trees does K 4 have? Explain. (a) Suppose a depth-first tree T D is formed from the graph G, starting at a particular node v. In a depth-first tree, there are no edges between nodes in different subtrees of T D in the original graph G; any edge (a, b) must connect an ancestor node (not necessarily parent) to a descendent node in T D. Page 7

Suppose a breadth-first tree T B is formed from the graph G, starting at the same node v. In a breadth-first tree, the distance of the two nodes from v in T B can differ by at most 1. In other words, all edges must connect nodes in either the same layer (or height) in T B, or nodes which reside in neighboring layers. Combining the two requirements, we note that for T D = T B, any edge (a, b) in G must connect an ancestor node in T D to a descendent node in the sane tree. Furthermore, the any edge (a, b) must connect two nodes which reside in neighboring layers of T D. The only possible edges, which could satisfy both requirements, are the original edges in the spanning trees T D (or T B ). Thus, T D = T B = G in order for both the depth-first tree and the breadth-first tree to be the same. (b) K 4 has 2 nonisomorphic spanning trees. They are shown in the diagram below. T 1 and T 2 are the only two nonisomorphic spanning trees of K 4. K 4 consists of 4 nodes. Therefore, a spanning tree of K 4 has three edges, and the sum of degrees of the vertices of the spanning tree must be 3 2 = 6. By definition of a spanning tree, none of the vertices must have degree zero. Therefore, the only two ways to allocate the degrees of the four vertices such that they sum up to 6 are (1, 2, 2, 1) and (1, 3, 1, 1). They correspond to T 1 and T 2 respectively. Any other spanning tree is isomorphic to either of these two spanning trees. CS 280 Spring 2002 Page 8