Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
11 views

Lecture 05 - Decrease and Conquer

The document discusses problem solving using the decrease and conquer technique. It explains different types of decrease and conquer including decreasing by a constant, decreasing by a constant factor, and variable size decreasing. Specific algorithms like insertion sort, graph traversal, and Euclid's algorithm are discussed as examples.

Uploaded by

waheedgx
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Lecture 05 - Decrease and Conquer

The document discusses problem solving using the decrease and conquer technique. It explains different types of decrease and conquer including decreasing by a constant, decreasing by a constant factor, and variable size decreasing. Specific algorithms like insertion sort, graph traversal, and Euclid's algorithm are discussed as examples.

Uploaded by

waheedgx
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

Algorithms and Problem

Solving
Lecture 05 – Problem Solving
using Decrease and Conquer

AL-Zaiem AL-Azhari University


Faculty of Computer Sciences and Information
Technology
Mohammed Mahmoud
Overview
• Reduce problem instance to smaller instance of
the same problem
• Solve smaller instance
• Extend solution of smaller instance to obtain
solution to original instance
• Can be implemented either top-down or
bottom-up
• Also referred to as inductive or incremental
approach
Type of Decrease and
Conquer
• Decrease by a constant (usually by 1):
• insertion sort
• graph traversal algorithms (DFS and BFS)
• topological sorting
• algorithms for generating permutations, subsets
• Decrease by a constant factor (usually by half)
• binary search and bisection method
• exponentiation by squaring
• multiplication à la russe
• Variable-size decrease
• Euclid’s algorithm
• selection by partition
• Nim-like games
• This usually results in a recursive algorithm.
Decrease By Constant –
Insertion Sort
• To sort array A[0..n-1], sort A[0..n-2]
recursively and then insert A[n-1] in its
proper place among the sorted A[0..n-2]
Usually implemented bottom up (non-
recursively).
• Example: Sort 6, 4, 1, 8, 5
6|4 1 8 5
4 6|1 8 5
1 4 6|8 5
1 4 6 8|5
1 4 5 6 8
Decrease By Constant –
Insertion Sort
Decrease By Constant –
Graph Traversal
• Many problems require processing all
graph vertices (and edges) in systematic
fashion
• Graph traversal algorithms:
• Depth-first search (DFS)
• Breadth-first search (BFS)
Depth-First Search (DFS)
• Visits graph’s vertices by always moving away from
last visited vertex to an unvisited one, backtracks if
no adjacent unvisited vertex is available.
• Recursive or it uses a stack
• a vertex is pushed onto the stack when it’s reached for
the first time
• a vertex is popped off the stack when it becomes a dead
end, i.e., when there is no adjacent unvisited vertex
• “Redraws” graph in tree-like fashion (with tree
edges and back edges for undirected graph)
Depth-First Search (DFS)

a b c d

e f g h

DFS traversal stack: DFS tree:


a
ab
abf
abfe 1 2 6 7
abf a b c d
ab
abg
abgc
abgcd e f g h
abgcdh
abgcd 4 3 5 8
Red edges are tree edges and
… white edges are back edges.
Breadth-first search (BFS)
• Visits graph vertices by moving across to
all the neighbors of the last visited vertex
• Instead of a stack, BFS uses a queue
• Similar to level-by-level tree traversal
• “Redraws” graph in tree-like fashion
(with tree edges and cross edges for
undirected graph)
Breadth-first search (BFS)
a b c d

e f g h

BFS traversal queue: BFS tree:


a
bef
efg 1 2 6 8
fg a b c d
g
ch e f g h
hd 3 4 5 7
d Red edges are tree edges and
white edges are cross edges.
DAGs and Topological
Sorting
A dag: a directed acyclic graph, i.e. a directed graph with no (directed)
cycles

a b a b
a not a
dag dag
c d c d

Arise in modeling many problems that involve prerequisite constraints


(construction projects, document version control)

Vertices of a dag can be linearly ordered so that for every edge its starting
vertex is listed before its ending vertex (topological sorting). Being a dag
is also a necessary condition for topological sorting to be possible.
Topological Sorting
Example
Order the following items in a food chain

tiger

human
fish
sheep
shrimp

plankton wheat
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
• Multiplication à la russe (Russian peasant
method)
• Fake-coin puzzle
• Josephus problem
Exponentiation by
Squaring
• The problem: Compute an where n is a
nonnegative integer
• The problem can be solved by applying
recursively the formulas:
For even values of n
a n = (a n/2 )2 if n > 0 and a 0 =
For odd values of n 1
a n
= (a (n-1)/2
)2 a
Recurrence: M(n) = M( n/2 ) + f(n),
where f(n) = 1 or 2,
M(0) = 0
Master Theorem: M(n)  Θ(log n) = Θ(b)
where b = log2(n+1)
Russian Peasant
Multiplication
• The problem: Compute the product of two
positive integers Can be solved by a decrease-
by-half algorithm based on the following
formulas.
• For Even Values of n:
n * m = n/2 * 2m
• For Odd values of n:
n * m = (n – 1)/2 * 2m + m, if n > 1 and m if n = 1
Example of Russian
Peasant Multiplication
Compute 20 * 26
n m
20 26
10 52
5 104 104
2 208 +
1 416 416
Fake-Coin Puzzle (simpler
version)
• There are n identically looking coins one of
which is fake.
• There is a balance scale but there are no
weights; the scale can tell whether two sets of
coins weigh the same and, if not, which of the
two sets is heavier (but not by how much, i.e.
3-way comparison).
• Design an efficient algorithm for detecting the
fake coin. Assume that the fake coin is known
to be lighter than the genuine ones.
• Decrease by factor 2 algorithm
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
Euclid’s Algorithm
• Euclid’s algorithm is based on repeated application of
equality, gcd(m, n) = gcd(n, m mod n)
• Ex:

gcd(80,44) = gcd(44,36) = gcd(36, 8) = gcd(8,4) = gcd(4,0) = 4

• One can prove that the size, measured by the first


number,
• decreases at least by half after two consecutive
iterations.
Binary Search Tree
Algorithms
• Several algorithms on BST requires
recursive processing of just one of
its subtrees, e.g.,
k
• Searching
• Insertion of a new key
• Finding the smallest (or the largest) key

<k >k
Searching in Binary Search
Tree
Algorithm BST(x, v)
//Searches for node with key equal to v in BST rooted at
node x
if x = NIL return -1
else if v = K(x) return x
else if v < K(x) return BST(left(x), v)
else return BST(right(x), v)

Efficiency
worst case: C(n) = n
average case: C(n) ≈ 2ln n ≈ 1.39log2 n, if the BST was
built from n random keys and v is chosen randomly.
END!

You might also like