Difference between pages "Chapter 8" and "Chapter 7"

From The Algorithm Design Manual Solution Wiki
(Difference between pages)
Jump to navigation Jump to search
 
 
Line 1: Line 1:
=Weighted Graph Algorithms=
+
=Graph Traversal=
  
 
===Simulating Graph Algorithms===
 
===Simulating Graph Algorithms===
  
:[[8.1]]. For the graphs in Problem 7-1:
+
:[[7.1]]. For the following graphs <math>G_1</math> (left) and <math>G_2</math> (right):
:(a) Draw the spanning forest after every iteration of the main loop in Kruskal’s algorithm.
+
:(see book for figures)
:(b) Draw the spanning forest after every iteration of the main loop in Prim’s algorithm.
 
:(c) Find the shortest-path spanning tree rooted in <math>A</math>.
 
:(d) Compute the maximum flow from <math>A</math> to <math>H</math>.
 
[[8.1|Solution]]
 
  
===Minimum Spanning Tree===
+
:(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>).
 +
[[7.1|Solution]]
  
:8.2. Is the path between two vertices in a minimum spanning tree necessarily a shortest path between the two vertices in the full graph? Give a proof or a counterexample.
 
  
 +
:7.2. Do a topological sort of the following graph <math>G</math>:
 +
:(see book for figure)
  
:[[8.3]]. Assume that all edges in the graph have distinct edge weights (i.e., no pair of edges have the same weight). Is the path between a pair of vertices in a minimum spanning tree necessarily a shortest path between the two vertices in the full graph? Give a proof or a counterexample.
+
===Traversal===
[[8.3|Solution]]
 
  
 +
:[[7.3]]
  
:8.4. Can Prim’s and Kruskal’s algorithms yield different minimum spanning trees? Explain why or why not.
 
  
 +
:7.4
  
:[[8.5]]. Does either Prim’s or Kruskal’s algorithm work if there are negative edge weights? Explain why or why not.
 
[[8.5|Solution]]
 
  
 +
:[[7.5]]
  
:8.6. (a) Assume that all edges in the graph have distinct edge weights (i.e., no pair of edges have the same weight). Is the ''minimum spanning tree'' of this graph unique? Give a proof or a counterexample.
 
:(b) Again, assume that all edges in the graph have distinct edge weights (i.e. no pair of edges have the same weight). Is the ''shortest-path spanning tree'' of this graph unique? Give a proof or a counterexample.
 
  
 +
:7.6
  
:[[8.7]]. Suppose we are given the minimum spanning tree <math>T</math> of a given graph <math>G</math> (with <math>n</math> vertices and <math>m</math> edges) and a new edge <math>e = (u, v)</math> of weight <math>w</math> that we will add to <math>G</math>. Give an efficient algorithm to find the minimum spanning tree of the graph <math>G + e</math>. Your algorithm should run in <math>O(n)</math> time to receive full credit.
 
[[8.7|Solution]]
 
  
 +
:[[7.7]]
  
:8.8. (a) Let <math>T</math> be a minimum spanning tree of a weighted graph <math>G</math>. Construct a new graph <math>G'</math> by adding a weight of <math>k</math> to every edge of <math>G</math>. Do the edges of <math>T</math> form a minimum spanning tree of <math>G'</math>? Prove the statement or give a counterexample.
 
:(b) Let <math>P = {s, . . . , t}</math> describe a shortest path between vertices <math>s</math> and <math>t</math> of a weighted graph <math>G</math>. Construct a new graph <math>G'</math> by adding a weight of <math>k</math> to every edge of <math>G</math>. Does <math>P</math> describe a shortest path from <math>s</math> to <math>t</math> in <math>G'</math>? Prove the statement or give a counterexample.
 
  
 +
:7.8
  
:[[8.9]]. Devise and analyze an algorithm that takes a weighted graph <math>G</math> and finds the smallest change in the cost to a non-minimum spanning tree edge that would cause a change in the minimum spanning tree of <math>G</math>. Your algorithm must be correct and run in polynomial time.
 
