Difference between pages "Chapter 2" and "Chapter 12"

From The Algorithm Design Manual Solution Wiki
(Difference between pages)
Jump to navigation Jump to search
 
 
Line 1: Line 1:
=Algorithm Analysis=
+
=Dealing with Hard Problems=\
  
===Program Analysis===
+
===Special Cases of Hard Problems===
  
:[[2.1]]. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using the Big Oh notation.
+
:[[12.1]]. Dominos are tiles represented by integer pairs <math>(x_i, y_i)</math>, where each of the values <math>x_i</math> and <math>y_i</math> are integers between 1 and <math>n</math>. Let <math>S</math> be a sequence of m integer pairs <math>[(x_1, y_1),(x_2, y_2), ...,(x_m, y_m)]</math>. The goal of the game is to create long chains <math>[(x_{i1}, y_{i1}),(x_{i2}, y_{i2}), ...,(x_{it}, y_{it})]</math> such that <math>y_{ij} = x_{i(j+1)}</math>. Dominos can be flipped, so <math>(x_i, y_i)</math> equivalent to <math>(y_i, x_i)</math>. For <math>S = [(1, 3),(4, 2),(3, 5),(2, 3),(3, 8)]</math>, the longest domino sequences include <math>[(4, 2),(2, 3),(3, 8)]</math> and <math>[(1, 3),(3, 2),(2, 4)]</math>.
  mystery(''n'')
+
::(a) Prove that finding the longest domino chain is NP-complete.
      r:=0
+
::(b) Give an efficient algorithm to find the longest domino chain where the numbers increase along the chain. For S above, the longest such chains are <math>[(1, 3),(3, 5)]</math> and <math>[(2, 3),(3, 5)]</math>.
      ''for'' i:=1 ''to'' n-1 ''do''
+
[[12.1|Solution]]
          ''for'' j:=i+1 ''to'' n ''do''
 
              ''for'' k:=1 ''to'' j ''do''
 
                  r:=r+1
 
        ''return''(r)
 
  
[[2.1|Solution]]
 
  
 +
:12.2. Let <math>G = (V, E)</math> be a graph and <math>x</math> and <math>y</math> be two distinct vertices of <math>G</math>. Each vertex <math>v</math> contains a given number of tokens <math>t(v)</math> that you can collect if you visit <math>v</math>.
 +
::(a) Prove that it is NP-complete to find the path from <math>x</math> to <math>y</math> where you can collect the greatest possible number of tokens.
 +
::(b) Give an efficient algorithm if <math>G</math> is a directed acyclic graph (DAG).
  
:2.2. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using Big Oh notation.
 
    pesky(n)
 
        r:=0
 
        ''for'' i:=1 ''to'' n ''do''
 
            ''for'' j:=1 ''to'' i ''do''
 
                ''for'' k:=j ''to'' i+j ''do''
 
                    r:=r+1
 
        return(r)
 
  
 +
:[[12.3]]. The ''Hamiltonian completion problem'' takes a given graph <math>G</math> and seeks an algorithm to add the smallest number of edges to <math>G</math> so that it contains a Hamiltonian cycle. This problem is NP-complete for general graphs; however, it has an efficient algorithm if <math>G</math> is a tree. Give an efficient and provably correct algorithm to add the minimum number of possible edges to tree <math>T</math> so that <math>T</math> plus these edges is Hamiltonian.
 +
[[12.3|Solution]]
  
:[[2.3]]. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using Big Oh notation.
+
===Approximation Algorithms===
    prestiferous(n)
 
        r:=0
 
        ''for'' i:=1 ''to'' n ''do''
 
            ''for'' j:=1 ''to'' i ''do''
 
                ''for'' k:=j ''to'' i+j ''do''
 
                    ''for'' l:=1 ''to'' i+j-k ''do''
 
                        r:=r+1
 
        return(r)
 
  
[[2.3|Solution]]
+
:12.4. In the ''maximum satisfiability problem'', we seek a truth assignment that satisfies as many clauses as possible. Give an heuristic that always satisfies at least half as many clauses as the optimal solution.
  
  
:2.4. What value is returned by the following function? Express your answer as a function of <math>n</math>. Give the worst-case running time using Big Oh notation.
+
:[[12.5]]. Consider the following heuristic for vertex cover. Construct a DFS tree of the graph, and delete all the leaves from this tree. What remains must be a vertex cover of the graph. Prove that the size of this cover is at most twice as large as optimal.
  conundrum(<math>n</math>)
