54
FIT 1029 Algorithmic Problem Solving Lecture 7 Brute Force Prepared by Julian García COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material has been reproduced and communicated to you by or on behalf of Monash University pursuant to Part VB of the Copyright Act 1968 (the Act).The material in this communication may be subject to copyright under the Act.Any further reproduction or communication of this material by you may be the subject of copyright protection under the Act. Do not remove this notice.

07 Brute Force

  • Upload
    stacy

  • View
    248

  • Download
    1

Embed Size (px)

DESCRIPTION

a

Citation preview

FIT 1029 Algorithmic Problem Solving

Lecture 7 Brute Force

Prepared by Julian García

COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969

WARNING This material has been reproduced and communicated to you by or on behalf of Monash University pursuant to Part VB of the Copyright Act 1968 (the Act).The material in this communication may be subject to copyright under the Act.Any further

reproduction or communication of this material by you may be the subject of copyright protection under the Act. Do not remove this notice.

4

62

8

10

124

62

8

10

12

4

62

8

10

12

4

62

8

10

124

62

8

10

12 224

62

10

Using Prim’s algorithm

Using Kruskal’s algorithm

4

62

8

10

12

4

62

8

10

124

62

8

10

12

4

62

8

10

12

4

62

8

10

12

4

62

8

10

12 224

62

10

Come up with all possible solutions or states...

Brute Force(just do it)

• Can be applied to most problems ...resulting in reasonable algorithms.

• Generally cheap to design and implement.

• Useful for small size instances of the problems.

• Generating and checking solutions can occur in parallel.

Brute Force is Good...

Finding a phone numberConsider the problem of trying to find a

telephone number in a phone book.

Aabataglilia A. ........9317 4532 Aabhas A. ...........0433 26 1471 Aabid Y & H. ............9729 8181 Aabryn B & K. ........ 9561 8162

ApproachInput: phoneNum, a phone numberOutput: Name corresponding to the phoneNum

Prints the name associated to a phone number (if the number exists in the telephone book)

Look at the first record

Does the record have the phone number?

Look at the next record

print name

yes

Is there another record?

no

yes

stop

Sequential Search! Given a value: target, and a list L.! Find the first item in L, which has the

value target.! Return: ◦ If target is found then return the index of

the item. ◦ If target not found then return -1.

Algorithm: Sequential SearchInput: target, and a list L[0..n-1]Output: If target is in L, returns the index of the first item with that value. Otherwise returns -1.

Search for target in L

k ← 0

k = length(L)

k ← k +1

return -1yes

L[k] = target?

no

no

yesreturn k

Algorithm: Sequential SearchInput: target, and a list L[0, n-1]Output: If target is in L, returns the index of the first item with that value. Otherwise returns -1.

Search for target in L

SequentialSearch(target, L)k ← 0 while (k ≠ length(L)) do {

if (L[k] = target ) { return k } k ← k+1

} return -1

k ← 0

k = length(L)

k ← k +1

return -1yes

L[k] = target?

no

no

yesreturn k

SequentialSearch(target, L) k ← 0 while (k ≠ length(L)) do {

if (L[k] = target ) { return k } k ← k+1

} return -1

Algorithm: Sequential SearchInput: target, and a list L[0.. n-1]Output: If target is in L, returns the index of the first item with that value. Otherwise returns -1.

SequentialSearch(target, L)k ← 0 while (k ≠ length(L)) do {

if (L[k] = target ) { return k } k ← k+1

} return -1

input: L = [3,1,4,5,9,2,6]; target =2 output: 5

input: L = [ ]; target =2 output: -1

input: L = [2,2,3,5,9,2]; target =2 output: 0

input: L = [3,1,4,9,5,6,1]; target =5 output: 4

input: L = [3,1,4,9,5,6,1]; target =8 output: -1

Is the substring cagcag in this string?

A) Yes

B) No

atggctaagtctatgctttctggaattgtttttgctggtcttgttgctgctgcagcggccagttcggccaacaacagcgccgccaacgtctccgttttggagagtgggcccgctgtgcaggaagtgccagcgcgcacggtcacagctcgcctggcgaagcctttgctgcttcttttctgctcttgctgcgactttggcagcagct

String Matching

A substring of a string S is another string T "that occurs in" S.

is T a substring of S?

atggctaagtctatgctttctggaattgtttttgctggtcttgttgctgctgcagcggccagttcggccaacaacagcgccgccaacgtctccgttttggagagtgggcccgctgtgcaggaagtgccagcgcgcacggtcacagctcgcctggcgaagcctttgctgcttcttttctgctcttgctgcgactttggcagcagct

cagcag

Brute Force String Matching

atggctaagtctatgctttctggaattgtttttgctggtcttgttgctgctgcagcggccagttcggccaacaacagcgccgccaacgtctccgttttggagagtgggcccgctgtgcaggaagtgccagcgcgcacggtcacagctcgcctggcgaagcctttgctgcttcttttctgctcttgctgcgactttggcagcagct

