Unit - 1
TITLE
Basics of Algorithms and Mathematics
1. Introduction
1.1 Overview
- The course introduces the Basics of Algorithms and Mathematics, covering foundational concepts required for problem-solving using computers.
2. What is an Algorithm?
2.1 Definition of an Algorithm
- A process or a set of rules to be followed to achieve a desired output, especially by a computer.
- A step-by-step procedure to solve different kinds of problems.
- An unambiguous (completely clear) sequence of computational steps that transform the input into the output.
- Any well-defined computational procedure that takes some value, or set of values, as input and produces some value, or set of values as output.
3. Characteristics of an Algorithm
3.1 Key Characteristics
- Finiteness: An algorithm must always terminate after a finite number of steps.
- Definiteness: Each step of an algorithm must be precisely defined.
- Input: An algorithm has zero or more inputs.
- Output: An algorithm must have at least one desirable output.
- Effectiveness: All the operations to be performed in the algorithm must be sufficiently basic so that they can in principle be done exactly and in a finite length of time.
4. Types of Algorithms
4.1 Common Types Elaborated
- Simple recursive algorithms: These algorithms solve a problem by calling themselves with smaller input values until a base case is reached.
- Example: Calculating Factorial, Fibonacci series.
- Backtracking algorithms: A refined brute-force approach that tries to build a solution incrementally, abandoning paths ("backtracking") that fail to satisfy problem constraints.
- Example: N-Queens problem, Sudoku solver.
- Divide and conquer algorithms: This paradigm involves dividing a problem into smaller, independent sub-problems, solving them recursively, and combining their results.
- Example: Merge Sort, Quick Sort.
- Dynamic programming algorithms: These algorithms optimize recursive approaches by storing the results of overlapping subproblems (memoization/tabulation) so they are computed only once.
- Example: Longest Common Subsequence, Knapsack problem.
- Greedy algorithms: This approach builds a solution piece by piece, always making a locally optimal choice with the hope that these choices lead to a global optimum.
- Example: Huffman Coding, Dijkstra's Shortest Path.
- Branch and bound algorithms: Used primarily for optimization problems. It systematically explores candidate solutions using a state-space tree, pruning ("bounding") branches that cannot yield better solutions than the best one found so far.
- Example: Traveling Salesman Problem (TSP).
- Brute force algorithms: A straightforward and exhaustive approach that enumerates all possible solutions and checks each one to see if it satisfies the problem statement.
- Example: Linear Search, Naive string matching.
- Randomized algorithms: These use a degree of randomness as part of their logic to reduce time or space complexity on average.
- Example: Randomized Quick Sort, Monte Carlo algorithms.
5. Simple Multiplication Methods
5.1 Schoolbook (Grade-School) Multiplication
- The traditional method where each digit of the multiplier is multiplied by every digit of the multiplicand. The resulting partial products are shifted and added together.
- Time Complexity: for two -digit numbers (since it requires single-digit multiplications).
5.2 Repeated Addition
- The most primitive form of multiplication where computing is achieved by adding to itself times.
- Example: .
- Time Complexity: Highly inefficient for large numbers, taking time.
5.3 Karatsuba Algorithm (Divide & Conquer Approach)
- While "schoolbook" multiplication is simple, the Karatsuba algorithm is a fundamental algorithm taught to improve it. It splits the -digit numbers into halves and uses mathematical tricks to perform only three multiplications instead of four.
- Time Complexity: , making it faster than schoolbook multiplication for large integers.
6. Questions
6.1 Review Questions
- Define the term: Algorithm
- List types of algorithms.
- Discuss key characteristics of algorithm.