Skip to content

Instantly share code, notes, and snippets.

@kartikkukreja
kartikkukreja / Segmented Least Squares Problem.py
Last active August 29, 2015 14:04
Segmented Least Squares Problem
OPT[0] = 0
for j = 1 to n:
for i = 1 to j:
compute e(i, j) for the segment pi, pi+1, ..., pj
for j = 1 to n:
OPT(j) = min((e(i,j) + C + OPT[i-1]) forall 1 ≤ i ≤ j)
return OPT[n]
@kartikkukreja
kartikkukreja / Weighted Interval Scheduling Problem.py
Last active August 29, 2015 14:04
Weighted Interval Scheduling Problem
OPT(j):
if j == 0:
return 0
return max(wj + OPT(p(j)), OPT(j-1))
@kartikkukreja
kartikkukreja / Interval Partitioning Problem.py
Last active August 29, 2015 14:04
Interval Partitioning Problem
Let R be the set of all requests
Let d = 0 be the number of resources
while !R.empty():
Choose a request i ∈ R that has the earliest start time
if i can be assigned to some resource k <= d:
assign request i to resource k
else:
allocate a new resource d+1
assign request i to resource d+1
d = d + 1
@kartikkukreja
kartikkukreja / Interval Partitioning Problem Implementation.py
Last active August 29, 2015 14:04
Interval Partitioning Problem Implementation
sort the n requests in order of start time
d = 1
assign request 1 to resource 1
min_pq.insert(1, f(i))
for i = 2 to n:
j = min_pq.minIndex()
if s(i) >= min_pq.keyOf(j):
assign request i to resource j
min_pq.increaseKey(j, f(i))
@kartikkukreja
kartikkukreja / Interval Scheduling Problem Algorithm.py
Last active August 29, 2015 14:04
Interval Scheduling Problem Algorithm
Let R be the set of all requests
schedule = {}
while !R.empty():
Choose a request i ∈ R that has the smallest finish time
Add i to schedule
Delete all requests from R that are incompatible with i
return schedule
@kartikkukreja
kartikkukreja / Interval Scheduling Problem Implementation.py
Last active August 29, 2015 14:04
Interval Scheduling Problem Implementation
sort the n requests in order of finish time
schedule = {}
last_finish = 0
for i = 1 to n:
if s(i) >= last_finish:
Add i to schedule
last_finish = f(i)
return schedule
@kartikkukreja
kartikkukreja / Stable Marriage Problem.py
Last active August 29, 2015 14:04
Stable Marriage Problem
Initially all m ∈ M and w ∈ W are free
while ∃ m who is free and hasn't proposed to every woman:
choose m
Let w be the highest-ranked woman in m's preference list to whom m has not yet proposed
if w is free:
(m, w) become engaged
elif w is currently engaged to m':
if w prefers m' to m:
m remains free
else w prefers m to m':
@kartikkukreja
kartikkukreja / Testing Bipartiteness.py
Last active August 29, 2015 14:04
Testing Bipartiteness
Assume graph G is connected. Otherwise, we can run the algorithm for each connected component of G.
Let q be an empty queue
Pick any vertex s ∈ V and color it Red
q.enqueue(s)
while !q.empty()
u = q.dequeue()
foreach v in u.adjList:
if v.color is nil:
@kartikkukreja
kartikkukreja / Interpolation Search.py
Last active April 26, 2019 04:54
Interpolation Search
interpolation_search(A[0...n-1], x):
i = 0, j = n-1, lo = A[i], hi = A[j]
if x < lo:
return -1
if x >= hi:
i = j
while i < j:
k = i + ((j - i) * (x - lo)) / (hi - lo)
@kartikkukreja
kartikkukreja / Graham Scan.py
Last active August 29, 2015 14:04
Graham Scan
foreach p of the remaining n-3 points in sorted order
while (point next to top in S, S.top(), p) do not form a counter-clockwise turn
S.pop()
S.push(p)
return S