Greatest Common Denominator
def computeGCD(x, y): 
   while(y): 
       x, y = y, x % y 
   return x 
Fibbonaci w/ Dynamic Programming
# DP w/ memoization (starts from 0 to n rather than n to 0)
def fib(n):
   fibValues = [0,1]
   for i in range(2, n+1):
      fibValues.append(fibValues[i-1] + fibValues[i-2])
   return fibValues[n]
def bfs(start):
    queue = []
    visited = []

    queue.append(start)
    visited.append(start)

    while len(queue) > 0:
        cur = queue.pop(0)
        visited.append(cur)
        if child not in visited:
            visited.append(child)
            for child in cur.children:
                queue.append(child)
    return visited
def dfs(start):
    stack = []
    visited = []
    stack.append(s)
    visited.append(s)
    while len(stack) > 0:
        cur = stack.pop(-1)
        if child not in visited:
            visited.append(child)
            for child in cur.children[::-1]:
                stack.insert(0, child)
    return visited

def dfsRecursive(start):
    visit(start)
    for child in start.children:
        bfsRecursive(start)

Dijkstra’s Algorithm and A-Star

def dijkstras(G, s):
    queue = prioQueue()
    dist = {}
    prev = {}
    for v in G:
        dist[v] = float('inf')
        prev[v] = None
    dist[s] = 0
    while !queue.isEmpty():
        node = queue.pop() #Gets node with smallest dist[n]
        for n in node.neighbors():
            alt = dist[node] + length(node, n)
            if alt < dist[n]:
                dist[n] = alt
                prev[n] = node
    return dist, prev
def aStar(G, s):
    queue = prioQueue()
    dist = {}
    prev = {}
    for v in G:
        dist[v] = float('inf')
        prev[v] = None
    dist[s] = 0
    while !queue.isEmpty():
        node = queue.pop() #Gets node with smallest fCost = dist[n] + h(node)
        for n in node.neighbors():
            alt = dist[node] + length(node, n)
            if alt < dist[n]:
                dist[n] = alt
                prev[n] = node
    return dist, prev

Greedy Algorithms