TOA4 Decrease and Conquer
TOA4 Decrease and Conquer
Theory of Algorithms –
Decrease and Conquer
James Gain
Department of Computer Science
University of Cape Town
jgain@cs.uct.ac.za
Introduction
Strategy:
Solve smaller instance
Extend solution of smaller instance to obtain solution to original
problem
Also called inductive or incremental
3 Variants:
Decrease by a constant
Decrease by a constant factor
Variable size decrease
Theory of Algorithms 2
Page 1
Theory of Algorithms 20 September 2021
Variants Illustrated
A PROBLEM OF
SIZE n
A SOLUTION TO
SUBPROBLEM
A SOLUTION TO THE
ORIGINAL PROBLEM
Theory of Algorithms 3
Page 2
Theory of Algorithms 20 September 2021
Theory of Algorithms 5
✗Weaknesses:
Less widely applicable (especially decrease by a constant factor)
Theory of Algorithms 6
Page 3
Theory of Algorithms 20 September 2021
Theory of Algorithms –
Decrease by a Constant
Insertion Sort
Idea:
Assume an already sorted list of size n-1
Insert the remaining element in the correct position
Before doing this recursively perform insertion sort on smaller list
Recursive idea but better performed iteratively bottom up
Decrease by 1 and conquer
Worst case efficiency – Θ(n2), but works well on partially
sorted inputs
Theory of Algorithms 8
Page 4
Theory of Algorithms 20 September 2021
Theory of Algorithms 9
Theory of Algorithms 10
10
Page 5
Theory of Algorithms 20 September 2021
Topological Sort
Problem:
In a directed acyclic graph, list the vertices in an order such that edge
direction is respected
That is for any directed edge the source vertex must appear before the
destination in the list
Sometimes multiple possible orderings
Not solvable if there are cycles
Can use Depth-First Search (from last year) to solve
Applications:
Ordering a set of courses that have pre-requisites
Evaluating formulae with dependencies in a spreadsheet
Ordering tasks in a complex project
Theory of Algorithms 11
11
12
Page 6
Theory of Algorithms 20 September 2021
Generating Permutations
Necessary as a component of Exhaustive Search
All possible orderings of numbers in range {1, …, n}
Can be used as indices for other objects
Solution:
Generate all (n-1)! permutations of {1, …, n-1}
Insert n into each possible position
During insert starting from the right or left alternately, satisfies
Minimal-Change requirement (next permutation obtained by
swapping two elements of previous)
Decrease by 1 and conquer
Theory of Algorithms 13
13
21 12
Example:
Start: 1
Insert 2: 12 21
Insert 3: 123 132 312 321 231 213
Theory of Algorithms 14
14
Page 7
Theory of Algorithms 20 September 2021
Johnson-Trotter Method
Alternative permutation generator that avoids
permutations of smaller lists
Use arrows to keep track of what permutation
comes next
An element k is mobile if its arrow points to an adjacent
element smaller than it
→ ← → ←
3 2 4 1
3 & 4 mobile. 1 & 2 not
Theory of Algorithms 15
15
Johnson-Trotter Algorithm
Theory of Algorithms 16
16
Page 8
Theory of Algorithms 20 September 2021
Johnson-Trotter Example
← ← ←
1 2 3
← ← ←
1 3 2 while last permutation
← ← ← has mobile elem:
3 1 2 - find its largest mobile element k
→ ← ← - swap k with neighbour it points to
3 2 1 - reverse direction of elements > k
← → ← - add the new permutation to the list
2 3 1
← ← →
2 1 3
Theory of Algorithms 17
17
Theory of Algorithms –
Decrease by a
Constant Factor
18
Page 9
Theory of Algorithms 20 September 2021
19
Multiplication a la Russe
Solution:
n*m = (n/2) * (2m) } if n is even
((n-1)/2 ) * (2m) + m } if n is odd
Using doubling and halving instead of multiplication is
efficient in hardware (just shift operations!)
Decrease by a constant factor of 2 and conquer
Example:
50 * 20 = (25 * 40) = (12 * 80) + 40 = (6 * 160) + 40
= (3 * 320) + 40 = (1 * 640) + 320 + 40 = 1000
Theory of Algorithms 20
20
Page 10
Theory of Algorithms 20 September 2021
Theory of Algorithms –
Variable Size Decrease
21
Euclid’s GCD
Problem:
Greatest Common Divisor of two integers m and n is the largest
integer that divides both exactly
Euclid’s Solution:
gcd(m, n) = gcd(n, m mod n)
gcd(m, 0) = m
Right-side args are smaller by neither a constant size nor factor
Example:
gcd(60, 24) = gcd(24, 12) = gcd(12, 0) = 12
Theory of Algorithms 22
22
Page 11
Theory of Algorithms 20 September 2021
23
Interpolation Search
Mimics the way humans search through a phone book (look
near the beginning for ‘Brown’)
Assumes that values between the leftmost (A[b]) and
rightmost (A[u]) elements increase linearly
value
Algorithm (key = v, find search index = i):
A[u]
Binary search with floating variable
v
at index i
Setup straight line through (b, A[b])
and (u, A[u]) A[b]
24
Page 12