Decrease & Conquer
Decrease & Conquer
Decide on : algorithm
design techniques etc.
Design an algorithm
Prove correctness
Read Chapter 4
Decrease and Conquer
Bottom-up: iterative
Top-down: recursive
Department of Computer Science
Top Down and Iterative
a problem of size n
subproblem
Recursive
of size n-1
a solution to the
subproblem
e.g., n!
a solution to
the original problem 11
Iterative
Department of Computer Science
Example-1: Computing
Brute Force Example a n
BF
Logic/Idea
29 34 45 68 89 90 | 17 29 45 68 89 90 | 34 17
45 68 89 | 90 29 34 17
29 34 45 68 89 90 | 17 29 45 68 89 90 | 34 17
45 68 89 | 90 29 34 17
45 68 89 | 90 29 34 17
45 68 89 90 | 29 34 17
29 45 68 89 90 | 34 17
29 34 45 68 89 90 | 17
17 29 34 45 68 89 90
ALGORITHM InsertionSortIter(A[0..n-1])
//An iterative implementation of insertion sort
//Input: An array A[0..n-1] of n orderable elements
//Output: Array A[0..n-1] sorted in nondecreasing order
key = A[i]
for j i – 1 to 0 do // j: the index of the sorted part of the array
if (key< A[j]) //compare the key with each element in the sorted part
A[j+1] A[j]
else
break
A[j +1] key //insert the key to the sorted part of the array
In place? Stable ?
Department of Computer Science
Insertion Sort: Decrease by one(Recursive Solution)
ALGORITHM InsertionSortRecur(A[0..n-1], n-1)
//A recursive implementation of insertion sort
//Input: An array A[0..n-1] of n orderable elements Index of the last element
//Output: Array A[0..n-1] sorted in nondecreasing order
if n > 1
InsertionSortRecur(A[0..n-1], n-2)
Insert(A[0..n-1], n-1) //insert A[n-1] to A[0..n-2]
Index of the element/key to be inserted.
ALGORITHM Insert(A[0..m], m)
//Insert A[m] to the sorted subarray A[0..m-1] of A[0..n-1]
//Input: A subarray A[0..m] of A[0..n-1]
//Output: Array A[0..m] sorted in nondecreasing order
key = A[m]
for j m – 1 to 0 do
if (key< A[j])
A[j+1] A[j]
else
break
A[j +1] Department
key of Computer Science 24
Example -2
Recurrence relation:
T(n) = T(n-1) + n
T(1) = 1
Telescoping:
T(n) = T(n-1) + n
T(n-1) = T(n-2) + n-1
T(n-2) = T(n-3) + n-2
…
T(2) = T(1 ) + 1
T (n) = T (n − 1) + n
T(n) = T(n-1) + n
Guess: T(n) = O(n2)
Induction goal: T(n) ≤ c n2, for some c and n ≥ n0
Induction hypothesis: T(n-1) ≤ c(n-1)2 for all k < n
BF
Adjacency matrix
n x n boolean matrix if |V| is n.
The element on the ith row and jth column is 1 if there’s an
edge from ith vertex to the jth vertex; otherwise 0.
The adjacency matrix of an undirected graph is symmetric.
It takes (comparisons) to figure out all the adjacent
nodes of a certain node i.
Adjacency linked lists
A collection of linked lists, one for each vertex, that contain all
the vertices adjacent to the list’s vertex.
An example
The idea
traverse “deeper” whenever possible.
On each iteration, the algorithm proceeds to an unvisited vertex that is
adjacent to the one it is currently in. If there are more than more
neighbors, break the tie by the alphabetic order of the vertices.
When reaching a dead end ( a vertex with no adjacent unvisited
vertices), the algorithm backs up one edge to the parent and tries to
continue visiting unvisited vertices from there.
The algorithm halts after backing up to the starting vertex, with the
latter being a dead end.
Similar to preorder tree traversals
It’s convenient to use a stack to trace the operation of depth-first
search.
Push a vertex onto the stack when the vertex is reached for the first
time.
Pop a vertex off the stack when it becomes a dead end.
An example
a b c d
e f g h
a b c d
e f g h
Applications:
Checking connectivity
Finding connected components
The idea
Traverse “wider” whenever possible.
Discover all vertices at distance k from s (on level k)
before discovering any vertices at distance k +1 (at
level k+1)
Similar to level-by-level tree traversals
Instead of a stack, breadth-first uses a queue.
a b c d
e f g h
Breadth-first traversal:
abefgchd
BFS(G)
count 0
bfs(v)
mark each vertex with 0
for each vertex v∈ V do count count + 1
if v is marked with 0 mark v with count //visit v
bfs(v) initialize queue with v //enqueue
while queue is not empty do
a front of queue //dequeue
for each vertex w adjacent to a do
if w is marked with 0 //w hasn’t been visited.
count count + 1
mark w with count //visit w
add w to the end of the queue //enqueue
remove a from the front of the queue
a b c d
e f g h
Breadth-first traversal:
abefghcd
Logic/Idea
Department of Computer Science 43
Directed Graph or Digraph
a 0 1 1 0 0
a b
b 1 0 1 0 0
c c 0 0 0 0 0
0 0 1 0 1
d d
0 0 0 0 0
e
a b c
e
b a c Not symmetric
c
d c e
e One node for one edge
Department of Computer Science
Directed Graph or Digraph
a b a b c
b a c
c c
d c e
e
d
a d
Tree edge
e
Back edge
b e
Forward edge
Directed cycle: a, b, a
Cross edge
c
DFS forest
a b a b
not a DAG
c d c d
DAG
C4 requires C3 C3
C5 requires C3 and C4
C2 C5
Example:
a b c d
e f g h
a b c d
e f g h
Other Examples:
Finding cycle in a graph.
Operation System deadlock detection.
Dependency resolution.
Sentence Ordering.
Critical Path Analysis.
Course Schedule problem.
Effectively, when
power is not
Suppose we want to compute 95
divisible by 2, we
make power even
by taking out the
extra 9. Then we
already know the
solution when
power is divisible by
2. Divide the power
Department of Computer Science 57
by 2 and multiply
Example-1: Computing
Brute Force Example a n
a problem of size n
subproblem
of size n/2
a solution to the
subproblem
a solution to
the original problem 61
Logic/Idea
repeatedly dividing the search interval in half.
Department of Computer Science 62
Example-2:
Brute Searching a Key
Force Example
nBegin with the mid element of the whole array as a search key.
nIf the value of the search key is equal to the item then return an index
of the search key.
nOr if the value of the search key is less than the item in the middle of
the interval, narrow the interval to the lower half.
nOtherwise, narrow it to the upper half.
nRepeatedly check from the second point until the value is found or the
interval is empty.
If ( value == middle element )
value is found
else if ( value < middle element )
search left-half of list with the same method
else
search right-half of list with the same method
Department of Computer Science 63
Searching an Array: Decrease by constant factor(half) Technique
a: 1 5 7 9 10 13 17 19 27
0 1 2 3 4 5 6 7 8
val = 7
a: 1 5 7 9 10 13 17 19 27
0 1 2 3 4 5 6 7 8
a: 1 5 7 9 10 13 17 19 27
0 1 2 3 4 5 6 7 8
a: 1 5 7 9 10 13 17 19 27
0 1 2 3 4 5 6 7 8
Department of Computer Science 69
Binary Search: Recursive Algorithm
CONCLUSION
Variable-size decrease
Euclid’s algorithm
Selection by partition
Nim-like games
Department of Computer Science
Design and Analysis of Algorithm
Decide on : algorithm
design techniques etc.
Design an algorithm
Prove correctness
Read Chapter 4
Decrease and Conquer
Bottom-up: iterative
Top-down: recursive
Department of Computer Science
Top Down and Iterative
a problem of size n
subproblem
Recursive
of size n-1
a solution to the
subproblem
e.g., n!
a solution to
the original problem 87
Iterative
Department of Computer Science
Example-1: Computing
Brute Force Example a n
BF
Logic/Idea
29 45 68 89 90 | 34 17
29 45 68 89 34 | 90 17
29 45 68 34 89 | 90 17
29 45 34 68 89 | 90 17
29 34 45 68 89 | 90 17
29 45 68 89 90 | 34 17
29 45 68 89 34 | 90 17
29 45 68 34 89 | 90 17
29 45 34 68 89 | 90 17
29 34 45 68 89 | 90 17
29 45 68 89 90 | 34 17
29 45 68 89 34 | 90 17
29 45 68 34 89 | 90 17
29 45 34 68 89 | 90 17
29 34 45 68 89 | 90 17
29 34 45 68 89 90 | 17 29 45 68 89 90 | 34 17
45 68 89 | 90 29 34 17
45 68 89 90 | 29 34 17
29 34 45 68 89 90 | 17 29 45 68 89 90 | 34 17
45 68 89 | 90 29 34 17
45 68 89 90 | 29 34 17
89 | 45 68 90 29 34 17
45 89 | 68 90 29 34 17
45 68 89 | 90 29 34 17
45 68 89 90 | 29 34 17
The improved version only requires N
29 45 68 89 90 | 34 17
+ 2 assignment statements to
accomplish the same task.
29 34 45 68 89 90 | 17
17 29 34 45 68 89 90
ALGORITHM InsertionSortIter(A[0..n-1])
//An iterative implementation of insertion sort
//Input: An array A[0..n-1] of n orderable elements
//Output: Array A[0..n-1] sorted in nondecreasing order
key = A[i]
for j i – 1 to 0 do // j: the index of the sorted part of the array
if (key< A[j]) //compare the key with each element in the sorted part
A[j+1] A[j]
else
break
A[j +1] key //insert the key to the sorted part of the array
j <- i-1 45 89 | 68 90 29 34 17
while j ≥ 0 and A[j] > v do
45 68 89 | 90 29 34 17
A[j+1] <- A[j]
j <- j-1 45 68 89 90 | 29 34 17
A[j+1] <- v
29 45 68 89 90 | 34 17
Input size: n 29 34 45 68 89 90 | 17
Basic op: A[j] > v
Why not j ≥ 0 ? 17 29 34 45 68 89 90
C(n) depends on input type ?
In place? Stable ?
Department of Computer Science
Insertion Sort: Decrease by one(Recursive Solution)
ALGORITHM InsertionSortRecur(A[0..n-1], n-1)
//A recursive implementation of insertion sort
//Input: An array A[0..n-1] of n orderable elements Index of the last element
//Output: Array A[0..n-1] sorted in nondecreasing order
if n > 1
InsertionSortRecur(A[0..n-1], n-2)
Insert(A[0..n-1], n-1) //insert A[n-1] to A[0..n-2]
Index of the element/key to be inserted.
ALGORITHM Insert(A[0..m], m)
//Insert A[m] to the sorted subarray A[0..m-1] of A[0..n-1]
//Input: A subarray A[0..m] of A[0..n-1]
//Output: Array A[0..m] sorted in nondecreasing order
key = A[m]
for j m – 1 to 0 do
if (key< A[j])
A[j+1] A[j]
else
break
A[j +1] Department
key of Computer Science 106
Example -2
Recurrence relation:
T(n) = T(n-1) + n
T(1) = 1
Telescoping:
T(n) = T(n-1) + n
T(n-1) = T(n-2) + n-1
T(n-2) = T(n-3) + n-2
…
T(2) = T(1 ) + 1
T (n) = T (n − 1) + n
T(n) = T(n-1) + n
Guess: T(n) = O(n2)
Induction goal: T(n) ≤ c n2, for some c and n ≥ n0
Induction hypothesis: T(n-1) ≤ c(n-1)2 for all k < n
BF
Adjacency matrix
n x n boolean matrix if |V| is n.
The element on the ith row and jth column is 1 if there’s an
edge from ith vertex to the jth vertex; otherwise 0.
The adjacency matrix of an undirected graph is symmetric.
It takes (comparisons) to figure out all the adjacent
nodes of a certain node i.
Adjacency linked lists
A collection of linked lists, one for each vertex, that contain all
the vertices adjacent to the list’s vertex.
An example
The idea
traverse “deeper” whenever possible.
On each iteration, the algorithm proceeds to an unvisited vertex that is
adjacent to the one it is currently in. If there are more than more
neighbors, break the tie by the alphabetic order of the vertices.
When reaching a dead end ( a vertex with no adjacent unvisited
vertices), the algorithm backs up one edge to the parent and tries to
continue visiting unvisited vertices from there.
The algorithm halts after backing up to the starting vertex, with the
latter being a dead end.
Similar to preorder tree traversals
It’s convenient to use a stack to trace the operation of depth-first
search.
Push a vertex onto the stack when the vertex is reached for the first
time.
Pop a vertex off the stack when it becomes a dead end.
An example
a b c d
e f g h
a b c d
e f g h
Applications:
Checking connectivity
Finding connected components
The idea
Traverse “wider” whenever possible.
Discover all vertices at distance k from s (on level k)
before discovering any vertices at distance k +1 (at
level k+1)
Similar to level-by-level tree traversals
Instead of a stack, breadth-first uses a queue.
a b c d
e f g h
Breadth-first traversal:
abefgchd
BFS(G)
count 0
bfs(v)
mark each vertex with 0
for each vertex v∈ V do count count + 1
if v is marked with 0 mark v with count //visit v
bfs(v) initialize queue with v //enqueue
while queue is not empty do
a front of queue //dequeue
for each vertex w adjacent to a do
if w is marked with 0 //w hasn’t been visited.
count count + 1
mark w with count //visit w
add w to the end of the queue //enqueue
remove a from the front of the queue
a b c d
e f g h
Breadth-first traversal:
abefghcd
a b a b
not a DAG
c d c d
DAG
Logic/Idea
C4 requires C3 C3
C5 requires C3 and C4
C2 C5
Example:
a b c d
e f g h
a b c d
e f g h
Other Examples:
Finding cycle in a graph.
Operation System deadlock detection.
Dependency resolution.
Sentence Ordering.
Critical Path Analysis.
Course Schedule problem.
Effectively, when
power is not
Suppose we want to compute 95
divisible by 2, we
make power even
by taking out the
extra 9. Then we
already know the
solution when
power is divisible by
2. Divide the power
Department of Computer Science 138
by 2 and multiply
Example-1: Computing
Brute Force Example a n
a problem of size n
subproblem
of size n/2
a solution to the
subproblem
a solution to
the original problem 142
Logic/Idea
repeatedly dividing the search interval in half.
Department of Computer Science 143
Example-2:
Brute Searching a Key
Force Example
nBegin with the mid element of the whole array as a search key.
nIf the value of the search key is equal to the item then return an index
of the search key.
nOr if the value of the search key is less than the item in the middle of
the interval, narrow the interval to the lower half.
nOtherwise, narrow it to the upper half.
nRepeatedly check from the second point until the value is found or the
interval is empty.
If ( value == middle element )
value is found
else if ( value < middle element )
search left-half of list with the same method
else
search right-half of list with the same method
Department of Computer Science 144
Searching an Array: Decrease by constant factor(half) Technique
a: 1 5 7 9 10 13 17 19 27
0 1 2 3 4 5 6 7 8
val = 7
a: 1 5 7 9 10 13 17 19 27
0 1 2 3 4 5 6 7 8
a: 1 5 7 9 10 13 17 19 27
0 1 2 3 4 5 6 7 8
a: 1 5 7 9 10 13 17 19 27
0 1 2 3 4 5 6 7 8
Department of Computer Science 150
Binary Search: Recursive Algorithm
CONCLUSION