cagcag

Brute Force String Matching

atggctaagtctatgctttctggaattgtttttgctggtcttgttgctgctgcagcggccagttcggccaacaacagcgccgccaacgtctccgttttggagagtgggcccgctgtgcaggaagtgccagcgcgcacggtcacagctcgcctggcgaagcctttgctgcttcttttctgctcttgctgcgactttggcagcagct

cagcag

Brute Force String Matching

atggctaagtctatgctttctggaattgtttttgctggtcttgttgctgctgcagcggccagttcggccaacaacagcgccgccaacgtctccgttttggagagtgggcccgctgtgcaggaagtgccagcgcgcacggtcacagctcgcctggcgaagcctttgctgcttcttttctgctcttgctgcgactttggcagcagct

cagcag

Brute Force String Matching

atggctaagtctatgctttctggaattgtttttgctggtcttgttgctgctgcagcggccagttcggccaacaacagcgccgccaacgtctccgttttggagagtgggcccgctgtgcaggaagtgccagcgcgcacggtcacagctcgcctggcgaagcctttgctgcttcttttctgctcttgctgcgactttggcagcagct

cagcag

Brute Force String Matching

Look athe the first length(T) carachters of S

Does the substring match

T?

Move one character along S

return true

yes

Are we at the end of S?

no

no

yes return false

Algorithm: StringMatchInput: string S, potential substring TOutput: true if T is in S, false if T is not in S.

Checks if a string T is a substring of S

Strings as Lists

0. a1. t

2. g

Str

Str = [a, t, g] or Str = “atg” Str[1] = “t”Str[1..2] = [t, g] = “tg”Str[0..1] = [a, t] = “at”

Substrings

Str = [a, g, t, t, a, c, g, a, t, t, a]0 1 2 3 4 5 6 7 8 9 10

Str[2..4] = [t, t, a]

Str[3..5] = [t, a, c]

Str[8 ..10] = [t, t, a]

k ← 0 while (k + length(T) ≤ length(S)) do {

If (S[k .. k+length(T)-1] = T) {

return true } k ← k + 1

} return false

Algorithm: StringMatch(S, T)Input: string S, substring TOutput: true if T is in S, false if T is not in S.

Checks if T is a substring of S

atggctaagtctatgctttctggaattgtttttgctggtcttgttgctgctgcagcggccagttcggccaacaacagcgccgccaacgtctccgttttggagagtgggcccgctgtgcaggaagtgccagcgcgcacggtcacagctcgcctggcgaagcctttgctgcttcttttctgctcttgctgcgactttggcagcagct

T = cagcaglenght(T) = 6

k=0 k+lenght(T)-1=5

atggctaagtctatgctttctggaattgtttttgctggtcttgttgctgctgcagcggccagttcggccaacaacagcgccgccaacgtctccgttttggagagtgggcccgctgtgcaggaagtgccagcgcgcacggtcacagctcgcctggcgaagcctttgctgcttcttttctgctcttgctgcgactttggcagcagct

T = cagcaglenght(T) = 6

k=1 k+lenght(T)-1=6

atggctaagtctatgctttctggaattgtttttgctggtcttgttgctgctgcagcggccagttcggccaacaacagcgccgccaacgtctccgttttggagagtgggcccgctgtgcaggaagtgccagcgcgcacggtcacagctcgcctggcgaagcctttgctgcttcttttctgctcttgctgcgactttggcagcagct

T = cagcaglenght(T) = 6

k=2 k+lenght(T)-1=7

atggctaagtctatgctttctggaattgtttttgctggtcttgttgctgctgcagcggccagttcggccaacaacagcgccgccaacgtctccgttttggagagtgggcccgctgtgcaggaagtgccagcgcgcacggtcacagctcgcctggcgaagcctttgctgcttcttttctgctcttgctgcgactttggcagcagct

T = cagcaglenght(T) = 6

k=3 k+lenght(T)-1=8

Check substring given by kStr[k..k+lenght(T)-1] has size length(T)

k k+ length(T)-1

k ← 0 while (k + length(T) ≤ length(S)) do {

If (S[k .. k+length(T)-1] = T) {

return true } k ← k + 1

} return false

Start checking at the beginning

Are there enough characters left?k + length(T) is the size of we have covered

Check substring given by kStr[k..k+lenght(T)-1] has size length(T)

k k+ length(T)-1

Ready for another substring

k ← 0 while (k + length(T) ≤ length(S)) do {

If (S[k .. k+length(T)-1] = T) {

return true } k ← k + 1

} return false

k ← 0 while (k + length(T) ≤ length(S)) do {

j ← 0 match ← true while (j < length(T) and match = true) do { If (S[j+k] ≠ T[j]) {

match ← false } j ← j+1

} If (match == true) {

return true } k ← k + 1

} return false

