0% found this document useful (0 votes)
22 views

6 Decrease and Conquer

This document introduces the decrease and conquer design technique for algorithms. It explains that this technique reduces a problem into smaller instances of the same problem. It then discusses different ways problems can be decreased, such as by a constant, constant factor, or variable size. Examples are provided for each, including exponentiation, binary search, and Euclid's algorithm. The document also covers applications of decrease and conquer in sorting algorithms like insertion sort.

Uploaded by

Nixon Somoza
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

6 Decrease and Conquer

This document introduces the decrease and conquer design technique for algorithms. It explains that this technique reduces a problem into smaller instances of the same problem. It then discusses different ways problems can be decreased, such as by a constant, constant factor, or variable size. Examples are provided for each, including exponentiation, binary search, and Euclid's algorithm. The document also covers applications of decrease and conquer in sorting algorithms like insertion sort.

Uploaded by

Nixon Somoza
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 61

Course Packet 6

Decrease and Conquer


Design Technique

Maria Lolita G. Masangcap


Introduction
This course packet introduces the topic decrease and conquer design technique and
enumerates its importance in algorithm designing.

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

At the end of this course packet, you will


be able to explain and apply the concept
of Decrease-and-Conquer algorithm
technique in designing algorithm
applicable in problem solving
Design and Analysis of
Algorithm
Decrease and Conquer Design Technique

Uses some technique to reduce the problem into a single


problem that is smaller than the original

Based on exploiting the relationship between a solution to a


given instance of a problem and a solution to a smaller
instance of the same problem

Sometimes called as Prune and Search


Decrease and Conquer Design Technique

• Reduce problem Step 2 • Extend solution of


instance to a • Solve smaller smaller instance to
smaller instance of obtain solution to
instance
the same problem original instance

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

Variable-size decrease • Euclid’s algorithm


• Selection by partition
Decrease-by-a-Constant
• The size of an instance is
reduced by the same constant
(1) in iteration of the algorithm
• Example
• Exponentiation problem of
computing positive integer
exponents.
Decrease-by-a-Constant
Example
TOP-DOWN (Recursively)
• Exponentiation problem
of computing positive 𝑓 𝑛−1 ∗𝑎 𝑖𝑓 𝑛 > 1
𝑓 𝑛 =$
integer exponents. 𝑎 𝑖𝑓 𝑛 = 1
• The relationship between
a solution to an instance BOTTOM-UP (Iteratively)
of size n and an instance
• Multiply a by itself n-1 times
of size n-1 is achieved by
using the formula:
an = an-1 * a
Decrease-by-a-Constant
Example
TOP-DOWN (Recursively)
• Exponentiation problem
of computing positive 𝑓 𝑛−1 ∗𝑎 𝑖𝑓 𝑛 > 1
𝑓 𝑛 =$
integer exponents. 𝑎 𝑖𝑓 𝑛 = 1
24 = 24-1 * 2
an = an-1 *a 24 = 23 * 2 23 = 23-1 * 2
24 = (22 * 2) * 2 22 = 22-1 * 2
Analysis: O(n) 24 = ((21 * 2) * 2) * 2 21 = 2
24 = ((2 * 2) * 2) * 2
24 = 16
Decrease-by-a-Constant
Example
BOTTOM-UP (Iteratively)
• Exponentiation problem
of computing positive Multiply a by itself n-1 times
integer exponents.
24 a = 2 n = 4
an = an-1 * a 24 = 2 * 2 * 2 * 2

Analysis: O(n) a 4 - 1 = 3 times

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

gcd(a, b) = gcd(b, a mod b)

Find the greatest common divisor of 24 and 36.

gcd(36,24) = gcd(24, 36 mod 24)


Variable Size Decrease
Euclid’s algorithm for computing the greatest common divisor.
Example 1: Find the greatest common divisor of 24 and 36.
gcd(36, 24) 36 divided by 24 = 1 remainder 12 Decreased by 12
gcd(36, 24) = gcd(24, 12) 24 divided by 12 = 2 remainder 0 Decreased by 12
= 12

