Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
80 views

Algorithms and Data Structures Cheatsheet - Algorithms Part

programming

Uploaded by

Kirthivasan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
80 views

Algorithms and Data Structures Cheatsheet - Algorithms Part

programming

Uploaded by

Kirthivasan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Algorithms and Data Structures Cheatsheet 11/5/20, 9:06 PM

Algorithms and Data Structures Cheatsheet


extracted from https://algs4.cs.princeton.edu/cheatsheet/ (Sedgewick’s Algorithms book)

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 IN PLACE STABLE. BEST AVERAGE WORST REMARKS


n exchanges;
selection sort 2 2 2 quadratic in best
✔ ½n ½n ½n
case
use for small or
insertion sort ✔ ✔ n ¼n2 ½n2 partially-sorted
arrays
rarely useful;
bubble sort n 2 2 use insertion
✔ ✔ ½n ½n
sort instead

https://algs4.cs.princeton.edu/cheatsheet/ Page 2 of 10
Algorithms and Data Structures Cheatsheet 11/5/20, 9:06 PM

n log3 tight code;


shellsort ✔ unknown c n 3/2 subquadratic
n
n log n
½ n lg
mergesort ✔ n lg n n lg n guarantee;
n
stable
n log n
probabilistic
quicksort ✔ n lg n 2 n ln n ½n2 guarantee;
fastest in
practice
n log n
heapsort n † 2 n lg n 2 n lg 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 INSERT DEL-MIN MIN DEC-KEY DELETE MERGE


STRUCTURE
array 1 n n 1 1 n
binary heap log n log n 1 log n log n n
d-way heap logd n d logd n 1 logd n d logd n n
binomial heap 1 log n 1 log n log n log n
Fibonacci
heap
1 log n † 1 1† log n † 1

† amortized guarantee

https://algs4.cs.princeton.edu/cheatsheet/ Page 3 of 10
Algorithms and Data Structures Cheatsheet 11/5/20, 9:06 PM

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 SEARCH INSERT DELETE SEARCH INSERT DELETE

sequential
search
n n n n n n
(in an
unordered list)
binary search
(in a sorted log n n n log n n n
array)
binary search
tree n n n log n log n sqrt(n)
(unbalanced)
red-black
BST log n log n log n log n log n log n
(left-leaning)
AVL log n log n log n log n log n log n
hash table
(separate- n n n 1† 1† 1†
chaining)
hash table
(linear- n n n 1† 1† 1†
probing)
† 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 TIME SPACE


path DFS E+V V
shortest path (fewest
BFS E+V V
edges)
cycle DFS E+V V

https://algs4.cs.princeton.edu/cheatsheet/ Page 4 of 10
Algorithms and Data Structures Cheatsheet 11/5/20, 9:06 PM

directed path DFS E+V V


shortest directed path
BFS E+V V
(fewest edges)
directed cycle DFS E+V V

https://algs4.cs.princeton.edu/cheatsheet/ Page 5 of 10
Algorithms and Data Structures Cheatsheet 11/5/20, 9:06 PM

topological sort DFS E+V V


bipartiteness / odd cycle DFS E+V V
connected components DFS E+V V
strong components Kosaraju–Sharir E+V V
strong components Tarjan E+V V
strong components Gabow E+V V
Eulerian cycle DFS E+V E+V
directed Eulerian cycle DFS E+V V
transitive closure DFS V (E + V) V2
minimum spanning tree Kruskal E log E E+V
minimum spanning tree Prim E log V V
minimum spanning tree Boruvka E log V V
shortest paths
Dijkstra E log V V
(nonnegative weights)
shortest paths (no
Bellman–Ford V (V + E) V
negative cycles)
shortest paths (no cycles) topological sort V+E V
all-pairs shortest paths Floyd–Warshall V3 V2
E V (E +
maxflow–mincut Ford–Fulkerson V
V)

bipartite matching Hopcroft–Karp V ½ (E + V


V)
successive shortest
assignment problem
paths n 3 log n n2

Last modified on September 12, 2020.

Copyright © 2000–2019 Robert Sedgewick and Kevin Wayne. All rights reserved.

https://algs4.cs.princeton.edu/cheatsheet/ Page 10 of 10

You might also like