4 views

Uploaded by Mohamed Said Daw

Brute Force Algorithms explained, enjoy!

Brute Force Algorithms explained, enjoy!

© All Rights Reserved

- Numpy Reference
- Others
- DAA-2-marks
- ADA - Lesson Plan
- Alg Resource Inequalities
- ABC_ Always Be Coding
- Computer Science in a Box
- Sorting
- Test1
- Lesson 4 - Part 1: GCF
- Cse-IV-Design and Analysis of Algorithms [10cs43]-Notes
- Algorithms
- database example
- Euler L. a Solution to a Problem of Fermat, On Two Numbers of Which the Sum is a Square and the Sum of Their Squares i[...]Range (1780)(en)(3s)
- Instruction Plan for CSE240
- Analysis and Design of Algorithms _ADA_ _Elective I
- Assignment No.2 Sort
- Ds 3 Sorting
- 1
- 17403_CS2finalreview

You are on page 1of 47

Algorithms

(CSCE 321)

Department of Computer Science, AUC

Brute Force Algorithms

Brute Force Algorithms

Brute Force Algorithms

Elementary Sorting Algorithms

Selection Sort

Bubble Sort

Insertion Sort

Brute Force Sequential Search

Brute Force String Matching

Closest Pair Problem

Exhaustive Search

Traveling Salesman Problem

0/1 Knapsack Problem

Assignment problem

Prof. Amr Goneid, AUC 3

1. Brute Force Algorithms

Brute Force is a straightforward approach to

solving a problem, usually directly based on

the problems statement and definitions of

the concepts involved

In many cases, Brute Force does not

provide you a very efficient solution

Brute Force may be enough for moderate

size problems with current computers.

2. Elementary Sorting Algorithms

Sorting

Selection Sort

Bubble Sort

Insertion Sort

Sorting

The Sorting Problem:

Input: A sequence of keys {a1 , a2 , , an}

output: A permutation (re-ordering) of the input,

{a1 , a2 , , an} such that a1 a2 an

Sorting is the most fundamental algorithmic

problem in computer science

Sorting can efficiently solve many problems

Different sorting algorithms have been

developed, each of which rests on a particular

idea.

Some Applications of Sorting

Efficient Searching

Ordering a set so as to permit efficient binary

search.

Uniqueness Testing

Test if the elements of a given collection of

items are all distinct.

Deleting Duplicates

Remove all but one copy of any repeated

elements in a collection.

Some Applications of Sorting

Closest Pair

Given a set of n numbers, find the pair of numbers that

have the smallest difference between them

Median/Selection

Find the kth largest item in set S. Can be used to find the

median of a collection.

Frequency Counting

Find the most frequently occurring element in S, i.e., the

mode.

Prioritizing Events

Sorting the items according to the deadline date.

Types of Sorts

In-Place Sort:

An in-place sort of an array occurs within the

array and uses no external storage or other

arrays

In-place sorts are more efficient in space

utilization

Stable Sorts:

A sort is stable if it preserves the ordering of

elements with the same key.

i.e. If elements e1 and e2 have the same key,

and e1 appears earlier than e2 before sorting,

then e1 is located before e2 after sorting.

Sorting

What will count towards T(n):

Comparisons of array elements

Swaps or shifts of array elements

We compare sorting methods on the

bases of:

1. The time complexity of the algorithm

2. If the algorithm is In-Place and

Stable or not

Sorting

We examine here 3 elementary sorting

algorithms:

Selection Sort

Bubble Sort

Insertion Sort

These have a complexity of O(n2)

(a) Selection Sort

The general idea of the selection sort is that for

each slot, find the element that belongs there

Assume elements to be in locations 0..n-1

Let (i) be the start of a sub-array of at least 2

elements, i.e. i = 0 .. n-2

for each i = 0 .. n-2

Find smallest element in sub-array a[i..n-1]

Swap that element with that at the start of the

sub-array (i.e. with a[i]).

How it works

4 3 1 6 2 5

1 3 4 6 2 5

1 2 4 6 3 5

1 2 3 6 4 5

1 2 3 4 6 5

1 2 3 4 5 6

Demos

http://www.cosc.canterbury.ac.nz/people/mukundan/dsal/SSort.html

http://coderaptors.com/?SelectionSort

Selection Sort Algorithm

First, let us compute the cost of finding the location of

the minimum element in a subarray a[s..e ]

minimum (a,s,e)

{ m=s; e

for j = s+1 to e

j s 1

if (a[j] a[m]) m = j ;

return m;

} 1comp

Hence, T(s,e) = e s

