Docsity

Algorithm Design Techniques: Brute Force Approach and Specific Algorithms - Prof. B. Karki, Exams of Computer Science

Partial preview of the text

Download Algorithm Design Techniques: Brute Force Approach and Specific Algorithms - Prof. B. Karki and more Exams Computer Science in PDF only on Docsity!

B.B. Karki, LSU CSC 3102

Algorithm Design Techniques

B.B. Karki, LSU CSC 3102

Algorithm Basics

(Chapters: 1 and 2)

 Notion of algorithm: Section 1.

 Fundamentals of algorithmic problem solving: Section 1.

 Important problem types: Section 1.

 Analysis of algorithmic efficiency: Sections 2.1 and 2.

 Non-recursive algorithms: Section 2.

 Recursive algorithms: Section 2.

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

Selection Sort

Algorithm SelectionSort ( A[0.. n-1] )

//Sorts a given array

//Input: An array A[0 ..n-1] of orderable elements

//Output: Array A[0 ..n-1] sorted in ascending order

for i ← 0 to n - 2 do

min ← i

for j ← i + 1 to n - 1 do

if A[ j] < A[min] min ← j

swap A[ i] and A[ min]

C ( n ) = 1

j = i + 1

n − 1

∑

= [( n − 1 ) − ( i + 1 ) + 1 ] = ( n − 1 − i )

i = 0

n − 2

∑

i = 0

n − 2

∑

i = 0

n − 2

∑

( n − 1 ) n

∈ Θ( n

2

 Scan the list repeatedly to find the elements, one at a time, in an non-

decreasing order.

 On the i

th

pass through the list, search for the smallest item among the last

n - i elements and swap it with A [ i ]. After n - 1 passes, the list is sorted.

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

Brute-Force Approach for Convex-Hull Problem

 A brute-force approach is based on the fact:

 A line segment connecting two points i and j is a part of its convex hull’s boundary if and only

if all other points of the set lie on the same side of the straight line through these points.

 Repeating this test for every pair of points yields a list of line segments that make up the

convex hull’s boundary.

 Find the equation of the straight line through two points (x1, y1) and (x2, y2)

ax + by = c , where a = y2 - y1, b = x1 - x2 and c = x1y2 - y1x2.

 This line divides the plane into two half-planes:

 For all the points in one of them ax + by > c , while for all other points in the other ax + by < c.

 For all points on the line itself, ax + by = c.

 Check whether the expression ax + by - c has the same sign at each of these points

 To check whether certain points lie on the same side of the line.

 If yes, the line forms one side of the polygon.

 Efficiency of the algorithm: O ( n

3

 For each of n ( n - 1)/2 pairs of distinct points, one needs to find the sign of ax + by - c for each

of the other n - 2 points.

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

Knapsack Problem

 Given n items of known weights

w

1

, …., w

n

and values v

1

, …, v

n

and

a knapsack of capacity W , find the

most valuable subset of the items

that fit into the knapsack.

 Consider all the subsets of the set

of n items given,

 Computing the total weight of

each subset in order to identify

feasible subsets (the ones with the

total not exceeding the knapsack’s

capacity).

 Finding a subset of the largest

value among them.

 Since the number of subsets of an

n - element set is 2

n

, the exhaustive

search leads to Ω(

n

) algorithm.

Knapsack item1 item2 item3 item

W = 10

w = 7

v = $

w = 3

v = $

w = 4

v = $

w = 5

v = $

Subset Total weight Total value

Null 0 $

{1} 7 $

{2} 3 $

{3} 4 $

{4} 5 $

{1,2} 10 $

{1,3} 11 not feasible

{1,4} 12 not feasible

{2,3} 7 $

{2,4} 8 $

{3,4} 9 $

{1,2,3} 14 not feasible

{1,2,4} 15 not feasible

…

{1,2,3,4} 19 not feasible

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 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.