Docsity

Algorithm Design Techniques: Brute Force, Divide-and-Conquer, Dynamic Programming, Greedy , Study notes of Computer Science

Partial preview of the text

Download Algorithm Design Techniques: Brute Force, Divide-and-Conquer, Dynamic Programming, Greedy and more Study notes Computer Science in PDF only on Docsity!

B.B. Karki, LSU CSC 3102

Algorithm Design Techniques

B.B. Karki, LSU CSC 3102

Algorithm Design Techniques

 Various design techniques exist:

 Classifying algorithms based on design ideas or commonality.

 General-problem solving strategies.

 Brute force

 Divide-and-conquer

 Decrease-and-conquer

 Transform-and-conquer

 Space-and-time tradeoffs

 Dynamic programming

 Greedy techniques

B.B. Karki, LSU CSC 3102

Brute Force

B.B. Karki, LSU CSC 3102

The Simplest Approach

 Brute force - the simplest of the design strategies

 Is a straightforward approach to solving a problem, usually directly based on

the problem’s statement and definitions of the concepts involved.

 Just do it - the brute-force strategy is easiest to apply.

 Results in an algorithm that can be improved with a modest amount of time.

 Brute force is important due to its wide applicability and simplicity.

 Weakness is subpar efficiency of most brute-force algorithms.

 Important examples:

 Selection sort, String matching, Convex-hull problem, and Exhaustive search.

B.B. Karki, LSU CSC 3102

Brute-Force String Matching

Algorithm BruteForceStringMatching ( T[0.. n - 1], P[0.. m - 1] )

//Implements string matching

//Input: An array T [0 ..n - 1] of n characters representing a text

// an array P [0 ..m - 1] of m characters representing a pattern

//Output: The position of the first character in the text that starts the first

// matching substring if the search is successful and -1 otherwise.

for i ← 0 to n - m do

j ← 0

while j < m and P[ j] = T[ i + j] do

j ← j + 1

if j = m return i

return -

In the worst case, the algorithm is Θ( mn ). But the average-case efficiency is shown to

be linear, i.e., Θ( m + n ) = Θ( n ) for searching in random texts.

 Align the pattern against the first m characters of the text and start matching the corresponding

pairs of characters from left to right until all m pairs match. But, if a mismatching pair is found,

then the pattern is shift one position to the right and character comparisons are resumed.

 The goal is to find i - the index of the leftmost character of the first matching substring in the text

such that t

i

= p

0

,….t

i+j

= p

j

, ….. T

i+m-

= p

m-

B.B. Karki, LSU CSC 3102

Convex-Hull Problem

 The convex hull of any

set S of n points is a

convex polygon with the

vertices at some of the

points (called extreme

points ) of S so that the

polygon contains all

points (either inside or on

its boundary)

 A set of points on the

plane is called convex if

for any two points in the

set, the entire line

segment with the end

points at P and Q

belongs to the set.

P
P
P
P
P
P
P
P

The convex hull for the set of eight points is the

convex polygon with its vertices at P1, P5, P6,

P7 and P

B.B. Karki, LSU CSC 3102

Exhaustive Search

 Exhaustive search is a brute-force approach to combinatorial problems.

 It suggests generating each and every combinatorial object of the problem,

selecting those of them of that satisfy the problem’s constraints and then

finding a desired object.

 Impratical for all but applicable to very small instances of problems.

 Examples:

 Traveling salesman problem

 Finding the shortest tour through a given set of n cities that visits each city

exactly once before returning to the city where it started.

 Knapsack problem

 Finding the most valuable list of out-of n items that fit into the knapscak.

 Assignment problem

 Finding an assignment of n people to execute n jobs with the smallest total

cost.

 These problems are the examples of so-called NP-hard problems.

B.B. Karki, LSU CSC 3102

Traveling Salesman Problem

 Traveling salesman problem

 Asks to find the shortest tour through a given

set of n cities that visits each city exactly

once before returning to the city where it

started.

 Finding the shortest Hamiltonian circuit of

the graph

 A cycle that passes through all the vertices of

the graph exactly once.

 A sequence of n +1 adjacent vertices v

0

, v

1

,

v

2

, … v

n -

, v

0

.

 Get all tours by generating all the

permutations of n - 1 intermediate cities,

compute the tour lengths, and find the

shortest among them.

 Efficiency:

 Total number of permutations = ( n - 1)!/

 Impractical but ok very small values of n.

5

1

7

8

2

3

Tour Length

a b c d a 2+8+1+7 = 18

a b d c a 2+3+1+5 = 11

a c b d a 23

a c d b a 11

a d b c a 23

a d c b a 18

More than one optimal solutions.

a

b

d c

B.B. Karki, LSU CSC 3102

Assignment Problem

 Given n people to be assigned to execute n

jobs, one person per job, find an assignment

with the smallest total cost.

C [ i, j ] is the cost for the i

th

person assigned to

the j

th

job.

 An instance of the assignment problem is

completely specified by its cost matrix C.

 Select one element in each row so that all

selected elements are in different columns

and the total sum of the selected elements is

the smallest possible.

 Generate all the permutations of integers 1,

2, … n , computing the total cost of each

assignment by summing up the

corresponding elements of the cost matrix,

and finally select the one with smallest sum.

 Complexity: n****! operations.

A small instance of the problem:

Job 1 Job 2 Job 3 Job 4

Person 1 9 2 7 8

Person 2 6 4 3 7

Person 3 5 8 1 8

Person 4 7 6 9 4

Total possible iterations: 4! = 24

Remaining iterations:

Permutation cost

<2 1 3 4> 2+6+1+4 = 13

<2 1 4 3> 2+6+8+9 = 25

.

<3 1 2 4> 7+6+8+4 = 25

.

<4 1 2 3> 8+6+8+9 = 31

The optimal solution is <2 1 3 4> with the minimum cost

of 13. So person 1 to job 2, person 2 to job 1, person 3 to

job 3 and person 4 to job 4.

B.B. Karki, LSU CSC 3102

Brute Force - Summary

 Simplest approach: just do it

 Selection sort: n

2

 String matching: mn

 Convex hull problem: n

3

 Exhaustive search

 Traveling salesman problem: ( n -1)!/

 Knapsack problem: 2

n

 Assignment problem: n!