[[8.9|Solution]]
 
  
 +
:[[7.9]]
  
:8.10. Consider the problem of finding a minimum-weight connected subset <math>T</math> of edges from a weighted connected graph <math>G</math>. The weight of <math>T</math> is the sum of all the edge weights in <math>T</math>.
 
:(a) Why is this problem not just the minimum spanning tree problem? (Hint: think negative weight edges.)
 
:(b) Give an efficient algorithm to compute the minimum-weight connected subset <math>T</math>.
 
  
 +
:7.10
  
:[[8.11]]. Let <math>T = (V, E')</math> be a minimum spanning tree of a given graph <math>G = (V, E)</math> with positive edge weights. Now suppose the weight of a particular edge <math>e \in E</math> is modified from <math>w(e)</math> to a new value <math>\hat{w}(e)</math>. We seek to update the minimum spanning tree <math>T</math> to reflect this change without recomputing the entire tree from scratch. For each of the following four cases, give a linear-time algorithm to update the tree:
 
:(a)<math>e \notin E'</math> and <math> \hat{w}(e) > w(e)</math>
 
:(b) <math>e \notin E'</math> and <math>\hat{w}(e) < w(e)</math>
 
:(c) <math>e \in E'</math> and <math>\hat{w}(e) < w(e)</math>
 
:(d) <math>e \in E' </math> and <math>\hat{w}(e) > w(e)</math>
 
[[8.11|Solution]]
 
  
 +
:[[7.11]]
  
:8.12. Let <math>G=(V,E)</math> be an undirected graph. A set <math>F \subseteq E</math> of edges is called a ''feedback-edge set'' if every cycle of <math>G</math> has at least one edge in <math>F</math>.
 
:(a)Suppose that <math>G</math> is unweighted. Design an efficient algorithm to find a minimum-size feedback-edge set.
 
:(b)Suppose that <math>G</math> is a weighted undirected graph with positive edge weights. Design an efficient algorithm to find a minimum-weight feedback-edge set.
 
  
===Union Find===
+
:7.12
  
:[[8.13]]. Devise an efficient data structure to handle the following operations on a weighted directed graph:
 
:(a)Merge two given components.
 
:(b)Locate which component contains a given vertex <math>v</math>.
 
:(c)Retrieve a minimum edge from a given component.
 
[[8.13|Solution]]
 
  
 +
===Applications===
  
:8.14. Design a data structure that can perform a sequence of, <math>m</math> ''union'' and  ''find'' operations on a universal set of <math>n</math> elements, consisting of a sequence of all ''unions'' followed by a sequence of all ''finds'', in time <math>O(m+n)</math>.
+
:[[7.13]]
  
===Shortest Paths===
 
  
:[[8:15]]. The ''single-destination shortest path'' problem for a directed graph seeks the shortest path ''from'' every vertex to a specified vertex <math>v</math>. Give an efficient algorithm to solve the single-destination shortest paths problem.
+
:7.14
[[8.15|Solution]]
 
  
  
:8.16. Let <math>G=(V,E)</math> be an undirected weighted graph, and let <math>T</math> be the shortest-path spanning tree rooted at a vertex <math>v</math>. Suppose now that all the edge weights in <math>G</math> are increased by a constant number <math>k</math>. Is <math>T</math> still the shortest-path spanning tree from <math>v</math>?
+
:[[7.15]]
  
 +
===Algorithm Design===
  
:[[8.17]]. (a)Give an example of a weighted connected graph <math>G=(V,E)</math> and a vertex <math>v</math>, such that the minimum spanning tree of <math>G</math> is the same as the shortest-path spanning tree rooted at <math>v</math>.
+
:7.16
:(b)Give an example of a weighted connected directed graph <math>G=(V,E)</math> and a vertex <math>v</math>, such that the minimum-cost spanning tree of <math>G</math> is very different from the shortest-path spanning tree rooted at <math>v</math>.
 
:(c)Can the two trees be completely disjointed?
 
[[8.17|Solution]]
 
  
  
:8.18. Either prove the following or give a counterexample:
+
:[[7.17]]
:(a)Is the path between a pair of vertices in a minimum spanning tree of an undirected graph necessarily the shortest (minimum weight) path?
 
:(b)Suppose that the minimum spanning tree of the graph is unique. Is the path between a pair of vertices in a minimum spanning tree of an undirected graph necessarily the shortest (minimum weight) path?
 
  
  
:[[8.19]]. Give an efficient algorithm to find the shortest path from <math>x</math> to <math>y</math> in an undirected weighted graph <math>G = (V, E)</math> with positive edge weights, subject to the constraint that this path must pass through a particular vertex <math>z</math>.
+
:7.18
  
  
:8.20. In certain graph problems, vertices have can have weights instead of or in addition to the weights of edges. Let <math>C_v</math> be the cost of vertex <math>v</math>, and <math>C_{(x,y)}</math> the cost of the edge <math>(x,y)</math>. This problem is concerned with finding the cheapest path between vertices <math>a</math> and <math>b</math> in a graph <math>G</math>. The cost of a path is the sum of the costs of the edges and vertices encountered on the path.
+
:[[7.19]]
:(a)Suppose that each edge in the graph has a weight of zero (while non-edges have a cost of <math>\infty</math>). Assume that <math>C_v = 1</math> for all vertices <math>1 \leq v \leq n</math> (i.e., all vertices have the same cost). Give an ''efficient'' algorithm to find the cheapest path from <math>a</math> to <math>b</math> and its time complexity.
 
:(b)Now suppose that the vertex costs are not constant (but are all positive) and the edge costs remain as above. Give an ''efficient'' algorithm to find the cheapest path from <math>a</math> to <math>b</math> and its time complexity.
 
:(c)Now suppose that both the edge and vertex costs are not constant (but are all positive). Give an ''efficient'' algorithm to find the cheapest path from <math>a</math> to <math>b</math> and its time complexity.
 
  
  
:[[8.21]]. Give an <math>O(n^3)</math> algorithm that takes an <math>n</math>-vertex directed graph <math>G</math> with positive edge lengths, and returns the length of the shortest cycle in the graph. This length is <math>\infty</math> in the case of an acyclic graph.
+
:7.20
[[8.21|Solution]]
 
  
  
:8.22. A highway network is represented by a weighted graph <math>G</math>, with edges corresponding to roads and vertices corresponding to road intersections. Each road is labeled with the maximum possible height of vehicles that can pass through the road. Give an efficient algorithm to compute the maximum possible height of vehicles that can successfully travel from <math>s</math> to <math>t</math>. What is the runtime of your algorithm?
+
:[[7.21]]]
  
  
:[[8.23]]. You are given a directed graph <math>G</math> with possibly negative weighted edges, in which the shortest path between any two vertices is guaranteed to have at most <math>k</math> edges. Give an algorithm that finds the shortest path between two vertices <math>u</math> and <math>v</math> in <math>O(k * (n + m))</math> time.
+
:7.22
[[8.23|Solution]]
 
  
  
