Вы находитесь на странице: 1из 2

# Data Structures - Cheat Sheet

Trees Binary

Red-Black Tree Melding: If the heap is represented by an array, link the two
arrays together and Heapify-Up. O (n).
1. Red Rule: A red child must have a black father
2. Black Rule: All paths to external nodes pass through the
Binomial
same number of black nodes.
3. All the leaves are black, and the sky is grey. Melding: Unify trees by rank like binary summation. O (log n)
Rotations are terminal cases. Only happen once per fixup.
If we have a series of insert-delete for which the insertion point Fibonacci Heap
is known, the amortized cost to each action is O (n). √
  (1+ 5)
Height:log n ≤ h ≤ 2 log n Maximum degree: D (n) ≤ logϕ n ; ϕ = 2
Limit of rotations: 2 per insert. Minimum size of degree k: sk ≥ Fk+2
Bound of ratios between two branches L, R: S (R) ≤ (S (L))
2 Marking: Every node which lost one child is marked.
Completely isomorphic to 2-4 Trees. Cascading Cut: Cut every marked node climbing upwards.
Keeps amortized O(log n) time for deleteMin. Otherwise

B-Tree O( n).
Proof of the ϕk node size bound:
d defines the minimum number of keys on a node 1. All subtrees of junction j, sorted by order of insertion are of
Height: h ≈ logd n degree D[si ] ≥ i − 2 (Proof: when x’s largest subtree was added,
1. Every node has at most d children and at least d2 children since D [x] was i − 1, so was the subtree. Since then, it could
(root excluded). lose only one child, so it is at least i − 2)
2. The root has at least 2 children if it isn’t a leaf. Pk
2. Fk+2 = 1 + i=0 Fi ; Fk+2 ≥ ϕk
3. A non-leaf node with k children contains k − 1 keys. 3. If x is a node and k = deg [x], Sx ≥ Fk+2 ≥ ϕk .
4. On B+ trees, leaves appear at the same level. (Proof: Assume induction after the base cases and then sk =
5. Nodes at each level form linked lists Pk Pk Pk
2 + i=2 Si−2 ≥ 2 + i=2 Fi = 1 + i=0 Fi = Fk+2 )
d is optimized for HDD/cache block size
Insert: Add to insertion point. If the node gets too large, Structures
split.O (log n) ≤ O (logd n)
Split: The middle of the node (low median) moves up to be the Median Heap: one min-heap and one max-heap with ∀x ∈
edge of the father node. O (d) min, y ∈ max : x > y then the minimum is on the median-heap
Delete: If the key is not in a leaf, switch with succ/pred. Delete,
and deal with short node v:
1. If v is the root, discard; terminate.
Sorting
2. If v has a non-short sibling, steal from it; terminate. Comparables
3. Fuse v with its sibling, repeat with p ← p [v].
Algorithm Expected Worst Storage

Traversals QuickSort O (n log n) O n2 In-Place
Partition recursively at each step.

BubbleSort O n2 In-Place
Traverse(t): 
SelectionSort O n2 In-Place
if t==null then return
Traverse n slots keeping score of the
→ print (t) //pre-order
Traverse(t.left) maximum. Swap it with A [n]. Repeat
→ (OR) print(t) //in-order for A [n − 1] .
Traverse(t.right) HeapSort O (n log n) Aux
→ (OR) print(t) //post-order InsertionSort Aux
MergeSort O (n log n) Aux

## Heaps Linear Time

