Skip to content

Instantly share code, notes, and snippets.

View gcandal's full-sized avatar
🧨
Dynamiting

Gabriel Candal gcandal

🧨
Dynamiting
View GitHub Profile
@gcandal
gcandal / percolation.java
Last active August 29, 2015 14:15
Percolation Problem
public class Percolation {
private WeightedQuickUnionUF unionUF, unionUFPerc;
private int N;
private boolean[][] open;
// create N-by-N grid, with all sites blocked
public Percolation(int N)
{
if (N <= 0)
throw new IllegalArgumentException();
@gcandal
gcandal / scc.py
Last active August 29, 2015 14:15
Kosaraju's SCC
'''
Gabriel C. 2015
Kosaraju's algorithm for strongly connected components in a DAG
'''
import sys
import resource
sys.setrecursionlimit(10 ** 6)
@gcandal
gcandal / mincut.py
Last active August 29, 2015 14:14
mincut
'''
Gabriel C. 2015
Karger's minimum-cut algorithm on a list of edges
(definitely not the best data structure choice)
'''
import random
def contract(edges, u, v, nodes):
@gcandal
gcandal / quicksort.py
Last active August 29, 2015 14:14
Quicksort
'''
Gabriel C. 2015
Quicksort with 3 different pivot-choosing methods
'''
def quicksort(array, method):
return quicksort_aux(array, 0, len(array) - 1, method)
def swap(array, a, b):
@gcandal
gcandal / mergesort.py
Last active August 29, 2015 14:14
Count Inversions + Mergesort
'''
Gabriel C. 2015
Counting inversions in an array
using merge sort with an auxiliary array
'''
def count_inversions_and_sort(array):
if len(array) == 1:
return 0;