# Algorithms Tutorial

** Algorithm **is a step-by-step procedure for solving a problem or accomplishing a task. In the context of data structures and algorithms, it is a set of well-defined instructions for performing a specific computational task. Algorithms are fundamental to computer science and play a very important role in designing efficient solutions for various problems. Understanding algorithms is essential for anyone interested in mastering data structures and algorithms.

Table of Content

## What is an Algorithm?

An ** algorithm **is a finite sequence of well-defined instructions that can be used to solve a computational problem. It provides a step-by-step procedure that convert an input into a desired output.

## How do Algorithms Work?

Algorithms typically follow a logical structure:

The algorithm receives input data.**Input:**The algorithm performs a series of operations on the input data.**Processing:**The algorithm produces the desired output.**Output:**

## Characteristics of an Algorithm:

Â The algorithm should be unambiguous. Each of its steps should be clear in all aspects and must lead to only one meaning.**Clear and Unambiguous:**If an algorithm says to take inputs, it should be well-defined inputs. It may or may not take input.**Well-defined Inputs:**The algorithm must clearly define what output will be yielded and it should be well-defined as well. It should produce at least 1 output.**Well-defined Outputs:**The algorithm must be finite, i.e. it should terminate after a finite time.**Finiteness:**The algorithm must be simple, generic, and practical, such that it can be executed using reasonable constraints and resources.**Feasible:**Algorithm must be language-independent, i.e. it must be just plain instructions that can be implemented in any language, and yet the output will be the same, as expected.**Language Independent:**

## What is the Need for Algorithms?

Algorithms are essential for solving complex computational problems efficiently and effectively. They provide a systematic approach to:

Algorithms break down problems into smaller, manageable steps.**Solving problems:**Algorithms find the best or near-optimal solutions to problems.**Optimizing solutions:**Algorithms can automate repetitive or complex tasks, saving time and effort.**Automating tasks:**

## Examples of Algorithms

Below are some example of algorithms:

Merge sort, Quick sort, Heap sort**Sorting algorithms:**Linear search, Binary search, Hashing**Searching algorithms:**Dijkstra’s algorithm, Prim’s algorithm, Floyd-Warshall algorithm**Graph algorithms:**Knuth-Morris-Pratt algorithm, Boyer-Moore algorithm**String matching algorithms:**

## How to Write an Algorithm?

To write an algorithm, follow these steps:

Clearly state the problem to be solved.**Define the problem:**Choose an appropriate algorithm design paradigm and develop a step-by-step procedure.**Design the algorithm:**Translate the algorithm into a programming language.**Implement the algorithm:**Execute the algorithm with various inputs to ensure its correctness and efficiency.**Test and debug:**Determine its time and space complexity and compare it to alternative algorithms.**Analyze the algorithm:**

**Learn Basics of Algorithms**

**Learn Basics of Algorithms**

- What is Algorithm | Introduction to Algorithms
- Definition, Types, Complexity, Examples of Algorithms
- Algorithms Design Techniques
- Why is analysis of an Algorithm important?

**Analysis of Algorithms**

**Analysis of Algorithms**

- Asymptotic Analysis
- Worst, Average and Best Cases
- Asymptotic Notations
- Lower and Upper Bound Theory
- Introduction to Amortized Analysis
- What does â€˜Space Complexityâ€™ mean?
- Polynomial Time Approximation Scheme
- Accounting Method | Amortized Analysis
- Potential Method in Amortized Analysis

## Types of Algorithms

Algorithms can be different types, depending on what they do and how they’re made. Some common types are:

**1. Searching and Sorting Algorithms**

**1. Searching and Sorting Algorithms**

- Introduction to Searching Algorithms
- Introduction to Sorting Algorithm
- Stable and Unstable Sorting Algorithms
- Lower bound for comparison based sorting algorithms
- Can Run Time Complexity of a comparison-based sorting algorithm be less than N logN?
- Which sorting algorithm makes minimum number of memory writes?

**2. Greedy Algorithms**

**2. Greedy Algorithms**

- Introduction to Greedy Algorithms
- Activity Selection Problem
- Huffman Coding
- Job Sequencing Problem
- Quiz on Greedy Algorithms
- Minimum Number of Platforms Required for a Railway/Bus Station