Binary Binomial Fibonacci BucketSort Θ (n):
findMin Θ(1) Θ(1) Θ(1) If the range is known, make the appropriate number of buckets,
deleteMin Θ(log n) Θ(log n) O(log n) then:
insert Θ(log n) O(log n) Θ(1) 1. Scatter: Go over the original array, putting each object in its
decreaseKey Θ(log n) Θ(log n) Θ(1) bucket.
meld Θ(n) Θ(log n) Θ(1) 2. Sort each non-empty bucket (recursively or otherwise)
1
3. Gather: Visit the buckets in order and put all elements back Two-Level Hashing
into the original array.  
Pn−1 ni
CountSort Θ (n): The number of collisions per level: i=0 = |Col|
2
1. Given an array A bounded in the discrete range C, initialize 1. Choose m = n and h such that |Col| < n.
an array with that size. 2. Store the ni elements hashed to i in a small table of size n2i
2. Passing through A, increment every occurence of a number i using a perfect hash function hi .
in its proper slot in C. Random algorithm for constructing a perfect two level
3. Passing through C, add the number represented by i into A hash table:
a total of C [i] times. 1. Choose a random h from H(n) and compute the number of
RadixSort Θ (n): collisions. If there are more than n collisions, repeat.
1. Take the least significant digit. 2. For each cell i,if ni > 1, choose a random hash function from
2. Group the keys based on that digit, but otherwise keep the H(ni2). If there are any collisions, repeat.
original order of keys. (This is what makes the LSD radix sort Expected construction time – O(n)
a stable sort). Worst Case search time - O (1)
3. Repeat the grouping process with each more significant digit.

Union-Find
Selection
MakeSet(x) Union(x, y) Find(x)

QuickSelect O (n) O n2 O (1) O (1) O (α (k))
5-tuple Select Union by Rank: The larger tree remains the master tree in
every union.
Path Compression: every find operation first finds the master
Hashing root, then repeats its walk to change the subroots.
Universal Family: a family of mappings H. ∀h ∈ H. h : U →
1
[m] is universal iff ∀k1 6= k2 ∈ U : P rh∈H [h(k1 ) = h(k2 )] ≤ m Recursion
Example: If U = [p] = {0, 1, . . . , p − 1}then Hp,m =
Master Theorem: for T (n) = aT nb + f (n) ; a ≥ 1, b >

{ha,b | 1 ≤ a ≤ p; 0 ≤ b ≤ p} and every hash function is
ha,b (k) = ((ak + b) mod (p)) mod (m)  ε > 0:
1,
 
Linear Probing: Search in incremental order through the table 

 T (n) = Θ nlogb a f (n) = O nlogb (a)−ε
    
from h (x) until a vacancy is found. T (n) = Θ nlogb a logk+1 n

f (n) = Θ nlogb a logk n ; k ≥ 0
Open Addressing: Use h1 (x) to hash and h2 (x)to permute. 
 f (n) = Ω nlogb a+ε
No pointers.

T (n) = Θ (f (n))

af nb ≥ cf (n)
 
Open Hashing:
Perfect Hash: When one function clashes, try another. O (∞). Building a recursion tree: build one tree for running times (at
Load Factor α: The length of a possible collision chain. When T (αn)) and one for f (n).
|U | = n, α = mn.

Orders of Growth
Methods f x→∞
f = O (g) lim supx→∞ fg < ∞ f = o(g) g → 0
Modular: Multipilicative, Additive, Tabular(byte)-additive f = Θ (g) limx→∞ fg ∈ R+
f x→∞
f = Ω (g) lim inf x→∞ fg > 0 f = ω(g) g → ∞
Performance
Chaining E [X] Worst Case Amortized Analysis
1
Successful Search/Del 2 (1 + α) n
Potential Method: Set Φ to examine a parameter on data
Failed Search/Verified Insert 1+α n
stucture Di where i indexes the state of the structure. If ci is
the actual cost of action i, then cˆi = ci + Φ (Di ) − Φ (Di−1 ).
Probing Pn
The total potential amortized cost will then be
Pn Pn i=1 cˆi =
Linear: h (k, i) = (h0 (k) + i) mod m (c
i=1 i + Φ (D i ) − Φ (Di−1 )) = c
i=1 i + Φ (D i ) − Φ (D0 )
Quadratic:h k, i) = h0 (k) + c1 i + c2 i2 mod m Deterministic algorithm: Always predictable.

√ n
Double: h (k, i) = (h1 (k) + ih2 (k)) mod m Stirling’s Approximation: n! ∼ 2πn ne ⇒ log n! ∼
E [X] Unsuccessful Search Successful Search x log x − x
1 1
Uni. Probing ln 1
 1−α  2  α 1−α 