+
[[12.5|Solution]]
      <math>r:=0</math>
 
      ''for'' <math>i:=1</math> ''to'' <math>n</math> ''do''
 
      ''for'' <math>j:=i+1</math> ''to'' <math>n</math> ''do''
 
      ''for'' <math>k:=i+j-1</math> ''to'' <math>n</math> ''do''
 
      <math>r:=r+1</math>
 
        return(<math>r</math>)
 
  
  
:[[2.5]]
+
:12.6. The ''maximum cut problem'' for a graph <math>G = (V, E)</math> seeks to partition the vertices <math>V</math> into disjoint sets <math>A</math> and <math>B</math> so as to maximize the number of edges <math>(a, b) \in E</math> such that <math>a \in A</math> and <math>b \in B</math>. Consider the following heuristic for maximum cut. First assign <math>v_1</math> to <math>A</math> and <math>v_2</math> to <math>B</math>. For each remaining vertex, assign it to the side that adds the most edges to the cut. Prove that this cut is at least half as large as the optimal cut.
  
:2.6. Suppose the following algorithm is used to evaluate the polynomial
 
::::::<math> p(x)=a_n x^n +a_{n-1} x^{n-1}+ \ldots + a_1 x +a_0</math>
 
    <math>p:=a_0;</math>
 
    <math>xpower:=1;</math>
 
    for <math>i:=1</math> to <math>n</math> do
 
    <math>xpower:=x*xpower;</math>
 
    <math>p:=p+a_i * xpower</math>
 
    end
 
#How many multiplications are done in the worst-case? How many additions?
 
#How many multiplications are done on the average?
 
#Can you improve this algorithm?
 
  
 +
:[[12.7]]. [5] In the ''bin-packing problem'', we are given n objects with weights <math>w_1, w_2, ..., w_n</math>, respectively. Our goal is to find the smallest number of bins that will hold the <math>n</math> objects, where each bin has a capacity of at most one kilogram.
 +
:The ''first-fit heuristic'' considers the objects in the order in which they are given. For each object, place it into the first bin that has room for it. If no such bin exists, start a new bin. Prove that this heuristic uses at most twice as many bins as the optimal solution.
 +
[[12.7|Solution]]
  
:2.7. Prove that the following algorithm for computing the maximum value in an array <math>A[1..n]</math> is correct.
 
  max(A)
 
      <math>m:=A[1]</math>
 
      ''for'' <math>i:=2</math> ''to'' n ''do''
 
            ''if'' <math>A[i] > m</math> ''then'' <math>m:=A[i]</math>
 
      ''return'' (m)
 
  
[[2.7|Solution]]
+
:12.8. For the first-fit heuristic described just above, give an example where the packing it finds uses at least 5/3 times as many bins as optimal.
  
===Big Oh===
 
  
 +
:[[12.9]]. Given an undirected graph <math>G = (V, E)</math> in which each node has degree ≤ d, show how to efficiently find an independent set whose size is at least <math>1/(d + 1)</math> times that of the largest independent set.
 +
[[12.9|Solution]]
  
:2.8
 
  
:2.9
+
:12.10. A vertex coloring of graph <math>G = (V, E)</math> is an assignment of colors to vertices of <math>V</math> such that each edge <math>(x, y)</math> implies that vertices <math>x</math> and <math>y</math> are assigned different colors. Give an algorithm for vertex coloring <math>G</math> using at most <math>\Delta + 1</math> colors, where <math>\Delta</math> is the maximum vertex degree of <math>G</math>.
  
:2.10
 
  
:2.11
+
:[[12.11]]. Show that you can solve any given Sudoku puzzle by finding the minimum vertex coloring of a specific, appropriately constructed (9×9)+9 vertex graph.
 +
[[12.11|Solution]]
  
:2.12
+
===Combinatorial Optimization===
 +
For each of the problems below, design and implement a simulated annealing heuristic to get reasonable solutions. How well does your program perform in practice?
  
:2.13
 
  
:2.14
+
:12.12. Design and implement a heuristic for the bandwidth minimization problem discussed in Section 16.2 (page 470).
  
:2.15
 
  
:2.16
+
:[[12.13]]. Design and implement a heuristic for the maximum satisfiability problem discussed in Section 17.10 (page 537).
 +
[[12.13|Solution]]
  
:2.17
 
  
:2.18
+
:12.14. Design and implement a heuristic for the maximum clique problem discussed in Section 19.1 (page 586).
  
