0% found this document useful (0 votes)
21 views2 pages

Big-O Complexity Cheat Sheet

cheat sheet

Uploaded by

filoyoh561
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views2 pages

Big-O Complexity Cheat Sheet

cheat sheet

Uploaded by

filoyoh561
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Big-O Algorithm Complexity Cheat Sheet

Searching
Space
Algorithm Data Structure Time Complexity
Complexity
Average Worst Worst
Graph of |V| vertices and |E|
Depth First Search (DFS) - O(|E| + |V|) O(|V|)
edges
Graph of |V| vertices and |E|
Breadth First Search (BFS) - O(|E| + |V|) O(|V|)
edges
Binary search Sorted array of n elements O(log(n)) O(log(n)) O(1)
Linear (Brute Force) Array O(n) O(n) O(1)
Shortest path by Dijkstra, O((|V| + |E|) log O((|V| + |E|) log
using a Min-heap as priority queue O(|V|)
Graph with |V| vertices and |V|) |V|)
(Binnary heap)
|E| edges
O(|E| + |V| log O(|E| + |V| log
Dijkstra using a Fibonacci heap: |V|) |V|)
O(|V|)

Shortest path by Dijkstra,


Graph with |V| vertices and
using an unsorted array as priority O(|V|^2) O(|V|^2) O(|V|)
|E| edges
queue
Graph with |V| vertices and
Shortest path by Bellman-Ford O(|V||E|) O(|V||E|) O(|V|)
|E| edges
Sorting
Algorithm Data Structure Time Complexity Worst Case Auxiliary Space Complexity
Best Average Worst Worst
Quicksort Array O(n log(n)) O(n log(n)) O(n^2) O(n)
Mergesort Array O(n log(n)) O(n log(n)) O(n log(n)) O(n)
Heapsort Array O(n log(n)) O(n log(n)) O(n log(n)) O(1)
Bubble Sort Array O(n) O(n^2) O(n^2) O(1)
Insertion Sort Array O(n) O(n^2) O(n^2) O(1)
Select Sort Array O(n^2) O(n^2) O(n^2) O(1)
Bucket Sort Array O(n+k) O(n+k) O(n^2) O(nk)
Radix Sort Array O(nk) O(nk) O(nk) O(n+k)

Heaps
Heaps Time Complexity
Heapify Find Max Extract Max Increase Key Insert Delete Merge
Linked List (sorted) - O(1) O(1) O(n) O(n) O(1) O(m+n)
Linked List (unsorted) - O(n) O(n) O(1) O(1) O(1) O(1)
Binary Heap O(n) O(1) O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(m+n)
Binomial Heap - O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(log(n))
Fibonacci Heap - O(1) O(log(n))* O(1)* O(1) O(log(n))* O(1)

Graphs
Node / Edge Management Storage Add Vertex Add Edge Remove Vertex Remove Edge Query
Adjacency list O(|V|+|E|) O(1) O(1) O(|V| + |E|) O(|E|) O(|V|)
Incidence list O(|V|+|E|) O(1) O(1) O(|E|) O(|E|) O(|E|)
Adjacency matrix O(|V|^2) O(|V|^2) O(1) O(|V|^2) O(1) O(1)
Incidence matrix O(|V| ⋅ |E|) O(|V| ⋅ |E|) O(|V| ⋅ |E|) O(|V| ⋅ |E|) O(|V| ⋅ |E|) O(|E|)
Data Structures
Space
Data Structure Time Complexity
Complexity
Average Worst Worst
Indexing Search Insertion Deletion Indexing Search Insertion Deletion
Basic Array O(1) O(n) - - O(1) O(n) - - O(n)
Dynamic Array O(1) O(n) O(n) O(n) O(1) O(n) O(n) O(n) O(n)
Singly-Linked
O(n) O(n) O(1) O(1) O(n) O(n) O(1) O(1) O(n)
List
Doubly-Linked
O(n) O(n) O(1) O(1) O(n) O(n) O(1) O(1) O(n)
List
Skip List O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(n) O(n) O(n) O(n) O(n log(n))
Hash Table - O(1) O(1) O(1) - O(n) O(n) O(n) O(n)
Binary Search
O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(n) O(n) O(n) O(n) O(n)
Tree
Cartresian Tree - O(log(n)) O(log(n)) O(log(n)) - O(n) O(n) O(n) O(n)
B-Tree O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(n)
Red-Black Tree O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(n)
Splay Tree - O(log(n)) O(log(n)) O(log(n)) - O(log(n)) O(log(n)) O(log(n)) O(n)
AVL Tree O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(n)