**3. Dynamic Programming Algorithms**

**3. Dynamic Programming Algorithms**

- Introduction to Dynamic Programming
- Overlapping Subproblems Property
- Optimal Substructure Property
- Longest Increasing Subsequence
- Longest Common Subsequence
- Min Cost Path
- Coin Change
- Matrix Chain Multiplication
- 0-1 Knapsack Problem
- Longest Palindromic Subsequence
- Palindrome Partitioning

**4. Pattern Searching Algorithms**

**4. Pattern Searching Algorithms**

- Introduction to Pattern Searching
- Naive Pattern Searching
- KMP Algorithm
- Rabin-Karp Algorithm
- Pattern Searching using a Trie of all Suffixes
- Aho-Corasick Algorithm for Pattern Searching
- Z algorithm (Linear time pattern searching Algorithm)

**5. Backtracking Algorithm**

**5. Backtracking Algorithm**

- Introduction to Backtracking
- Print all permutations of a given string
- The Knightâ€™s tour problem
- Rat in a Maze
- N Queen Problem
- Subset Sum
- m Coloring Problem
- Hamiltonian Cycle
- Sudoku

**6. Divide and Conquer Algorithm**

**6. Divide and Conquer Algorithm**

- Introduction to Divide and Conquer
- Merge Sort
- Write your own pow(x, n) to calculate x*n
- Count Inversions
- Closest Pair of Points
- Strassenâ€™s Matrix Multiplication

**7. Geometric Algorithm**

**7. Geometric Algorithm**

- Introduction to Geometric Algorithms
- Closest Pair of Points | O(nlogn) Implementation
- How to check if a given point lies inside or outside a polygon?
- How to check if two given line segments intersect?
- Given n line segments, find if any two segments intersect
- How to check if given four points form a square
- Convex Hull using Jarvisâ€™ Algorithm or Wrapping

**8. Mathematical Algorithms**

**8. Mathematical Algorithms**

- Introduction to Mathematical Algorithms
- Write an Efficient Method to Check if a Number is Multiple of 3
- Write a program to add two numbers in base 14
- Program for Fibonacci numbers
- Average of a stream of numbers
- Multiply two integers without using multiplication, division and bitwise operators, and no loops
- Babylonian method for square root
- Sieve of Eratosthenes
- Pascalâ€™s Triangle
- Given a number, find the next smallest palindrome
- Program to add two polynomials
- Multiply two polynomials
- Count trailing zeroes in factorial of a number

**9. Bitwise Algorithms**

**9. Bitwise Algorithms**

- Introduction to Bitwise Algorithms
- Little and Big Endian
- Detect opposite signs
- Swap bits
- Turn off the rightmost set bit
- Rotate bits
- Next higher number with same number of set bits
- Swap two nibbles in a byte

**10. Graph Algorithms**

**10. Graph Algorithms**

- Introduction to Graph Algorithms
- BFS, DFS
- Cycles in Graph
- Shortest paths
- MST
- Topological Sorting
- Connectivity
- Max Flow

**11. Randomized Algorithms**

**11. Randomized Algorithms**

- Introduction to Randomized Algorithms
- Linearity of Expectation
- Expected Number of Trials until Success
- Randomized Algorithms | Set 0 (Mathematical Background)
- Randomized Algorithms | Set 1 (Introduction and Analysis)
- Randomized Algorithms | Set 2 (Classification and Applications)
- Randomized Algorithms | Set 3 (1/2 Approximate Median)
- Reservoir Sampling

**12. Branch and Bound Algorithms**

**12. Branch and Bound Algorithms**

- Branch and Bound | Set 1 (Introduction with 0/1 Knapsack)
- Branch and Bound | Set 2 (Implementation of 0/1 Knapsack)
- Branch and Bound | Set 3 (8 puzzle Problem)
- Branch And Bound | Set 4 (Job Assignment Problem)
- Branch and Bound | Set 5 (N Queen Problem)
- Branch And Bound | Set 6 (Traveling Salesman Problem)

**Quizzes:**

- Analysis of Algorithms
- Sorting
- Divide and Conquer
- Greedy Algorithms
- Dynamic Programming
- Backtracking
- NP Complete
- Searching
- Recursion
- Bit Algorithms