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

Fady Ebeid - Data Structures and Algorithms

Uploaded by

Adam Won
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)
13 views

Fady Ebeid - Data Structures and Algorithms

Uploaded by

Adam Won
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
You are on page 1/ 4

Data Structures and Algorithms - Formula Sheet - by Fady Morris Ebeid (2024)

. .
Data Structures and Algorithms .
. 3.2 Binary Search .
. Iterative Version
. .
. .
Formula Sheet .
.
.
. Algorithm 2: BinarySearchIt(A[1. . . n], key)
. .
Fady Morris Ebeid .
.
Complexity .
. Data: Sorted Sequence A[1 . . . n], key
. .
(2024) .
.
.
.
Result: Index of key, -1 if not found
. . 1 low ← 1
. To search for a key, the algorithm makes a single recursive call for . 2 high ← n
. .
. .
Chapter 1 .
.
.
a problem of size n/2. Outside this call it spends time O(1).
Therefore a = 1, b = 2, d = 0.
.
.
.
3 while low ≤ high:
high - low

. . 4 mid ← low +
2
Intro .
.
.
.
.
. 5 if key = A[mid] :
. n . 6 return mid
. .
1 Series .
.
T (n) = T + O(1) .
. 7 elif key < A[mid] :
. 2 .
Geometric series: . . 8 high = mid - 1
n−1
. . 9 else:
. .
X
ari = a + ar + ar2 + . . . + arn−1 . . 10 low = mid + 1
. Unwinding the recurrence relations: .
. .
i=0 . . return low - 1
. . 11
1 − rn . .
=a . .
1−r . T (n) = T (n/2) + c .
. . Binary Search with Duplicates
. = T (n/4) + 2c .
Order of growth of a geometric series: . .
. .
. = T (n/8) + 3c .

n . . Algorithm 3: BinarySearch(A[1. . . n], key)
n Θ(r ) if r > 1,
 . .
X . .. .
ri = Θ(n) if r = 1, . . . Data: Sorted Sequence A[1 . . . n], key
. .
. . Result: Index of key, -1 if not found

i=0 
Θ(1) if r < 1 . k
= T (n/2 ) + kc .
. . 1 low ← 1
Arithmetic series: . .
. .. . 2 high ← n
n . .
X . . . 3 while low < high:
i = 1 + 2 + ... + n . . low + high

. = T (1) + log2 n · c .
i=1 . . 4 mid ←
. . 2
n(n + 1) . = O(log n) .
= . . 5 if key <= A[mid] :
. .
2 . . 6 high = mid
. .
Harmonic Series: . . 7 else:
. .
. . 8 low = mid + 1
n . .
X 1 1 1 1 1 1 . Recursive Version .
= 1 + + + + + ... + . . 9 if A[left] == key:
i=1
i 2 3 4 5 n . .
. . 10 return low
. .
= ln n + γ . . 11 else:
. .
. Algorithm 1: BinarySearch(A[1. . . n], low, high, . 12 return -1
Where γ ≈ 0.577 is the Euler-Mascheroni constant . .
. .
. key) .
. .
2 Complexity Comparison .
. Data: Sorted Sequence A[1 . . . n], low, high, key .
. 3.3 Sorting
√ . .
log n ≺ n ≺ n ≺ n log n ≺ n2 ≺ n3 ≺ 2n . Result: Index of key, -1 if not found .
. if high < low : . Merge Sort
. 1 .
. return low -1 .
3 Divide and Conquer .
.
2 .
. Complexity
. .
 
high - low
3.1 Master Theorem l n m .
. 3 mid ← low + .
. The algorithm breaks an array of length n into two subarrays of
2
 
Theorem 3.1 (Master Theorem). If T (n) = aT + O nd . . size n/2 and sorts them recurisvely, then merges the output. Time
b . 4 if key = A[mid] : .
. . spent before and after the recursive calls is O(n). Therefore
(for consants a > 0, b > 1, d ≥ 0), then: . 5 return mid .
. . a = 2, b = 2, d = 1.
. 6 elif key < A[mid] : .
. .
  
d
O n if d > logb a . return BinarySearch(A, low, mid - 1, key) .
 7 n
. .

. . T (n) = 2T + O(n)
 
T (n) = O nd log n if d = logb a . 8 else: . 2
 . 9 return BinarySearch(A, mid + 1, high, key) .
. .
 
O nlogb a

if d < logb a Unwinding:

1
Data Structures and Algorithms - Formula Sheet - by Fady Morris Ebeid (2024)
. .
. . Randomized QuickSort
.
.
Algorithm 6: CountSort(A[1. . . n]) .
.
n
. .
T (n) = 2T + cn . Data: A[1 . . . n] with elements that are all integers from 1 .
2 . .
 n . to M .
n n
. . Algorithm 9: RandomizedQuickSort(A, ℓ, r)
= 2 2T +c· + cn = 4T + 2cn . Result: A’[1 . . . n] .
4 2 4 . Count[1 . . . M] ← [0, . . . , 0] .
 n n n . 1 . Data: A[1 . . . n], ℓ, r
= 4 2T +c· + 2cn= 8T + 3cn . for i from 1 to n: .
. 2 . 1 if ℓ ≥ r:
8 4 8 . Count[A[i]] ← Count[A[i]] + 1 .
. 3 . 2 return
.. . .
. /* k appears Count[k] times in A */ .
. . . 3 k ← random number between ℓ and r
. 4 Pos[1 . . . M] ← [0, . . . , 0] . swap A[ℓ] and A[k]
n . . 4
= 2k T + kcn . 5 Pos[1] ← 1 . m ← Partition(A, ℓ, r)
2k . . 5
. 6 for j from 2 to M : . /* A[m] is in the final position */
.. . .
. 7 Pos[j] ← Pos[j - 1] + Count[j - 1] . RandomizedQuickSort(A, ℓ, m − 1)
. . . 6
. . RandomizedQuickSort(A, m + 1, r)
. /* k will occupy range [Pos[k]...Pos[k + 1] - 1] */ . 7
= nT (1) + log2 n · cn . .
. 8 for i from 1 to n: .
= O(n log n) . A’[Pos[A[i]]] ← A[i] .
. 9 .
. Pos[A[i]] ← Pos[A[i]] + 1 .
. 10 .
Algorithm . . QuickSort with Tail Recursion Elimination:
. 11 return A’ .
. .
Algorithm 4: Merge Sort(A[1 . . . n]) . .
. .
. .
Data: Sequence A[1 . . . n] . .
. . Algorithm 10: QuickSort(A[1. . . n], ℓ, r)
Result: Permutation A′ [1 . . . n] of A in non-decreasing . .
. Quick Sort .
order . . Data: A[1 . . . n], ℓ, r
. .
1 if n = 1 : . . 1 while ℓ < r:
. .
2 return A . . 2 m ← Partition(A, ℓ, r)
. Average case: O(n log n) .
jnk . . 3 if (m − ℓ) < (r − m):
m← . .
3 . . 4 QuickSort(A, ℓ, m − 1)
2 . Worst case: O(n2 ) .
4 B ← MergeSort(A[1 . . . m]) . . 5 ℓ←m+1
. .
5 C ← MergeSort(A[m + 1 . . . n]) . . 6 else:
. .
A′ ← Merge(B,C ) . . QuickSort(A, m + 1, r)
6
. Algorithm 7: QuickSort(A[1. . . n], ℓ, r) . 7

return A′ . . 8 r ←m−1
7 . Data: A[1 . . . n], ℓ, r .
. .
. if ℓ ≥ r: .
. 1 .
Algorithm 5: Merge(B[1. . . p], C[1. . . q]) . return .
. 2 .
. . Worst-case space requirements: O(log n).
Data: Sequences B[1 . . . p], C[1 . . . q] . 3 m ← Partition(A, ℓ, r) .
. .
/* B and C re sorted */ . /* A[m] is in the final position */ .
. .
1 D ← empty array of size p + q . 4 QuickSort(A, ℓ, m − 1) .
. .
2 while B and C are both non-empty: . 5 QuickSort(A, m + 1, r) . Randomized QuickSort with Equal Elements
. .
3 b ← the first element of B . .
. .
4 c ← the first element of C . .
. .
5 if b ≤ c: . . Sort a given sequence of numbers that may contain duplicates.
.
.
Algorithm 8: Partition2(A[1. . . n], ℓ, r) .
.
6 move b from B to the end of D
. .
7 else: . Data: A[1 . . . n], ℓ, r .
. . Algorithm 11: RandomizedQuickSort(A, ℓ, r)
8 move c from C to the end of D . Result: j .
. .
. 1 x ← A[ℓ] /* pivot */ . Data: A[1 . . . n], ℓ, r
9 move the rest of B and C to the end of D . j←ℓ .
. 2 . if ℓ ≥ r:
10 return D . for i from ℓ + 1 to r: . 1
. 3 . 2 return
. if A[i] ≤ x: .
. 4 .
. j ←j+1 . 3 k ← random number between ℓ and r
Count Sort . 5 .
. swap A[j] and A[i] . 4 swap A[ℓ] and A[k]
. 6 .
Is a non-comparison based sorting algorithm. . . 5 (m1 , m2 ) ← Partition3(A, ℓ, r)
. /* A[ℓ + 1 . . . j] ≤ x, A[j + 1 . . . r] > x */ .
Useful if the array contents are small integers that have large . . /* A[m] is in the final position */
. .
frequencies. . 7 swap A[ℓ] and A[j] . 6 RandomizedQuickSort(A, ℓ, m1 − 1)
. .
. 8 return j . 7 RandomizedQuickSort(A, m2 + 1, r)
The complexity is O(n + M ).
2
Data Structures and Algorithms - Formula Sheet - by Fady Morris Ebeid (2024)
. .
. Complexity: O(mn) space and time. .
Algorithm 12: Partition3(A[1. . . n], ℓ, r) .
.
.
.
Algorithm 16: Knapsack(W, weights[w1 . . . wn ],
. . vals[v1 . . . vn ])
Data: A[1 . . . n], ℓ, r . .
. Algorithm 14: GetLCS2(A[1..m], B[1..n]) .
Result: m2 . . Data: Weights w1 , . . . , wn , values v1 , . . . , vn , and total
x ← A[ℓ] /* pivot */ . .
1 . for j ← 0 to n: . weight W
m1 ← ℓ . 1 .
2 . LCS[0, j] ← 0 . Result: The maximum value of items whose weight
m2 ← ℓ . 2 .
3 . . doesn’t exceed W . Each item can be used at most
for i from ℓ + 1 to r: . 3 for i ← 1 to m: .
4 . . once.
if A[i] ≤ x: . 4 LCS[i, 0] ← 0 .
5 . . 1 initialize all value[0, j] ← 0
m2 ← m2 + 1 . 5 for j ← 1 to n: .
6 . . 2 initialize all value[w, 0] ← 0
swap A[i] and A[m2 ] . 6 if A[i] = B[j]: .
7
. . 3 for i from 1 to n:
. 7 LCS[i, j] ← LCS[i − 1, j − 1] + 1 . for w from 1 to W :
8 if A[m2 ] < A[m1 ]: . . 4
. 8 else: . value[w, i] ← value[w, i-1]
9 swap A[m1 ] and A[m2 ] . . 5
. 9 LCS[i, j] ← max{LCS[i, j − 1], LCS[i − 1, j]} . if wi ≤ w:
10 m1 ← m1 + 1 . . 6
. . val ← value[w − wi , i − 1] + vi
/* A[ℓ + 1 . . . m1 ] ≤ x, A[m2 + 1 . . . r] > x */ . . 7
. 10 return LCS[m, n] . if value[w, i] < val:
. . 8
11 return (m1 , m2 ) . . 9 value[w, i] ← val
. .
. .
. .
. .
. .
4 Dynamic Programming .
. 4.3 Knapsack .
.
10 return value[W, n]
4.1 Edit Distance . .
. .
. .
[Eri19, p. 129] [KP18, p. 195] .
. Knapsack with Repititions .
. 4.4 Placing Parentheses
The Edit function satisfies the following recurrence: . .
. . [KP18, p. 226]
. .
. .
. .

i if j = 0 Example:


 .
.
Algorithm 15: Knapsack(W, weights[w1 . . . wn ], .
.
j if i = 0 . .


vals[v1 . . . vn ]) How to place parentheses to maximize the expression
 . .
Edit(i, j) =  Edit(i, j − 1) + 1  .
.
.
.
5 − 8 + 7 × 4 − 8 + 9.
Data: Weights w1 , . . . , wn , values v1 , . . . , vn , and total
 

 Edit(i − 1, j) + 1 otherwise . . Solution:

 . weight W .

Edit(i − 1, j − 1) + [A[i] ̸= B[j]]

 . .