:2.19
 
  
:2.20
+
:[[12.15]]. Design and implement a heuristic for the minimum vertex coloring problem discussed in Section 19.7 (page 604).
 +
[[12.15|Solution]]
  
:2.21
 
  
:2.22
+
:12.16. Design and implement a heuristic for the minimum edge coloring problem discussed in Section 19.8 (page 608).
  
:2.23
 
  
:2.24
+
:[[12.17]]. Design and implement a heuristic for the minimum feedback vertex set problem discussed in Section 19.11 (page 618).
 +
[[12.7|Solution]]
  
:2.25
 
  
:2.26
+
:12.18. Design and implement a heuristic for the set cover problem discussed in Section 21.1 (page 678).
  
:2.27
+
==="Quantum" Computing===
  
:2.28
+
:[[12.19]]
  
:2.29
 
  
:2.30
+
:12.20
  
:2.31
 
  
:2.32
+
:[[12.21]]
  
:2.33
 
  
:2.34
+
:12.22
 
 
:2.35
 
 
 
:2.36
 
 
 
===Summations===
 
 
 
 
 
:2.37
 
 
 
:2.38
 
 
 
:2.39
 
 
 
:2.40
 
 
 
:2.41
 
 
 
:2.42
 
 
 
:2.43
 
 
 
===Logartihms===
 
 
 
 
 
:2.44
 
 
 
:2.45
 
 
 
:2.46
 
 
 
:2.47
 
 
 
===Interview Problems===
 
 
 
 
 
:2.48
 
 
 
:2.49
 
 
 
:2.50
 
 
 
:2.51
 
 
 
:2.52
 
 
 
:2.53
 
 
 
:2.54
 
 
 
:2.55
 
  
  
 
Back to [[Chapter List]]
 
Back to [[Chapter List]]

Revision as of 20:57, 10 September 2020

=Dealing with Hard Problems=\

Special Cases of Hard Problems

12.1. Dominos are tiles represented by integer pairs [math]\displaystyle{ (x_i, y_i) }[/math], where each of the values [math]\displaystyle{ x_i }[/math] and [math]\displaystyle{ y_i }[/math] are integers between 1 and [math]\displaystyle{ n }[/math]. Let [math]\displaystyle{ S }[/math] be a sequence of m integer pairs [math]\displaystyle{ [(x_1, y_1),(x_2, y_2), ...,(x_m, y_m)] }[/math]. The goal of the game is to create long chains [math]\displaystyle{ [(x_{i1}, y_{i1}),(x_{i2}, y_{i2}), ...,(x_{it}, y_{it})] }[/math] such that [math]\displaystyle{ y_{ij} = x_{i(j+1)} }[/math]. Dominos can be flipped, so [math]\displaystyle{ (x_i, y_i) }[/math] equivalent to [math]\displaystyle{ (y_i, x_i) }[/math]. For [math]\displaystyle{ S = [(1, 3),(4, 2),(3, 5),(2, 3),(3, 8)] }[/math], the longest domino sequences include [math]\displaystyle{ [(4, 2),(2, 3),(3, 8)] }[/math] and [math]\displaystyle{ [(1, 3),(3, 2),(2, 4)] }[/math].
(a) Prove that finding the longest domino chain is NP-complete.
(b) Give an efficient algorithm to find the longest domino chain where the numbers increase along the chain. For S above, the longest such chains are [math]\displaystyle{ [(1, 3),(3, 5)] }[/math] and [math]\displaystyle{ [(2, 3),(3, 5)] }[/math].

Solution


12.2. Let [math]\displaystyle{ G = (V, E) }[/math] be a graph and [math]\displaystyle{ x }[/math] and [math]\displaystyle{ y }[/math] be two distinct vertices of [math]\displaystyle{ G }[/math]. Each vertex [math]\displaystyle{ v }[/math] contains a given number of tokens [math]\displaystyle{ t(v) }[/math] that you can collect if you visit [math]\displaystyle{ v }[/math].
(a) Prove that it is NP-complete to find the path from [math]\displaystyle{ x }[/math] to [math]\displaystyle{ y }[/math] where you can collect the greatest possible number of tokens.
(b) Give an efficient algorithm if [math]\displaystyle{ G }[/math] is a directed acyclic graph (DAG).


