Lecture 5
Lecture 5
Example: Sort 6, 4, 1, 8, 5
6|4 1 8 5
4 6|1 8 5
1 4 6|8 5
1 12/24/2024
4 6 8|5
University of gadarif, Presented by: Mutasim Idreis
1 4 5 6 8 4
Decrease-and-Conquer
Pseudocode of Insertion Sort
12/24/2024
University of gadarif, Presented by: Mutasim Idreis
5
12/24/2024
University of gadarif, Presented by: Mutasim Idreis
6
Decrease-and-Conquer
Analysis of Insertion Sort
Time efficiency
Cworst(n) = n(n-1)/2 Θ(n2)
Cavg(n) ≈ n2/4 Θ(n2)
Cbest(n) = n - 1 Θ(n) (also fast on almost sorted arrays)
c d c d
tiger
human
fish
sheep
shrimp
plankton
12/24/2024
wheat
University of gadarif, Presented by: Mutasim Idreis
9
Decrease-and-Conquer
DFS-based Algorithm
DFS-based algorithm for topological sorting
• Perform DFS traversal, noting the order vertices are
popped off the traversal stack
• Reverse order solves topological sorting problem
• Back edges encountered?→ NOT a dag!
Example:
a b c d
e f g h
12/24/2024
Efficiency: Ꝋ(𝒗𝟐 ) 10
12/24/2024
University of gadarif, Presented by: Mutasim Idreis
11
Decrease-and-Conquer
Source Removal Algorithm
Source removal algorithm
Repeatedly identify and remove a source (a vertex with no
incoming edges) and all the edges incident to it until either
no vertex is left (problem is solved) or there is no source
among remaining vertices (not a dag)
Example:
a b c d
e f g h
12/24/2024
Efficiency: same University
as efficiency of the
of gadarif, Presented DFS-based
by: Mutasim Idreis algorithm
12
12/24/2024
University of gadarif, Presented by: Mutasim Idreis
13
Decrease-and-Conquer
Generating Permutations
Minimal-change decrease-by-one algorithm
If n = 1 return 1; otherwise, generate recursively the list of all
permutations of 12…n-1 and then insert n into each of
those permutations by starting with inserting n into 12...n-1
by moving right to left and then switching direction for
each new permutation
Example: n=3
start 1
insert 2 into 1 right to left 12 21
insert 3 into 12 right to left 123 132 312
insert 3 into 21 left to right 321 231 213
12/24/2024
University of gadarif, Presented by: Mutasim Idreis
14
Decrease-and-Conquer
Other permutation generating algorithms
Johnson-Trotter (p. 145)
Heap’s algorithm ()
12/24/2024
University of gadarif, Presented by: Mutasim Idreis
15
Decrease-and-Conquer
Generating Subsets
Binary reflected Gray code: minimal-change algorithm for
generating 2n bit strings corresponding to all the subsets of
an n-element set where n > 0
If n=1 make list L of two bit strings 0 and 1
else
generate recursively list L1 of bit strings of length n-1
copy list L1 in reverse order to get list L2
add 0 in front of each bit string in list L1
add 1 in front of each bit string in list L2
append L2 to L1 to get L
return L
12/24/2024
University of gadarif, Presented by: Mutasim Idreis
16
Decrease-and-Conquer
Decrease-by-Constant-Factor Algorithms
In this variation of decrease-and-conquer, instance size
is reduced by the same factor (typically, 2)
Examples:
• binary search and the method of bisection
• exponentiation by squaring
• fake-coin puzzle
12/24/2024
• University of gadarif, Presented by: Mutasim Idreis
Josephus problem 17
Decrease-and-Conquer
Binary Search
Very efficient algorithm for searching in sorted array:
K
vs
A[0] . . . A[m] . . . A[n-1]
If K = A[m], stop (successful search); otherwise, continue
searching by the same method in A[0..m-1] if K < A[m]
and in A[m+1..n-1] if K > A[m]
l 0; r n-1
while l r do
m (l+r)/2
if K = A[m] return m
else if K < A[m] r m-1
else l m+1
12/24/2024
return -1 University of gadarif, Presented by: Mutasim Idreis
18
Decrease-and-Conquer
Analysis of Binary Search
Time efficiency
• worst-case recurrence: Cw (n) = 1 + Cw( n/2 ), Cw (1) = 1
solution: Cw(n) = log2(n+1)
n * m = n * 2m
2
n * m = n – 1 * 2m + m if n > 1 and m if n = 1
2
12/24/2024
University of gadarif, Presented by: Mutasim Idreis
20
Decrease-and-Conquer
Example of Russian Peasant Multiplication
Compute 20 * 26
n m
20 26
10 52
5 104 104
2 208 +
1 416 416
520
Note: Method reduces to adding m’s values corresponding to
odd n’s.
12/24/2024
University of gadarif, Presented by: Mutasim Idreis
21
Decrease-and-Conquer
Variable-Size-Decrease Algorithms
In the variable-size-decrease variation of decrease-and-
conquer, instance size reduction varies from one
iteration to another
Examples:
• Euclid’s algorithm for greatest common divisor
• partition-based algorithm for selection problem
• interpolation search
• some algorithms on binary search trees
• Nim and Nim-like games
12/24/2024
University of gadarif, Presented by: Mutasim Idreis
22
Decrease-and-Conquer
Euclid’s Algorithm
Euclid’s algorithm is based on repeated application of equality
gcd(m, n) = gcd(n, m mod n)
12/24/2024
University of gadarif, Presented by: Mutasim Idreis
23
Decrease-and-Conquer
Selection Problem
Find the k-th smallest element in a list of n numbers
k = 1 or k = n
median: k = n/2
Example: 4, 1, 10, 9, 7, 12, 8, 2, 15 median = ?
12/24/2024
University of gadarif, Presented by: Mutasim Idreis
24
Decrease-and-Conquer
Algorithms for the Selection Problem
The sorting-based algorithm: Sort and return the k-th element
Efficiency (if sorted by mergesort): Θ(nlog n)
12/24/2024
University of gadarif, Presented by: Mutasim Idreis
26
Thank you
12/24/2024
University of gadarif, Presented by: Mutasim Idreis
27