k ← 0 while (k + length(T) ≤ length(S)) do {

If (S[k .. k+length(T)-1] = T) {

return true } k ← k + 1

} return false

j ← 0 match ← true while (j < length(T) and match = true) do { If (S[j+k] ≠ T[j]) {

match ← false } j ← j+1

}

Check substring given by kStr[k..k+lenght(T)-1] has size length(T)

k k+ length(T)-1

j

T

k ← 0 while (k + length(T) ≤ length(S)) do {

j ← 0 match ← true while (j < length(T) and match = true) do { If (S[j+k] ≠ T[j]) {

match ← false } j ← j+1

} If (match == true) {

return true } k ← k + 1

} return false

k ← 0 while (k + length(T) ≤ length(S)) do {

If (S[k .. k+length(T)-1] == T) {

return true } k ← k + 1

} return false

Why a,g, t and c?

A Boat ProblemA farmer wishes to take a goat, a cabbage and a wolf across

a river. However, his boat can only take one of them at a time. Therefore he will need to make several trips. Also, he cannot leave the goat alone with the cabbage, and cannot

leave the wolf alone with the goat. Find a way for the farmer to get everything across the river.

Cover Design of Anany Levitin, Introduction to the Design and Analysis of Algorithms (2nd Edition)

Left of the river

Right of the river

farmerwolf

cabbagegoat

Left of the river

Right of the river

farmerwolf

cabbagegoat

Left of the river

Right of the river

farmergoat cabbage

wolf

Left of the river

Right of the river

farmercabbage

wolfgoat

List StatesFarmer Wolf Goat CabbageR R R RR R R LR R L RR R L LR L R RR L R LR L L RR L L LL R R RL R R LL R L RL R L LL L R RL L R LL L L RL L L L

R = right side of the riverL = left side of the river

END

START

List StatesFarmer Wolf Goat CabbageR R R RR R R LR R L RR R L LR L R RR L R LR L L RR L L LL R R RL R R LL R L RL R L LL L R RL L R LL L L RL L L L

R = right side of the riverL = left side of the river

END

START

Representation of StatesFarmer Wolf Goat CabbageR R R R FWGCR R R L FWGcR R L R FWgCR L R R fwGCR L R L FwGcL R L R fWgCL R L L fWgcL L R L fwGcL L L R fwgCL L L L fwgc

! lower case they on the left side ! UPPER CASE they on the right side

! R means they are on the right side of the river ! L means they are on the left side of the river

Representation of StatesFarmer Wolf Goat CabbageR R R R FWGCR R R L FWGcR R L R FWgCR L R R fwGCR L R L FwGcL R L R fWgCL R L L fWgcL L R L fwGcL L L R fwgCL L L L fwgc

! lower case they on the left side ! UPPER CASE they on the right side

! R means they are on the right side of the river ! L means they are on the left side of the river

Representation of StatesFarmer Wolf Goat CabbageR R R R FWGCR R R L FWGcR R L R FWgCR L R R fwGCR L R L FwGcL R L R fWgCL R L L fWgcL L R L fwGcL L L R fwgCL L L L fwgc

! lower case they on the left side ! UPPER CASE they on the right side

! R means they are on the right side of the river ! L means they are on the left side of the river

fwgc FWGC

fWgC

Representation of StatesFarmer Wolf Goat CabbageR R R R FWGCR R R L FWGcR R L R FWgCR L R R fwGCR L R L FwGcL R L R fWgCL R L L fWgcL L R L fwGcL L L R fwgCL L L L fwgc

! R means they are on the right side of the river ! L means they are on the left side of the river ! lower case they on the left side ! UPPER CASE they on the right side

Final State

Start State

Transitions between states

FwGc

fwGc

FwGC

FWGc

fwgCfWgc

FWgC

fWgCFWGC

fwgc

Transitions between states

FwGc

fwGc

FwGC

FWGc

fwgCfWgc

FWgC

fWgCFWGC

fwgc

FwGc

fwGc

FwGC

FWGc

fwgCfWgc

FWgC

fWgCFWGC

fwgc

farmercabbage

wolf

goat

Boat Solution

fwgc FwGc fwGc

FwGC

FWGc

fwgC

fWgc

FWgC fWgC FWGC

Brute force approach to the boat problem

1. List all possible states2. Eliminate states that are not valid3. Identify transitions between states4. Look for a path that starts at the initial state, and ends at the end state

Traveling SalesmanSuppose you are given the following driving distances in

kms between the following capital cities.

Find the shortest route that enables a salesman to start at Canberra, visit all the other cities, before returning to

Canberra.

Adelaide Brisbane Canberra Darwin Sydney

Adelaide 2053 1155 3017 1385

Brisbane 2053 1080 3415 939

Canberra 1155 1080 3940 285

Darwin 3017 3415 3940 3975

Sydney 1385 939 285 3975

Before Next Lecture

On the FIT1029 Moodle site: • Watch the following video:

• The 8 Queens Problem

Think about the traveling salesman problem