Example 2: Find the greatest common divisor of 60 and 39


gcd(60, 39) 60 divided by 39 = 1 remainder 21 Decreased by 21
gcd(60, 39) = gcd(39, 21) 39 divided by 21 = 1 remainder 18 Decreased by 18

= gcd(21, 18) 21 divided by 18 = 1 remainder 3 Decreased by 3


= gcd(18, 3) 18 divided by 3 = 6 remainder 0 Decreased by 15
=3
Decreased by Variable size!
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)

In practice, insertion sort does better


than most of the quadratic sorting, like
selection sort or bubble sort.
Graph Traversals
Used to decide the A graph traversal finds
A technique used for
order of vertices which the edges to be used in
searching vertex in a
is visited in the search the search process
graph
process without creating loops.

That means using graph Two graph traversals:


traversal we visit all the • Depth First Graph
vertices of the graph Traversal (DFS)
without getting into • Breadth First Graph
looping path. Traversal (BFS)
Depth-First Search Traversal Algorithm
Steps in Implementing DFS
1. Define a Stack of size total number of vertices in the
DFS 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 push it on to the Stack.
3. Visit any one of the non-visited adjacent vertices of a
vertex which is at the top of stack and push it on to the
Spanning Tree is a graph without
stack.
loops.
4. Repeat step 3 until there is no new vertex to be visited
from the vertex which is at the top of the stack.
5. When there is no new vertex to visit then use back
Stack with maximum size of total tracking and pop one vertex from the stack.
number of vertices in the graph is used
to implement DFS traversal. 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
Depth-First Search Traversal Algorithm
Consider the graph below to perform DFS traversal:
Depth-First Search Traversal Algorithm

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.

A directed acyclic graph is a


Topological Ordering is
graph that has a direction as
well as lack of cycles. not unique!

*Not solvable if directed graph has a cycle.


Application in Topological Ordering
Which job
• First studied in 1960s in the context of PERT
must be
(Project Evaluation and Review Technique) for
performed
scheduling in project management.
first?
• Jobs are vertices. An edge from x to y means
Represent job
job x must be completed before job y can be
dependencies started
using a
• Topological sorting gives an order in which to
digraph.
perform the jobs subject to dependencies.
Application in Topological Ordering
Example:

Set of 5 courses: { C1, C2, C3, C4, C5 }

C1 and C2 have no prerequisites


C3 requires C1 and C2
C4 requires C3
C5 requires C3 and C4
Application in Topological Ordering
The topological sort is a simple but useful
adaptation of a depth first search. The algorithm
for the topological sort is as follows:

1. Call dfs(g) for some graph g. The main


reason we want to call depth first
search is to compute the finish times for
each of the vertices.
2. As each vertex is finished, insert it onto
the front of the list (ordering).
3. Output the list.

How a hypothetical man get dressed


Application in Topological Ordering
1. Call dfs(g) for some graph g. The
main reason we want to call depth
first search is to compute the
finish times for each of the
vertices.
2. Store the vertices in a list in
decreasing order of finish time.
3. Return the ordered list as the
result of the topological sort.
Application in Topological Ordering
Analysis:
The time complexity is
O(V + E)
where V is the number of vertices
and E is the number of edges.

Result of Topological Sort on Directed Acyclic Graph


Application in Incremental Permutation

Each permutation
Let S be a finite set of Permutation is the
must include all
size n. different orderings of S.
elements of S.

An incremental Method: Analysis: O(n!)


• Assume that we already have (n-1)!
• Insert n in each of the n possible positions among elements of every
permutation of n-1 elements
Application in Incremental Permutation
Example:
If n = 4, S = {1, 2, 3, 4} à 4! = 24 permutations in total.
Insert one element at a time in all possible insertion points

Start 1 Insert 2 in 2 possible positions: 12, 21


Insert 3 in 3 312, 132, 123 Insert 4 in 4 possible
possible positions 321, 231, 213 positions of each previous
of each previous permutation:
permutation:
Q&A

Any
Question???
CP6 Decrease and Conquer Design Technique

You might also like