0 ratings0% found this document useful (0 votes) 33 views5 pagesAlgorithms and Data Structures Cheatsheet
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here.
Available Formats
Download as PDF or read online on Scribd
Algorithms and Data Structures Cheatsheet
‘We summarize the performance characteristics of classic algorithms and data structures for soning, priority queues, symbol ables, and graph processing,
sou formulas and
We also summarize some ofthe mathematics useful inthe analysis of algorithms, including eommonty encountered functions
approximations; properties oF Togarithnss; asymptotic notations; and solutions to dvide-an-conguer recurrences.
Sorting.
‘The table below summarizes the number of compare fora variety of sorting algrithms, as implemented in this textbook. It includes leading constants but
ignores loweronder terms.
weteenet inion " ot ES
ie nlogs” — unknown en 32 ee
shellsort ‘Shell java v tog i subquadratic
wepert agian wh tetera eat:
Yn Ign all keys are distinct
Priority queues.
table below summarizes the order of growth ofthe running time of aperations for a variety of priority queues, as implemented in this textbook. It ignores
leading constants and lower-ordr terms. Except as noted, all runing times are worst-case running times.
DATA STRUCTURE cone INSERT DEL-MIN MIN. DEC-KEY DELETE MERGE
array BnusindexMinPQjava 1 ” ” 1 1 ”
binary heap IndexMin java logn og 1 ogn log ”
dovay heap IndexMulinayMin?O inn logy = dogg loggn—dlogyn on
binomial heap ndesBinomisIMinPO jaa 1 loge 1 logn—togn hogan
Fibonacci heap IndeafiboaassiMinPQjava 1 login! 1 Ht tognt 1
* amortized guarantee
Symbol tables.
“The table below summarizes the order of growth ofthe running time of operations fora varity of symbol tables, as implemented inthis textbook. It ignores
leading constants and lower-order terms,
worst case average case
DATA STRUCTURE cope SEARCH INSERT DELETE SEARCH INSERT DELETE
sequential seareh .
‘ScouentilScarchSTis ” a 1 n . "
(Gn an unordered list) in
binary search
Gnawnciany) — BinanScarchSTjava lege a . log n ” ®
binary search toe
ies BSTjava ” ” . log —fogm satin)
red-black BST
(lef-lesning) RedBlackHST java og tog tog tog topo
AVL AVLTresSTjava loge fog» dog dog tog oghash able ScparisChainineHashSjsa ” . wt 1t ut
(separate-chaining)
nash table
‘ - : a fe + '
(liner probing) _stsatBaabinglashS java 1 1 t
Graph processing.
“The table below summarizes the order of growth ofthe worst-case running time and memory usage (beyond the memory forthe graph itselP) fora variety of
_raply-processing problems, as implemented inthis textbook. It ignores leading constants and lower-order terms. All running times are worst-case running times,
PROBLEM ALGORITHM, cone, ME SPACE
path DFS spina java ety v
shortest path fewest edges) BFS BrcadthfinstPaths java ee v
cycle brs Cysle java ety
rected path Des DspiistDinstedPathsiawa = + v
shortest rected path (Fewest ees) Bes RecadbiceDinstodPubsin EB +V v
directed eyele Des DirestedCyse jana eer v
topologieal sort DFS ‘Topological java Ee Y
bipartiteness/ odd eyele Drs Binartiejaa eer v
connected components bes cava eer v
strong components Kesaraju-Shart ‘KosumjuSharisSCCjana ety v
strong components Tarjan TarjanSCCjava ee Y
strong components Gabow GabowSCC java eer v
Eulerian eycte Drs EulerinCyst java eer eee
directed Eulerian cycle DES DirwciedEwlerianCysieja E+ r
transitive eosure prs TinsitiClosueina = FE +H)
‘minimum spanning tree Kruskal KeuskalMSTjava Else
‘minimum spanning tree Prien PrimM Sasa Flog ¥
‘minimum spanning tree Boruvka BonwkaMSTjnva Elog¥ v
shortest paths (nonnegative weights) Dijkstra Diss Pjana Flog v
shortest paths (no negative eyes) Bllman-Ford Bellman FondSPjave vote i
shortest paths (no eyes) topotogicst sort ‘AcyelicS jaa Vee y
alk-pars shortest paths Floyd-Warsbll ‘loyWarsal java ve vt
smaxflow-mincut Ford-Fulkerson Fondfulksrsonjova = EVE +) Y
Dipartite matching Hoperott-Karp HoperofiKr java ren Us
‘assignment problem successive shortest paths AssignmmtPicblemjava nog 2
‘Commonly encountered functions.
ere are some fanetions that are commonly encountered when analyzing algorithms.
FUNCTION NOTATION DEFINITION
oor lz] retest integer So
ceiling [z] somllest integer > 3
binary logarithm Iga or logy 2 1y such that2”
natural logarithm Ing or log, = such that e
common logarithm logig 2 ‘ysuch that 109 =
iterated binary logrithm Iga Oite <1; 141g (Iga) otherwise
hharmonie number Hy 141/2+1/34...+1/n
factorial nt 1x2K3xX.enbinomial coeTicint (9
Useful formulas and approximations.
Here are some useful formulas for approximations that are widely use inthe analysis of algorithn,
+ Harmonie sum: 141/2-+1/3+...41/n~ Inn
+ Triangular sum: 1+2+3-4...4n=n (n+l) /2~n?/2
+ Sumofsqures: 1? +22 +3? ..4n? wn /3
+ Geometric sun: Ihr Z Athen +r te? tb tote = (et 1) / (P12)
oad 1$1/241/44 18+... 41/2" ~2
ora 14244484
+ n/24+n=2n—1~2n., when m isa power of2
+ Sirting’s approximation: g(n!) = Ig + Ig2-+1g3+...+ Ign ~ nign
+ Exponent: (141/n)" ~e; (1-1/n)"~ Te
+ Binomial eoofcens: (2) ~ nk / kt when kis smal constant
+ Appoints ine I f(2 sa monty ican ton en fle) de < SFU) < ff * ja) ae
Properties of logarithms.
+ Definition: Tog, a = ¢ means BF = a. We refertoB asthe base ofthe logarithm,
‘Special cases: logy b= 1, log, 1=0
+ Inverse of exponential: I°%* = x
© Product: logy(x x y) = log, « + logy y
+ Division: logs (2
logy 2 — logs y
+ Finite prod: logy (21 X 22%... X ty) = logy a1 + logy 22 +... + logs tn
+ Changing bases: log, 2 = log. / log. b
+ Rearranging exponents: 6 ¥ = yl *
+ Exponentiation: log, (2") = ylog, x
Asymptotic notations: definitions.
NAME NOTATION sacairena
sis tl
tude fen)~atn) ne tea ho
meor fo) 0Gfo) 0) Mamaled ety
mom fopuaigny fe ermblnrmn ge) arena
2) mde hove dtc g
BieTHta — ffn)ieo(gin)) 1) ca)
feo a
seo in) ixololn)) £07) sdminated by g(n) soya
Lines Fin)islaln)) 1) oe
csoomee, Hesicdgiey) Es) Sathana
(Ggnoring constant factors)
DEFINITION
there exist constants © > 0 and mg > 0 such that
O< f(r) ng
gn) is O(f(n))
J(n) is both O(g(n)) and Q(9(n))
tim JC)
oe oa
a(n) is of f{n))‘Common orders of growth.
NAME NOTATION EXAMPLE CODE FRAGMENT
aay access
Constant 001) arithmetic operation 99005
fietion eal
binary seach in sored amy ae cL as 4 ge nj 28
Logarithmic O(log-n) inser inabinary heap fF (eRe S = 3 4 Ae mr t= 248)
search in ed-black toe i
sequential search stem i
Linear O(n) _grade-school addition es
BFPRT median Finding u
rergesort for (int 4 =
Linearithnic O(log) heaps ‘or ie get Soa} ean
fast Fourier transform
numer alu for ane = 0
Quadratic O(n?) rca ft 5 ea) say
pate etnol muliplction
emumeravatlvipies for {ine 65 Lew to
Cubie O(n) Floyd-Warshal, err Eg
aradeschool mattis muiptcstion ote
tipo algritn frLP
Polynomial O(n
Exponential 290)
Asymptotic notations: properties.
+ efesvin: f(r) s OF(0))
© Constants: If f(n) is O(g(n)) and © > 0, then e f(n) is O(g(n))).
+ Products: I fa(n) t= O(93(n)) and fo(r) is O(ga(n))) then fa(n) - fan) i OC94(n) -g21n)))
+ Sams: 1 f(n) is O(a) ad f(b OL 4a), shen fie) + fal) f Olena), 90(%)})
+ Trarsinviys 16 f(n) i OC g(n)) and gn) is OC()). then fn) is OCA(n))
+ Polynomials: Let f(n) = a9 + arn+... aun! with ay > 0. Then, f(r) is (4)
+ Losorithns and polynomials: logy nis O(n) for every b > 0 and every d > 0.
+ Exponemtials and polynomials: nis O(+) for every F > O and every d > 0.
© Factorials; n\ is 29" "8"
+ Lins 16 i, rol = forsome constan 0 <¢ < 00, then f(n) is @(a(n))
Md
© Limits: If iz ‘any ), then f(m) is O(g(n)) but not (g(r).
© Limits: If tn, = 00, then f(n) is 2(g(n)) but not O(g(n)).
ere are some examples
FUNCTION —o{n?) O(n?) O(n?) O(n?) ~ On? ~ dn?
logy n voy
10m +45 voy
2n? + 45n +12 vovoyv a4n? — 20 vow ¥
ant 4o¥
” yoy
Divide-and-conquer recurrences.
For each ofthe following recurrences we assume T(1) = 0 and that m /'2 means either [m/ 2] of [n /2]
RECURRENCE T(n) EXAMPLE
T(n) =T(n/2) +1 ~wign ‘binary search
~nlgn smorgesot
~int insertion sort
IT (n /2)+1 ~n tree traversal
T(n)=2T(n—-1)+1 en ‘towers of Hanoi
T(n) = 37 (n/2) + O(n) O(nl#4) = O(n'58-) Karatsuba multiplication
T(n) = 7T(n /2) + O(n?) O(n?) = @(n281-) Strasse multiplication
T(n) =27(n/2) + O(nlogn) (nog? n) oe
Master theorem.
Leta > 1,6 > 2,ande > 0 and suppose that T(2) i function onthe non-negative integers tht satisfies the divide-and-conquerreeurence
Tin) =aT(n/b) + O(n")
with T(0) = 0 and T(1) = O(1), here /B means cither [n./b] or either fn / 6]
+ Ife < logy a.then T(n) = O(n¥s+*)
+ te = logs a, then T(n) = O(n log)
4 Ife > logs a, then T(n) = O(n")