Special Notes:
The complexity of Dijkstra's shortest path algorithm is:
O(|E| |decrease-key(Q)| + |V| |extract-min(Q)|)
To summarize:
 Fibonacci heap: O(|E| + |V| log |V|)
 binary heap: O((|E| + |V|) log |V|)
 unsorted array: O(|V|^2)

Common questions

Powered by AI

A binary heap is preferred over an unsorted array for Dijkstra's algorithm due to its efficient log-time operations for inserting and extracting the minimum, which are crucial in pathfinding. The time complexity with a binary heap is O((|V| + |E|) log |V|), versus O(|V|^2) with an unsorted array, as the latter requires linear time extraction of the smallest element, drastically increasing computational effort with larger graphs .

Dijkstra's algorithm demonstrates different time complexities depending on the priority queue data structure used. When using a binary heap, the time complexity is O((|V| + |E|) log |V|), while using a Fibonacci heap reduces it to O(|E| + |V| log |V|) because the Fibonacci heap allows more efficient decrease-key operations, which are frequent in Dijkstra's algorithm .

Merge Sort has an auxiliary space complexity of O(n) because it necessitates additional storage equivalent to the size of the array for temporary merging. QuickSort, on the other hand, requires O(log(n)) space on average for the recursive call stack if implemented in-place, making it significantly more space-efficient compared to Merge Sort in usual scenarios .

Binary Search in a sorted array has a time complexity of O(log(n)), which allows it to efficiently halve the search space with each step. In contrast, Linear Search has a time complexity of O(n), as it requires checking each element sequentially. Therefore, Binary Search is significantly more efficient for large datasets where the array is sorted .

An adjacency list stores vertices and their edges as a list, providing an efficient representation for sparse graphs with time complexities of O(|V|+|E|) for storage and O(1) for adding edges. An adjacency matrix, however, uses a two-dimensional array, which results in O(|V|^2) for storage, optimal for dense graphs, and provides constant time O(1) complexity for edge queries. The matrix can be wasteful for space, particularly for graphs with fewer edges .

Radix Sort is preferred for sorting fixed-width data types like integers when the number of elements n and range k are such that k is linear relative to n, leading to O(nk) time complexity. It efficiently sorts by processing digits or bits independently, which can outperform comparison-based sorts on narrow data types, leveraging O(n) linear operations per pass without relying on element comparisons, making it optimal for sizable uniform input distributions .

The choice of sorting algorithm heavily influences computational complexity, especially for large datasets. Algorithms like QuickSort and MergeSort with average time complexities of O(n log(n)) tend to perform better in practice for sizable data due to efficient divide-and-conquer strategies. However, QuickSort may degrade to O(n^2) in the worst case, making MergeSort preferable for consistent performance. Conversely, selection-intensive or small workload contexts may justify simpler algorithms like Insertion Sort, despite its O(n^2) complexity, due to negligible overhead .

Both Depth First Search (DFS) and Breadth First Search (BFS) have a time complexity of O(|E| + |V|) when traversing a graph with |V| vertices and |E| edges. This reflects the need to visit every vertex and edge in their respective traversal processes, making them equally efficient in terms of time complexity for graph traversal .

QuickSort has a best-case time complexity of O(n log(n)) when the pivot divides the input into two even halves at every step, achieving efficient recursive subarray sorting. However, in the worst case, such as when the smallest or largest element is consistently chosen as the pivot (common in already sorted arrays), the time complexity degrades to O(n^2) due to the lack of divide-and-conquer efficiencies .

AVL Trees are typically favored over Red-Black Trees when applications demand more frequent read or search operations due to stricter balancing, which results in faster queries. AVL Trees maintain a guarantee of stricter O(log(n)) heights, which translates to consistent and often faster search times relative to the amortized balancing of Red-Black Trees, suitable for environments prioritizing read efficiency over writes .

You might also like