Algorithm Paradigms

advertisement

Algorithm Paradigms

High Level Approach

To solving a Class of Problems

Paradigms for Algorithm Design

To aid in designing algorithms for new problems, we create a taxonomy of high level patterns or paradigms, for the purpose of structuring a new algorithm along the lines of one of these paradigms.

A paradigm can be viewed as a very high level algorithm for solving a class of problems.

Incremental Paradigm

Applicability

: Can be applied to develop an algorithm to solve a problem for which

– the input is a sequence and

– the output can be computed incrementally from the last output and the next term of the input.

Pseudocode

– S n

= Incremental(x

• Calculate S

0

1

, x

2

, . . x n

) for initial sequence x

1

• for i = initial index to n

– S i+1

= S i or empty sequence

 x i+1

(

 represents arbitrary operation/function)

Examples

– Sum, Maximum, Minimum of numerical sequence, insertion sort

Divide and Conquer Paradigm

• Applicability

– Can be applied to develop an algorithm to solve a problem for which

– the problem can be divided into smaller problems of the same type and

– the output can be computed from solutions to the smaller problems in top down direction until division results in “atomic” problems which can be solved directly.

• Pseudocode

– Answer = D&C( Input )

• If Atomic( Input ) then

Else

Return Answer(Input)

Divide Input into Input

1 and Input

2

Answer

1

= D&C( Input

1

)

Answer

2

= D&C( Input

2

)

End If

Return Answer(Input) = Combination( Answer

1

, Answer

2

)

• Examples : Merge Sort, Quick Sort, C(n,k)

Dynamic Programming Paradigm

• Applicability

– Can be applied to develop an algorithm to solve a problem for which

– the problem can be divided into smaller problems of the same type and

– the output can be computed from solutions to the smaller problems in bottom up direction by solving “atomic” problems first, and by

– storing solutions to all problems in a table to be used in solving larger problems.

• Pseudocode

– Given P.Input, construct table T for DP( P ) where X is index of problem

P to be solved

– For every atomic problem A with index Y, calculate DP( A ) and store in table T(Y).

– While T( X ) is null,

• Identify an intermediate problem I with index V for which T( V ) is null and

T(U) is non null for every immediate subproblem I’ with index U of I.

• Store DP( I ) in T(V) as a combination of T( U ) entries

• Examples : Knapsack, C(n,k), Edit Distance, Optimum

Search Tree

Greedy Paradigm

• Applicability

– Can be applied to solve a problem for which the solution can be obtained by

– making a sequence of choices or selections

,

• subject to some constraint

• to optimize some objective function.

• Pseudocode

– While a solution has not been found do

• Selection : select or make decision to maximize improvement in objective function.

• Feasibility : accept selection or decision if it does not violate the problem constraint.

• Solution : check to see if a solution has been reached.

• Examples : Shortest Path, Minimum Spanning Tree, File

Compression by Huffman Encoding

Backtrack Programming Paradigm

• Applicability

Applicable to problems where

• the output is a sequence over an alphabet A (possibly ordered)

• with a possible constraint on the sequence

• and a possible objective function to be optimized

• Pseudocode

– Finds an optimum sequence over A subject to constraint C corresponding to a path from root to leaf in a search tree T

– Expand(s : String)

• curvalue = value of string s

• /* number of colors, length of path, sum of sizes */

• If s is a solution (path to leaf of T) then

– if curvalue better_than best then

» best = curvalue

» s* = s

• Else

– for each a in such that Constraint(s+a)

» Expand (s + a)

• End if

– Backtrack( P : problem input )

• best = value of

• expand (

)

• Output s*, best

• Examples : Vertex Coloration, Knapsack, Longest Path

Download