This repository, I will use Python to implement some famous algorithms. The algorithms are arranged according to the strategy used.
Each algorithm will have an explanation to the problem it attempts to solves and some relevant resources.
(The goal of this repository is to create a beautiful README for all of these brilliant algorithms that I have reviewed.)
Content:
This section, I will talk about the famous divide and conquer strategy and show some applications of this strategy.

The standard procedure for multiplication of two n-digit numbers requires a number of elementary operations proportional to . As for The Karatsuba algorithm, it reduces the running time to at most
Key idea
The basic step of Karatsuba's algorithm is a formula that allows one to compute the product of two large numbers and using three multiplications of smaller numbers, each with about half as many digits as or , plus some additions and digit shifts.
Properties
Back to Top
The worst running time for this algorithm is .
Gif above shows how merge sort works:

Key idea
Divide the unsorted list into n sublists, each containing 1 element (a list of 1 element is considered sorted) and repeatedly merge sublists to produce new sorted sublists until there is only 1 sublist remaining. This will be the sorted list.
Properties
Back to Top
Key idea
Like the figure above, when we first take in element from right sub-array in merge operation, that indicates the right element is smaller than ( length of left sub-array - the index of left element) elements.
As the algorithm progresses, add all the inversions will give us the total inversions.
Properties
Back to Top
Properties
Back to TopThis section I will talk about two algorithms which has used random variable inside.

Key idea
Quicksort first divides a large array into two smaller sub-arrays: the low elements and the high elements relative to a randomly chosen element. Quicksort can then recursively sort the sub-arrays. So, the key point in quick sort is to choose partition element.
Properties
Back to Top
Key idea
By repeating the contraction algorithm times with independent random choices and returning the smallest cut, the probability of not finding a minimum cut is
Properties
Back to TopTo place data structures as an independent section is misleading; however, I will introduce perplexing problems which are elegantly solved by data structures. Some data structures may have an algorithm design strategy that have not been reviewed yet.

Key idea
BFS is used to count reachable nodes from a source node in a undirected or directed graph. Reachable nodes are printed in the order found. A queue is used to store the nodes colored grey (see the gif below). The term "breadth" in BFS means finding a reachable node with the shortest length. The breadth extends the border between the visited nodes and the undiscovered nodes.

Properties
Back to Top
Key idea
And DFS is used in directed graph and it tells how many nodes a source node can reach and print them out by the order we find them. We use stack to store the nodes we classify as the start points for graph. The "depth" in its name means that this algorithm will go as deeply as it can for a given source and when it reaches the endpoint, it returns to the start node.

Properties
Back to Top
Median Maintenance problem is that if integers are read from a data stream, find median of elements read so for in efficient way. For simplicity assume there are no duplicates.
Key idea
We can use a max heap on left side to represent elements that are less than effective median, and a min heap on right side to represent elements that are greater than effective median. When the difference between size of two heaps is greater or equal to 2, we switch one element to another smaller size heap.
Properties
Back to Top
Key idea
Through simple observation, we find out that tranpose of graph has the same SCCs as the original graph. We run DFS twice. First time, we run it on G and compute finishing time for each vertex. And then, we run DFS on G^T but in the main loop of DFS, consider the vertices in order of decreasing finishing time.
Properties
Back to Top
Key idea
In an undirected graph, we can use this data structure to find out how many SCCs. The figure below shows how it works.

Properties
Back to TopIn this section, I'm going to introducce greedy algorithms, one powerful algorithm design strategy.
From Wikipedia, a greedy algorithm is an algorithmic paradigm that follows the problem solving heuristic of making the locally optimal choice at each stage[1] with the hope of finding a global optimum. In many problems, a greedy strategy does not in general produce an optimal solution, but nonetheless a greedy heuristic may yield locally optimal solutions that approximate a global optimal solution in a reasonable time.
In activity selection problem, every activity has its own weight and length. And our goal is to minimize the sum of weight*length.
It is a very easy and great example to show how greedy algorithm works and provide an elegant proof using argument exchange technique.
Key idea
If we sort activity by value weight/length, we can prove an existing optimal structure cannot be better than this arrangement.