12.3. The Hamiltonian completion problem takes a given graph [math]\displaystyle{ G }[/math] and seeks an algorithm to add the smallest number of edges to [math]\displaystyle{ G }[/math] so that it contains a Hamiltonian cycle. This problem is NP-complete for general graphs; however, it has an efficient algorithm if [math]\displaystyle{ G }[/math] is a tree. Give an efficient and provably correct algorithm to add the minimum number of possible edges to tree [math]\displaystyle{ T }[/math] so that [math]\displaystyle{ T }[/math] plus these edges is Hamiltonian.

Solution

Approximation Algorithms

12.4. In the maximum satisfiability problem, we seek a truth assignment that satisfies as many clauses as possible. Give an heuristic that always satisfies at least half as many clauses as the optimal solution.


12.5. Consider the following heuristic for vertex cover. Construct a DFS tree of the graph, and delete all the leaves from this tree. What remains must be a vertex cover of the graph. Prove that the size of this cover is at most twice as large as optimal.

Solution


12.6. The maximum cut problem for a graph [math]\displaystyle{ G = (V, E) }[/math] seeks to partition the vertices [math]\displaystyle{ V }[/math] into disjoint sets [math]\displaystyle{ A }[/math] and [math]\displaystyle{ B }[/math] so as to maximize the number of edges [math]\displaystyle{ (a, b) \in E }[/math] such that [math]\displaystyle{ a \in A }[/math] and [math]\displaystyle{ b \in B }[/math]. Consider the following heuristic for maximum cut. First assign [math]\displaystyle{ v_1 }[/math] to [math]\displaystyle{ A }[/math] and [math]\displaystyle{ v_2 }[/math] to [math]\displaystyle{ B }[/math]. For each remaining vertex, assign it to the side that adds the most edges to the cut. Prove that this cut is at least half as large as the optimal cut.


12.7. [5] In the bin-packing problem, we are given n objects with weights [math]\displaystyle{ w_1, w_2, ..., w_n }[/math], respectively. Our goal is to find the smallest number of bins that will hold the [math]\displaystyle{ n }[/math] objects, where each bin has a capacity of at most one kilogram.
The first-fit heuristic considers the objects in the order in which they are given. For each object, place it into the first bin that has room for it. If no such bin exists, start a new bin. Prove that this heuristic uses at most twice as many bins as the optimal solution.

Solution


12.8. For the first-fit heuristic described just above, give an example where the packing it finds uses at least 5/3 times as many bins as optimal.


12.9. Given an undirected graph [math]\displaystyle{ G = (V, E) }[/math] in which each node has degree ≤ d, show how to efficiently find an independent set whose size is at least [math]\displaystyle{ 1/(d + 1) }[/math] times that of the largest independent set.

Solution


12.10. A vertex coloring of graph [math]\displaystyle{ G = (V, E) }[/math] is an assignment of colors to vertices of [math]\displaystyle{ V }[/math] such that each edge [math]\displaystyle{ (x, y) }[/math] implies that vertices [math]\displaystyle{ x }[/math] and [math]\displaystyle{ y }[/math] are assigned different colors. Give an algorithm for vertex coloring [math]\displaystyle{ G }[/math] using at most [math]\displaystyle{ \Delta + 1 }[/math] colors, where [math]\displaystyle{ \Delta }[/math] is the maximum vertex degree of [math]\displaystyle{ G }[/math].


12.11. Show that you can solve any given Sudoku puzzle by finding the minimum vertex coloring of a specific, appropriately constructed (9×9)+9 vertex graph.

Solution

Combinatorial Optimization

For each of the problems below, design and implement a simulated annealing heuristic to get reasonable solutions. How well does your program perform in practice?


12.12. Design and implement a heuristic for the bandwidth minimization problem discussed in Section 16.2 (page 470).


12.13. Design and implement a heuristic for the maximum satisfiability problem discussed in Section 17.10 (page 537).

Solution


12.14. Design and implement a heuristic for the maximum clique problem discussed in Section 19.1 (page 586).


12.15. Design and implement a heuristic for the minimum vertex coloring problem discussed in Section 19.7 (page 604).

Solution


12.16. Design and implement a heuristic for the minimum edge coloring problem discussed in Section 19.8 (page 608).


12.17. Design and implement a heuristic for the minimum feedback vertex set problem discussed in Section 19.11 (page 618).

Solution


12.18. Design and implement a heuristic for the set cover problem discussed in Section 21.1 (page 678).

"Quantum" Computing

12.19


12.20


12.21


12.22


Back to Chapter List