🎉 从330转为WashU帮助手册啦!

CSE347
模块
Cse347 L1

Lecture 1

Greedy Algorithms

  • Builds up a solution by making a series of small decisions that optimize some objective.
  • Make one irrevocable choice at a time, creating smaller and smaller sub-problems of the same kind as the original problem.
  • There are many potential greedy strategies and picking the right one can be challenging.

A Scheduling Problem

You manage a giant space telescope.

  • There are nn research projects that want to use it to make observations.
  • Only one project can use the telescope at a time.
  • Project pip_i needs the telescope starting at time sis_i and running for a length of time tit_i.
  • Goal: schedule as many as possible

Formally

Input:

  • Given a set PP of projects, P=n|P|=n
  • Each request piPp_i\in P occupies interval [si,fi)[s_i,f_i), where fi=si+tif_i=s_i+t_i

Goal: Choose a subset ΠP\Pi\sqsubseteq P such that

  1. No two projects in Π\Pi have overlapping intervals.
  2. The number of selected projects Π|\Pi| is maximized.

Shortest Interval

Counter-example: [1,10],[9,12],[11,20]

Earliest start time

Counter-example: [1,10],[2,3],[4,5]

Fewest Conflicts

Counter-example: [1,2],[1,4],[1,4],[3,6],[7,8],[5,8],[5,8]

Earliest finish time

Correct... but why

Theorem of Greedy Strategy (Earliest Finishing Time)

Say this greedy strategy (Earliest Finishing Time) picks a set Π\Pi of intervals, some other strategy picks a set OO of intervals.

Assume sorted by finishing time

  • Π={i1,i2,...,ik},Π=k\Pi=\{i_1,i_2,...,i_k\},|\Pi|=k
  • O={j1,j2,...,jm},O=mO=\{j_1,j_2,...,j_m\},|O|=m

We want to show that ΠO,k>m|\Pi|\geq|O|,k>m

Lemma: For all r<k,firfjrr<k,f_{i_r}\leq f_{j_r}

We proceed the proof by induction.

  • Base Case, when r=1. Π\Pi is the earliest finish time, and OO cannot pick a interval with earlier finish time, so firfjrf_{i_r}\leq f_{j_r}

  • Inductive step, when r>1. Since Πr\Pi_r is the earliest finish time, so for any set in OrO_r, fir1fjr1f_{i_{r-1}}\leq f_{j_{r-1}}, for any jrj_r inserted to OrO_r, it can also be inserted to Πr\Pi_r. So OrO_r cannot pick an interval with earlier finish time than PiPi since it will also be picked by definition if OrO_r is the optimal solution OPTOPT.

Problem of “Greedy Stays Ahead” Proof

  • Every problem has very different theorem.
  • It can be challenging to even write down the correct statement that you must prove.
  • We want a systematic approach to prove the correctness of greedy algorithms.

Road Map to Prove Greedy Algorithm

1. Make a Choice

Pick an interval based on greedy choice, say qq

Proof: Greedy Choice Property: Show that using our first choice is not "fatal" – at least one optimal solution makes this choice.

Techniques: Exchange Argument: "If an optimal solution does not choose qq, we can turn it into an equally good solution that does."

Let Π\Pi^* be any optimal solution for project set PP.

  • If qΠq\in \Pi^*, we are done.
  • Otherwise, let xx be the optimal solution from Π\Pi^* that does not pick qq. We create another solution Πˉ\bar{\Pi^*} that replace xx with qq, and prove that the Πˉ\bar{\Pi^*} is as optimal as Π\Pi^*

2. Create a smaller instance PP' of the original problem

PP' has the same optimization criteria.

Proof: Inductive Structure: Show that after making the first choice, we're left with a smaller version of the same problem, whose solution we can safely combine with the first choice.

Let PP' be the subproblem left after making first choice qq in problem PP and let Π\Pi' be an optimal solution to PP'. Then Π=Π{q}\Pi=\Pi^*\cup\{q\} is an optimal solution to PP.

