













Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An in-depth look into decrease-and-conquer algorithms, a subcategory of divide-and-conquer techniques used in computer science. Various types of decrease-and-conquer algorithms, including decrease-by-one, decrease-by-half, and variable-size-decrease, along with examples and efficiency analysis. Students of computer science can benefit from this document as study notes, summaries, or as a reference for understanding decrease-and-conquer algorithms.
Typology: Study notes
1 / 21
This page cannot be seen from the preview
Don't miss anything!














Decrease (by one)-and-conquer technique is common.
Exponentiation problem of computing an. Exploit relation an^ = an^ -^^1. a
Top down solution uses the recursive definition
Bottom up solution multiplies a by itself n - 1.
Requires O( n ) operations.
Problem of size n
subproblem of size n-
solution to the subproblem
solution to the original problem
€
f ( n ) =
f ( n − 1 ). a a
for n > 1 for n = 1
Decrease (by half)-and-conquer technique is common. Decrease the problem size by a factor of 2.
Exponentiation problem of computing an. Exploit relation an^ = ( a n /2)2) for even n Recursive approach:
Requires O(log n ) operations.
Note that the divide-and-conquer actually solves two instances of the problem of size n /2.
Problem of size n
subproblem of size n/
solution to the subproblem
solution to the original problem
€
f ( n ) =
( an^ /^2 )^2 ( a ( n −^1 )/^2 )^2. a a
for even n > 1 for odd n > 1 for n = 1
Algorithm InsertionSort ( A[0.. n - 1] ) for i ← 1 to n -1 do v ← A[ i] j ← i - 1 while j ≥ 0 and A[ j] > v do A[ j + 1] ← A[ j] j ← j - 1 A[ j + 1] ← v
Generate all n****! permutations for a set of integers {1, 2, ….. n }.
Decrease-by-one approach: Exploit the relation n****! = n ( n - 1)! Insert n in each of the n possible positions among elements of every permutations of n - 1 elements (either left to right or right to left)
Bottom-up minimal change algorithm Each permutation is obtained from its immediate predecessor by exchanging just two elements in it.
Start 1 1
Insert 2 12 21
Insert 3 123 132 312 321 231 213
B.B. Karki, LSU
CSC 3102
Johnson-Trotter Algorithm generates
n
! without explicitly generating
permutations for smaller values of
n
.
Associate a direction with each component
k
in a permutation
)
Direction is represented by an arrow above the component. )
The component
k
is said to be
mobile
, if the arrow points to a smaller
value adjacent to it.
For
the components 3 and 4 are mobile while 2 and 1 are
not.
rs 3 2
r 4 s 1
(^31) 2
(^13) 2
(^12) 3
(^21) 3
(^23) 1
(^32) 1
rs s
sr s
ss r
ss s
ss s
ss s
Algorithm
JohnsonTrotter
( n
initialize
the first permutation
with
while
there exists a mobile integer
k
do
find
the largest mobile integer
k
swap
k
and the adjacent integer its arrow points to
reverse
the direction of all integers that are larger than
k Example,
n
= 3
s 1 s 2...........
s n
Selection problem: find the k th^ smallest element in a list of n numbers. k = n /2 , the median k = 1 , the smallest number k = n , the largest element
Set 1: Elements that are less than or equal to some value p Set 2: Elements that are greater than or equal to p
A directed graph or digraph is a graph with directions specified for all its edges
Resulting forest are complex: DFS forest exhibits all four types of edges possible: Tree edges: ab, bc, de Back edges: ba Forward edges: ac Cross edges: dc
Directed acyclic graph ( dag ): has no back edges.
Directed cycle: A back edge in DFS forest can connect a vertex to its parent. The presence of a back edge indicates that diagraph has a directed cycle.
c
a
d
b
e
a
b
c
d
e
Solution: Verify whether a given digraph is a dag and, if it is, produce an ordering of vertices. Two algorithms for solving the problem. They may give different (alternative) solutions.
DFS-based algorithm Perform DFS traversal and note the order in which vertices become dead ends (that is, are popped of the traversal stack). Reversing this order yields the desired solution, provided that no back edge has been encountered during the traversal.
Source removal algorithm Identify a source, which is a vertex with no incoming edges and delete it along with all edges outgoing from it. There must be at least one source to have the problem solved. Repeat this process in a remaining diagraph. The order in which the vertices are deleted yields the desired solution.
DFS traversal stack with the subscript numbers indicating the popping off order.
All edges in the sorted list point from left to right.
Time efficiency is in O(| V^2 |) for the adjacency matrix representation and O(| V |+| E |) for the adjacency linked list representation. Since the reversing requires only Θ (| V |) and it can stop before processing the entire digraph if a back edge is encountered.
C
C
C
C
C4 (^) The popping-off order: C5, C4, C3, C1, C
The topologically sorted list: C2 C1 C3 C4 C
C5 1 C4 2 C3 3 C1 4 C2 5