📕
Dan Fitz's Notes
  • README
  • Ai
    • Supervised Machine Learning
      • Introduction To Machine Learning
      • Regression With Multiple Input Variables
      • Classification
  • Csharp
    • C Sharp Advanced
      • Generics
      • Delegates
      • Lambda Expressions
      • Events
    • C Sharp Fundamentals
      • Intro To C
      • Primitive Types And Expressions
      • Non Primitive Types
      • Control Flow
      • Arrays And Lists
      • Working With Dates
      • Working With Text
      • Working With Files
      • Debugging Applications
    • C Sharp Intermediate
      • Classes
      • Association Between Classes
      • Inheritance
      • Polymorphism
      • Interfaces
  • Java
    • Inheritance Data Structures Java
      • Inheritance Polymorphism Using Overriding And Access Modifiers
      • Abstract Classes And Debugging
      • File I O And Exceptions
      • Collections Maps And Regular Expressions
    • Intro To Java
      • Introduction To Java Classes And Eclipse
      • Unit Testing Arrays And Array Lists
      • Static Variables Methods And Polymorphism Using Overloading
  • Javascript
    • Algorithms Data Structures
      • Big O Notation
      • Analyzing Performance Of Arrays And Objects
      • Problem Solving Approach
      • Problem Solving Patterns
      • Recursion
      • Searching Algorithms
      • Bubble Selection And Insertion Sort
      • Merge Sort
      • Quick Sort
      • Radix Sort
      • Data Structures Introduction
      • Singly Linked Lists
      • Doubly Linked Lists
      • Stacks And Queues
      • Binary Search Trees
      • Tree Traversal
      • Binary Heaps
    • Complete Nodejs
      • Understanding Node.js
      • REST AP Is And Mongoose
      • API Authentication And Security
      • Node.js Module System
      • File System And Command Line Args
      • Debugging Node.js
      • Asynchronous Node.js
      • Web Servers
      • Accessing API From Browser
      • Application Deployment
      • Mongo DB And Promises
    • Complete React Native
      • Working With Content
      • Building Lists
      • Navigating Users Between Screens
      • State Management
      • Handling Screen Layout
      • Setting Up An App
      • More On Navigation
      • Advanced Statement Management With Context
      • Building A Custom Express API
      • In App Authentication
    • Epic React
      • React Fundamentals
      • React Hooks
      • Advanced React Hooks
      • Advanced React Patterns
      • React Performance
    • Fireship Firestore
      • Firestore Queries And Data Modeling Course
      • Model Relational Data In Firestore No SQL
    • Functional Light Javascript
      • Intro
      • Function Purity
      • Argument Adapters
      • Point Free
      • Closure
      • Composition
      • Immutability
      • Recursion
      • List Operations
      • Transduction
      • Data Structure Operations
      • Async
    • Js Weird Parts
      • Execution Contexts And Lexical Environments
      • Types And Operators
      • Objects And Functions
      • Object Oriented Java Script And Prototypal Inheritance
      • Defining Objects
    • Mastering Chrome Dev Tools
      • Introduction
      • Editing
      • Debugging
      • Networking
      • Auditing
      • Node.js Profiling
      • Performance Monitoring
      • Image Performance
      • Memory
    • React Complete Guide
      • What Is React
      • React Basics
      • Rendering Lists And Conditionals
      • Styling React Components
      • Debugging React Apps
      • Component Deep Dive
      • Building A React App
      • Reaching Out To The Web
      • Routing
    • React Testing
      • Intro To Jest Enzyme And TDD
      • Basic Testing
      • Redux Testing
      • Redux Thunk Testing
    • Serverless Bootcamp
      • Introduction
      • Auction Service Setup
      • Auction Service CRUD Operations
      • Auction Service Processing Auctions
    • Testing Javascript
      • Fundamentals Of Testing
      • Static Analysis Testing
      • Mocking Fundamentals
      • Configuring Jest
      • Test React Components With Jest And React Testing Library
    • Typescript Developers Guide
      • Getting Started With Type Script
      • What Is A Type System
      • Type Annotations In Action
      • Annotations With Functions And Objects
      • Mastering Typed Arrays
      • Tuples In Type Script
      • The All Important Interface
      • Building Functionality With Classes
    • Web Performance With Webpack
      • Intro
      • Code Splitting
      • Module Methods Magic Comments
  • Other
    • Algo Expert
      • Defining Data Structures And Complexity Analysis
      • Memory
      • Big O Notation
      • Logarithm
      • Arrays
      • Linked Lists
      • Hash Tables
      • Stacks And Queues
      • Strings
      • Graphs
      • Trees
    • Aws Solutions Architect
      • AWS Fundamentals IAM EC 2
    • Fundamentals Math
      • Numbers And Negative Numbers
      • Factors And Multiples
      • Fractions
    • Mysql Bootcamp
      • Overview And Installation
      • Creating Databases And Tables
      • Inserting Data
      • CRUD Commands
      • The World Of String Functions
      • Refining Our Selections
      • The Magic Of Aggregate Functions
    • Random Notes
      • Understanding React Hooks
  • Python
    • Data Analysis Using Python
      • Loading Querying And Filtering Data Using The Csv Module
      • Loading Querying Joining And Filtering Data Using Pandas
      • Summarizing And Visualizing Data
    • Intro To Python
      • Course Introduction Intro To Programming And The Python Language Variables Conditionals Jupyter Notebook And IDLE
      • Intro To Lists Loops And Functions
      • More With Lists Strings Tuples Sets And Py Charm
      • Dictionaries And Files
