Skip to content

Instantly share code, notes, and snippets.

@kartikkukreja
kartikkukreja / Alpha Beta Search Pseudocode.py
Last active August 29, 2015 14:03
Alpha Beta Search Pseudocode
def next-move(state, depth):
bestMove = nil
alpha = -INF
for next-state in state.nexts():
result = minscore(next-state, depth-1, alpha, INF)
if result > alpha:
alpha = result
bestMove = next-state.move()
if alpha >= MAX-POSSIBLE-SCORE:
break
@kartikkukreja
kartikkukreja / Evaluation Function for Nine Mens' Morris
Created July 15, 2014 06:26
Evaluation Function for Nine Mens' Morris
Evaluation function for Phase 1 = 18 * (1) + 26 * (2) + 1 * (3) + 9 * (4) + 10 * (5) + 7 * (6)
Evaluation function for Phase 2 = 14 * (1) + 43 * (2) + 10 * (3) + 11 * (4) + 8 * (7) + 1086 * (8)
Evaluation function for Phase 3 = 16 * (1) + 10 * (5) + 1 * (6) + 1190 * (8)
@kartikkukreja
kartikkukreja / Point Updates and Range Queries.py
Last active August 29, 2015 14:03
Point Updates and Range Queries with BIT
# Add v to A[p]
update(p, v):
for (; p <= N; p += p&(-p))
ft[p] += v
# Return sum A[1...b]
query(b):
sum = 0
for(; b > 0; b -= b&(-b))
sum += ft[b]
@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 / 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 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 / Scheduling to minimize lateness algorithm.py
Last active August 29, 2015 14:04
Scheduling to minimize lateness algorithm
sort the jobs in non-decreasing order of their deadlines
last_finished = s[1]
for i = 1 to n:
Assign job i to the interval [last_finished, last_finished + t[i]]
last_finished += t[i]
@kartikkukreja
kartikkukreja / Articulation points or cut vertices in a graph.py
Last active August 29, 2015 14:04
Articulation points or cut vertices in a graph
Let disc_time[v] = -1 and explored[v] = false forall v
dfscounter = 0
DFS(v):
explored[v] = true
disc_time[v] = low[v] = ++dfscounter
foreach edge (v,x):
if !explored[x]:
DFS(x)
low[v] = min(low[v], low[x])
@kartikkukreja
kartikkukreja / Matching Nuts and Bolts Problem.py
Last active August 29, 2015 14:04
Matching Nuts and Bolts Problem
Partition(bolts[lo...hi], nut):
j = lo
for i in range(lo, hi):
if bolts[i] < nut:
swap(bolts[i], bolts[j])
j += 1
elif bolts[i] == nut:
swap(bolts[i], bolts[hi])
# the bolt that ends up at i might be larger than nut
i -= 1
@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]