Algorithms

Algorithms

  • An algorithm is a step-by-step procedure for solving a problem or accomplishing a specific task.
  • Not all procedures that solve a problem can be called an algorithm; it should be effective, meaning it should solve the problem within a finite number of steps.
  • Algorithms are language-agnostic, meaning they can be implemented in any programming language.
  • Efficiency of an algorithm can be measured using time complexity (how execution time scales with the size of input) and space complexity (how memory use scales with the size of input).
  • Time complexity can be determined using ‘Big O’ notation, which describes the worst-case scenario in terms of time or space. For example, O(n) means the time taken grows linearly with input size, whereas O(n^2) indicates it grows quadratically.
  • Basic types of algorithms include recursive, iterative, divide and conquer, greedy, backtracking, and dynamic programming.
  • A recursive algorithm is one that makes calls to itself to solve a smaller version of the same problem. It requires a base case to stop the chain of recursive calls.
  • An iterative algorithm uses loops to repeatedly perform an operation until a certain condition is met.
  • Divide and conquer algorithms break down a problem into smaller instances of the same problem, solve these independently, and then combine the results.
  • Greedy algorithms make locally optimal choices at each step in the hope of finding a global optimum solution.
  • Backtracking algorithms try different solutions until they find one that works, going back and changing previous choices if necessary.
  • Dynamic programming algorithms solve complex problems by breaking them down into simpler, overlapping subproblems and building up solutions in a systematic way (memoization).
  • Search algorithms like linear search, binary search and hash tables are used to find a specific item in a data structure.
  • Sorting algorithms like bubble sort, insertion sort, quick sort, merge sort and selection sort are used to organise data in a particular order.
  • Graph algorithms are used to solve problems involving networks, including finding the shortest path between nodes, determining whether a route is possible, and finding cycles.