1 1 1 1
Lin. Probing 2 1 + 1−α 2 1 + 1−α
So Linear Probing is slightly worse but better for cache.
Collision Expectation: P [X ≤ 2E [X]] ≥ 12
So:
Scribbled by Omer Shapira, based on the course “Data Structures” at Tel Aviv
1. if m = n then E [|Col| < n] ≥ n2 University.
2. if m = n2 then E [|Col| < 1] ≥ 12 And with 2 there are no Redistribute freely.
collisions. Website: http://www.omershapira.com

## Вам также может понравиться

• DS Cheatsheet DS Cheatsheet
Omer Shapira
• Mementopython3 English Mementopython3 English
Gabriele Gatti
• CS2040Note CS2040Note
ChoonKit -
• Sessions4_5_AlgorithmAnalysis Sessions4_5_AlgorithmAnalysis
alex tyler
• Path Compression Path Compression
zhuqi316
• cheat sheet 1 cheat sheet 1
Charlie Zhang
• EE521_2006_02_15 EE521_2006_02_15
Bibek Boxi
• Orthogonal Frequency Division Multiplexing Orthogonal Frequency Division Multiplexing
hossam_kasem
• Putnam 2008 Putnam 2008
Sarthak Mishra
• Root Locus Root Locus
Priyan Jerome deepa2400
• lecture-4 lecture-4
Ijaz Talib
• Notes 2a 2018-19 Elementary Prime Theory Notes 2a 2018-19 Elementary Prime Theory
• Putnam_2008 Putnam_2008
Kamran Mehdiyev
• Binomial Heap Binomial Heap
tanuja
• Cs711 Mihir Sankalp Report Cs711 Mihir Sankalp Report
Mihir Vahanwala
• factorial.pdf factorial.pdf
Swapnaneel Bhattacharyya
• 12 12
Josh Man
• Magic 082 Notes Magic 082 Notes
Michael Gregory
• 12_DC 12_DC
• Csl Csl
Mohit Bansal
• me-lect5 me-lect5
abdo
• fibo fibo
Christina Robinson
• 2 1 Ordenation Algorithms 2 1 Ordenation Algorithms
Dany A. Córdova Ryan Blanc
• 2021_Putnam (1) 2021_Putnam (1)
Pătularu andrei
• Problem Set 1 Problem Set 1
Marc Asenjo
• 2IL50-2018-19-Q3-3-Heaps 2IL50-2018-19-Q3-3-Heaps
Jhon
• Práctica Fourier Series Práctica Fourier Series
MIGUEL ANGEL BARRERA TAMAYO
• 26-notes-mlmath-f17 26-notes-mlmath-f17
mamurtaza
• random-matrix-4_ppt random-matrix-4_ppt
Susa Ak
• lec07.pdf lec07.pdf
• Dois Discrete Math Dois Discrete Math
Gabriel Paul Tesoro
• Ehe Ehe
Gabriel Paul Tesoro
• DAA Week_04 DAA Week_04
ASIM
• HW 6 Solutions HW 6 Solutions
Antonio
• CSC263 Spring 2011 Assignment 1 Solutions CSC263 Spring 2011 Assignment 1 Solutions
yellowmoog
• lec20.pdf lec20.pdf
• Algorithms Algorithms
BrandonMaushund
• On_the_square_free_sieve.pdf On_the_square_free_sieve.pdf
Nikos Mantzakouras
• 2. Convergence of Fourier Series 2. Convergence of Fourier Series
Jelo Artoza
• D1LB4-Rocca.pdf D1LB4-Rocca.pdf
Viol Tailer Harry Haller
• Chapter 02 Chapter 02
Jack Ignacio Nahmías
• hw06.1.01.solutions hw06.1.01.solutions
ranv
• chap16 chap16
Gagne
• c3_torus.pdf c3_torus.pdf
laurindo_tais
• Neerc 2011 Analysis Neerc 2011 Analysis
xennygrimmato
• Calc1_week10 Calc1_week10
Roy Vesey
• assignment 1 solution assignment 1 solution
sams
• OCR A Level Mathematics Sample Question Paper OCR A Level Mathematics Sample Question Paper
A
• EE210_S22_LecSlides_02_2pp (1) EE210_S22_LecSlides_02_2pp (1)
Marcel Huber
• Depth of complete binary tree Depth of complete binary tree
Rashid Mahmood
• 39-parts 39-parts
Bernard Vincent Guitan Minero
• Amra Vjezbe 3 Amra Vjezbe 3
Amra Gvozden
• CVX_ProjectionIsNonexpansive CVX_ProjectionIsNonexpansive
Oscar Lazo Macedo
• H18-solution H18-solution
• Tables of Weber Functions: Mathematical Tables, Vol. 1 Tables of Weber Functions: Mathematical Tables, Vol. 1
I. Ye. Kireyeva
• On the Tangent Space to the Space of Algebraic Cycles on a Smooth Algebraic Variety. (AM-157) On the Tangent Space to the Space of Algebraic Cycles on a Smooth Algebraic Variety. (AM-157)
Mark Green
• Tables of the Function w (z)- e-z2 ? ex2 dx: Mathematical Tables Series, Vol. 27 Tables of the Function w (z)- e-z2 ? ex2 dx: Mathematical Tables Series, Vol. 27
K. A. Karpov
• Oracle Developer 2000 Oracle Developer 2000
Gurpreet Singh Munjal
• AMORE tutorial AMORE tutorial
Joel Schwartz
• An Android Application Sandbox System for Suspicious Software Detection An Android Application Sandbox System for Suspicious Software Detection
elvictorino
• 20765C_01 20765C_01
douglas
• SQL functions SQL functions
Mahendra Singh Tomar
• Android App Development Cheat Sheet Ootpapps.com Publication Android App Development Cheat Sheet Ootpapps.com Publication
Ben Rathi
• Detecting Source Code Plagiarism With CodeMatch Detecting Source Code Plagiarism With CodeMatch
Imam Muarif
• Electricity Billing System SRS1_2 Electricity Billing System SRS1_2
Sneha Jain Vipul Agarwal
• Expert Data Structures With c PDF Expert Data Structures With c PDF
Tisha
• 16 Conclusion 16 Conclusion
Anonymous WZvRdM
• PIC185452 Specifications PIC185452 Specifications
Sampath Kumar Duraisamy
• Update ZE-NC2010 en RevB 01 Update ZE-NC2010 en RevB 01
liv_ovidiu
• Common Problems and Solutions Common Problems and Solutions
• Configuracion Original Huawei Ar161f Configuracion Original Huawei Ar161f
Arquimedes
• 10 COMMAND GPON ZTE.txt 10 COMMAND GPON ZTE.txt
hikma ningsih
• CS_IT Gate Books List CS_IT Gate Books List
Satyam Gupta
• PD_FLOW PD_FLOW
UTt
• Oracle Interview Experience Oracle Interview Experience
Senthil Kumar Jayabalan
• Noisy Channel Theorem Noisy Channel Theorem
ChaseVetruba
• Unit 1 and 2 PPTs Unit 1 and 2 PPTs
Kundankumar Saraf
• Bdc Consums mat Bdc Consums mat
go2kaatt
• Operation Research-D.M.Marathe.docx Operation Research-D.M.Marathe.docx
akshay shinde
• 0.0.1.SAP Fiori Custom App Development _ SAP Blogs 0.0.1.SAP Fiori Custom App Development _ SAP Blogs
sathish
• cs332-Ex1-testBank cs332-Ex1-testBank
Peter Vasilescu
• Configuring Vim for C++ - Stack Overflow Configuring Vim for C++ - Stack Overflow
Frankie Liu
• All MATLAB Books Collection All MATLAB Books Collection
Srinivas Karatlapelli
• fdisk fdisk
Nicolas Fernando Schiappacasse Vega
• Lect1 - Algorithms and Flowchart Lect1 - Algorithms and Flowchart
MM Ayehsa Allian Schück