algo

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.

Upload: stacy

Post on 17-Jul-2016

214 views

Category:

Documents


0 download

DESCRIPTION

force

TRANSCRIPT

Page 1: algo

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.

Page 2: algo

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

Page 3: algo

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

Page 4: algo

Come up with all possible solutions or states...

Brute Force(just do it)

Page 5: algo

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

Page 6: algo

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

Page 7: algo

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

Page 8: algo

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.

Page 9: algo

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

Page 10: algo

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

Page 11: algo

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

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

} return -1

Page 12: algo

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

Page 13: algo

Is the substring cagcag in this string?

A) Yes

B) No

atggctaagtctatgctttctggaattgtttttgctggtcttgttgctgctgcagcggccagttcggccaacaacagcgccgccaacgtctccgttttggagagtgggcccgctgtgcaggaagtgccagcgcgcacggtcacagctcgcctggcgaagcctttgctgcttcttttctgctcttgctgcgactttggcagcagct

Page 14: algo

String Matching

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

is T a substring of S?

Page 15: algo

atggctaagtctatgctttctggaattgtttttgctggtcttgttgctgctgcagcggccagttcggccaacaacagcgccgccaacgtctccgttttggagagtgggcccgctgtgcaggaagtgccagcgcgcacggtcacagctcgcctggcgaagcctttgctgcttcttttctgctcttgctgcgactttggcagcagct

cagcag

Brute Force String Matching

Page 16: algo

atggctaagtctatgctttctggaattgtttttgctggtcttgttgctgctgcagcggccagttcggccaacaacagcgccgccaacgtctccgttttggagagtgggcccgctgtgcaggaagtgccagcgcgcacggtcacagctcgcctggcgaagcctttgctgcttcttttctgctcttgctgcgactttggcagcagct

cagcag

Brute Force String Matching

Page 17: algo

atggctaagtctatgctttctggaattgtttttgctggtcttgttgctgctgcagcggccagttcggccaacaacagcgccgccaacgtctccgttttggagagtgggcccgctgtgcaggaagtgccagcgcgcacggtcacagctcgcctggcgaagcctttgctgcttcttttctgctcttgctgcgactttggcagcagct

cagcag

Brute Force String Matching

Page 18: algo

atggctaagtctatgctttctggaattgtttttgctggtcttgttgctgctgcagcggccagttcggccaacaacagcgccgccaacgtctccgttttggagagtgggcccgctgtgcaggaagtgccagcgcgcacggtcacagctcgcctggcgaagcctttgctgcttcttttctgctcttgctgcgactttggcagcagct

cagcag

Brute Force String Matching

Page 19: algo

atggctaagtctatgctttctggaattgtttttgctggtcttgttgctgctgcagcggccagttcggccaacaacagcgccgccaacgtctccgttttggagagtgggcccgctgtgcaggaagtgccagcgcgcacggtcacagctcgcctggcgaagcctttgctgcttcttttctgctcttgctgcgactttggcagcagct

cagcag

Brute Force String Matching

Page 20: algo

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

Page 21: algo

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

Page 22: algo

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]

Page 23: algo

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

Page 24: algo

atggctaagtctatgctttctggaattgtttttgctggtcttgttgctgctgcagcggccagttcggccaacaacagcgccgccaacgtctccgttttggagagtgggcccgctgtgcaggaagtgccagcgcgcacggtcacagctcgcctggcgaagcctttgctgcttcttttctgctcttgctgcgactttggcagcagct

T = cagcaglenght(T) = 6

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

Page 25: algo

atggctaagtctatgctttctggaattgtttttgctggtcttgttgctgctgcagcggccagttcggccaacaacagcgccgccaacgtctccgttttggagagtgggcccgctgtgcaggaagtgccagcgcgcacggtcacagctcgcctggcgaagcctttgctgcttcttttctgctcttgctgcgactttggcagcagct

T = cagcaglenght(T) = 6

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

Page 26: algo

atggctaagtctatgctttctggaattgtttttgctggtcttgttgctgctgcagcggccagttcggccaacaacagcgccgccaacgtctccgttttggagagtgggcccgctgtgcaggaagtgccagcgcgcacggtcacagctcgcctggcgaagcctttgctgcttcttttctgctcttgctgcgactttggcagcagct

T = cagcaglenght(T) = 6

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

Page 27: algo

atggctaagtctatgctttctggaattgtttttgctggtcttgttgctgctgcagcggccagttcggccaacaacagcgccgccaacgtctccgttttggagagtgggcccgctgtgcaggaagtgccagcgcgcacggtcacagctcgcctggcgaagcctttgctgcttcttttctgctcttgctgcgactttggcagcagct

T = cagcaglenght(T) = 6

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

Page 28: algo

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

k k+ length(T)-1

Page 29: algo

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

Page 30: algo

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

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

return true } k ← k + 1

} return false

Page 31: algo

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

Page 32: algo

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

Page 33: algo

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

Page 34: algo

Why a,g, t and c?

Page 35: algo

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)

Page 36: algo

Left of the river

Right of the river

farmerwolf

cabbagegoat

Page 37: algo

Left of the river

Right of the river

farmerwolf

cabbagegoat

Page 38: algo

Left of the river

Right of the river

farmergoat cabbage

wolf

Page 39: algo

Left of the river

Right of the river

farmercabbage

wolfgoat

Page 40: algo
Page 41: algo

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

Page 42: algo

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

Page 43: algo

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

Page 44: algo

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

Page 45: algo

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

Page 46: algo

fwgc FWGC

fWgC

Page 47: algo

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

Page 48: algo

Transitions between states

FwGc

fwGc

FwGC

FWGc

fwgCfWgc

FWgC

fWgCFWGC

fwgc

Page 49: algo

Transitions between states

FwGc

fwGc

FwGC

FWGc

fwgCfWgc

FWgC

fWgCFWGC

fwgc

Page 50: algo

FwGc

fwGc

FwGC

FWGc

fwgCfWgc

FWgC

fWgCFWGC

fwgc

farmercabbage

wolf

goat

Page 51: algo

Boat Solution

fwgc FwGc fwGc

FwGC

FWGc

fwgC

fWgc

FWgC fWgC FWGC

Page 52: algo

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

Page 53: algo

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

Page 54: algo

Before Next Lecture

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

• The 8 Queens Problem

Think about the traveling salesman problem