Greedy algorithms Flashcards

1
Q

What is a greedy algorithm?

A

The greedy:

  • builds the solution in steps
  • In every step it adds an element from the input to the solution, according to some choosing rule
  • The algorithm _does not regret its choices._
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the template for a greedy algorithm?

A

Input:

  • S={1,…n} events.

Initialization:

  • empty set G

step:

  • While S is not empty:
    • Choose i from S according to some rule
    • Add i to G
    • Remove from S all the events which intersect with i(including i itself)

End:

  • Return G
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the template for a mathematical proof of greedy algorithms?

  • Observation: how the algorithm provides a legal solution?
  • Claim: let G be the solution output by the algorithm and O be the optimum solution. If G is different from O, then we can tweak O to get another O* that is different from O and strictly better than O.
    • we prove it by choosing the smallest index by which Oi not equals Ki, then we’ll change Oi to be Si(greedy choice at the ith step), and then show that this leads to an even better solution.(this involves observations regarding the problem).
A

greedy algorithm proof template:

*

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How the algorithm(shortest end time) for the activities problem is proved to be correct?

A
  • Observation:
    • the greedy creates a legal solution.
  • Explanation:
    • it removes all intersection with i in S.
  • claim:
    • In every step of the algorithm there exists an optimal solution O which contains G.
  • proof by induction:
    • Assumption holds for the (k-1)th step. There exists such O
    • step: let i be the kth choice. There are two cases:
      • i belongs to O - we’re done.
      • i does not belong to O.
        • we want to compre a choice from O/G(k-1) to i
          • We compare by the choosing rule
          • let j be the activity with the smallest end time in O/G(K-1)
        • why O/G(k-1) must have an element?
          • Because Gk is a legal solution according to the observation, and O is a maximal one.
          • If O/G(k-1) is empty, it can’t be maximal
        • O*=(O{j}) U {i} contains G.
          • O{j}) contains G(k-1) and {i} contains i.
        • O* is of maximal size
          • O is of maximal size, so is O*
        • O* is legal
          • Gk is legal so i does not intersect with G(k-1)
          • Define A= O{j}\G(k-1).
            • O is legal - thus j does not intersect with G(k-1) thus at the kth step j was part of S. The fact i was chosen shows the end time of i is earlier than the end time of j.
            • O is legal, thus any activity in A starts not before j is finished. Therefore, any activity in A starts not before i is finished.
  • Correctness statement:
    • The algorithm returns a legal set of maximal size.
  • Proof:
    • According to the claim, when S is empty, there exists a solution O which contains G. If S is empty it means there is no element in the input which does not intersect with any of the elements of G. That means there can’t be in O also an elements which intersects with G, thus G=O.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Activities problem S

Legal solution:

  • A division of S into sets.
  • Find a legal solution of minimum number of sets.*

Show an algorithm for this problem.

A

Initialization:

  • k = 1
  • C1 is the empty set
  • Sort S by start time

loop: for all i in the sorted S

  • if i can be added to some set Ci, do it
  • else, open a new set: k=k+1 , C{k} = {i}.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Activities problem(division)

what is a depth of an instance of the problem?

A

It is the maximal number of sections which intersect.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Activities problem(division)

Stages in the proof:

  • What is the observation?
A
  • In any legal solution, the number of sets is greater or equal to the depth.*
  • Explanation:*
  • Assume the number of sets is smaller than the depth. Contradiction by the peigion principal.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Activities problem(division)

Stages in the proof:

  • What is the correctness statement?
A

The algorithm returns a legal solution with minimum number of sets.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Activities problem(division)

Stages in the proof:

  • What is the helping statement?
A

The number of sets the algorithm opens is smaller or equal to the depth of the instance for the problem.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Activities problem(division)

Stages in the proof:

  • What is the proof of the correctness statement using the helping statement?
A

Observation: the algorithm returns a legal solution

  • All the actvities are distributed
  • We never assign activity such that it intersects with one of the elements in its set.

Proof:

  • Assume the algorithm open d sets.
  • By helping statement : d <= depth
  • By observation, depth <= any legal solution
  • we get: d<=depth<any></any>
  • That is, d is minimal.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Activities problem(division)

Stages in the proof:

  • What is the proof of the helping statement?
A
  • Let i the the elements which makes the algorithm create the last set d.*
  • That is, i satisfies two traits:*
  • There exists x in every set Ck ,[1,d-1], s.t.:
    • start time of i < end time of x.
  • According to the sorting:
    • start time of x <= start time of i

That means, there are d-1 elements which intersect with i, and because i’s length is positive, there is a point in which all d elements intersects. Thus, the depth is at least d.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Activities problem(division)

How is it implemented:

  • Before the implementation we use two observations. what are they?
A
  1. i can be added to Cj if and only if Fj<=Si
    • Si - start time of i
    • Fj - maximal end time of Cj
  2. There exists such set Cj for whichi Fj<=Si if and only if Min {Fj}<=Sj.
    • That is, we should look for the minimal Fj and check this condition only.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Activities problem(division)

How is it implemented:

  • Which data structure helps us keep Min{Fj}?
  • What is its initilized value?
  • How do we sort the activities
A
  • Minimum heap Q
    • {Cj, Fj} - each set and its maximal end time
  • Initialized to:
    • Q = {emptyset, -infinity}
  • Sorting by start time
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Activities problem(division)

How is it implemented:

  • What is the loop part of the algorithm?
A

Loop:

  • For each activity i:
    • ExtractMin to get (Cj,Fj)
    • if Fj<=Si then:
      • Cj = Cj U {i}
      • Fj=Fi
      • IncreaseKey(Q,(Cj,Fj))
    • else
      • open a new set C = {i}
      • Insert (Q, (C,Fi))

End:

  • Return all the sets in Q.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Heap:

  • What is the run-time for:
    • Extract-Min
    • Insert
    • Increase-Key
A

O(log(n))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly