Algorithms-C1-Foundations

Keywords: InsertSort, MergeSort, Divide and Conquer, C++

Getting Started

InsertionSort

1
2
3
4
5
6
7
8
9
INSERTION-SORT(A)
for j = 2 to A.length
key = A[j]
// Insert A[j] into the sorte sequence A[1...j-1].
i = j - 1
while i > 0 and A[i] > key
A[i + 1] = A[i]
i = i - 1
A[i + 1] = key

conventions in our pseudocode:

  1. Indentation indicates block structure.
  2. The looping constructs while, for, and repeat-until and the if-else conditional construct have interpretations similar to those in C, C++, Java, Python, and Pascal.
  3. We typically organize compound data into objects, which are composed of attributes. A.length

MergeSort

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
MERGE(A,p,q,r)
n_1 = q - p + 1
n_2 = r - q
let L[1..n_1+1] and R[1..n_2+1] be new arrays
for i = 1 to n_1
L[i] = A[p + i - 1]
for j = 1 to n_2
R[j] = A[q + j]
L[n_1 + 1] = infty
R[n_2 + 1] = infty
i = 1
j = 1
for k = p to r
if L[i] <= R[j]
A[k] = L[i]
i = i + 1
else
A[k] = R[j]
j = j + 1

MERGE-SORT(A,p,r)
if p < r
q = floor (p + r)/2
MERGE-SORT(A, p, q)
MERGE-SORT(A, q + 1, r)
MERGE(A, p, q, r)

Divide-and-Conquer

the maximum-subarray problem

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
FIND-MAX-CROSSING-SUBARRAY(A,low,mid,high)
left-sum = -infty
sum = 0
for i = mid downto low
sum = sum + A[i]
if sum > left-sum
left-sum = sum
max-left = i
right-sum = -infty
sum = 0
for j = mid+1 to high
sum = sum + A[j]
if sum > right-sum
right-sum = sum
max-right = j
return (max-left, max-right, left-sum+right-sum)

FIND-MAXIMUM-SUBARRAY(A,low,high)
if high == low
return (low, high, A[low])
else
mid = (low + high) / 2
(left-low, left-high, left-sum) = FIND-MAXIMUM-SUBARRAY(A, low, mid)
(right-low, right-high, right-sum) = FIND-MAXIMUM-SUBARRAY(A, mid+1, high)
(cross-low, cross-high, cross-sum) = FIND-MAX-CROSSING-SUBARRAY(A, low, mid, high)
if left-sum >= right-sum and left-sum >= cross-sum
return(left-low, left-high, left-sum)
else if right-sum >= left-sum and right-sum >= cross-sum
return(right-low, right-high, right-sum)
else return (cross-low, cross-high, cross-sum)

Strassen’s algorithm for matrix multiplication

1
2
3
4
5
6
7
8
9
SQUARE-MATRIX-MULTIPLY(A,B)
n = A.rows
Let C be a new n x n matrix
for i = 1 to n
for j = 1 to n
c_ij = 0
for k = 1 to n
c_ij = c_ij + a_ik b_kj
return C

this simple divide-and-conquer approach is no faster than the straightforward SQUARE-MATRIX-MULTIPLY procedure.

1
2
3
4
5
6
7
8
9
10
11
12
SQUARE-MATRIX-MULTIPLY-RECURSIVE(A,B)
n = A.rows
Let C be a new n x n matrix
if n == 1
c_11 = a_11 b_11
else
partition A,B,and C as in equations(4.9)
C_11 = SQUARE-MATRIX-MULTIPLY-RECURSIVE(A_11,B_11) + SQUARE-MATRIX-MULTIPLY-RECURSIVE(A_12,B_21)
C_12 = SQUARE-MATRIX-MULTIPLY-RECURSIVE(A_11,B_12) + SQUARE-MATRIX-MULTIPLY-RECURSIVE(A_12,B_22)
C_21 = SQUARE-MATRIX-MULTIPLY-RECURSIVE(A_21,B_11) + SQUARE-MATRIX-MULTIPLY-RECURSIVE(A_22,B_21)
C_22 = SQUARE-MATRIX-MULTIPLY-RECURSIVE(A_21,B_12) + SQUARE-MATRIX-MULTIPLY-RECURSIVE(A_22,B_22)
return C

$$
A =
\begin{bmatrix}
A_{11} & A_{12}\\
A_{21} & A_{22}
\end{bmatrix},
B =
\begin{bmatrix}
B_{11} & B_{12}\\
B_{21} & B_{22}
\end{bmatrix},
$$

$$
C =
\begin{bmatrix}
C_{11} & C_{12}\\
C_{21} & C_{22}
\end{bmatrix}
=\begin{bmatrix}
A_{11} & A_{12}\\
A_{21} & A_{22}
\end{bmatrix}\begin{bmatrix}
B_{11} & B_{12}\\
B_{21} & B_{22}
\end{bmatrix}
\tag{4.9}
$$

Probabilistic Analysis and Randomized Algorithms

the hiring problem

1
2
3
4
5
6
7
HIRE-ASSISTANT(n)
best = 0 //candidate 0 is a least-qualified dummy candidate
for i = 1 to n
interview candidate i
if candidate i is better than candidate best
best = i
hire candidate i

Comments

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×