Data Structures Algorithm Cheatsheet, Cheat Sheet of Computer science

Data Structures Algorithm Cheatsheet

Typology: Cheat Sheet

2019/2020

Uploaded on 01/07/2023

kamalkhanbd
kamalkhanbd 🇧🇩

3 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Algorithms and Data Structures Cheatsheet
We summarize the performance characteristics of classic algorithms and data structures for sorting, priority queues, symbol tables, and graph processing.
We also summarize some of the mathematics useful in the analysis of algorithms, including commonly encountered functions; useful formulas and
approximations; properties of logarithms; asymptotic notations; and solutions to divide-and-conquer recurrences.
Sorting.
The table below summarizes the number of compares for a variety of sorting algorithms, as implemented in this textbook. It includes leading constants but
ignores lower-order terms.
ALGORITHM CODE IN PLACE STABLE BEST AVERAGE WORST REMARKS
selection sort Selection.java ½ n 2 ½ n 2 ½ n 2 n exchanges;
quadratic in best case
insertion sort Insertion.java n¼ n 2 ½ n 2 use for small or
partially-sorted arrays
bubble sort Bubble.java n½ n 2 ½ n 2 rarely useful;
use insertion sort instead
shellsort Shell.java n log3 nunknown c n 3/2 tight code;
subquadratic
mergesort Merge.java ½ n lg n n lg n n lg nn log n guarantee;
stable
quicksort Quick.java n lg n2 n ln n½ n 2 n log n probabilistic guarantee;
fastest in practice
heapsort Heap.java n 2 n lg n2 n lg nn log n guarantee;
in place
n lg n if all keys are distinct
Priority queues.
The table below summarizes the order of growth of the running time of operations for a variety of priority queues, as implemented in this textbook. It ignores
leading constants and lower-order terms. Except as noted, all running times are worst-case running times.
DATA STRUCTURE CODE INSERT DEL-MIN MIN DEC-KEY DELETE MERGE
array BruteIndexMinPQ.java 1nn11n
binary heap IndexMinPQ.java log nlog n1 log nlog n n
d-way heap IndexMultiwayMinPQ.java logd n d logd n1logd n d logd nn
binomial heap IndexBinomialMinPQ.java 1 log n1 log nlog nlog n
Fibonacci heap IndexFibonacciMinPQ.java 1log n 11 log n 1
amortized guarantee
Symbol tables.
The table below summarizes the order of growth of the running time of operations for a variety of symbol tables, as implemented in this textbook. It ignores
leading constants and lower-order terms.
worst case average case
DATA STRUCTURE CODE SEARCH INSERT DELETE SEARCH INSERT DELETE
sequential search
(in an unordered list) SequentialSearchST.java nnnnnn
binary search
(in a sorted array) BinarySearchST.java log nn nlog n n n
binary search tree
(unbalanced) BST.java nnnlog nlog nsqrt(n)
red-black BST
(left-leaning) RedBlackBST.java log nlog nlog nlog nlog nlog n
AVL AVLTreeST.java log nlog nlog nlog nlog nlog n
pf3
pf4
pf5

Partial preview of the text

Download Data Structures Algorithm Cheatsheet and more Cheat Sheet Computer science in PDF only on Docsity!

Algorithms and Data Structures Cheatsheet

We summarize the performance characteristics of classic algorithms and data structures for sorting, priority queues, symbol tables, and graph processing.

We also summarize some of the mathematics useful in the analysis of algorithms, including commonly encountered functions; useful formulas and

approximations; properties of logarithms; asymptotic notations; and solutions to divide-and-conquer recurrences.

Sorting.

The table below summarizes the number of compares for a variety of sorting algorithms, as implemented in this textbook. It includes leading constants but

ignores lower-order terms.

ALGORITHM CODE IN PLACE STABLE BEST AVERAGE WORST REMARKS

selection sort Selection.java ✔ ½ n

2

½ n

2

½ n

2

n exchanges;

quadratic in best case

insertion sort Insertion.java ✔ ✔ n ¼ n

2

½ n

2

use for small or

partially-sorted arrays

bubble sort Bubble.java ✔ ✔

n ½ n

2

½ n

2

rarely useful;

use insertion sort instead

shellsort Shell.java ✔

n log

3

n unknown

c n

3/

tight code;

subquadratic

mergesort Merge.java ✔

½ n lg n n lg n n lg n

n log n guarantee;

stable

quicksort Quick.java ✔ n lg n 2 n ln n ½ n

2

n log n probabilistic guarantee;

fastest in practice

heapsort Heap.java ✔ n

2 n lg n 2 n lg n

n log n guarantee;

in place

n lg n if all keys are distinct

Priority queues.

The table below summarizes the order of growth of the running time of operations for a variety of priority queues, as implemented in this textbook. It ignores

