Skip to content

Instantly share code, notes, and snippets.

@kartikkukreja
kartikkukreja / A* Tree Search Pseudocode.py
Last active September 22, 2022 16:44
A* Tree Search Pseudocode
def A*-TREE-SEARCH (start):
Let pq be an empty min priority queue
g(start) = 0
f(start) = h(start)
path(start) = []
pq.push(start, f(start))
while not pq.empty():
top = pq.pop()
@kartikkukreja
kartikkukreja / A* Graph Search Pseudocode.py
Last active September 22, 2022 16:44
A* Graph Search Pseudocode
def A*-GRAPH-SEARCH (start):
Let pq be an empty min priority queue
Let closed be an empty set
g(start) = 0
f(start) = h(start)
path(start) = []
pq.push(start, f(start))
while not pq.empty():
@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 / Range Updates and Point Queries.py
Last active May 1, 2016 19:24
Range Updates and Point Queries with BIT
# A[] is the original array
# ft[] is the fenwick tree maintaining the diffs initialized with 0
# Add v to A[p]
update(p, v):
for (; p <= N; p += p&(-p))
ft[p] += v
# Add v to A[a...b]
update(a, b, v):
@kartikkukreja
kartikkukreja / Range Updates and Range Queries.py
Last active November 1, 2017 20:07
Range Updates and Range Queries with BIT
update(ft, p, v):
for (; p <= N; p += p&(-p))
ft[p] += v
# Add v to A[a...b]
update(a, b, v):
update(B1, a, v)
update(B1, b + 1, -v)
update(B2, a, v * (a-1))
update(B2, b + 1, -v * b)
@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