For an array of size (n) it will cost T(n) = n-1 comparisons

Selection Sort Algorithm

SelectSort (a[0..n-1 ]) n 2

{

i 0

for i = 0 to n-2 (n 1 i ) comp

{

m = minimum (a , i , n-1) ;

swap (a[i] , a[m]);

}

} 1swap

Analysis of Selection Sort

n2

Tswap (n) 1swap (n 1)

i 0

n2

n(n 1)

Tcomp (n) n 1 i comp

i 0 2

T (n) 0.5n 0.5n 1 (n )

2 2

Exact algorithm

Performance of Selection Sort

independent of the initial data ordering.

In-Place Sort Yes

Stable Algorithm No

efficient enough for large amounts of data.

(b) Bubble Sort

and swap if necessary

Assume elements to be in locations 0..n-1

Let (i) be the index of the last element in a sub-

array of at least 2 elements, i.e. i = n-1 .. 1

Prof. Amr Goneid, AUC 19

Bubble Sort Algorithm

for each i = n-11

Compare adjacent elements aj and aj+1 , j = 0..i-1 and

swap them if aj > aj+1

a[0..i] to location (i).

How it works

4 3 1 2

3 4 1 2

3 1 4 2

3 1 2 4

1 3 2 4

1 2 3 4

Demos

http://www.cosc.canterbury.ac.nz/people/mukundan/dsal/BSort.html

http://coderaptors.com/?BubbleSort

Bubble Sort Algorithm

BubbleSort (a[ 0..n-1])

1

{ i n 1

for j = 0 to i-1 j0

}

1comp 1swap

Analysis of Bubble Sort

i 1

n(n 1) n 1

Tcomp (n) 1comp i

1

i n 1 j 0 i 1 2

n(n 1)

Similarly Tswap

2

Hence T (n) n 2 n O(n 2 )

Bubble Sort (a variant)

void bubbleSort (itemType a[ ], int n)

{ int i , j; bool swapped;

for (i = n-1; i >= 1; i--)

{ swapped = false;

for (j = 0; j < i; j++ )

{ if (a[j] > a[j+1] )

{ swap(a[j] , a[j+1]); swapped = true; }

}

if (!swapped) return;

}

}

Prof. Amr Goneid, AUC 25

Performance of Bubble Sort

when the array is inversely sorted: O(n2).

The variant has a best case when the array is

already sorted in ascending order: (n). Outer

loop works for only i = n-1 and there will be no

swaps.

In-Place Sort Yes

Stable Algorithm Yes

For small jobs, not efficient enough for large

amounts of data.

(c) Insertion Sort

The general idea of the insertion sort is that

for each element, find the slot where it

belongs.

Performs successive scans through the data.

When an element is out of sequence (less

than its predecessor), it is pulled out and then

inserted where it should belong.

Array elements have to be shifted to the right

to make space for the insertion.

Prof. Amr Goneid, AUC 27

How it works

2 7 1 5 8 6 3 4

1 2 7 5 8 6 3 4

1 2 5 7 8 6 3 4

1 2 5 6 7 8 3 4

1 2 3 5 6 7 8 4

1 2 3 4 5 6 7 8

Demos

http://www.cosc.canterbury.ac.nz/people/mukundan/dsal/ISort.html

http://coderaptors.com/?InsertionSort

Insertion Sort Algorithm

InsertSort (a[0..n-1 ]) n 1

{ for i =1 to n-1

{ i 1

j = pointer to element ai; 1comp

v = copy of ai; Best 1 time

while( j > 0 && aj-1 > v) Worst i times

{ move data right: aj aj-1

move pointer left: j-- } 1shift

Insert v at last (j) location: aj v;

}

}

Prof. Amr Goneid, AUC 30

Analysis of Insertion Sort

loop iterates (i) times:

n 1 n 1

1 2

i 1

Tcomp ( n ) i 1comp i ( n n )

i 1 2

1 2

Similarly Tshift ( n ) ( n n )

2

Hence T ( n ) ( n 2 n ) O( n 2 )

Analysis of Insertion Sort

ascending order,while loop works zero times

n 1

T ( n ) 1comp ( n 1 ) ( n )

i 1

Performance of Insertion Sort

is when the array is inversely sorted: O(n2).

Best case when the array is already sorted in

ascending order: (n). While loop will not

execute and there will be no data movement.

In-Place Sort Yes

Stable Algorithm Yes

For modest jobs, not efficient enough for large

amounts of data.

3. Brute Force Sequential Search

// Search key K in A[0..n - 1]

Worst case:

