Before the invention of computers, there were algorithms. Now computers are everywhere, so algorithms are everywhere! Algorithms lie at the heart of computing. If we observe our surroundings, we can find several algorithms working to solve our daily life problems. Social media Networks, GPS applications, Google search, e-commerce platforms, Netflix recommendation systems, etc. applications are powered by algorithms. Even it is also popular during the coding interview to get a high-paying job in the software industry. So learning algorithms is one of the critical career skills for programmers.
Introduction to algorithms
An algorithm is a step-by-step procedure to transform a given input to the desired output and solve a computational problem. In other words, an algorithm is a tool for solving a well-specified computational problem. (Think!)
The computational problem is a collection of questions that computers might be able to solve. For example, the problem of sorting — “Given a sequence of n integer, arrange it into increasing order.” is a computational problem.
Example: Linear search
Given an array A of n elements, write an algorithm to search a given element k in A. If the element is present then return the index of the element otherwise return -1 or false.
Extracting all relevant details
- Input size or total elements in the input = n
- Input data structure : Array or sequential memory
- Input data type : Integer which can be both positive or negative.
- Input distribution or constraint : There is no constraint is given in the input. All integers are stored in random order.
- Input : Array of integers and a value k
Output : If the value is present then return the index otherwise return -1
Input : X = [ 5, 8, - 3, - 4, 13, 9, - 17, 7, 12], k = 13
Output : 4, 13 is present at index 4 in X
Input : X = [ 5, 8, - 3, - 4, 13, 9, - 17, 7, 12], k = 11
Output : -1, 11 is not present in X
Linear search idea
The value k can be present at any index in the array because we don’t know the distribution of the input. So a simple strategy can be following —
- Start from the leftmost element of A and one by one compare k with each element of A.
- If k matches with an element, return the index.
- If k doesn’t match with any of the elements, return -1.
Linear search pseudocode
int linearSearch(int X, int n, int k)
for(int i = 0; i < n; i = i + 1)
if(X[i] == k)
Critical ideas to remember!
Always ask the following critical questions for every computational problem:
- What is the size of the input? like 10 or 1000 or 10 billion!
- What is the data type of the input? It can be, integer, floating-point number, character, string, or boolean!
- How our input values are stored? It can be stored in a data structure like an array, linked list, tree, graph, etc.
- Is there some information available for the distribution of the input? like values are stored in sorted order, the input is allowed in a certain range, some permutation of the input is allowed only, etc.
Properties of a good algorithm
A good algorithm must be finite, correct, efficient, and easy to implement.
- Input specified: Input must be clearly specified in an algorithm where we need to know all relevant details like input data type, input size, data structure, input distribution, etc. It can be a value or some set of values input.
- Output specified: Output must be clearly specified in an algorithm where we need to know all relevant details related to the output. It can be a value or some set of values input or there is no such value!
- Correctness: For every set of inputs our algorithm must halt with the correct output. In other words, our algorithm must be correct. The incorrect algorithm might halt with incorrect answers or not halt at the correct output for some input.
- Efficiency: A computer may be very fast but it can not be infinitely fast and similarly, a memory may be available easily but it is not for free. Both are bounded resources and our algorithms must use them wisely.
- Finiteness: Our algorithm must terminate after a finite set of instructions. We should always avoid infinite recursion or infinite loop condition.
- Simple and elegant: An algorithm should be easy to understand and implement
Why analysing efficiency is important?
Suppose computers were infinitely fast and computer memory was free. Would you have any reason to study algorithms? But the reality is: computers may be fast but they are not infinitely fast and memory may be inexpensive but it is not free. So, running time and space is an important resource for defining the performance of the computer program. (Think!)
In computer science, These things are as crucial as an algorithm’s performance: Correctness of the code, Functionality, User Friendliness, Modularity, Scalability, Security, Maintainability, Programmers time. But now the critical question is : why do we analyze the performance of an algorithm? Here are the few important reasons -
- The performance draws a line between feasible and infeasible.
- It provides a clean standard to think about the program or system behavior.
- Performance is just like money where we use it to pay for more functionality or user-friendliness. For example, we code in Java for the OOPS features, even though Java is approx 3 times slower than C. In other words, we are willing to pay the performance by a factor of 3 to get more functionalities.
- Speed is always fun and we humans love it!
Suppose we would like to run two different sorting algorithms on two different computers A and B, where computer B is 1000 times slower than computer A. For comparing the performances, we are running the slower sorting algorithm (Insertion sort) on faster computer A and running the faster sorting algorithm (Merge sort) on slower computer B.
What difference do we observe? Still, computer B is taking much less time than computer A, if the input size is large. This gap will increase further if we increase the input size. Great! This would be one of the reasons for learning algorithms and their efficiency (Think).
An algorithm is a technology!
There can be different solutions (or algorithms) for the same computational problem and these solutions may differ in terms of efficiency. These differences can be much more significant than differences due to hardware and software. So the system performance depends on choosing efficient algorithms as much as on choosing fast hardware. Even applications that do not require algorithm directly at the application level, relies heavily upon algorithms. For examples:
- Does the application require fast hardware? The hardware design used algorithms.
- Does the application depend upon the user interface? The design of the user interface relies on algorithms.
- Does the application rely on fast networking? Networking relies heavily on routing algorithms.
Overall, algorithms are at the core of most real-life applications. Just as rapid innovations are being made in other computer technologies, they are also being made in algorithms.
Real-life applications of algorithms and data structures
Application of array
- Arranging a particular type of data in a sequential arrangement: storing contacts on our phone, storing speech signals in speech processing, etc.
- Implementing of Stack and Queue, Adjacency matrix representation of Graphs, Implementing hash tables and heaps.
Application of linked list
- Singly-linked list: Maintaining a directory of names, Performing arithmetic operations on long integers, Manipulation of polynomials, Implementing of Stack and Queue, Adjacency list representation of Graphs, Hashing by chaining method, Dynamic memory allocation
- Doubly-linked list: Implementing LRU Cache, Implementing Fibonacci heap, Thread scheduler in operating systems, Representing deck of cards game, Implement undo and redo functionality in applications, Previous and next page in a web browser.
- Circular linked list: Processes scheduling in the operating system, Keep track of the turn in a multi-player game
Application of stack
- Storing browser history, UNDO/REDO options in a text editor
- Process scheduling, Static memory allocation
- Storing function calls in recursion
- In IDE or a compiler to know are missing braces
- Expression evaluation, Syntax Parsing
Application of queue
- Process scheduling in operating systems (CPU and IO scheduling)
- Interprocess communications
- Waiting list in Applications
- Customer care calls management
Application of hashing
- Accessing website using keywords in Search Engines
- Finding phone numbers in mobile devices, Employees Information System
- Spelling checkers in word processing software, Symbol table in a compiler
- Encrypting critical data(Cryptography)
Application of tree data structure
- Binary tree: Store hierarchical data, like folder structure, organization structure, File Systems in Linux, Document Object Model to represent documents with a logical tree
- Binary search tree: Used in applications where continuous insertion and deletion of data is happening, To implement map and set objects in languages' libraries.
- Trie data structure: Autocompletion in google search, spell-checking in word processors, Contact search on a phone book, swipe features in your mobile keypad, Network browser history of the URL's, Longest prefix matching used by routers in internet protocols, Predictive text technology (T9 dictionary in mobile phones)
- Heap data structure: Used in implementing efficient priority queues, scheduling processes in operating systems, Dynamic memory allocation, Order Statistics
- Advanced data structures: Compiler design for parsing of the syntax (Syntax Tree), Wireless networking and a memory allocation (Treap), Range query processing on a data set (Segment Tree or Binary Index Tree), finding the nearest neighbor to a particular point (K Dimensional Tree), To store data on the drive (B-tree), 3D computer graphics(BSP tree), Fast full-text search in word processors (Suffix tree)
Application of graph data structure
- Shortest path algorithm: Network routing protocols, Google maps, Shipment routing in e-commerce, Robot path planning
- Breadth-first search (BFS): Social network websites to find people with a given distance k from a person, Web crawling in a google search (BFS), Finding all neighbor nodes in the peer to peer network like BitTorrent (BFS), Network Broadcasting, Finding neighboring places via GPS navigation
- Depth-first search (DFS): Detecting cycle in a graph, Bipartiteness test in a graph, Finding strongly connected components, Solving maze puzzles, Topological sorting, Path-finding
- Topological sorting: Used in many different scenarios that involve scheduling several tasks with inter-dependencies. For example, 1) Generates a dependency graph of the dependent modules in IDE Build-Systems 2) Determining order to create complex database tables with interdependencies 3) Determining order to take courses and their pre-requisites to complete a degree.
- Other important applications: Assigning fastest pick-ups to Uber drivers (Hungarian algorithm), Facebook’s Friend Suggestion algorithm, Google Page Ranking algorithm where web pages are considered to be the vertices, Resource Allocation Graph (Operating System), Transaction graphs in cryptocurrency (Blockchain, which is a large graph), Artificial neural networks, Facebook Graph Search, Google Knowledge Graph, Product Recommendation Graphs (Amazon Recommendation System)
Application of dynamic programming
- Sequence Alignment, Document diffing Algorithms, Document Distance Algorithm (Edit Distance), Plagiarism Detection, a Typesetting system
- Duckworth Lewis Method in cricket, Flight control
- Speech recognition, Image processing, Machine learning algorithms
- Economics, Financial Trading, Bioinformatics, Operations research
Application of greedy algorithms
- Loss-less data compression of .png and .mp3 file-formats (Huffman Coding)
- Shortest Path Algorithms (Dijkstra Algorithms), Minimum spanning tree (Kruskal and Prim's algorithms), Approximation algorithms for NP-hard problems
- Solving activity selection and other optimization problems
Application of backtracking
- Solving famous puzzles like N-queens, crosswords, verbal arithmetic, Sudoku
- Solving various optimization and constraint satisfaction problem
Application of number theory
- Cryptography and Computer graphics
- Designing Hash functions and Random number generators
- Fast arithmetic operations
Other applications of algorithms and Data Structures
- Load Balancing (Randomised Algorithms)
- Exact String Matching (KMP Algorithms)
- Image editing software like photoshop (Convex-hull algorithms)
- Fast search in a sorted dataset (Binary Search)
- Filter out stories that people have seen before (Quora uses bloom filter for this)
- Breaking down signals into frequencies (Fast Fourier Transform)
- Block-sorting compression (Suffix Array)
Coding problems to warm-up in algorithms
You can find these problems on the Internet.
- Reverse an array, Find the transpose of a matrix
- Bubble sort, Selection sort, Insertion sort
- Convert Roman to Integer, Find GCD of two numbers
- Given a number n, check if it is prime or not
- Check if a given string is a palindrome
- Print a given matrix in spiral form
- Find the largest element in an array
- Algorithms by CLRS
- The Algorithm Design Manual by Steven Skiena
Enjoy learning, Enjoy coding, Enjoy algorithms!