![]() |
VOOZH | about |
An algorithm is a step-by-step procedure or set of rules to solve a specific problem. It is a logical sequence of instructions designed to achieve a desired output for given inputs.
Analysis of Algorithm
1) Worst Case Analysis (Usually Done): In the worst case analysis, we calculate upper bound on running time of an algorithm by considering worst case (a situation where algorithm takes maximum time)
2) Average Case Analysis (Sometimes done): In average case analysis, we take all possible inputs and calculate computing time for all of the inputs.
3)Best Case Analysis (Bogus): In the best case analysis, we calculate lower bound on running time of an algorithm.
Table of Content
Θ((g(n)) = {f(n): there exist positive constants c1, c2 and n0 such that
0 <= c1*g(n) <= f(n) <= c2*g(n) for all n >= n0}
O(g(n)) = { f(n): there exist positive constants c and n0 such that
0 <= f(n) <= cg(n) for all n >= n0}
Ω (g(n)) = {f(n): there exist positive constants c and n0 such that
0 <= cg(n) <= f(n) for all n >= n0}.
read more about - Asymptotic Notations
Solving recurrences
T(n) = aT(n/b) + f(n) where a >= 1 and b > 1
| Algorithm | Worst | |||
| Θ(n2) | Θ(n2) | Θ(n) | 0 | Θ(n2) |
| Θ(n2) | Θ(n2) | Θ(n2) | 0 | Θ(n) |
| Θ(n2) | Θ(n2) | Θ(n) | 0 | Θ(n2) |
| Θ(n2) | Θ(nlgn) | Θ(nlgn) | 0 | Θ(n2) |
| Θ(nlgn) | Θ(nlgn) | Θ(nlgn) | Is not in-place sorting | Is not in-place sorting |
| Θ(nlgn) | Θ(nlgn) | Θ(nlgn) | O(nlgn) | Θ(nlgn) |
| Algorithm | Worst Case | Average Case | Best Case |
| Θ(n) | Θ(n) | Θ(1) | |
| O (logn) | O (logn) | O (1) |
Unlike Arrays, Linked Lists, Stack and queues, which are linear data structures, trees are hierarchical data structures. Depth First
Important Tree Properties and Formulas
(a) Inorder
(b) Preorder
(c) Postorder
Binary Search Tree, is a node-based binary tree data structure which has the following properties:
AVL tree is a self-balancing Binary Search Tree (BST) where the difference between heights of left and right subtrees cannot be more than one for all nodes.
B-Tree is a self-balancing search tree. In most of the other self-balancing search trees (like B-Tree and Red Black Trees), it is assumed that everything is in main memory. To understand use of B-Trees, we must think of huge amount of data that cannot fit in main memory. When the number of keys is high, the data is read from disk in the form of blocks. Disk access time is very high compared to main memory access time. The main idea of using B-Trees is to reduce the number of disk accesses.
Properties of Prim’s Minimum Spanning Tree Algorithm,
Minimum Spanning Tree (MST) problem: Given connected graph G with positive edge weights, find a min weight set of edges that connects all of the vertices. MST is fundamental problem with diverse applications.
Example: Kruskal’s Minimum Spanning Tree Algorithm
Graph is a data structure that consists of following two components:
1. A finite set of vertices also called as nodes.
2. A finite set of ordered pair of the form (u, v) called as edge. The pair is ordered because (u, v) is not same as (v, u) in case of directed graph(di-graph). The pair of form (u, v) indicates that there is an edge from vertex u to vertex v. The edges may contain weight/value/cost. Following two are the most commonly used representations of graph.
Graph Algorithms
| Algorithm | Time Complexity |
| Breadth First Traversal for a Graph | O(V+E) for adjacency list representation and O(V * V) for adjacency matrix representation. |
| Depth First Traversal for a Graph | O(V+E) for adjacency list representation and O(V * V) for adjacency matrix representation. |
| Dijkstra’s shortest path algorithm | Adjacency matrix- O(V^2). Adjacency list- O(E log V) |
| Topological Sorting: Shortest Path in Directed Acyclic Graph | O(V+E) |
Some Interesting Graph Questions
If a problem of size n is divided into k subproblems, each of size n/m, and the merging step takes O(f(n)), the time complexity is given by the Master Theorem:
T(n) = kT(n/m) + O(f(n))
Following are some standard algorithms that are Divide and Conquer algorithms.
Binary Search is a searching algorithm. This algorithm include following steps:
Time Complexity: O(log n)
It is a sorting algorithm. The algorithm picks a pivot element, rearranges the array elements in such a way that all elements smaller than the picked pivot element move to left side of pivot, and all greater elements move to right side. Finally, the algorithm recursively sorts the subarrays on left and right of pivot element.
Time Complexity: Best and Average Case: O(n log n), Worst Case: O(n²) (when the pivot is poorly chosen)
It is also a sorting algorithm. The algorithm divides the array in two halves, recursively sorts them and finally merges the two sorted halves.
Time Complexity: Worst, Average, and Best Case: O(n log n)
The problem is to find the closest pair of points in a set of points in x-y plane. The problem can be solved in O(n^2) time by calculating distances of every pair of points and comparing the distances to find the minimum. The Divide and Conquer algorithm solves the problem in O(nLogn) time.
Greedy is an algorithmic paradigm that builds up a solution piece by piece, always choosing the next piece that offers the most obvious and immediate benefit. Greedy algorithms are used for optimization problems. An optimization problem can be solved using Greedy if the problem has the following property:
At every step, we can make a choice that looks best at the moment, and we get the optimal solution of the complete problem.
Following are some standard algorithms that are Greedy algorithms:
In Kruskal’s algorithm, we create a MST by picking edges one by one. The Greedy Choice is to pick the smallest weight edge that doesn’t cause a cycle in the MST constructed so far.
Time Complexity: O(E log V).
In Prim’s algorithm also, we create a MST by picking edges one by one. We maintain two sets: set of the vertices already included in MST and the set of the vertices not yet included. The Greedy Choice is to pick the smallest weight edge that connects the two sets.
Time Complexity: O(E log V).
The Dijkstra’s algorithm is very similar to Prim’s algorithm. The shortest path tree is built up, edge by edge. We maintain two sets: set of the vertices already included in the tree and the set of the vertices not yet included. The Greedy Choice is to pick the edge that connects the two sets and is on the smallest weight path from source to the set that contains not yet included vertices.
Time Complexity: O((V + E) log V).
Huffman Coding is a loss-less compression technique. It assigns variable length bit codes to different characters. The Greedy Choice is to assign least bit length code to the most frequent character.
Time Complexity:O(n log n).
read more about - Greedy Approach
Dynamic Programming (DP) is a problem-solving approach that breaks a problem into smaller overlapping subproblems, solves each subproblem once, and stores its solution to avoid redundant computations. This technique is especially useful for optimization problems
F(n) = F(n-1) + F(n-2)LCS(X, Y, i, j) = 1 + LCS(X, Y, i-1, j-1) if X[i] == Y[j]. Otherwise, LCS(X, Y, i, j) = max(LCS(X, Y, i-1, j), LCS(X, Y, i, j-1))m and n.K(i, W) = max(K(i-1, W), K(i-1, W-w[i]) + v[i]). If item i is included, add its value v[i] and reduce capacity W by its weight w[i]. Otherwise, skip the item.n is the number of items and W is the knapsack capacity.M(i, j) = min(M(i, k) + M(k+1, j) + p[i-1]*p[k]*p[j]) for all k from i to j-1read more about - Dynamic Programming
Backtracking is a general algorithmic technique that involves exploring all possible solutions to a problem by building a solution incrementally and abandoning solutions that fail to satisfy the constraints. It is often used for solving problems that require finding combinations, permutations, or subsets.
N queens on an N x N chessboard so that no two queens threaten each other.read more about - Backtracking