P=P{q}{P'=P-\{q\}-\{projects conflicting with q}q\}

3. Solution: Union of choices that we made

Union of choices that we made.

Proof: Optimal Substructure: Show that if we solve the subproblem optimally, adding our first choice creates an optimal solution to the whole problem.

Let qq be the first choice, PP' be the subproblem left after making qq in problem PP, Π\Pi' be an optimal solution to PP'. We claim that Π=Π{q}\Pi=\Pi'\cup \{q\} is an optimal solution to PP.

We proceed the proof by contradiction.

Assume that Π=Π+{q}\Pi=\Pi'+\{q\} is not optimal.

By Greedy choice property GCPGCP. we already know that \exists an optimal solution Π\Pi^* for problem PP that contains qq. If Π\Pi is not optimal, cost(Π)<cost(Π)cost(\Pi^*)<cost(\Pi). Then since Πq\Pi^*-q is also a feasible solution to PP'. cost(Πq)>cost(Πq)=Πcost(\Pi^*-q)>cost(\Pi-q)=\Pi' which leads to contradiction that Π\Pi' is an optimal solution to PP'.

4. Put 1-3 together to write an inductive proof of the Theorem

This is independent of problem, same for every problem.

Use scheduling problem as an example:

Theorem: given a scheduling problem PP, if we repeatedly choose the remaining feasible project with the earliest finishing time, we will construct an optimal feasible solution to PP.

Proof: We proceed by induction on P|P|. (based on the size of problem PP).

  • Base case: P=1|P|=1.
  • Inductive step.
    • Inductive hypothesis: For all problems of size <n<n, earliest finishing time (EFT) gives us an optimal solution.
    • EFT is optimal for problem of size nn.
    • Proof: Once we pick q, because of greedy choice. P=P={q}{P'=P=\{q\} -\{interval that conflict with q}q\}. P<n|P'|<n, By Inductive hypothesis, EFT gives us an optimal solution to PP', but by inductive substructure, and optimal substructure. Π\Pi' (optimal solution to PP'), we have optimal solution to PP.

this step always holds as long as the previous three properties hold, and we don't usually write the whole proof.

# Algorithm construction for Interval scheduling problem
def schedule(p):
  # sorting takes O(n)=nlogn
  p=sorted(p,key=lambda x:x[1])
  res=[P[0]]
  # O(n)=n
  for i in p[1:]:
    if res[-1][-1]<i[0]:
      res.append(i)
  return res

Extra Examples:

File compression problem

You have nn files of different sizes fif_i.

You want to merge them to create a single file. merge(fi,fj)merge(f_i,f_j) takes time fi+fjf_i+f_j and creates a file of size fk=fi+fjf_k=f_i+f_j.

Goal: Find the order of merges such that the total time to merge is minimized.

Thinking process: The merge process is a binary tree and each of the file is the leaf of the tree.

The total time required =i=1ndifi\sum^n_{i=1} d_if_i, where did_i is the depth of the file in the compression tree.

So compressing the smaller file first may yield a faster run time.

Proof:

Greedy Choice Property

Construct part of the solution by making a locally good decision.

Lemma: \exist some optimal solution that merges the two smallest file first, lets say [f1,f2][f_1,f_2]

Proof: Exchange argument

  • Case 1: Optimal choice already merges f1,f2f_1,f_2, done. Time order does not matter in this problem at some point.
    • eg: [2,2,3], merge 2,3 and 2,2 first don't change the total cost
  • Case 2: Optimal choice does not merges f1f_1 and f2f_2.
    • Suppose the optimal solution merges fx,fyf_x,f_y as the deepest merge.
    • Then dxd1,dyd2d_x\geq d_1,d_y\geq d_2. Exchanging f1,f2f_1,f_2 with fx,fyf_x,f_y would yield a strictly less greater solution since f1,f2f_1,f_2 already smallest.

Inductive Structure

  • We can combine feasible solution to the subproblem PP' with the greedy choice to get a feasible solution to PP
  • After making greedy choice qq, we are left with a strictly smaller subproblem PP' with the same optimality criteria of the original problem

Proof: Optimal Substructure: Show that if we solve the subproblem optimally, adding our first choice creates an optimal solution to the whole problem.

Let qq be the first choice, PP' be the subproblem left after making qq in problem PP, Π\Pi^* be an optimal solution to PP'. We claim that Π=Π{q}\Pi=\Pi'\cup \{q\} is an optimal solution to PP.

We proceed the proof by contradiction.

Assume that Π=Π+{q}\Pi=\Pi^*+\{q\} is not optimal.

By Greedy choice property GCPGCP. we already know that Π\Pi^* is optimal solution that contains qq. Then Π>Π|\Pi^*|>|\Pi| Πq\Pi^*-q is also feasible solution to PP'. Πq>Πq=Π|\Pi^*-q|>|\Pi-q|=\Pi' which is an optimal solution to PP' which leads to contradiction.

Proof: Smaller problem size

After merging the smallest two files into one, we have strictly less files waiting to merge.

Optimal Substructure

  • We can combine optimal solution to the subproblem PP' with the greedy choice to get a optimal solution to PP

Step 4 ignored, same for all greedy problems.

Conclusion: Greedy Algorithm

  • Algorithm
  • Runtime Complexity
  • Proof
    • Greedy Choice Property
      • Construct part of the solution by making a locally good decision.
    • Inductive Structure
      • We can combine feasible solution to the subproblem PP' with the greedy choice to get a feasible solution to PP
      • After making greedy choice qq, we are left with a strictly smaller subproblem PP' with the same optimality criteria of the original problem
    • Optimal Substructure
      • We can combine optimal solution to the subproblem PP' with the greedy choice to get a optimal solution to PP
  • Standard Contradiction Argument simplifies it

Review:

Essence of master method

Let a1a\geq 1 and b>1b>1 be constants, let f(n)f(n) be a function, and let T(n)T(n) be defined on the nonnegative integers by the recurrence

T(n)=aT(nb)+f(n)T(n)=aT(\frac{n}{b})+f(n)

where we interpret n/bn/b to mean either ceiling or floor of n/bn/b. ccrit=logbac_{crit}=\log_b a Then T(n)T(n) has to following asymptotic bounds.

  • Case I: if f(n)=O(nc)f(n) = O(n^{c}) (f(n)f(n) "dominates" nlogbacn^{\log_b a-c}) where c<ccritc<c_{crit}, then T(n)=Θ(nccrit)T(n) = \Theta(n^{c_{crit}})

  • Case II: if f(n)=Θ(nccrit)f(n) = \Theta(n^{c_{crit}}), (f(n),nlogbacf(n), n^{\log_b a-c} have no dominate) then T(n)=Θ(nlogbalog2n)T(n) = \Theta(n^{\log_b a} \log_2 n)

    Extension for f(n)=Θ(ncritical_value(logn)k)f(n)=\Theta(n^{critical\_value}*(\log n)^k)

    • if k>1k>-1

      T(n)=Θ(ncritical_value(logn)k+1)T(n)=\Theta(n^{critical\_value}*(\log n)^{k+1})

    • if k=1k=-1

      T(n)=Θ(ncritical_valueloglogn)T(n)=\Theta(n^{critical\_value}*\log \log n)

    • if k<1k<-1

      T(n)=Θ(ncritical_value)T(n)=\Theta(n^{critical\_value})

  • Case III: if f(n)=Ω(nlogba+c)f(n) = \Omega(n^{log_b a+c}) (nlogbacn^{log_b a-c} "dominates" f(n)f(n)) for some constant c>0c >0, and if a f(n/b)<=cf(n)f(n/b)<= c f(n) for some constant c<1c <1 then for all sufficiently large nn, T(n)=Θ(nlogba+c)T(n) = \Theta(n^{log_b a+c})