. Result: The maximum value of items whose weight . The maximum is 200
. .
Complexity: O(mn) space and time. . doesn’t exceed W .
. . Given by 5 − (8 + 7) × (4 − (8 + 9))
. 1 value[0] ← 0 .
Algorithm 13: EditDistance(A[1..m], B[1..n]) . .
. 2 for w from 1 to W : . Subproblems:
. .
1 for j ← 0 to n: . 3 value[w] ← 0 . Let Ei,j be the subexpression
. .
2 Edit[0, j] ← j . 4 for i from 1 to n: .
. if wi ≤ w: .
. 5 . di opi . . . opj−1 dj
3 for i ← 1 to m: . val ← value[w − wi ] + vi .
. 6 .
4 Edit[i, 0] ← i . if val > value[w]: .
. 7 .
5 for j ← 1 to n: . value[w] ← val . M (i, j) = Maximum value of Ei,j
. 8 .
6 ins ← Edit[i, j − 1] + 1 . .
. . m(i, j) = Minimum value of Ei,j
7 del ← Edit[i − 1, j] + 1 . .
rep ← Edit[i − 1, j − 1] . return value[W ] .
8 . 9 .
if A[i] ̸= B[j]: . . 
9 . . M (i, k) opk M (k + 1, j)
rep ← rep + 1 . . 

10 . . M (i, k) opk m(k + 1, j)
. . M (i, j) = max
11 Edit[i, j] ← min{ins, del, rep} . . m(i, k)
i≤k≤j−1  opk M (k + 1, j)
. Knapsack without Repititions .
. . 
m(i, k) opk m(k + 1, j)

. .
12 return Edit[m, , n] . .
. .
. Subproblems: .
. . 
4.2 Longest Common Subsequence . .  M (i, k) opk M (k + 1, j)
. . 

[KP18, p. 205] . . M (i, k) opk m(k + 1, j)
. value[w, i] = max{value[w − wi , i − 1] + vi , value[w, i − 1]} . m(i, j) = min
. . i≤k≤j−1  m(i, k) opk M (k + 1, j)
 . . 
. . 
0 if i = 0 or j = 0 m(i, k) opk m(k + 1, j)

 . .
LCS(j, j) = LCS(i − 1, j − 1) + 1 . .
if A[i] = B[j] . .
. .
Running time: O(nW ) Running time: O(n3 )

max{LCS(i, j − 1), LCS(i − 1, j)} otherwise

3
Data Structures and Algorithms - Formula Sheet - by Fady Morris Ebeid (2024)
. .
. .
Algorithm 17: MinAndMax(i,j) .
.
.
.
Data: M , m : 2D Matrices holding the maximum and . .
. .
minimum values, respectively. . .
. .
min ← +∞ . .
1 . .
max ← −∞ . .
2 . .
3 for k from i to j − 1: . .
. .
4 a ← M (i, k) opk M (k + 1, j) . .
. .
5 b ← M (i, k) opk m(k + 1, j) . .
. .
6 c ← m(i, k) opk M (k + 1, j) . .
. .
7 d ← m(i, k) opk m(k + 1, j) . .
. .
8 min ← min(min, a, b, c, d) . .
. .
9 max ← max(max, a, b, c, d) . .
. .
. .
10 return (min, max) . .
. .
. .
. .
Algorithm 18: Parentheses(d1 op1 d2 . . . opn−1 dn ) . .
. .
. .
Data: A sequence of digits d1 , . . . dn and a sequence of . .
. .
operations op1 , . . . opn ∈ {+, −, ×} . .
. .
Result: Maximum value that can be obtained by optmial . .
. .
parenthesizing of the expression . .
. .
1 for i from 1 to n: . .
. .
2 m(i, i) ← di , M (i, i) ← di . .
. .
. .
3 for s from 1 to n − 1: . .
. .
4 for i from 1 to n − s: . .
. .
5 j ←i+s . .
. .
6 m(i, j), M (i, j) ← MinAndMax(i, j) . .
. .
. .
7 return M (1, n) . .
. .
. .
. .
. .
. .
. .
. .
. .
. .
. .
. .
. .
. .
. .
. .
. .
. .
. .
. .
. .
. .
. .
. .
© 2024 Fady Morris Ebeid . .
https://github.com/FadyMorris/formula-sheets . .
. .
. .
. .
. .
References .
.
.
.
.
.
.
.
.
.
. .
[Eri19] J. Erickson. Algorithms. Jeff Erickson, 2019. isbn: . .
. .
9781792644832. url: https: . .
. .
//books.google.com.eg/books?id=K1uIxwEACAAJ. . .
. .
. .
[KP18] Alexander S Kulikov and Pavel Pevzner. Learning . .
. .
Algorithms Through Programming and Puzzle Solving. . .
. .
Active Learning Technologies, 2018. isbn: . .
9780985731212. url: https://cogniterra.org/a/24. . .
. .
. .

You might also like