Algorithms

  • Greedy
    • [Exchange Arguments](#exchange\ arguments)
    • [Greedy stays ahead](#greedy\ stays\ ahead)
    • [fractional knapsack](#fractional\ knapsack\ problem)
    • activity selection
    • interval scheduling
    • [huffman encoding](#huffman\ encoding)

Proving that the greedy algorithm is optimal:

  • Exchange Arguments
  • Greedy stays ahead

Exchange Arguments

Consider any possible solution to the problem and gradually transform into the solution found by the greedy algorithm without hurting its quality eg Problem: schedule jobs with deadlines and length to a machine that can only do one job at a time Greedy Solution: Earliest Deadline First. Sort jobs in increasing order of deadlines and schedule jobs in that order Exchange Argument Proof: Consider an Optimal Solution . The optimal solution will not allow the machine to ever be idle Let us an define an inversion as being scheduled before despite

  • the greedy solution will have no inversions

to prove: there is an optimal solution with no inversions and no idle time

it is trivial to say the optimal solution has no idle time

swapping an adjacent inverted pair does not affect the finishing time of any other tasks, and also reduces task ‘s lateness. We only need to check the lateness of task then.

The point to note is that lateness of can not be more than the lateness of before swapping

therefore the overall lateness is being reduced

Here swapping is the transformation we are doing to our “optimal” solution to make it align with the greedy solution while not losing its optimalness


Greedy Stays Ahead

consider the optimal solution with compatible tasks with finish times and all tasks being compatible

obviously

now our greedy solution always prioritizes choosing the minimum after job is done. This means that at worst it always has the option of choosing job if not a job with therefore our greedy solution stays ahead of the “optimal” one.


Fractional Knapsack Problem

Problem Given objects with a certain value and weight, and a container with capacity, how do you choose which objects to take to maximum the value in the container. You can break objects for partial value.

Solution You sort the objects by ratio of and add the highest ratioed object or whatever fraction of it you can


Huffman Encoding

consider a string with unique characters each with a frequency sort the characters in increasing order of build a bottom up tree

  • choose the 2 elements with the least frequency (first two elements)
  • put them in 2 leafs and have their root be their combined frequency
  • put this combined frequency back in the sorted list of and repeat
Huffman (C)
n = C.length // no. of unique characters
Q = C
for i = 0 to n - 2
	allocate new node z
	z.left = x = Extract-Min(Q)
	z.right = y = Extract-Min(Q)
	z.freg = x.freq + y.freq
	insert(Q, z)
return Extract-Min(Q) // this is the root

below is an image for the input

CC.freq
o1
u1
x1
p1
r1
l1
f3
s2
h2
i2
m2
t2
n2
’ ‘7
a4
e4

to create the huffman code starting from the root traverse to each leaf. Assign a to each left node and a to each right node. The huffman code for each character will be string encountered as you traverse from root to leaf