title: Charging argument
text: # Charging argument
  
  In computer science, a charging argument is used to compare the output of an optimization algorithm to an optimal solution. It is typically used to show that an algorithm produces optimal results by proving the existence of a particular injective function. For profit maximization problems, the function can be any one-to-one mapping from elements of an optimal solution to elements of the algorithm's output. For cost minimization problems, the function can be any one-to-one mapping from elements of the algorithm's output to elements of an optimal solution.
  
  ## Correctness
  
  In order for an algorithm to optimally solve a profit maximization problem, the algorithm must produce an output that has as much profit as the optimal solution for every possible input. Let |A(I)| denote the profit of the algorithm's output given an input I, and let |OPT(I)| denote the profit of an optimal solution for I. If an injective function h : OPT(I) → A(I) exists, it follows that |OPT(I)| ≤ |A(I)|. Since the optimal solution has the greatest profit attainable, this means that the output given by the algorithm is just as profitable as the optimal solution, and so the algorithm is optimal.
  
  The correctness of the charging argument for a cost minimization problem is symmetric. If |A(I)| and |OPT(I)| denote the cost of the algorithm's output and optimal solution respectively, then the existence of an injective function h : A(I) → OPT(I) would mean that |A(I)| ≤ |OPT(I)|. Since the optimal solution has the lowest cost, and the cost of the algorithm is the same as the cost of the optimal solution of the minimization problem, then the algorithm also optimally solves the problem.
  
  ## Variations
  
  Charging arguments can also be used to show approximation results. In particular, it can be used to show that an algorithm is an n-approximation to an optimization problem. Instead of showing that an algorithm produces outputs with the same value of profit or cost as the optimal solution, show that it attains that value within a factor of n. Rather than proving the existence of a one-to-one function, the charging argument focuses on proving that an n-to-one function exists in order to prove approximation results.
  
  ## Examples
  
  ### Interval Scheduling Problem
  
  Given a set of n intervals I = ${I_1,$ $I_2,$ ... , $I_n},$ where each interval $I_i$ ∈ I has a starting time $s_i$ and a finishing time $f_i,$ where $s_i$ < $f_i,$ the goal is to find a maximal subset of mutually compatible intervals in I. Here, two intervals $I_j$ and $I_k$ are said to be compatible if they do not overlap, in that $s_j$ < $f_j$ ≤ $s_k$ < $f_k.$
  
  Consider the earliest finish time greedy algorithm, described as follows:
  
  * Begin with an empty set of intervals.
  * Sort the intervals in I by ascending finishing times.
  * Consider each interval in I in sorted order. Add the interval into the set if it does not conflict with intervals already contained in the set. Otherwise, disregard the interval.
  
  The interval scheduling problem can be viewed as a profit maximization problem, where the number of intervals in the mutually compatible subset is the profit. The charging argument can be used to show that the earliest finish time algorithm is optimal for the interval scheduling problem.
  
  Given a set of intervals I = ${I_1,$ $I_2,$ ... , $I_n},$ let OPT(I) be any optimal solution of the interval scheduling problem, and let EFT(I) be the solution of the earliest finishing time algorithm. For any interval J ∈ OPT(I), define h(J) as the interval J' ∈ EFT(I) that intersects J with the earliest finishing time amongst all intervals in EFT(I) intersecting J. To show that the earliest finish time algorithm is optimal using the charging argument, h must be shown to be a one-to-one function mapping intervals in OPT(I) to those in EFT(I). Suppose J is an arbitrary interval in OPT(I).
  
  Show that h is a function mapping OPT(I) to EFT(I).
  
      Assume for a contradiction that there is no interval J' ∈ EFT(I) satisfying h(J) = J'. By definition of h, this means that no interval in EFT(I) intersects with J. However, this would also mean that J is compatible with every interval in EFT(I), and so the earliest finishing time algorithm would have added J into EFT(I), and so J ∈ EFT(I). A contradiction arises, since J was assumed to not intersect with any interval in EFT(I), yet J is in EFT(I), and J intersects with itself. Thus by contradiction, J must intersect with at least one interval in EFT(I).
      It remains to show that h(J) is unique. Based on the definition of compatibility, it can never be the case that two compatible intervals have the same finishing time. Since all intervals in EFT(I) are mutually compatible, none of these intervals have the same finishing time. In particular, every interval in EFT(I) that intersects with J have distinct finishing times, and so h(J) is unique.
  
  Show that h is one-to-one.
  
      Assume for a contradiction that h is not injective. Then there are two distinct intervals in OPT(I), $J_1$ and $J_2,$ such that h maps both $J_1$ and $J_2$ to the same interval J' ∈ EFT(I). Without loss of generality, assume that $f_1$ < $f_2.$ The intervals $J_1$ and $J_2$ cannot intersect because they are both in the optimal solution, and so $f_1$ ≤ $s_2<$ $f_2.$ Since EFT(I) contains J' instead of $J_1,$ the earliest finishing time algorithm encountered J' before $J_1.$ Thus, f' ≤ $f_1.$ However, this means that f' ≤ $f_1$ ≤ $s_2<$ $f_2,$ so J' and $J_2$ do not intersect. This is a contradiction because h cannot map $J_2$ to J' if they do not intersect. Thus by contradiction, h is injective.
  
  Therefore, h is a one-to-one function mapping intervals in OPT(I) to those in EFT(I). By the charging argument, the earliest finishing time algorithm is optimal.
  
  ### Job Interval Scheduling Problem
  
  Consider the job interval scheduling problem, an NP-hard variant of the interval scheduling problem visited earlier. As before, the goal is to find a maximal subset of mutually compatible intervals in a given set of n intervals, I = ${I_1,$ $I_2,$ ... , $I_n}.$ Each interval $I_i$ ∈ I has a starting time $s_i,$ a finishing time $f_i,$ and a job class $c_i.$ Here, two intervals $I_j$ and $I_k$ are said to be compatible if they do not overlap and have different classes.
  
  Recall the earliest finishing time algorithm from the previous example. After modifying the definition of compatibility in the algorithm, the charging argument can be used to show that the earliest finish time algorithm is a 2-approximation algorithm for the job interval scheduling problem.
  
  Let OPT(I) and EFT(I) denote the optimal solution and the solution produced by the earliest finishing time algorithm, as earlier defined. For any interval J ∈ OPT(I), define h as follows:
  
      $h(J)={\begin{cases}{\mbox{the interval in EFT(I) with the same job class as J, if one exists}}\\{\mbox{the interval with the earliest finishing time amongst all intervals in EFT(I) intersecting J, otherwise}}\end{cases}}$
  
  To show that the earliest finish time algorithm is a 2-approximation algorithm using the charging argument, h must be shown to be a two-to-one function mapping intervals in OPT(I) to those in EFT(I). Suppose J is an arbitrary interval in OPT(I).
  
  Show that h is a function mapping OPT(I) to EFT(I).
  
      First, notice that there is either some interval in EFT(I) with the same job class as J, or there isn't.
      Case 1. Suppose that some interval in EFT(I) has the same job class as J.
         If there is an interval in EFT(I) with the same class as J, then J will map to that interval. Since the intervals in EFT(I) are mutually compatible, every interval in EFT(I) must have a different job class. Thus, such an interval is unique.
      Case 2. Suppose that there are no intervals in EFT(I) with the same job class as J.
         If there are no intervals in EFT(I) with the same class as J, then h maps J to the interval with the earliest finishing time amongst all intervals in EFT(I) intersecting J. The proof of existence and uniqueness of such an interval is given in the previous example.
  
  Show that h is two-to-one.
  
      Assume for a contradiction that h is not two-to-one. Then there are three distinct intervals in OPT(I), $J_1,$ $J_2,$ and $J_3,$ such that h maps each of $J_1,$ $J_2,$ and $J_3$ to the same interval J' ∈ EFT(I). By the pigeonhole principle, at least two of the three intervals were mapped to J' because they have the same job class as J ', or because J ' is the interval with the earliest finishing time amongst all intervals in EFT(I) intersecting both intervals. Without loss of generality, assume that these two intervals are $J_1$ and $J_2.$
      Case 1. Suppose $J_1$ and $J_2$ were mapped to J ' because they have the same job class as J '.
         Then each J ', $J_1,$ and $J_2$ have the same job class. This is a contradiction, since the intervals in the optimal solution must be compatible, yet $J_1$ and $J_2$ are not.
      Case 2. Suppose J ' is the interval with the earliest finishing time amongst all intervals in EFT(I) intersecting both $J_1$ and $J_2.$
         The proof of this case is equivalent to the one in the previous example that showed injectivity. A contradiction follows from the proof above.
  
  Therefore, h maps no more than two distinct intervals in OPT(I) to the same interval in EFT(I), and so h is two-to-one. By the charging argument, the earliest finishing time algorithm is a two-approximation algorithm for the job interval scheduling problem.
  
  