# Difference between revisions of "Chapter 7"

Jump to navigation
Jump to search

(8 intermediate revisions by the same user not shown) | |||

Line 6: | Line 6: | ||

:(see book for figures) | :(see book for figures) | ||

− | :(a)Report the order of the vertices encountered on a breadth-first search starting from vertex <math>A</math>. Break all ties by picking the vertices in alphabetical order (i.e., <math>A</math> before <math>Z</math>). | + | :(a) Report the order of the vertices encountered on a breadth-first search starting from vertex <math>A</math>. Break all ties by picking the vertices in alphabetical order (i.e., <math>A</math> before <math>Z</math>). |

− | :(b)Report the order of the vertices encountered on a depth-first search starting from vertex <math>A</math>. Break all ties by picking the vertices in alphabetical order (i.e., <math>A</math> before <math>Z</math>). | + | :(b) Report the order of the vertices encountered on a depth-first search starting from vertex <math>A</math>. Break all ties by picking the vertices in alphabetical order (i.e., <math>A</math> before <math>Z</math>). |

[[7.1|Solution]] | [[7.1|Solution]] | ||

Line 58: | Line 58: | ||

===Applications=== | ===Applications=== | ||

− | :[[7.13]] | + | :[[7.13]]. The Chutes and Ladders game has a board with n cells where you seek to travel from cell 1 to cell <math>n</math>. To move, a player throws a six-sided dice to determine how many cells forward they move. This board also contains chutes and ladders that connect certain pairs of cells. A player who lands on the mouth of a chute immediately falls back down to the cell at the other end. A player who lands on the base of a ladder immediately travels up to the cell at the top of the ladder. Suppose you have rigged the dice to give you full control of the number for each roll. Give an efficient algorithm to find the minimum number of dice throws to win. |

+ | [[7.13|Solution]] | ||

− | :7.14 | + | :7.14. Plum blossom poles are a Kung Fu training technique, consisting of <math>n</math> large posts partially sunk into the ground, with each pole <math>p_i</math> at position <math>(x_i, y_i)</math>. Students practice martial arts techniques by stepping from the top of one pole to the top of another pole. In order to keep balance, each step must be more than <math>d</math> meters but less than <math>2d</math> meters. Give an efficient algorithm to find a safe path from pole <math>p_s</math> to <math>p_t</math> if it exists. |

− | :[[7.15]] | + | :[[7.15]]. You are planning the seating arrangement for a wedding given a list of guests, <math>V</math>. For each guest <math>g</math> you have a list of all other guests who are on bad terms with them. Feelings are reciprocal: if <math>h</math> is on bad terms with <math>g</math>, then <math>g</math> is on bad terms with <math>h</math>. Your goal is to arrange the seating such that no pair of guests sitting at the same table are on bad terms with each other. There will be only two tables at the wedding. Give an efficient algorithm to find an acceptable seating arrangement if one exists. |

+ | [[7.15|Solution]] | ||

===Algorithm Design=== | ===Algorithm Design=== | ||

− | :7.16 | + | :7.16. The ''square'' of a directed graph <math>G=(V,E)</math> is the graph <math>G^2=(V,E^2)</math> such that <math>(u,w) \in E^2</math> iff there exists <math>v \in V</math> such that <math>(u,v) \in E</math> and <math>(v,w) \in E</math>; i.e., there is a path of exactly two edges from <math>u</math> to <math>w</math>. |

+ | :Give efficient algorithms for both adjacency lists and matrices. | ||

− | :[[7.17]] | + | :[[7.17]]. A ''vertex cover'' of a graph <math>G=(V,E)</math> is a subset of vertices <math>V'</math> such that each edge in <math>E</math> is incident on at least one vertex of <math>V'</math>. |

+ | :(a)Give an efficient algorithm to find a minimum-size vertex cover if <math>G</math> is a tree. | ||

+ | :(b)Let <math>G=(V,E)</math> be a tree such that the weight of each vertex is equal to the degree of that vertex. Give an efficient algorithm to find a minimum-weight vertex cover of <math>G</math>. | ||

+ | :(c)Let <math>G=(V,E)</math> be a tree with arbitrary weights associated with the vertices. Give an efficient algorithm to find a minimum-weight vertex cover of <math>G</math>. | ||

+ | [[7.17|Solution]] | ||

