Decrease-and-Conquer Algorithms - Prof. B. Karki, Study notes of Computer Science

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

Pre 2010

Uploaded on 12/04/2009

buju21
buju21 🇺🇸

13 documents

1 / 21

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
B.B. Karki, LSU
0.1
CSC 3102
Algorithm Design Techniques
Brute force
Divide-and-conquer
Decrease-and-conquer
Transform-and-conquer
Space-and-time tradeoffs
Dynamic programming
Greedy techniques
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15

Partial preview of the text

Download Decrease-and-Conquer Algorithms - Prof. B. Karki and more Study notes Computer Science in PDF only on Docsity!

Algorithm Design Techniques

 Brute force

 Divide-and-conquer

 Decrease-and-conquer

 Transform-and-conquer

 Space-and-time tradeoffs

 Dynamic programming

 Greedy techniques

Decrease-and-Conquer

Decrease-by-One

 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

 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

Insertion Sort

 Insertion sort is based on the decrease (by one)-and-conquer

approach:

 Provided that a smaller array A [0.. n - 2] is already sorted,

now sort the original array A [0.. n - 1].

 Find an appropriate position for an element A [ n - 1]

among the sorted n - 1 elements and insert it there.

 Right-to-left scan:

 Scan the sorted subarray from right to left until the first

element smaller than or equal to A [ n - 1] is encountered

and then insert A [ n - 1] right after that element.

Iterative Approach

 Starting with A [1] and ending with A [ n - 1], A [ i ] is

inserted in its appropriate place among the first i

elements of the array that have already been sorted.

Algorithm InsertionSort ( A[0.. n - 1] ) for i1 to n -1 do vA[ i] ji - 1 while j ≥ 0 and A[ j] > v do A[ j + 1]A[ j] jj - 1 A[ j + 1]v

Permutations: Minimal Change Approach

 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

Permutations: Johnson-Trotter Algorithm

„

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

Ternary Search

 Searching in a sorted array A [0, 1, ….. n - 1]

 Search recursively by comparing the search key K with

A [  n /3  ], and if K is larger, compare it with A [  2 n /3  ] to

determine which third of the array to continue the search.

 The recurrence relation for the number of the number of key

comparisons in the worst case:

C ( n ) = C ( n /3) + 2 for n = 3 k^ ( k > 0 ) and C (1) = 1

The solution is C ( n ) = 2 log 3 n + 1

Computing a Median

 A median is an element that is greater than one half of the

list’s elements and smaller than the other half.

 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

 Algorithm based on partitioning of an array’s elements into

two subsets (with p as a pivot)

 Set 1: Elements that are less than or equal to some value p  Set 2: Elements that are greater than or equal to p

Topological Sorting

Digraph

 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

Solving Topological Sorting Problem

 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-Based Topological Sorting

 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