Tag algorithms

Generating All the Catalan Sequences

Given \(N\), how do I generate all the valid push/pop sequences (or draw all the trees)? I’ll generate the sequence of 1’s and 0’s....

Equivalence of The Number of Valid Push/Pop Sequences and the number of Binary Trees

We showed that the number of binary trees with \(N\) nodes is the Catalan numbers. It is also the number of valid push/pop sequences for...

What Fraction of Push/Pop Sequences in a Stack Are Valid?

Say we have the numbers from 1 to \(N\) in sequence. We will do a random sequence of pushes and pops into a stack: \(N\) pushes and...

Fibonacci Numbers

How fast is the iterative version of the Fibonacci algorithm? You’d think it is \(O(n)\), but it’s really \(O(n^{2})\), because for...

Tips For Backtracking

In backtracking algorithms, you make a sequence of decisions. Every recursive call will make one decision, and will take as an input...

Subset Sum

Given a set \(X\) of positive integers, can you find a subset that sums to \(T\)? The general idea is that you have two choices: Either...

Game Trees

Suppose you have a game on an \(n\times n\) board with some rules and players take turns. Assume no randomness in the game. For most...

Russian Peasant Multiplication

Let \(x,y\) be two non-negative integers. We want to compute \(p\), the product: Set p=0 While x > 0: if x is odd: p = p + y x = x/2...

Burrows-Wheeler Transform

The method: Given a string of, say, length 8, form all 8 rotations of it, and then sort them lexicographically. Then form the 8 letter...

LZW Compression

The idea: First assign a number to all the letters of the alphabet. Say A is 41, B is 42, etc. Now let’s say we’re compressing the...

Huffman Encoding

The problem with run length encoding was that our chunk size (e.g. 4 bits) is fixed. This is nonoptimal. With Huffman encoding, we can...

Run Length Encoding

We have a large string (e.g. text file). Usually, ASCII is not the optimal space allocation for the file. There are various schemes for...

Substring Matching

We have a string of length \(N\), and wish to find an \(M\) character string in it. Brute Force Keep moving through the string till the...

Tries

An R-way trie is a tree where each node has \(R\) children. It’s best described by an example: Each node consists only of two...

Symbol Tables