− | :7.18 | + | :7.18. A ''vertex cover'' of a graph <math>G=(V,E)</math> is a subset of vertices <math>V' \in V</math> such that every edge in <math>E</math> contains at least one vertex from <math>V'</math>. Delete all the leaves from any depth-first search tree of <math>G</math>. Must the remaining vertices form a vertex cover of <math>G</math>? Give a proof or a counterexample. |

− | :[[7.19]] | + | :[[7.19]]. An ''independent set'' of an undirected graph <math>G=(V,E)</math> is a set of vertices $U$ such that no edge in <math>E</math> is incident on two vertices of <math>U</math>. |

+ | :(a)Give an efficient algorithm to find a maximum-size independent set if <math>G</math> is a tree. | ||

+ | :(b)Let <math>G=(V,E)</math> be a tree with weights associated with the vertices such that the weight of each vertex is equal to the degree of that vertex. Give an efficient algorithm to find a maximum independent set of <math>G</math>. | ||

+ | :(c)Let <math>G=(V,E)</math> be a tree with arbitrary weights associated with the vertices. Give an efficient algorithm to find a maximum independent set of <math>G</math>. | ||

+ | [[7.19|Solution]] | ||

− | :7.20 | + | :7.20. A ''vertex cover'' of a graph <math>G=(V,E)</math> is a subset of vertices <math>V' \in V</math> such that every edge in <math>E</math> contains ''at least one'' vertex from <math>V'</math>. An ''independent set'' of graph <math>G=(V,E)</math> is a subset of vertices <math>V' \in V</math> such that no edge in <math>E</math> contains both vertices from <math>V'</math>. |

+ | :An ''independent vertex cover'' is a subset of vertices that is both an independent set and a vertex cover of <math>G</math>. Give an efficient algorithm for testing whether <math>G</math> contains an independent vertex cover. What classical graph problem does this reduce to? | ||

− | :[[7.21]]] | + | :[[7.21]]. Consider the problem of determining whether a given undirected graph <math>G=(V,E)</math> contains a ''triangle'' or cycle of length 3. |

+ | :(a)Give an <math>O(|V|^3)</math> to find a triangle if one exists. | ||

+ | :(b)Improve your algorithm to run in time <math>O(|V| \cdot |E|)</math>. You may assume <math>|V| \leq |E|</math>. | ||

+ | :Observe that these bounds gives you time to convert between the adjacency matrix and adjacency list representations of <math>G</math>. | ||

+ | [[7.21|Solution]] | ||

− | :7.22 | + | :7.22. Consider a set of movies <math>M_1, M_2, \ldots, M_k</math>. There is a set of customers, each one of which indicates the two movies they would like to see this weekend. Movies are shown on Saturday evening and Sunday evening. Multiple movies may be screened at the same time. |

+ | :You must decide which movies should be televised on Saturday and which on Sunday, so that every customer gets to see the two movies they desire. Is there a schedule where each movie is shown at most once? Design an efficient algorithm to find such a schedule if one exists. | ||

− | :[[7.23]] | + | :[[7.23]].The ''diameter'' of a tree <math>T=(V,E)</math> is given by |

+ | <center><math>\max_{u,v\in V} \delta(u,v)</math></center> | ||

+ | :(where <math>\delta(u,v)</math> is the number of edges on the path from <math>u</math> to <math>v</math>). Describe an efficient algorithm to compute the diameter of a tree, and show the correctness and analyze the running time of your algorithm. | ||

+ | [[7.23|Solution]] | ||

− | :7.24 | + | :7.24. Given an undirected graph <math>G</math> with <math>n</math> vertices and <math>m</math> edges, and an integer <math>k</math>, give an <math>O(m+n)</math> algorithm that finds the maximum induced subgraph <math>H</math> of <math>G</math> such that each vertex in <math>H</math> has degree <math>\geq k</math>, or prove that no such graph exists. An induced subgraph <math>F=(U,R)</math> of a graph <math>G=(V,E)</math> is a subset of <math>U</math> of the vertices <math>V</math> of <math>G</math>, and all edges <math>R</math> of <math>G</math> such that both vertices of each edge are in <math>U</math>. |