Powered by GitBook
On this page
  • How to measure complexity
  • Why we write O(1)
  • Combining complexities
  • Worst-case vs. best-case scenarios
  • Multiple inputs
  1. Other
  2. Algo Expert

Big O Notation

How to measure complexity

When we measure time complexity of an algorithm, we are actually aiming to measure how its speed is affected by the size of its input. This is known as asymptotic analysis.

Here are the most common measures of time complexity (from best to worst):

  • Constant time complexity O(1)

    • As the input increases, the time to perform the operations doesn't change

  • Logarithmic time complexity O(log n)

    • As the input increases, the time to perform the operations increases logarithmically

  • Linear time complexity O(n)

    • As the input increases, the time to perform the operations increases linearly

  • Product of logarithmic and linear time complexity O(n * log n)

  • Exponential time complexity O(n^2), O(n^3), O(n^4)

    • As the input increases, the time to perform the operations increases exponentially

Note: There are even worse complexities like O(2^n) and O(n!), where n! is worse because most of the factors of the product are larger than 2.

Why we write O(1)

Imagine your algorithm just takes the first value in an array and adds 1:

const f = arr => arr[0] + 1

This algorithm is considered O(1). But why? Technically, the function performs more than one operation. For example, if we're dealing with 32-bit integers, accessing 1 and arr[0] could each require accessing 4 memory slots. So, maybe it's more accurate to say f's time complexity is O(8).

The reason we don't write O(8) is that asymptotic analysis doesn't care about the exact number of operations as long as those operations don't have any relation to the size of the input arr. As the size of arr increases, f still performs 8 operations no matter what.

For this reason, asymptotic analysis simplifies the result to O(1).

(The same reasoning would apply to more complex operations as long as the operations are constant. Examples of such operations might be addition, multiplication, etc.)

Combining complexities

Imagine you have 3 functions:

const constantF = arr => arr[0] + 1
const linearF = arr => arr.reduce((sum, x) => sum + x, 0);
const quadraticF = arr => arr.map(x => arr.map(y => [x, y]));

Now imagine you create a function that uses all 3:

const combinedF = arr => {
    constantF(arr);
    linearF(arr);
    quadraticF(arr);
}

The complexity of combinedF would be O(n^2 + n + 1). However, as the input size of n gets huge, n + 1 will be insignificant compared to n^2. Thus, according to asymptotic analysis, we simplify it to O(n^2).

Similar reasoning applies when you run quadraticF multiple times:

const repeatedF = arr => {
    quadraticF(arr);
    quadraticF(arr);
    quadraticF(arr);
}

You could say repeatedF has a time complexity of O(3n^2), but we simplify it to O(n^2).

Important: One of the only times we don't drop constants is with exponential time complexities. There is big difference between O(n^2) and O(n^4), so we keep the exponent to communicate the difference.

Worst-case vs. best-case scenarios

In all of our complexity analyses, we always assume worst-case scenario: what happens to an algorithm when it gets a huge input size.

However, some algorithms have different complexity measurements in the best-case scenario vs. average-case scenario vs. worst-case scenario, and you may pick an algorithm for its best-case or average-case results.

Multiple inputs

Suppose you have a function that takes in 2 inputs:

const twoInputF = (arr1, arr2) => {
    // ...
}

Now suppose the operations we perform on arr1 are O(n^2), while the operations performed on arr2 are O(m). In total, that means the time complexity is O(n^2 + m).

Do we drop m since n^2 could get so much bigger?

The answer is no because the input sizes n and m are independent: the size of one doesn't relate to the size of another. As a result, you want to capture each of them in your complexity analysis.

PreviousMemoryNextLogarithm

Last updated 3 years ago