A symbol table is an associative array. It’s a good idea to make the keys orderable for performance reasons (it also opens up more...

Bloom Filters

Bloom filters are an application of hashing. It is a data structure that allows us to check if an object has already been seen (without...

Hash Functions

We often want to store an object for quick retrieval, or see if a given object has been observed before in \(O(1)\) time. Let \(U\) be...

Suffix Arrays

A suffix array is an array formed by taking a string, and storing pointers to all the suffixes in the array. As an example, if the...

Radix Sorts

Least Significant Digit Suppose we have \(N\) strings of length \(W\). The idea behind the LSD sort is to first sort the last “digit”...

Counting Sort

Counting Sort Suppose we have \(N\) elements, but we know the elements are drawn from a fixed, finite list (called the alphabet in this...

Max Flows and Min Cuts

Mincut Problem An s-t cut is a cut with \(s\) in one set and \(t\) in the other. Call the sets \(A\) and \(B\). Given a digraph, with...

Shortest Paths Summary

There are many “kinds” of shortest paths. Algorithm Constraint Typical Worst Case Topological Sort No directed cycles \(N+M\) \(N+M\)...

Negative Cycles

A negative cycle is a directed cycle where the sum of the edge weights is negative. A shortest paths tree exists if and only if there...

Shortest Paths

Given a digraph, we may wish to find the shortest distance between two nodes, or between a node and all other nodes, etc. Each edge has...

Minimum Spanning Trees

Given a connected graph \(G\), with positive edge weights, the spanning tree is a subgraph \(T\) that is both a tree (connected and...

Finding the Strongly Connected Components of a Digraph

Applications When looking at code/library dependencies, one may want to see which code pieces form strongly connected components, and...

Topological Sort

Topological Sort A topological sort is a traversal of a digraph with ordering constraints. Let each node have some value/weight. It is a...

Minimum Cut of a Graph

A cut of a graph is a partitioning of the nodes into two nonempty sets. A minimum cut is a cut that minimizes the number of edges...

Karatsuba Multiplication

In grade school we learn an algorithm for multiplying two \(N\) digit integers. Let our basic operation to be the multiplication or...

Rectangle Intersection

Given \(N\) rectangles, find all the intersections. This algorithm is very similar to the 1-D line intersection search. The difference...

1-D Interval Intersection

Suppose we have \(N\) 1-D intervals of the form \((a,b)\) where \(a

2-D Range Search, Points in a Box Search and K-D Trees

This is like the 1-D range search, but where the keys can now have two coordinates. It’s finding the number of points in a rectangle...

1-D Range Search and Line Intersection Search

Sample problems we want to solve: What are all the keys between two keys? (Relevant to databases) How many keys exist between two keys?...

Red Black Trees

We introduced 2-3 trees with highly desirable properties. Here we’ll convert them into binary trees while maintaining all the...

2-3 Trees

A 2-node is like a regular node in a binary search tree: It has one key and two children. A 3-node has 2 keys and 3 children. The middle...

Binary Search Trees

A binary search tree is a fairly useful structure for storing ordered data. The invariant for a binary search tree is straightforward:...

Heap Sort

One way to sort a list is to use the heap data structure. Given an array of \(N\) unsorted elements, build a max heap in place. Start...

Binary Heaps & Priority Queues

At times we want a structure similar to a queue or stack, but instead of LIFO or FIFO, we want it to pop the maximum element each time....

K-th Largest Element

Sometimes we wish to find the k-th largest element in a list. Brute Force A brute force way to do this would be to sort the list and...

Quick Sort

A quick sort works recursively: Shuffle the list. This protects against a carefully crafted input that will make the algorithm...

Convex Hull Problem

Say you have \(N\) points on a Euclidean plane. You want to find the polygon with the smallest area that can be formed connecting a...

Sorting Complexity Analysis

Any comparison based sorting algorithm must use at least \(\lg(N!)\sim N\lg N\) comparisons for the worst case. This is absent any...

Finding the Closest Pairs in a Plane

Suppose we have \(N\) points in a plane, and we wish to find the closest pair. The brute force algorithm takes \(\Theta(N^{2})\). We can...

Counting Inversions

Why would we want to count the number of inversions? One application may be to see how close two different people’s rankings of the same...

Merge Sort

A merge sort works recursively: Split the list into two roughly equal halves. Sort each half using mergesort. Merge the two...

Shuffling Caveats

When using a random number generator for shuffling, be wary of simple issues. Using cards as an example: If the seed is 32 bits, you’ll...

Shell Sort

A shell sort works somewhat recursively. It first sorts every \(k\) elements (i.e. the sublist formed by elements 0, \(k\), \(2k\),...

Insertion Sort

An insertion sort starts from the first element in the list, and then for each element will check with the previous one to see if it is...

Inversions & Partial Sortedness

An inversion in a list of elements is a pair of elements where the 2nd element is smaller than the first (i.e. the 2nd element would be...

Selection Sort

A selection sort starts out by traversing the whole list, finding the minimal element, and swapping it with the first element. It then...

Loop Invariants

A loop invariant is a statement that holds true just before and after each iteration. It may be violated within the iteration, but the...

Graphs Using NetworkX

NetworkX is a Python library for handling graphs. In [1]: %matplotlib inline In [14]: import networkx as nx import pylab as plt In [3]:...

Breadth First Search And Finding the Distance Between Two Nodes

We wish to find the distance between two nodes \(a\) and \(b\). Let’s generalize this further and find the distance from \(a\) to all...

Depth First Search

The depth first search algorithm is a way to traverse all the nodes in a graph. The algorithm is trivial: Start at a given node. Mark it...

Queues

A queue is a data structure for holding objects. It is a FIFO structure. It supports two operations: enqueue and dequeue dequeue returns...

Graphs

An undirected graph is defined by \((V, E)\) where \(V\) is a set of nodes and \(E\) is a set of edges (each edge is an unordered set of...

Stacks

A stack is a data structure for holding objects. It is a LIFO structure. It supports two operations: pop and push. pop returns (and...

2-Sum and 3-Sum

2-Sum Problem Statement Given a set of \(N\) integers, find how many pairs sum to 0. Brute Force Sum all pairs and count whichever sum...

Percolation: An Application of Union Find

Consider an \(N\times N\) square grid, where all the grids are black. Now let each square in the grid be white with probability \(p\)....

Union-Find Algorithm

Dynamic Connectivity & Disjoint Sets Given \(N\) nodes, some connected amongst themselves, does a path exist between any two...

Asymptotic Notation

An asymptotically positive function \(f(n)\) is one that is always positive for sufficiently large \(n\). A similar definition holds for...