− | :[[7.25]] | + | :[[7.25]]. Let <math>v</math> and <math>w</math> be two vertices in a directed graph <math>G=(V,E </math>. Design a linear-time algorithm to find the ''number'' of different shortest paths (not necessarily vertex disjoint) between <math>v</math> and <math>w</math>. Note: the edges in <math>G</math> are unweighted. |

+ | [[7.25|Solution]] | ||

− | :7.26 | + | :7.26. Design a linear-time algorithm to eliminate each vertex <math>v</math> of degree 2 from a graph by replacing edges <math>(u,v)</math> and <math>(v,w)</math> by an edge <math>(u,w)</math>. We also seek to eliminate multiple copies of edges by replacing them with a single edge. Note that removing multiple copies of an edge may create a new vertex of degree 2, which has to be removed, and that removing a vertex of degree 2 may create multiple edges, which also must be removed. |

− | |||

===Directed Graphs=== | ===Directed Graphs=== | ||

− | :[[7.27]] | + | :[[7.27]]. The reverse of a directed graph <math>G = (V, E)</math> is another directed graph <math>G^R = (V, E^R)</math> on the same vertex set, but with all edges reversed; that is, <math>E^R = {(v, u) : (u, v) \in E}</math>. Give an <math>O(n + m)</math> algorithm for computing the reverse of an <math>n</math>-vertex <math>m</math>-edge graph in adjacency list format. |

+ | [[7.27|Solution]] | ||

− | :7.28 | + | :7.28. Your job is to arrange <math>n</math> ill-behaved children in a straight line, facing front. You are given a list of <math>m</math> statements of the form “<math>i</math> hates <math>j</math>.” If <math>i</math> hates <math>j</math>, then you do not want to put <math>i</math> somewhere behind <math>j</math>, because then <math>i</math> is capable of throwing something at <math>j</math>. |

+ | :(a) Give an algorithm that orders the line (or says that it is not possible) in <math>O(m + n)</math> time. | ||

+ | :(b) Suppose instead you want to arrange the children in rows such that if <math>i</math> hates <math>j</math>, then <math>i</math> must be in a lower numbered row than <math>j</math>. Give an efficient algorithm to find the minimum number of rows needed, if it is possible. | ||

− | :[[7.29]] | + | :[[7.29]]. A particular academic program has <math>n</math> required courses, certain pairs of which have prerequisite relations so that <math>(x, y)</math> means you must take course <math>x</math> before <math>y</math>. How would you analyze the prerequisite pairs to make sure it is possible for people to complete the program? |

+ | [[7.29|Solution]] | ||

− | :7.30 | + | :7.30. Gotcha-solitaire is a game on a deck with <math>n</math> distinct cards (all face up) and <math>m</math> gotcha pairs <math>(i, j)</math> such that card <math>i</math> must be played sometime before card <math>j</math>. You play by sequentially choosing cards, and win if you pick up the entire deck without violating any gotcha pair constraints. Give an efficient algorithm to find a winning pickup order if one exists. |

− | :[[7.31]] | + | :[[7.31]]. You are given a list of <math>n</math> words each of length <math>k</math> in a language you don’t know, although you are told that words are sorted in lexicographic (alphabetical) order. Reconstruct the order of the α alphabet letters (characters) in that language. |

+ | :For example, if the strings are <math>{QQZ, QZZ, XQZ, XQX, XXX}</math>, the character order must be <math>Q</math> before <math>Z</math> before <math>X</math>. | ||

+ | :(a) Give an algorithm to efficiently reconstruct this character order. (Hint: use a graph structure, where each node represents one letter.) | ||

+ | :(b) What is its running time, as a function of <math>n, k</math>, and <math>\alpha</math>? | ||

+ | [[7.31|Solution]] | ||

− | :7.32 | + | :7.32. A ''weakly connected component'' in a directed graph is a connected component ignoring the direction of the edges. Adding a single directed edge to a directed graph can reduce the number of weakly connected components, but by at most how many components? What about the number of strongly connected components? |

− | :[[7.33]] | + | :[[7.33]]. Design a linear-time algorithm that, given an undirected graph <math>G</math> and a particular edge <math>e</math> in it, determines whether <math>G</math> has a cycle containing <math>e</math>. |

+ | [[7.33|Solution]] | ||

− | :7.34 | + | :7.34. An ''arborescence'' of a directed graph <math>G</math> is a rooted tree such that there is a directed path from the root to every other vertex in the graph. Give an efficient and correct algorithm to test whether <math>G</math> contains an arborescence, and its time complexity. |

− | :[[7.35]] | + | :[[7.35]]. A mother vertex in a directed graph <math>G = (V, E)</math> is a vertex <math>v</math> such that all other vertices <math>G</math> can be reached by a directed path from <math>v</math>. |

+ | :(a) Give an <math>O(n + m)</math> algorithm to test whether a given vertex <math>v</math> is a mother of <math>G</math>, where <math>n = |V|</math> and <math>m = |E|</math>. | ||

+ | :(b) Give an <math>O(n + m)</math> algorithm to test whether graph <math>G</math> contains a mother vertex. | ||

+ | [[7.35|Solution]] | ||

− | :7.36 | + | :7.36. Let <math>G</math> be a directed graph. We say that <math>G</math> is ''k-cyclic'' if every (not necessarily simple) cycle in <math>G</math> contains at most <math>k</math> distinct nodes. Give a linear-time algorithm to determine if a directed graph <math>G</math> is <math>k</math>-cyclic, where <math>G</math> and <math>k</math> are given as inputs. Justify the correctness and running time of your algorithm. |

− | :[[7.37]] | + | :[[7.37]]. A ''tournament'' is a directed graph formed by taking the complete undirected graph and assigning arbitrary directions on the edges—that is, a graph <math>G = (V, E)</math> such that for all <math>u,v \in V</math>, exactly one of <math>(u, v)</math> or <math>(v, u)</math> is in <math>E</math>. Show that every tournament has a Hamiltonian path—that is, a path that visits every vertex exactly once. Give an algorithm to find this path. |

− | + | [[7.37|Solution]] | |

===Articulation Vertices=== | ===Articulation Vertices=== | ||

− | :7.38 | + | :7.38. An articulation vertex of a connected graph <math>G</math> is a vertex whose deletion disconnects <math>G</math>. Let <math>G</math> be a graph with <math>n</math> vertices and <math>m</math> edges. Give a simple <math>O(n+m)</math> algorithm for finding a vertex of <math>G</math> that is not an articulation vertex—that is, whose deletion does not disconnect <math>G</math>. |

− | |||

− | |||

+ | :[[7.39]]. Following up on the previous problem, give an <math>O(n+m)</math> algorithm that finds a deletion order for the <math>n</math> vertices such that no deletion disconnects the graph. (Hint: think DFS/BFS.) | ||

+ | [[7.39|Solution]] | ||

− | |||

+ | :7.40. Suppose <math>G</math> is a connected undirected graph. An edge <math>e</math> whose removal disconnects the graph is called a bridge. Must every bridge <math>e</math> be an edge in a depth-first search tree of <math>G</math>? Give a proof or a counterexample. | ||

− | |||

+ | :[[7.41]]. A city that only has two-way streets has decided to change them all into one-way streets. They want to ensure that the new network is strongly connected so everyone can legally drive anywhere in the city and back. | ||

+ | :(a) Let <math>G</math> be the original undirected graph. Prove that there is a way to properly orient/direct the edges of <math>G</math> provided <math>G</math> does not contain a bridge. | ||

+ | :(b) Give an efficient algorithm to orient the edges of a bridgeless graph <math>G</math> so the result is strongly connected. | ||

+ | [[7.41|Solution]] | ||

===Interview Problems=== | ===Interview Problems=== | ||

− | :7.42 | + | :7.42. Which data structures are used in depth-first and breath-first search? |

− | :[[7.43]] | + | :[[7.43]]. Write a function to traverse binary search tree and return the ith node in sorted order. |

+ | [[7.43|Solution]] | ||

Back to [[Chapter List]] | Back to [[Chapter List]] |

## Latest revision as of 18:09, 1 October 2020

## Contents

# Graph Traversal

### Simulating Graph Algorithms

- 7.1. For the following graphs (left) and (right):
- (see book for figures)

- (a) Report the order of the vertices encountered on a breadth-first search starting from vertex . Break all ties by picking the vertices in alphabetical order (i.e., before ).
- (b) Report the order of the vertices encountered on a depth-first search starting from vertex . Break all ties by picking the vertices in alphabetical order (i.e., before ).

- 7.2. Do a topological sort of the following graph :
- (see book for figure)

### Traversal

- 7.3. Prove that there is a unique path between any pair of vertices in a tree.

- 7.4. Prove that in a breadth-first search on a undirected graph , every edge is either a tree edge or a cross edge, where is neither an ancestor nor descendant of in cross edge .

- 7.5. Give a linear algorithm to compute the chromatic number of graphs where each vertex has degree at most 2. Any bipartite graph has a chromatic number of 2. Must such graphs be bipartite?

- 7.6. You are given a connected, undirected graph with vertices and edges. Give an algorithm to identify an edge you can remove from while still leaving connected, if one exists. Can you reduce the running time to ?

- 7.7. In breadth-first and depth-first search, an undiscovered node is marked
*discovered*when it is first encountered, and marked*processed*when it has been completely searched. At any given moment, several nodes might be simultaneously in the*discovered*state. - (a) Describe a graph on vertices and a particular starting vertex such that nodes are simultaneously in the
*discovered*state during a*breadth-first search*starting from . - (b) Describe a graph on vertices and a particular starting vertex such that nodes are simultaneously in the
*discovered*state during a*depth-first search*starting from . - (c) Describe a graph on vertices and a particular starting vertex such that at some point nodes remain
*undiscovered*, while nodes have been*processed*during a*depth-first search*starting from .(Note, there may also be*discovered*nodes.)

- 7.8. Given pre-order and in-order traversals of a binary tree (discussed in Section 3.4.1), is it possible to reconstruct the tree? If so, sketch an algorithm to do it. If not, give a counterexample. Repeat the problem if you are given the pre-order and post-order traversals.

- 7.9. Present correct and efficient algorithms to convert an undirected graph between the following graph data structures. You must give the time complexity of each algorithm, assuming vertices and edges.
- (a)Convert from an adjacency matrix to adjacency lists.
- (b)Convert from an adjacency list to an incidence matrix. An incidence matrix has a row for each vertex and a column for each edge, such that if vertex is part of edge , otherwise .
- (c)Convert from an incidence matrix to adjacency lists.

- 7.10. Suppose an arithmetic expression is given as a tree. Each leaf is an integer and each internal node is one of the standard arithmetical operations . For example, the expression is represented by the tree in Figure 7.17(a). Give an algorithm for evaluating such an expression, where there are nodes in the tree.

- 7.11. Suppose an arithmetic expression is given as a DAG (directed acyclic graph) with common subexpressions removed. Each leaf is an integer and each internal node is one of the standard arithmetical operations . For example, the expression is represented by the DAG in Figure (see book)(b). Give an algorithm for evaluating such a DAG, where there are nodes and edges in the DAG. Hint: modify an algorithm for the tree case to achieve the desired efficiency.

- 7.12. The war story of Section 7.4 (page 210) describes an algorithm for constructing the dual graph of the triangulation efficiently, although it does not guarantee linear time. Give a worst-case linear algorithm for the problem.

### Applications

- 7.13. The Chutes and Ladders game has a board with n cells where you seek to travel from cell 1 to cell . To move, a player throws a six-sided dice to determine how many cells forward they move. This board also contains chutes and ladders that connect certain pairs of cells. A player who lands on the mouth of a chute immediately falls back down to the cell at the other end. A player who lands on the base of a ladder immediately travels up to the cell at the top of the ladder. Suppose you have rigged the dice to give you full control of the number for each roll. Give an efficient algorithm to find the minimum number of dice throws to win.

- 7.14. Plum blossom poles are a Kung Fu training technique, consisting of large posts partially sunk into the ground, with each pole at position . Students practice martial arts techniques by stepping from the top of one pole to the top of another pole. In order to keep balance, each step must be more than meters but less than meters. Give an efficient algorithm to find a safe path from pole to if it exists.

- 7.15. You are planning the seating arrangement for a wedding given a list of guests, . For each guest you have a list of all other guests who are on bad terms with them. Feelings are reciprocal: if is on bad terms with , then is on bad terms with . Your goal is to arrange the seating such that no pair of guests sitting at the same table are on bad terms with each other. There will be only two tables at the wedding. Give an efficient algorithm to find an acceptable seating arrangement if one exists.

### Algorithm Design

- 7.16. The
*square*of a directed graph is the graph such that iff there exists such that and ; i.e., there is a path of exactly two edges from to . - Give efficient algorithms for both adjacency lists and matrices.

- 7.17. A
*vertex cover*of a graph is a subset of vertices such that each edge in is incident on at least one vertex of . - (a)Give an efficient algorithm to find a minimum-size vertex cover if is a tree.
- (b)Let be a tree such that the weight of each vertex is equal to the degree of that vertex. Give an efficient algorithm to find a minimum-weight vertex cover of .
- (c)Let be a tree with arbitrary weights associated with the vertices. Give an efficient algorithm to find a minimum-weight vertex cover of .

- 7.18. A
*vertex cover*of a graph is a subset of vertices such that every edge in contains at least one vertex from . Delete all the leaves from any depth-first search tree of . Must the remaining vertices form a vertex cover of ? Give a proof or a counterexample.

- 7.19. An
*independent set*of an undirected graph is a set of vertices $U$ such that no edge in is incident on two vertices of . - (a)Give an efficient algorithm to find a maximum-size independent set if is a tree.
- (b)Let be a tree with weights associated with the vertices such that the weight of each vertex is equal to the degree of that vertex. Give an efficient algorithm to find a maximum independent set of .
- (c)Let be a tree with arbitrary weights associated with the vertices. Give an efficient algorithm to find a maximum independent set of .

- 7.20. A
*vertex cover*of a graph is a subset of vertices such that every edge in contains*at least one*vertex from . An*independent set*of graph is a subset of vertices such that no edge in contains both vertices from . - An
*independent vertex cover*is a subset of vertices that is both an independent set and a vertex cover of . Give an efficient algorithm for testing whether contains an independent vertex cover. What classical graph problem does this reduce to?

- 7.21. Consider the problem of determining whether a given undirected graph contains a
*triangle*or cycle of length 3. - (a)Give an to find a triangle if one exists.
- (b)Improve your algorithm to run in time . You may assume .
- Observe that these bounds gives you time to convert between the adjacency matrix and adjacency list representations of .

- 7.22. Consider a set of movies . There is a set of customers, each one of which indicates the two movies they would like to see this weekend. Movies are shown on Saturday evening and Sunday evening. Multiple movies may be screened at the same time.
- You must decide which movies should be televised on Saturday and which on Sunday, so that every customer gets to see the two movies they desire. Is there a schedule where each movie is shown at most once? Design an efficient algorithm to find such a schedule if one exists.

- 7.23.The
*diameter*of a tree is given by

- (where is the number of edges on the path from to ). Describe an efficient algorithm to compute the diameter of a tree, and show the correctness and analyze the running time of your algorithm.

- 7.24. Given an undirected graph with vertices and edges, and an integer , give an algorithm that finds the maximum induced subgraph of such that each vertex in has degree , or prove that no such graph exists. An induced subgraph of a graph is a subset of of the vertices of , and all edges of such that both vertices of each edge are in .

- 7.25. Let and be two vertices in a directed graph . Design a linear-time algorithm to find the
*number*of different shortest paths (not necessarily vertex disjoint) between and . Note: the edges in are unweighted.

- 7.26. Design a linear-time algorithm to eliminate each vertex of degree 2 from a graph by replacing edges and by an edge . We also seek to eliminate multiple copies of edges by replacing them with a single edge. Note that removing multiple copies of an edge may create a new vertex of degree 2, which has to be removed, and that removing a vertex of degree 2 may create multiple edges, which also must be removed.

### Directed Graphs

- 7.27. The reverse of a directed graph is another directed graph on the same vertex set, but with all edges reversed; that is, . Give an algorithm for computing the reverse of an -vertex -edge graph in adjacency list format.

- 7.28. Your job is to arrange ill-behaved children in a straight line, facing front. You are given a list of
**Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://en.wikipedia.org/api/rest_v1/":): {\displaystyle m}**statements of the form “**Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://en.wikipedia.org/api/rest_v1/":): {\displaystyle i}**hates**Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://en.wikipedia.org/api/rest_v1/":): {\displaystyle j}**.” If**Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://en.wikipedia.org/api/rest_v1/":): {\displaystyle i}**hates**Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://en.wikipedia.org/api/rest_v1/":): {\displaystyle j}**, then you do not want to put**Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://en.wikipedia.org/api/rest_v1/":): {\displaystyle i}**somewhere behind**Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://en.wikipedia.org/api/rest_v1/":): {\displaystyle j}**, because then - (a) Give an algorithm that orders the line (or says that it is not possible) in
**Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://en.wikipedia.org/api/rest_v1/":): {\displaystyle O(m + n)}**time. - (b) Suppose instead you want to arrange the children in rows such that if

- 7.29. A particular academic program has
**Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://en.wikipedia.org/api/rest_v1/":): {\displaystyle n}**required courses, certain pairs of which have prerequisite relations so that**Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://en.wikipedia.org/api/rest_v1/":): {\displaystyle (x, y)}**means you must take course**Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://en.wikipedia.org/api/rest_v1/":): {\displaystyle x}**before**Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://en.wikipedia.org/api/rest_v1/":): {\displaystyle y}**. How would you analyze the prerequisite pairs to make sure it is possible for people to complete the program?

- 7.30. Gotcha-solitaire is a game on a deck with
**Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://en.wikipedia.org/api/rest_v1/":): {\displaystyle n}**distinct cards (all face up) and**Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://en.wikipedia.org/api/rest_v1/":): {\displaystyle m}**gotcha pairs**Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://en.wikipedia.org/api/rest_v1/":): {\displaystyle (i, j)}**such that card

- 7.31. You are given a list of
**Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://en.wikipedia.org/api/rest_v1/":): {\displaystyle n}**words each of length**Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://en.wikipedia.org/api/rest_v1/":): {\displaystyle k}**in a language you don’t know, although you are told that words are sorted in lexicographic (alphabetical) order. Reconstruct the order of the α alphabet letters (characters) in that language. - For example, if the strings are , the character order must be before before .
- (a) Give an algorithm to efficiently reconstruct this character order. (Hint: use a graph structure, where each node represents one letter.)
- (b) What is its running time, as a function of , and ?

- 7.32. A
*weakly connected component*in a directed graph is a connected component ignoring the direction of the edges. Adding a single directed edge to a directed graph can reduce the number of weakly connected components, but by at most how many components? What about the number of strongly connected components?

- 7.33. Design a linear-time algorithm that, given an undirected graph and a particular edge in it, determines whether has a cycle containing .

- 7.34. An
*arborescence*of a directed graph is a rooted tree such that there is a directed path from the root to every other vertex in the graph. Give an efficient and correct algorithm to test whether contains an arborescence, and its time complexity.

- 7.35. A mother vertex in a directed graph is a vertex such that all other vertices can be reached by a directed path from .
- (a) Give an algorithm to test whether a given vertex is a mother of , where and .
- (b) Give an algorithm to test whether graph contains a mother vertex.

- 7.36. Let be a directed graph. We say that is
*k-cyclic*if every (not necessarily simple) cycle in contains at most distinct nodes. Give a linear-time algorithm to determine if a directed graph is -cyclic, where and are given as inputs. Justify the correctness and running time of your algorithm.

- 7.37. A
*tournament*is a directed graph formed by taking the complete undirected graph and assigning arbitrary directions on the edges—that is, a graph such that for all , exactly one of or is in . Show that every tournament has a Hamiltonian path—that is, a path that visits every vertex exactly once. Give an algorithm to find this path.

### Articulation Vertices

- 7.38. An articulation vertex of a connected graph is a vertex whose deletion disconnects . Let be a graph with vertices and edges. Give a simple algorithm for finding a vertex of that is not an articulation vertex—that is, whose deletion does not disconnect .

- 7.39. Following up on the previous problem, give an algorithm that finds a deletion order for the vertices such that no deletion disconnects the graph. (Hint: think DFS/BFS.)

- 7.40. Suppose is a connected undirected graph. An edge whose removal disconnects the graph is called a bridge. Must every bridge be an edge in a depth-first search tree of ? Give a proof or a counterexample.

- 7.41. A city that only has two-way streets has decided to change them all into one-way streets. They want to ensure that the new network is strongly connected so everyone can legally drive anywhere in the city and back.
- (a) Let be the original undirected graph. Prove that there is a way to properly orient/direct the edges of provided does not contain a bridge.
- (b) Give an efficient algorithm to orient the edges of a bridgeless graph so the result is strongly connected.

### Interview Problems

- 7.42. Which data structures are used in depth-first and breath-first search?

- 7.43. Write a function to traverse binary search tree and return the ith node in sorted order.

Back to Chapter List