CSC373 Week 2: Greedy Algorithms
373F19 - Karan Singh 1
Week 2: Greedy Algorithms Karan Singh 373F19 - Karan Singh 1 - - PowerPoint PPT Presentation
CSC373 Week 2: Greedy Algorithms Karan Singh 373F19 - Karan Singh 1 Recap Divide & Conquer Master theorem Counting inversions in ( log ) Finding closest pair of points in 2 in log Fast
373F19 - Karan Singh 1
373F19 - Karan Singh 2
π(π)
373F19 - Karan Singh 3
π¦ may be a set, composed of its elements)
(this needs to be defined carefully for each problem)
373F19 - Karan Singh 4
π and finishes at time π π
373F19 - Karan Singh 5
π
π
π β π‘ π
π, where π π is the
373F19 - Karan Singh 6
π
π
π β π‘ π
π, where π π is the number of
remaining jobs that conflict with π
373F19 - Karan Singh 7
earliest start time Counterexamples for shortest interval fewest conflicts
373F19 - Karan Singh 8
1 β€ π 2 β€ β― β€ π π
π β₯ π πβ, where πβ is the last added job
π β€ π πβ
added job
373F19 - Karan Singh 9
finish time) which matches greedy for as long as possible
373F19 - Karan Singh 10
selection (π1 = π1, β¦ , ππ = ππ )
ππ +1 β€ π ππ +1)
Another standard method is induction
373F19 - Karan Singh 11
π and finishes at time π π
the same partition are compatible
greedy EFT algorithm, call it one partition, recurse on the remaining jobs.
373F19 - Karan Singh 12
373F19 - Karan Singh 13
373F19 - Karan Singh 14
π
π
π β π‘ π
π, where π π is the
number of remaining jobs that conflict with π
373F19 - Karan Singh 15
assign each lecture to an arbitrary feasible classroom, three of these heuristics do not work.
(next slide)
373F19 - Karan Singh 16
373F19 - Karan Singh 17
some classroom
π at least as large as the minimum key?β
increase its key to π
π
π
373F19 - Karan Singh 18
many classrooms!
373F19 - Karan Singh 19
π
π
π, we have π overlapping lectures
373F19 - Karan Singh 20
373F19 - Karan Singh 21
incompatibility of intervals
representation of the graph
373F19 - Karan Singh 22
π, it will finish at π π = π‘ π + π’π
π β ππ
π
βπ
373F19 - Karan Singh 23
Input An example schedule
373F19 - Karan Singh 24
processing time π’π
373F19 - Karan Singh 25
373F19 - Karan Singh 26
373F19 - Karan Singh 27
373F19 - Karan Singh 28
373F19 - Karan Singh 29
increase lateness but reduces #inversions by one
β² for all π β π, π
β² β€ βπ
373F19 - Karan Singh 30
increase lateness but reduces #inversions by one
β² = π π β² β ππ = π π β ππ β€ π π β ππ = βπ
β², βπ β², max πβ π,π βπ β²
β€ max βπ, βπ, max
πβ π,π βπ β€ π
373F19 - Karan Singh 31
stays optimal) and reduces the number of inversions by 1
schedules.
373F19 - Karan Singh 32
have π = 26, so we can use 5 bits.
373F19 - Karan Singh 33
document than π¦, π, π¨?
373F19 - Karan Singh 34
distinct labels π¦ and π§, π(π¦) is not a prefix of π π§
β¦β¦β¦β¦β¦β¦β¦β¦β¦..
becomes a valid encoding, decode the label, and continue
π(π¦) π(π§)
373F19 - Karan Singh 35
prefix-free encoding with lengths (β1, β¦ , βπ) assigned to the symbols which minimizes Οπ=1
π
π
π₯π β βπ is the length of the compressed document
373F19 - Karan Singh 36
π β 0, π β 100, π β 101, π β 1100, π β 1101, π β 111
373F19 - Karan Singh 37
373F19 - Karan Singh 38
373F19 - Karan Singh 39
373F19 - Karan Singh 40
373F19 - Karan Singh 41
373F19 - Karan Singh 42
373F19 - Karan Singh 43
π β 0, π β 100, π β 101, π β 1100, π β 1101, π β 111
373F19 - Karan Singh 44
encodings, both are optimal, assign 1 bit to each symbol
373F19 - Karan Singh 45
the overall length (exercise!).
least frequent symbols are siblings.
length assigned to them. Then, if theyβre not siblings, chop and rearrange the tree to make them siblings (exercise!).
vs such an optimal tree π
373F19 - Karan Singh 46
π¦π§ as one symbol with frequency π₯π¦ + π₯π§
373F19 - Karan Singh 47