ALGORITHM SequentialSearch( A[ 0..n ], K )

Key not found.

A[ n ] K T(n) = O(n)

i 0 comparisons

while A[ i ] K do

i i 1

if i n return i

else return - 1

4. Brute Force String Matching

Text: a (longer) string of n characters to search in

Problem: find a substring in the text that matches

the pattern

Example: Pattern NOT,

text NOBODY_NOTICED_HIM

Typical Applications

find function in the text editor, e.g., MS-Word,

Google search

Brute Force String Matching

Brute-force algorithm

Step 1 Align pattern at beginning of text

Step 2 Moving from left to right, compare each character of

pattern to the corresponding character in text until

all characters are found to match (successful search);

or

a mismatch is detected

Step 3 While pattern is not found and the text is not yet

exhausted, realign pattern one position to the right and

repeat Step 2

Brute Force String Matching

ALGORITHM BFStringMa tch( T [ 0..n 1 ], P [ 0..m 1 ])

for i 0 to n-m do

j0

while j m and P [ j ] T [ i j ]

j j1

if j m return i

return -1

5. Closest Pair Problem

Problem:

Find the two closest points in a set of n points (in the two

dimensional Cartesian plane).

Brute-force algorithm

Compute the distance between

every pair of distinct points

Return the indexes of the points for

which the distance is the smallest.

Closest Pair Problem

T( n ) cost for sqrt

1 (n-i)

i 1 j i 1

i

i 1

n(n-1)/2

i 1

O(n 2

)

Closest Pair Problem

The Hamming distance between two strings of equal length

is defined as the number of positions at which the

corresponding symbols are different. It is named after Richard

Hamming (19151998), a prominent American scientist.

Suppose that the points in the problem of closest

pairs are strings and hence the distance between

two of such strings is now measured by the

Hamming Distance. Write an algorithm to find the

closest pair of strings in an array of strings and find

the number of comparisons done by this algorithm.

6. Exhaustive Search

A brute-force approach to combinatorial problem

Generate each and every element of the problems domain

Then compare and select the desirable element that

satisfies the set constraints

Involve combinatorial objects such as permutations,

combinations, and subsets of a given set

The time efficiency is usually bad usually the

complexity grows exponentially with the input size

Three examples

Traveling salesman problem

Knapsack problem

Assignment problem

(a)Traveling Salesman Problem (TSP)

Given n cities with known distances between each pair, find the

shortest tour that passes through all the cities exactly once

before returning to the starting city

Alternatively: Find shortest Hamiltonian circuit in a weighted

connected graph (The circuit problem is named after Sir

William Rowan Hamilton) 2

Example: a b

5 3

8 4

c 7 d

Traveling Salesman Problem (TSP)

abcda 2+3+7+5 = 17 Optimal

abdca 2+4+7+8 = 21

acbda 8+3+4+5 = 20

acdba 8+7+4+2 = 21

adbca 5+4+3+8 = 20

adcba 5+7+3+2 = 17 Optimal

Very High complexity O(n!)

(b) 0/1 Knapsack Problem

Given n items:

weights: w1 w2 wn

values: v1 v2 v n

a knapsack of capacity W

Find most valuable subset of the items that fit into the knapsack

item weight value ($)

1 2 20

2 5 30

3 10 50

4 5 10

0/1 Knapsack Problem

{ } 0 $00

{1} 2 $20

{2}

{3}

5

10

$30

$50

Generates 2n

{4} 5 $10 possible

{1,2} 7 $50

{1,3} 12 $70 subsets,

{1,4} 7 $30

{2,3} 15 $80 Optimal T(n) = O(2n)

{2,4} 10 $40

{3,4} 15 $60

{1,2,3} 17 not feasible

{1,2,4} 12 $60

{1,3,4} 17 not feasible

{2,3,4} 20 not feasible

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

(c) Assignment Problem by Exhaustive Search

per job. The cost of assigning person i to job j is C[i,j]. Find an

assignment that minimizes the total cost.

Person 1 9 2 7 8

Person 2 6 4 3 7

Person 3 5 8 1 8

Person 4 7 6 9 4

Algorithmic Plan:

Generate all legitimate assignments, compute their costs, and select the

cheapest one.

How many assignments are there? n!

Pose the problem as one about a cost matrix:

Assignment Problem by Exhaustive Search

9 2 7 8

6 4 3 7

C

5 8 1 8

T(n) = # of

7 6 9 4 assignments = O(n!)

1, 2, 3, 4 9+4+1+4=18

1, 2, 4, 3 9+4+8+9=30