leading constants and lower-order terms. Except as noted, all running times are worst-case running times.

DATA STRUCTURE CODE INSERT DEL-MIN MIN DEC-KEY DELETE MERGE

array BruteIndexMinPQ.java 1 n n 1 1 n

binary heap IndexMinPQ.java log n log n 1 log n log n n

d -way heap IndexMultiwayMinPQ.java

log

d

n d log

d

n

1

log

d

n d log

d

n

n

binomial heap IndexBinomialMinPQ.java 1 log n 1 log n log n log n

Fibonacci heap IndexFibonacciMinPQ.java 1

log n

1

1

log n

1

amortized guarantee

Symbol tables.

The table below summarizes the order of growth of the running time of operations for a variety of symbol tables, as implemented in this textbook. It ignores

leading constants and lower-order terms.

worst case average case

DATA STRUCTURE CODE SEARCH INSERT DELETE SEARCH INSERT DELETE

sequential search

(in an unordered list)

SequentialSearchST.java n n n n n n

binary search

(in a sorted array)

BinarySearchST.java log n n n log n n n

binary search tree

(unbalanced)

BST.java n n n log n log n sqrt( n )

red-black BST

(left-leaning)

RedBlackBST.java log n log n log n log n log n log n

AVL AVLTreeST.java log n log n log n log n log n log n

hash table

(separate-chaining)

SeparateChainingHashST.java n n n

1

1

1

hash table

(linear-probing)

LinearProbingHashST.java n n n

1

1

1

uniform hashing assumption

Graph processing.

The table below summarizes the order of growth of the worst-case running time and memory usage (beyond the memory for the graph itself) for a variety of

graph-processing problems, as implemented in this textbook. It ignores leading constants and lower-order terms. All running times are worst-case running times.

PROBLEM ALGORITHM CODE TIME SPACE

path DFS DepthFirstPaths.java E + V V

shortest path (fewest edges) BFS BreadthFirstPaths.java E + V V

cycle DFS Cycle.java E + V V

directed path DFS DepthFirstDirectedPaths.java E + V V

shortest directed path (fewest edges) BFS BreadthFirstDirectedPaths.java E + V V

directed cycle DFS DirectedCycle.java E + V V

topological sort DFS Topological.java E + V V

bipartiteness / odd cycle DFS Bipartite.java E + V V

connected components DFS CC.java E + V V

strong components Kosaraju–Sharir KosarajuSharirSCC.java E + V V

strong components Tarjan TarjanSCC.java E + V V

strong components Gabow GabowSCC.java E + V V

Eulerian cycle DFS EulerianCycle.java E + V E + V

directed Eulerian cycle DFS DirectedEulerianCycle.java E + V V

transitive closure DFS TransitiveClosure.java V ( E + V ) V

2

minimum spanning tree Kruskal KruskalMST.java E log E E + V

minimum spanning tree Prim PrimMST.java E log V V

minimum spanning tree Boruvka BoruvkaMST.java E log V V

shortest paths (nonnegative weights) Dijkstra DijkstraSP.java E log V V

shortest paths (no negative cycles) Bellman–Ford BellmanFordSP.java V ( V + E ) V

shortest paths (no cycles) topological sort AcyclicSP.java V + E V

all-pairs shortest paths Floyd–Warshall FloydWarshall.java

V

3

V

2

maxflow–mincut Ford–Fulkerson FordFulkerson.java E V ( E + V ) V

bipartite matching Hopcroft–Karp HopcroftKarp.java V

½

( E + V )

V

assignment problem successive shortest paths AssignmentProblem.java

n

3

log n n

2

Commonly encountered functions.

Here are some functions that are commonly encountered when analyzing algorithms.

FUNCTION NOTATION DEFINITION

floor greatest integer

ceiling smallest integer

binary logarithm or such that

natural logarithm or such that

common logarithm such that

iterated binary logarithm if otherwise

harmonic number

factorial

x ⌋ ≤ x

x ⌉ ≥ x

lg x log x

2

y 2 = x

y

ln x log x

e

y e = x

y

log x

10

y 10 = x

y

lg x

0 x ≤ 1; 1 + lg (lg x )

H

n

1 + 1/2 + 1/3 + … + 1/ n

n! 1 × 2 × 3 × … × n

Common orders of growth.

NAME NOTATION EXAMPLE CODE FRAGMENT

Constant

array access

arithmetic operation

function call

op();

Logarithmic

binary search in a sorted array

insert in a binary heap

search in a red–black tree

for (int i = 1; i <= n; i = 2*i)

op();

Linear

sequential search

grade-school addition

BFPRT median finding

for (int i = 0; i < n; i++)

op();

Linearithmic

mergesort

