Skip to content

Instantly share code, notes, and snippets.

@kartikkukreja
kartikkukreja / Tic Tac Toe Heuristic.cpp
Last active November 1, 2022 13:04
Tic Tac Toe Heuristic
#define Possible_Wins 8
const int Three_in_a_Row[Possible_Wins][3] = {
{ 0, 1, 2 },
{ 3, 4, 5 },
{ 6, 7, 8 },
{ 0, 3, 6 },
{ 1, 4, 7 },
{ 2, 5, 8 },
{ 0, 4, 8 },
{ 2, 4, 6 }
@kartikkukreja
kartikkukreja / Kruskal's MST Algorithm.py
Last active August 29, 2015 14:04
Kruskal's MST Algorithm
sort edges in order of increasing cost
MST = {}
foreach edge (u, v) in order of increasing cost
if MST ∪ (u, v) does not create a cycle
add (u, v) to MST
return MST
@kartikkukreja
kartikkukreja / Prim's MST Algorithm.py
Last active August 29, 2015 14:04
Prim's MST Algorithm
Let the MST contain a single vertex v, where v is any arbitrary vertex of the graph
while it is possible to add an edge (u, v) to the MST such that u ∈ MST and v ∉ MST
add v and the least-cost edge (u, v) to the MST such that u ∈ MST and v ∉ MST
return MST
@kartikkukreja
kartikkukreja / Dijkstra's Single Source Shortest Path Algorithm.py
Last active August 29, 2015 14:04
Dijkstra's Single Source Shortest Path Algorithm
foreach vertex v in graph:
dist[v] = infinity
previous[v] = null
dist[s] = 0
Q = min-priority queue, holding all nodes v in the graph, ordered by dist[v]
while !Q.empty():
u = vertex in Q having smallest key
Q.deleteMin()
@kartikkukreja
kartikkukreja / Closest Pair of Points Problem.py
Last active August 29, 2015 14:04
Closest Pair of Points Problem
let P be the array of N points
minDist = infinity
for i = 1 to N-1
for j = i+1 to N
if dist(P[i], P[j]) < minDist
minDist = dist(P[i], P[j])
closestPair = (P[i], P[j])
return closestPair
@kartikkukreja
kartikkukreja / Minimum Subarray Problem.py
Last active July 11, 2017 00:42
Minimum Subarray Problem
let A be the given array of integers
let minSum = infinity, minLeft = 0, minRight = 0, currentMin = 0, left = 0, right = 0
for i = 0 to A.length - 1
currentMin += A[i]
if currentMin < minSum
minSum = currentMin
right = i;
minLeft = left
minRight = right
if currentMin > 0
@kartikkukreja
kartikkukreja / Maximum Subarray Problem.py
Last active August 29, 2015 14:04
Maximum Subarray Problem
let A be the given array of integers
let maxSum = -infinity, maxLeft = 0, maxRight = 0, currentMax = 0, left = 0, right = 0
for i = 0 to A.length - 1
currentMax += A[i]
if currentMax > maxSum
maxSum = currentMax
right = i;
maxLeft = left
maxRight = right
if currentMax < 0
@kartikkukreja
kartikkukreja / Topological Sorting.py
Last active August 29, 2015 14:04
Topological Sorting
Let L be an empty list
Let all vertices be initially unmarked
while there are unmarked vertices:
select an unmarked vertex u
dfs(u)
def dfs(vertex u):
mark u
foreach edge u -> v
@kartikkukreja
kartikkukreja / Multiway merge.py
Last active August 29, 2015 14:04
Multiway merge
Let H be a min heap of size m
add first element of each list to H
Let O be the output list
while !H.empty()
e = H.deleteMin()
add e to O
add to H the next element of the list e was taken from
return O
@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