:8.24. Can we solve the single-source ''longest''-path problem by changing ''minimum'' to ''maximum'' in Dijkstra’s algorithm? If so, then prove your algorithm correct. If not, then provide a counterexample.
+
:[[7.23]]
  
  
:[[8.25]]. Let <math>G = (V, E)</math> be a weighted acyclic directed graph with possibly negative edge weights. Design a linear-time algorithm to solve the single-source shortest-path problem from a given source <math>v</math>.
+
:7.24
[[8.25|Solution]]
 
  
  
:8.26. Let <math>G=(V,E)</math> be a directed weighted graph such that all the weights are positive. Let <math>v</math> and <math>w</math> be two vertices in <math>G</math> and <math>k \leq |V|</math> be an integer. Design an algorithm to find the shortest path from <math>v</math> to <math>w</math> that contains exactly <math>k</math> edges. Note that the path need not be simple.
+
:[[7.25]]
  
  
:[[8.27]].''Arbitrage'' is the use of discrepancies in currency-exchange rates to make a profit. For example, there may be a small window of time during which 1 U.S. dollar buys 0.75 British pounds, 1 British pound buys 2 Australian dollars, and 1 Australian dollar buys 0.70 U.S. dollars. At such a time, a smart trader can trade one U.S. dollar and end up with <math>0.75 \times 2 \times 0.7 = 1.05</math> U.S. dollars---a profit of 5%. Suppose that there are <math>n</math> currencies <math>c_1,...,c_n</math> and an <math>n \times n</math> table <math>R</math> of exchange rates, such that one unit of currency <math>c_i</math> buys <math>R[i,j]</math> units of currency <math>c_j</math>. Devise and analyze an algorithm to determine the maximum value of <math> R[c_1,c_{i1}] \cdot R[c_{i1},c_{i2}] \cdots R[c_{i{k-1}},c_{ik}] \cdot R[c_{ik},c_1] </math> Hint: think all-pairs shortest path.
+
:7.26
[[8.27|Solution]]
 
  
===Network Flow and Matching===
 
  
:8.28
+
===Directed Graphs===
  
 +