heapsort

fast Fourier transform

for (int i = 1; i <= n; i++)

for (int j = i; j <= n; j = 2*j)

op();

Quadratic

enumerate all pairs

insertion sort

grade-school multiplication

for (int i = 0; i < n; i++)

for (int j = i+1; j < n; j++)

op();

Cubic

enumerate all triples

Floyd–Warshall

grade-school matrix multiplication

for (int i = 0; i < n; i++)

for (int j = i+1; j < n; j++)

for (int k = j+1; k < n; k++)

op();

Polynomial

ellipsoid algorithm for LP

AKS primality algorithm

Edmond's matching algorithm

Exponential

enumerating all subsets

enumerating all permutations

backtracking search

Asymptotic notations: properties.

Reflexivity: is.

Constants: If is and , then is.

Products: If is and is , then is.

Sums: If is and is , then is.

Transitivity: If is and is , then is.

Polynomials: Let with. Then, is.

Logarithms and polynomials: is for every and every.

Exponentials and polynomials: is for every and every.

Factorials: is.

Limits: If for some constant , then is.

Limits: If , then is but not.

Limits: If , then is but not.

Here are some examples.

FUNCTION

✔ ✔

✔ ✔

✔ ✔ ✔ ✔

O (1)

O (log n )

O ( n )

O ( n log n )

O ( n )

2

O ( n )

3

O ( n )

c

O ( n )

c

f ( n ) O ( f ( n ))

f ( n ) O ( g ( n )) c > 0 cf ( n ) O ( g ( n )))

f ( n )

1

O ( g ( n ))

1

f ( n )

2

O ( g ( n )))

2

f ( n ) ⋅ ( n )

1

f

2

O ( g ( n ) ⋅ ( n )))

1

g

2

f ( n )

1

O ( g ( n ))

1

f ( n )

2

O ( g ( n )))

2

f ( n ) + ( n )

1

f

2

O (max{ g ( n ), ( n )})

1

g

2

f ( n ) O ( g ( n )) g ( n ) O ( h ( n )) f ( n ) O ( h ( n ))

f ( n ) = a + n + … +

0

a

1

a

d

n

d

a > 0

d

f ( n ) Θ( n )

d

log n

b

O ( n )

d

b > 0 d > 0

n

d

O ( r )

n

r > 0 d > 0

n! 2

Θ( n log n )

lim = c

n →∞

f ( n )

g ( n )

0 < c < ∞ f ( n ) Θ( g ( n ))

lim = 0

n →∞

f ( n )

g ( n )

f ( n ) O ( g ( n )) Θ( g ( n ))

lim = ∞

n →∞

f ( n )

g ( n )

f ( n ) Ω( g ( n )) O ( g ( n ))

o ( n )

2

O ( n )

2

Θ( n )

2

Ω( n )

2

ω ( n )

2

∼ 2 n

2

∼ 4 n

2

log n

2

10 n + 45

2 n + 45 n + 12

2

✔ ✔ ✔ ✔

✔ ✔

✔ ✔

Divide-and-conquer recurrences.

For each of the following recurrences we assume and that means either or.

RECURRENCE EXAMPLE

binary search

mergesort

insertion sort

tree traversal

towers of Hanoi

Karatsuba multiplication

Strassen multiplication

closest pair

Master theorem.

Let , , and and suppose that is a function on the non-negative integers that satisfies the divide-and-conquer recurrence

with and , where means either or either.

If , then

If , then

If , then

4 n − 2

2

n

3 n

3

n

T (1) = 0 n / 2 ⌊ n / 2⌋ ⌈ n / 2⌉

T ( n )

T ( n ) = T ( n / 2) + 1 ∼ lg n

T ( n ) = 2 T ( n / 2) + nn lg n

T ( n ) = T ( n − 1) + n

1

2

n

2

T ( n ) = 2 T ( n / 2) + 1 ∼ n

T ( n ) = 2 T ( n − 1) + 1

∼ 2

n

T ( n ) = 3 T ( n / 2) + Θ( n ) Θ( n ) = Θ( )

log 3

2

n

1.58...

T ( n ) = 7 T ( n / 2) + Θ( ) n

2

Θ( n ) = Θ( )

log 7

2

n

2.81...

T ( n ) = 2 T ( n / 2) + Θ( n log n )

Θ( n log n )

2

a ≥ 1 b ≥ 2 c > 0 T ( n )

T ( n ) = a T ( n / b ) + Θ( ) n

c

T (0) = 0 T (1) = Θ(1) n / bn / b ⌋ ⌈ n / b

c < log a

b

T ( n ) = Θ( n )

log a

b

c = log a

b

T ( n ) = Θ( n log n )

c

c > log a

b

T ( n ) = Θ( n )

c