1, 3, 2, 4 9+3+8+4=24

1, 3, 4, 2 9+3+8+6=26

1, 4, 2, 3 9+7+8+9=33

1, 4, 3, 2 9+7+1+6=23

etc.

- Numpy ReferenceUploaded bypagol_23_smh
- OthersUploaded byKRISHNAVINOD
- DAA-2-marksUploaded byVds Krishna
- ADA - Lesson PlanUploaded bysoniadrall
- Alg Resource InequalitiesUploaded byallisonbrussell10
- ABC_ Always Be CodingUploaded byMircea Enache
- Computer Science in a BoxUploaded byppant21
- SortingUploaded byAnto Padaunan
- Test1Uploaded byRajat Kalia
- Lesson 4 - Part 1: GCFUploaded bymisterreid
- Cse-IV-Design and Analysis of Algorithms [10cs43]-NotesUploaded bySampreeth03
- AlgorithmsUploaded byupkilo
- database exampleUploaded byapi-372210487
- Euler L. a Solution to a Problem of Fermat, On Two Numbers of Which the Sum is a Square and the Sum of Their Squares i[...]Range (1780)(en)(3s)Uploaded byeihdqdlm
- Instruction Plan for CSE240Uploaded bySohail Choudhary
- Analysis and Design of Algorithms _ADA_ _Elective IUploaded byDebashish Mahapatra
- Assignment No.2 SortUploaded byDenise Nelson
- Ds 3 SortingUploaded byVinamra Mittal
- 1Uploaded by1012804201
- 17403_CS2finalreviewUploaded byjocansino4496
- Design $ Analysis of AlgorithmUploaded bySiva Sankar
- c ComplexitiesUploaded bygmr352
- DS Searching AlgorithmsUploaded byAhmed Mostafa
- computer programming Two marksUploaded byThirunavukkarasuKumarasamy
- comparison_sortUploaded bycali_ban
- 20170919130927Introduction.pptxUploaded bynasilalap
- 15745_K1R03 CSE205Uploaded byChetan Sharma
- Data Structure UNIT I Part 1Uploaded byGanesh Partheeban
- gcse-1Uploaded byPrashantBhattacharji
- AlgorithmsUploaded bySohail

- Forecasting_5_TrendSeasonality.pptxUploaded byJatin Madaan
- Paper 22-Dynamics of Mandelbrot Set With Transcendental FunctionUploaded byEditor IJACSA
- Mr 6 2017 Solutions 1Uploaded byStephen
- week 1 fundamantal Concepts of Alegbra.docxUploaded byConroy John
- Generalized_Residual_Entropy_and_Upper_Record_Valu.pdfUploaded byZahid Bhat
- Drawing the HypercubeUploaded bydan17klvj
- l5c_2Uploaded byRINU SEBASTIAN
- Lecture03 SlidesUploaded byYasir Ali
- Lets Learn MathematicsUploaded byamjadullah
- Algebra II - Worded ProblemsUploaded byDwight Solina
- ProbabilityUploaded bydanielherreros
- Vector FittingHeinzelUploaded byFranco Carlos
- Problem Solving and Proofs 27 30.PDF [SHARED]Uploaded byjuanmompotes
- RMO-12-PAPER-4Uploaded byNilesh Gupta
- Prediction of Pressure Drop of Slurry FLow in Pipeline by Hybrid (...) (Ej) [LAHIRI, S.K.; GHANTA, K.C.] [Chinese J. of Ch. Eng. Vol 16 n6; 2008] {9s}Uploaded byR_M_M_
- 04641865Uploaded byFatihUniversitesi SummerSchool
- Ellipse Formulae SheetUploaded byShehbaz Thakur
- mathapril11 1Uploaded byapi-301961986
- Game theory in math educationUploaded byAndreiNicolaiPacheco
- edma360 lessonsUploaded byapi-299037283
- Coordination of Directional Overcurrent Relays Using Seeker AlgoUploaded bySoumikDas
- Trigonometry-3 JEE MAIN AND ADVANCED.pdfUploaded byNirmal yadav s channel
- daahwsolUploaded bypraneethkoralla
- EjemploUploaded byAstrid Carolina Matamoros Lugo
- versace tiana 18354795 edpr3003 a2Uploaded byapi-314238812
- 501_10_08Uploaded bygestaf
- Legend Logic DesignUploaded byMohamedSalah
- Fractions and Conversion to DecimalUploaded byAbdul Rasheed
- An Introduction of a Soft Topological SpacesUploaded bymath_tapas
- Contemporary Mathematics FINALSUploaded byJeibi Atienza