:[[7.27]]
  
:[[8.29]]
+
 
 +
:7.28
 +
 
 +
 
 +
:[[7.29]]
 +
 
 +
 
 +
:7.30
 +
 
 +
 
 +
:[[7.31]]
 +
 
 +
 
 +
:7.32
 +
 
 +
 
 +
:[[7.33]]
 +
 
 +
 
 +
:7.34
 +
 
 +
 
 +
:[[7.35]]
 +
 
 +
 
 +
:7.36
 +
 
 +
 
 +
:[[7.37]]
 +
 
 +
 
 +
===Articulation Vertices===
 +
 
 +
:7.38
 +
 
 +
 
 +
:[[7.39]]
 +
 
 +
 
 +
:7.40
 +
 
 +
 
 +
:[[7.41]]
 +
 
 +
 
 +
===Interview Problems===
 +
 
 +
:7.42
 +
 
 +
 
 +
:[[7.43]]
  
  
 
Back to [[Chapter List]]
 
Back to [[Chapter List]]

Revision as of 16:13, 14 September 2020

Graph Traversal

Simulating Graph Algorithms

7.1. For the following graphs [math]\displaystyle{ G_1 }[/math] (left) and [math]\displaystyle{ G_2 }[/math] (right):
(see book for figures)
(a)Report the order of the vertices encountered on a breadth-first search starting from vertex [math]\displaystyle{ A }[/math]. Break all ties by picking the vertices in alphabetical order (i.e., [math]\displaystyle{ A }[/math] before [math]\displaystyle{ Z }[/math]).
(b)Report the order of the vertices encountered on a depth-first search starting from vertex [math]\displaystyle{ A }[/math]. Break all ties by picking the vertices in alphabetical order (i.e., [math]\displaystyle{ A }[/math] before [math]\displaystyle{ Z }[/math]).

Solution


7.2. Do a topological sort of the following graph [math]\displaystyle{ G }[/math]:
(see book for figure)

Traversal

7.3


7.4


7.5


7.6


7.7


7.8


7.9


7.10


7.11


7.12


Applications

7.13


7.14


7.15

Algorithm Design

7.16


7.17


7.18


7.19


7.20


7.21]


7.22


7.23


7.24


7.25


7.26


Directed Graphs

7.27


7.28


7.29


7.30


7.31


7.32


7.33


7.34


7.35


7.36


7.37


Articulation Vertices

7.38


7.39


7.40


7.41


Interview Problems

7.42


7.43


Back to Chapter List