Difference between revisions of "Chapter 7"
Jump to navigation Jump to search
|Line 58:||Line 58:|
Revision as of 16:31, 14 September 2020
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)
- 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.
- 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.
Back to Chapter List