As the figure shown above, we consider the cost caused by two activities that are ranged differently in two arrangement (i, j). We find out that the cost in greedy algorithm is smaller than optimal structure by the value of wi*lj - wj*li, which is greater than or equal to 0.
Properties
Back to Top
Key idea
We sorted the array according to its finish time.
The algorithm put the first job whose start time is bigger than last job's finish time.
Properties
Back to Top
One way to encode this book is to use fixed length coding. As shown below:

As for huffman coding, the actual tree structure looks like this:

Key idea
We maintain a binary tree and create a new node as the parent for two least-frequent letters. And the key for this new node is the sum of keys for its two children. We repeat this until no nodes left in this "book".

Properties
Back to TopDijkstra's algorithm is an algorithm for finding the shortest paths between nodes in a graph. However, it has one prerequisite, all paths have to be greater or equal to 0.

Key idea
Separate nodes into two groups, one group is marked as explored. And we update the distance from unexplored group to explored group by the shortest distance.
Properties
Back to Top
Key idea
The algorithm may informally be described as performing the following steps:
Initialize a tree with a single vertex, chosen arbitrarily from the graph.
Grow the tree by one edge: of the edges that connect the tree to vertices not yet in the tree, find the minimum-weight edge, and transfer it to the tree.
Repeat step 2 (until all vertices are in the tree).
Properties
Back to Top
Key idea
Very similar to SCC, we can early stop the alogrithm to control number of classes in our graph, which is to say we can cluster the graph.
Properties
Back to TopIn this section, I'm going to introduce dynamic algorithms, one powerful algorithm design strategy.
From Wikipedia, dynamic programming (also known as dynamic optimization) is a method for solving a complex problem by breaking it down into a collection of simpler subproblems, solving each of those subproblems just once, and storing their solutions.

So, if length of the rod is 8 and the values of different pieces are given as following, then the maximum obtainable value is 22 (by cutting in two pieces of lengths 2 and 6).
Key idea
We view a decomposition as consisting of a first piece of length i cut off the left-hand end, and then a right-hand remainder of length n - i.
So, the pseudocode looks like:

The recursion tree showing recursive calls resulting from a call CUT_ROD(p, n) looks like:

In order to save the repeated computation for small sub-problems, we memorized an array to store these values.
Properties
Back to Top
Key idea
Optimal structure:

Properties
Back to TopKey idea
From CLRS, the optimal structure for this problem is:

Properties
Back to Top
Key idea
This algorithm is based on very intuitive observation. Suppose we have a subset {1, 2, 3, 4, ..., k} (denote as V(k)) of original vertices group {1, 2, 3, ..., n}. If p is a shortest path from i to j in V(k), we have two cases. First, if k is not in p, then p must be a shortest path in V(k-1). Second, if k is in p, then we can seperate the path into two, P1: ik, P2:kj. and P1 must be the shortest path from i to k with V(k-1), P2 must be the shortest path from k to j with V(k-1).

Properties
Back to TopFrom From Wikipedia, an NP-complete decision problem is one belonging to both the NP and the NP-hard complexity classes. In this context, NP stands for "nondeterministic polynomial time". The set of NP-complete problems is often denoted by NP-C or NPC.
In this section, I am going to introduce three very famous NPC problems and explain approximation algorithms to approach them.

Key idea
It is very difficult to find a minimum vertex cover but we can find a approximate cover with at most twice the vertices in polynomial time.

Properties
Back to Top
Key idea
The approximation algorithm for tsp is a greedy algorithm (CLRS P1114). Here, I also want to introduce an exact algorithm for tsp using dynamic programming.
Subproblem: for every destination j ∈ {1,2,...,n}, every subset S ⊆ {1,2,...,n} that contains 1 and j, let L S,j = minimum length of a path from 1 to j that visits precisely the vertices of S [exactly once each]. So, Corresponding recurrence:

Properties
Back to Top3-SAT is one of Karp's 21 NP-complete problems, and it is used as a starting point for proving that other problems are also NP-hard.
Herein, I introduce Papadimitriou’s Algorithm for 2-SAT (This is a very very very interesting algorithm , so I decide to introduce it even though 2-SAT is not NPC).
Key idea
Choose random initial assignment and pick arbitrary unsatisfied clause and flip the value of one of its variable.
Here is the pseudocode:

Such a casual dealing with clauses would suprisingly give us a very large probability to find the real answer. The mechanism lies in a physics term (random walk). If you are interested, I strongly recommend you to go through how random walk related to Papadimitriou.
Properties
Back to Top