Difference between pages "Chapter 12" and "Chapter 10"

From The Algorithm Design Manual Solution Wiki
(Difference between pages)
Jump to navigation Jump to search
 
 
Line 1: Line 1:
=Dealing with Hard Problems=\
+
=Dynamic Programming=
  
===Special Cases of Hard Problems===
+
===Elementary Recurrences===
  
:[[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>.
+
:[[10.1]]. Up to <math>k</math> steps in a single bound! A child is running up a staircase with <math>n</math> steps and can hop between 1 and <math>k</math> steps at a time. Design an algorithm to count how many possible ways the child can run up the stairs, as a function of <math>n</math> and <math>k</math>. What is the running time of your algorithm?
::(a) Prove that finding the longest domino chain is NP-complete.
+
[[10.1|Solution]]
::(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>.
 
[[12.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>.
+
:10.2. Imagine you are a professional thief who plans to rob houses along a street of <math>n</math> homes. You know the loot at house <math>i</math> is worth <math>m_i</math>, for <math>1 \le i \le n</math>, but you cannot rob neighboring houses because their connected security systems will automatically contact the police if two adjacent houses are broken into. Give an efficient algorithm to determine the maximum amount of money you can steal without alerting the police.
::(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).
 
  
  
:[[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.
+
:[[10.3]]. Basketball games are a sequence of 2-point shots, 3-point shots, and 1-point free throws. Give an algorithm that computes how many possible mixes (1s,2s,3s) of scoring add up to a given <math>n</math>. For <math>n</math> = 5 there are four possible solutions: (5, 0, 0), (2, 0, 1), (1, 2, 0), and (0, 1, 1).
[[12.3|Solution]]
+
[[10.3|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.
+
:10.4. Basketball games are a sequence of 2-point shots, 3-point shots, and 1-point free throws. Give an algorithm that computes how many possible scoring sequences add up to a given <math>n</math>. For <math>n</math> = 5 there are thirteen possible sequences, including 1-2-1-1, 3-2, and 1-1-1-1-1.
  
  
:[[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.
+
:[[10.5]]. Given an <math>s * t</math> grid filled with non-negative numbers, find a path from top left to bottom right that minimizes the sum of all numbers along its path. You can only move either down or right at any point in time.
[[12.5|Solution]]
+
::(a) Give a solution based on Dijkstra’s algorithm. What is its time complexity as a function of <math>s</math> and <math>t</math>?
 +
::(b) Give a solution based on dynamic programming. What is its time complexity as a function of <math>s</math> and <math>t</math>?
  
 +
===Edit Distance===
  
: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.
+
:10.6
  
  
:[[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.
+
:[[10.7]]
: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]]
 
  
  
: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.
+
:10.8
  
  
:[[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.
+
:[[10.9]]
[[12.9|Solution]]
 
  
  
: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>.
+
:10.10
  
  
:[[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.
+
===Greedy Algorithms===
[[12.11|Solution]]
 
  
===Combinatorial Optimization===
+
:[[10.11]]
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).
+
:10.12
  
  
:[[12.13]]. Design and implement a heuristic for the maximum satisfiability problem discussed in Section 17.10 (page 537).
+
:[[10.13]]
[[12.13|Solution]]
 
  
  
:12.14. Design and implement a heuristic for the maximum clique problem discussed in Section 19.1 (page 586).
+
:10.14
  
  
:[[12.15]]. Design and implement a heuristic for the minimum vertex coloring problem discussed in Section 19.7 (page 604).
+
===Number Problems===
[[12.15|Solution]]
 
  
 +
:[[10.15]]
  
:12.16. Design and implement a heuristic for the minimum edge coloring problem discussed in Section 19.8 (page 608).
 
  
 +
:10.16
  
:[[12.17]]. Design and implement a heuristic for the minimum feedback vertex set problem discussed in Section 19.11 (page 618).
 
[[12.7|Solution]]
 
  
 +
:[[10.17]]
  
:12.18. Design and implement a heuristic for the set cover problem discussed in Section 21.1 (page 678).
 
  
==="Quantum" Computing===
+
:10.18
  
:[[12.19]]. Consider an <math>n</math> qubit “quantum” system <math>Q</math>, where each of the <math>N = 2^n</math> states start out with equal probability <math>p(i) = 1/2^n</math>. Say the ''Jack''<math>(Q, 0^n)</math> operation doubles the probability of the state where all qubits are zero. How many calls to this ''Jack'' operation are necessary until the probability of sampling this null state becomes ≥ 1/2?
 
[[12.19|Solution]]
 
  
 +
:[[10.19]]
  
:12.20. For the satisfiability problem, construct (a) an instance on <math>n</math> variables that has exactly one solution, and (b) an instance on <math>n</math> variables that has exactly <math>2^n</math> different solutions.
 
  
 +
:10.20
  
:[[12.21]]. Consider the first ten multiples of 11, namely 11, 22, . . . 110. Pick two of them (<math>x</math> and <math>y</math>) at random. What is the probability that ''gcd''<math>(x, y) = 11</math>?
 
[[12.21|Solution]]
 
  
 +
:[[10.21]]
 +
 +
 +
:10.22
 +
 +
 +
:[[10.23]]
 +
 +
 +
:10.24
 +
 +
 +
:[[10.25]]
 +
 +
 +
:10.26
 +
 +
 +
===Graphing Problem===
 +
 +
:[[10.27]]
 +
 +
 +
:10.28
 +
 +
 +
:[[10.29]]
 +
 +
 +
===Design Problems===
 +
 +
:10.30
 +
 +
 +
:[[10.31]]
 +
 +
 +
:10.32
 +
 +
 +
:[[10.33]]
 +
 +
 +
:10.34
 +
 +
 +
:[[10.35]]
 +
 +
 +
:10.36
 +
 +
 +
:[[10.37]]
 +
 +
 +
:10.38
 +
 +
 +
===Interview Problems===
 +
 +
:[[10.39]]
 +
 +
 +
:10.40
 +
 +
 +
:[[10.41]]
  
:12.22. IBM quantum computing (https://www.ibm.com/quantum-computing/) offers the opportunity to program a quantum computing simulator. Take a look at an example quantum computing program and run it to see what happens.
 
  
  
 
Back to [[Chapter List]]
 
Back to [[Chapter List]]

Revision as of 22:23, 11 September 2020

Dynamic Programming

Elementary Recurrences

10.1. Up to [math]\displaystyle{ k }[/math] steps in a single bound! A child is running up a staircase with [math]\displaystyle{ n }[/math] steps and can hop between 1 and [math]\displaystyle{ k }[/math] steps at a time. Design an algorithm to count how many possible ways the child can run up the stairs, as a function of [math]\displaystyle{ n }[/math] and [math]\displaystyle{ k }[/math]. What is the running time of your algorithm?

Solution


10.2. Imagine you are a professional thief who plans to rob houses along a street of [math]\displaystyle{ n }[/math] homes. You know the loot at house [math]\displaystyle{ i }[/math] is worth [math]\displaystyle{ m_i }[/math], for [math]\displaystyle{ 1 \le i \le n }[/math], but you cannot rob neighboring houses because their connected security systems will automatically contact the police if two adjacent houses are broken into. Give an efficient algorithm to determine the maximum amount of money you can steal without alerting the police.


10.3. Basketball games are a sequence of 2-point shots, 3-point shots, and 1-point free throws. Give an algorithm that computes how many possible mixes (1s,2s,3s) of scoring add up to a given [math]\displaystyle{ n }[/math]. For [math]\displaystyle{ n }[/math] = 5 there are four possible solutions: (5, 0, 0), (2, 0, 1), (1, 2, 0), and (0, 1, 1).

Solution


10.4. Basketball games are a sequence of 2-point shots, 3-point shots, and 1-point free throws. Give an algorithm that computes how many possible scoring sequences add up to a given [math]\displaystyle{ n }[/math]. For [math]\displaystyle{ n }[/math] = 5 there are thirteen possible sequences, including 1-2-1-1, 3-2, and 1-1-1-1-1.


10.5. Given an [math]\displaystyle{ s * t }[/math] grid filled with non-negative numbers, find a path from top left to bottom right that minimizes the sum of all numbers along its path. You can only move either down or right at any point in time.
(a) Give a solution based on Dijkstra’s algorithm. What is its time complexity as a function of [math]\displaystyle{ s }[/math] and [math]\displaystyle{ t }[/math]?
(b) Give a solution based on dynamic programming. What is its time complexity as a function of [math]\displaystyle{ s }[/math] and [math]\displaystyle{ t }[/math]?

Edit Distance

10.6


10.7


10.8


10.9


10.10


Greedy Algorithms

10.11


10.12


10.13


10.14


Number Problems

10.15


10.16


10.17


10.18


10.19


10.20


10.21


10.22


10.23


10.24


10.25


10.26


Graphing Problem

10.27


10.28


10.29


Design Problems

10.30


10.31


10.32


10.33


10.34


10.35


10.36


10.37


10.38


Interview Problems

10.39


10.40


10.41


Back to Chapter List