6 Decrease and Conquer
6 Decrease and Conquer
Also, it discusses how this technique is applied to various sorting and searching algorithm.
It explains how this technique is applied in solving different types of computing problems.
At the end of this activity, students are expected to apply decrease and conquer design
technique to the given problems.
Also, students are expected to design an algorithm implementing the concept of decrease and
conquer in solving a computing problem.
Introduction
Step 1 Step 3
Illustration of Decrease and Conquer
• Variations of Decrease-and-
Conquer
• Decrease-by-a constant
• Decrease-by-a constant
factor
• Variable size decrease
Application
Decrease-and-Conquer
Application
Algorithm
Decrease by a constant • Exponentiation
(usually decreased by 1) • Insertion sort
• Graph traversal algorithm (DFS and BFS)
• Topological sorting
• Algorithms for generating permutations, subsets
Decrease by a constant factor • Binary search and bisection method
(usually by half) • Exponentiation
24 = 16
Decrease-by-a-Constant Factor
• Suggests reducing a problem’s
instance by the same constant factor
in iteration of the algorithm and
typically in most applications, this
constant factor is equal to two
• Example
• Exponentiation problem of
computing positive integer
exponents.
Decrease-by-a-Constant Factor
Example
• Exponentiation problem of computing positive integer exponents.
We have an instance of size n that should be computed as an, where the
instance of half its size is computed using the formula a n/2
an = (a n/2)2
(𝑎+/-)- If n is even and positive
𝑎+ = #(𝑎 +./ /-)- ∗ 𝑎 If n is odd and greater than 1
𝑎 If n = 1
Analysis: O(log n)
Decrease-by-a-Constant Factor
Example
• Exponentiation problem of computing
positive integer exponents.
21 = 2 24 = (2!/# )# 27 = (2(%&')/# )# * 2
24 = (2# )# 27 = (2)/# )# * 2 "#$ +! 2
27 = ( 2 ∗ 2) # * 2
24 = (4)# 27 = (2* ) # * 2 ! +! 2
27 =( 2 ∗ 2) # * 2
24 = 16 27 = (8) # * 2 27 = (22 ∗ 2) # * 2
27 = 64 * 2 27 = (4 ∗ 2) # * 2
27 = 128
Variable Size Decrease
• The size-reduction pattern varies from one iteration of an algorithm to
another
• Example
• Euclid’s algorithm for computing the greatest common divisor
gcd(a, b) = gcd(b, a mod b)
• This method asks you to perform successive division, first of the smaller of
the two numbers into the larger, followed by the resulting remainder
divided into the divisor of each division until the remainder is equal to
zero. At that point, look at the remainder of the previous division – that
will be the greatest common divisor.
Variable Size Decrease
Euclid’s algorithm for computing the greatest
common divisor
Algorithm
Input: Two integers a and b
Output: The greatest common divisor of a and b
While b is not 0 do
r = a mod b
a=b
b=r
end
return a Analysis: O(log n)
Application in Insertion Sort
Insertion An in-place comparison-based sorting algorithm
Sort The data structure is virtually divided into two: the sorted and
unsorted parts.
The sorted part contains elements which are already sorted while
an element at the unsorted part is to be inserted in the sorted part.
The ‘insertion’ is done by finding the right place in the sorted part;
hence, the name insertion sort.
A real-life example of this sorting algorithm is how players are
sorting playing cards in their hand during card game.
Application in Insertion Sort
Insertion Sort Algorithm (Sorting in Ascending Order)
1. Pick the first item in the list and make it the
first item in the sorted sublist.
2. Choose the next item in the unsorted sublist
and compare it with all the elements in the
sorted sublist to determine the position
where the item will be inserted.
3. Move all the elements greater than the item
to be inserted to the right then insert the
item.
4. Repeat the process until all items are already
sorted.
Application in Insertion Sort
Insertion Sort
Overall complexity
Worst and Average: O(n2)
Best: O(n)
Output: A
Depth-First Search Traversal Algorithm
Output: A B
Depth-First Search Traversal Algorithm
Output: A B C
Depth-First Search Traversal Algorithm
Output: A B C E
Depth-First Search Traversal Algorithm
Output: A B C E D
Depth-First Search Traversal Algorithm
Output: A B C E D
Back tracking is coming back to the vertex from which we reached the current
vertex.
Depth-First Search Traversal Algorithm
Output: A B C E D F
Depth-First Search Traversal Algorithm
Output: A B C E D F G
Depth-First Search Traversal Algorithm
Output: A B C E D F G
Depth-First Search Traversal Algorithm
Output: A B C E D F G
Depth-First Search Traversal Algorithm
Output: A B C E D F G
Depth-First Search Traversal Algorithm
Output: A B C E D F G
Depth-First Search Traversal Algorithm
Output: A B C E D F G
Depth-First Search Traversal Algorithm
Output: A B C E D F G
Depth-First Search Traversal Algorithm
Output: A B C E D F G
Depth-First Search Traversal Algorithm
Steps in Implementing DFS
1. Define a Stack of size total number of vertices in the
2.
graph.
Select any vertex as starting point for traversal. Visit that
Analysis:
vertex and push it on to the Stack.
3. Visit any one of the non-visited adjacent vertices of a The time complexity is
vertex which is at the top of stack and push it on to the
stack.
O(V + E)
4. Repeat step 3 until there is no new vertex to be visited where V is the number
from the vertex which is at the top of the stack.
5. When there is no new vertex to visit then use back of vertexes and E is the
tracking and pop one vertex from the stack. number of edges.
6. Repeat steps 3, 4 and 5 until stack becomes Empty.
7. When stack becomes Empty, then produce final spanning
tree by removing unused edges from the graph
http://www.btechsmartclass.com/data_structures/graph-traversal-dfs.html
Breadth-First Search Traversal Algorithm
Steps in Implementing BFS
1. Define a Queue of size total number of vertices in the
BFS traversal of a graph produces graph.
a spanning tree as final result.
2. Select any vertex as starting point for traversal. Visit that
vertex and insert it into the Queue.
3. Visit all the non-visited adjacent vertices (alphabetically)
Spanning Tree is a graph without of the vertex which is at front of the Queue and insert
loops. them into the Queue.
4. When there is no new vertex to be visited from the vertex
which is at front of the Queue then delete that vertex.
Queue with maximum size of total
number of vertices in the graph is used 5. Repeat steps 3 and 4 until queue becomes empty.
to implement DFS traversal. 6. When queue becomes empty, then produce final spanning
tree by removing unused edges from the graph
http://www.btechsmartclass.com/data_structures/graph-traversal-dfs.html
Breadth-First Search Traversal Algorithm
Consider the graph below to perform BFS traversal:
Breadth-First Search Traversal Algorithm
Output: A
Breadth-First Search Traversal Algorithm
(B, D, E)
B D E
Output: A B D E
Breadth-First Search Traversal Algorithm
Step 3
(B, D,
- Visit all adjacent vertices of B (front of queue) which E)not visited (C)
are
- Delete B from the Queue
C
B D E C
Output: A B D E C
Breadth-First Search Traversal Algorithm
Step 4
- Visit all adjacent vertices of D (front of queue) which are not visited (there
is no vertex) (B, D, E)
- Delete D from the Queue
C
D E C
Output: A B D E C
Breadth-First Search Traversal Algorithm
Step 5
- Visit all adjacent vertices of E (front of queue) which
(B, D,are
E) not visited (F)
- Delete E from the Queue
E C F
Output: A B D E C F
Breadth-First Search Traversal Algorithm
Step 6
- Visit all adjacent vertices of C (front of queue) which
(B, D,are
E) not visited (G)
- Delete C from the Queue
G C F G
Output: A B D E C F G
Breadth-First Search Traversal Algorithm
Step 7
- Visit all adjacent vertices of F (front of queue) which are not visited (There
is no vertex) (B, D, E)
- Delete F from the Queue
C
G F G
Output: A B D E C F G
Breadth-First Search Traversal Algorithm
Step 8
- Visit all adjacent vertices of G (front of queue) which are not visited (There
is no vertex) (B, D, E)
- Delete G from the Queue
C
G F G
Output: A B D E C F G
Breadth-First Search Traversal Algorithm
Queue became Empty. So, stop the BFS process.
Final result of BFS is a Spanning Tree as shown below: (B, D, E)
G Output: A B D E C F G
F
Breadth-First Search Traversal Algorithm
Steps in Implementing BFS
1. Define a Queue of size total number of vertices in the
graph.
Analysis:
2. Select any vertex as starting point for traversal. Visit that
vertex and insert it into the Queue. The time complexity is
3. Visit all the non-visited adjacent vertices (alphabetically) O(V + E)
of the vertex which is at front of the Queue and insert
them into the Queue. where V is the number
4. When there is no new vertex to be visited from the vertex of vertexes and E is the
which is at front of the Queue then delete that vertex.
5. Repeat steps 3 and 4 until queue becomes empty. number of edges.
6. When queue becomes empty, then produce final spanning
tree by removing unused edges from the graph
http://www.btechsmartclass.com/data_structures/graph-traversal-dfs.html
Application in Topological Ordering
Topological Ordering or Topological Sorting of a directed acyclic graph (DAG)
is a linear ordering of its vertices such that for every directed edge(u, v) from
vertex u to v, u comes before v in the ordering.
Each permutation
Let S be a finite set of Permutation is the
must include all
size n. different orderings of S.
elements of S.
Any
Question???
CP6 Decrease and Conquer Design Technique