Algorithms studied [00OY]
Algorithm. Incrementing a binary counter [00SY]
Here is an algorithm to solve the binary counter problem.
Algorithm. Ford-Fulkerson [00SE]
Algorithm. Greedy Flow [00S4]
Algorithm. Floyd-Warshall [00RT]
After the algorithm is finished, to reconstruct the shortest path from u to v, we simply look up each step along the path using the \mathit {next} matrix. First we look up \mathit {next}[u,v] = u_2, then we look up \mathit {next}[u_2,v] = u_3, and so on, until reaching v.
Algorithm. Quickselect [00QD]
\mathsf {Partition}(A) picks a random pivot value in A, and returns two lists: one containing all the values in A which are less than the pivot, and one containing all values greater than or equal to the pivot.
Note that in practice, one would implement this so it all works in-place on the array A, instead of generating new arrays A_1 and A_2, e.g. by passing along extra \mathit {hi} and \mathit {lo} parameters to specify which part of the array to focus on.
Algorithm. Karatsuba's Algorithm [00Q8]
Now, back to n-bit integer multiplication! In 1960 it seemed “obvious” that integer multiplication was \Omega (n^2); Andrey Kolmogorov (a very prominent mathematician at the time) posed it as a conjecture. At a special seminar organized by Kolmogorov to investigate this conjecture and other related problems, a previously unknown 23-year-old graduate student, Anatoly Karatsuba, disproved the conjecture by coming up with a faster algorithm!
Break up A and B into two pieces of size n/2 as before: A = A_1 2^{n/2} + A_2 and B = B_1 2^{n/2} + B_2. Now for Karatsuba’s clever insight. Define \begin {align*} P_1 &= A_1 B_1 \\ P_2 &= A_2 B_2 \\ P_3 &= (A_1 + A_2)(B_1 + B_2) \end {align*} Note P_3 - P_1 - P_2 = A_1B_2 + A_2 B_1. Hence AB = P_1 2^n + (P_3 - P_1 - P_2) 2^{n/2} + P_2. This requires only three multiplications of size n/2! (Along with two additions of size n/2, four additions or subtractions of size n, and two shifts—more work than before, but all still \Theta (n) in total.)
Hence T(n) = 3T(n/2) + \Theta (n), so we can apply the Master Theorem with a = 3, b = 2, and d = 1. 3 > 2^1 so we are in the third case of the theorem (the work is concentrated at the bottom of the call tree), and we conclude that this algorithm is O(n^{\log _2 3}) \approx O(n^{1.59}).
This is not even the fastest known algorithm—for example, the Toom-Cook algorithm is a generalization of Karatsuba's algorithm which splits up the numbers into more than two pieces; and the fastest algorithm actually used in practice is the Schönhage-Strassen algorithm, which is based on different ideas (the Fast Fourier Transform) can multiply two n-bit integers in O(n \log n \log \log n). In 2019 it was even proved that it is possible to multiply two n-bit integers in \Theta (n \lg n) time, although the algorithm involved would not be faster in practice for any numbers that fit in our universe!
Algorithm. Mergesort [00PP]
Algorithm. Kruskal's Algorithm (with union-find) [00P3]
Given a union-find data structure, we can implement Kruskal's Algorithm as follows:
Algorithm. Kruskal's Algorithm (pseudocode) [00OX]
Algorithm. Dijkstra's Algorithm [00O4]
Here is the updated version of Dijkstra's Algorithm using a priority queue for tracking unvisited vertices.
Algorithm. Dijkstra's Algorithm (basic version) [00NY]
Here’s the basic algorithm, which we can think of as a generalization of BFS to weighted graphs. Recall that BFS keeps track of:
- which vertices have been visited so far,
- a layer map, giving the layer of each visited vertex, and
- a parent map recording the parent of each vertex in the BFS tree.
Algorithm. Kahn's Algorithm [00NI]
The resulting algorithm is known as Kahn’s Algorithm, and was first published by Arthur Kahn in 1962.
Algorithm. BFS implementation [00MP]
The parent and layer maps are optional, and can be used to find actual shortest paths and the shortest distance from s to each vertex, respectively.
Algorithm. DFS (version 2, with parent tree) [00M9]
What if we want to find an explicit path from s to t? As it stands, we can't: we need more information. In particular, we need to know how we got to each vertex. We can add a parent dictionary which stores the parent of each visited vertex. Every time we visit a vertex, we record its parent, i.e. the neighbor we "came from".
Note that the resulting \mathit {parent} dictionary records a tree, representing the paths we took while exploring the graph. However, instead of the each node recording its children, each node simply records its parent. So we can easily move up the tree, but not down.
That's OK though—moving up is all we need! In order to find a path from s to t (in reverse order) we can look up \mathit {parent}[t], \mathit {parent}[\mathit {parent}[t]], and so on.
Algorithm. DFS (version 1) [00M8]
Notice that we implement DFS via recursion. Perhaps you have also seen a version that uses an explicit stack data structure. By using recursion, we essentially get our programming language's runtime system to keep track of the stack for us. Maintaining a stack ourselves can get annoying and messy, especially if we want to create any extra local variables during the DFS. However, a word of caution: using recursion in this way does not work so well in certain languages (where by "certain languages" I mean "Python"), especially if the graph is large.
To see if vertices s and t are connected, we can just run \mathsf {DFS}(G, s) and see if t is marked visited. Optionally, we can also stop early as soon as we find t during the DFS (left as an exercise).
Algorithm. Iterative Euclidean Algorithm [00K5]
The second implementation of the Euclidean Algorithm is iterative:
Algorithm. Recursive Euclidean Algorithm [00K4]
The first implementation of the Euclidean Algorithm is recursive: