Lecture 7
Brute Force
FIT 1029
Algorithmic Problem Solving
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.
2
4
12
2
4
12
10
10
2
4
12
8
12
10
12
10
10
10
22
12
10
4
12
10
12
10
12
2
4
2
4
12
10
10
2
4
8
12
10
2
4
8
10
22
Brute Force
(just do it)
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.
Approach
Prints the name associated to a phone number
(if the number exists in the telephone book)
yes
print
name
no
Is there another record?
yes
stop
Sequential Search
!
k = length(L)
yes
return -1
no
yes
L[k] = target?
no
k k +1
return k
k0
k = length(L)
yes
return -1
no
yes
L[k] = target?
no
k k +1
return k
SequentialSearch(target, L)
k0
while (k length(L)) do
{
if (L[k] = target )
{
return k
}
k k+1
}
return -1
SequentialSearch(target, L)
k0
while (k length(L)) do
{
if (L[k] = target )
{
return k
}
k k+1
}
return -1
SequentialSearch(target, L)
k0
while (k length(L)) do
{
if (L[k] = target )
{
return k
}
k k+1
}
return -1
String Matching
is T a substring of S?
A substring of a string S is another string T "that occurs in" S.
Algorithm: StringMatch
Checks if a string T is a substring of S
Does the
substring match
T?
yes
return
true
no
Are we at the
end of S?
no
Move one
character along S
yes
return
false
Strings as Lists
Str
0. a
1. t
2. g
Str = [a, t, g] or Str = atg
Str[1] = t
Str[1..2] = [t, g] = tg
Substrings
Str[0..1] = [a, t] = at
Str = [a, g, t, t, a, c, g, a, t, t, a]
0
Str[2..4] = [t, t, a]
Str[3..5] = [t, a, c]
Str[8 ..10] = [t, t, a]
10
Algorithm: StringMatch(S, T)
Checks if T is a substring of S
k0
while (k + length(T) length(S)) do
{
If (S[k .. k+length(T)-1] = T)
{
return true
}
kk+1
}
return false
T = cagcag
lenght(T) = 6
k=0
k+lenght(T)-1=5
atggctaagtctatgctttctggaattgtttttgctggtcttgttg
ctgctgcagcggccagttcggccaacaacagcgccgc
caacgtctccgttttggagagtgggcccgctgtgcaggaa
gtgccagcgcgcacggtcacagctcgcctggcgaagc
ctttgctgcttcttttctgctcttgctgcgactttggcagcagct
T = cagcag
lenght(T) = 6
k=1
k+lenght(T)-1=6
atggctaagtctatgctttctggaattgtttttgctggtcttgttg
ctgctgcagcggccagttcggccaacaacagcgccgc
caacgtctccgttttggagagtgggcccgctgtgcaggaa
gtgccagcgcgcacggtcacagctcgcctggcgaagc
ctttgctgcttcttttctgctcttgctgcgactttggcagcagct
T = cagcag
lenght(T) = 6
k=2
k+lenght(T)-1=7
atggctaagtctatgctttctggaattgtttttgctggtcttgttg
ctgctgcagcggccagttcggccaacaacagcgccgc
caacgtctccgttttggagagtgggcccgctgtgcaggaa
gtgccagcgcgcacggtcacagctcgcctggcgaagc
ctttgctgcttcttttctgctcttgctgcgactttggcagcagct
T = cagcag
lenght(T) = 6
k=3
k+lenght(T)-1=8
atggctaagtctatgctttctggaattgtttttgctggtcttgttg
ctgctgcagcggccagttcggccaacaacagcgccgc
caacgtctccgttttggagagtgggcccgctgtgcaggaa
gtgccagcgcgcacggtcacagctcgcctggcgaagc
ctttgctgcttcttttctgctcttgctgcgactttggcagcagct
k+ length(T)-1
k0
while (k + length(T) length(S)) do
{
If (S[k .. k+length(T)-1] = T)
{
return true
Check substring
given by k
Str[k..k+lenght(T)-1]
has size length(T)
kk+1
}
return false
k
k+ length(T)-1
k0
while (k + length(T) length(S)) do
{
If (S[k .. k+length(T)-1] = T)
{
return true
}
kk+1
}
return false
k 0
while (k + length(T) length(S)) do
{
j0
k0
while (k + length(T) length(S)) do
{
If (S[k .. k+length(T)-1] = T)
{
return true
}
kk+1
}
return false
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
}
kk+1
}
return false
k+ length(T)-1
j0
match true
while (j < length(T) and match = true) do
{
If (S[j+k] T[j]) {
match false
}
j j+1
j
T
k 0
while (k + length(T) length(S)) do
{
j0
k0
while (k + length(T) length(S)) do
{
If (S[k .. k+length(T)-1] == T)
{
return true
}
kk+1
}
return false
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
}
kk+1
}
return false
A Boat Problem
A 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
farmer
wolf
cabbage
goat
Right of the
river
Left of the
river
Right of the
river
farmer
wolf
cabbage
goat
Left of the
river
farmer
goat
Right of the
river
cabbage
wolf
Left of the
river
farmer
cabbage
Right of the
river
goat
wolf
List States
END
START
Farmer
Wolf
Goat
Cabbage
List States
END
START
Farmer
Wolf
Goat
Cabbage
Representation of States
Farmer
Wolf
Goat
Cabbage
FWGC
FWGc
FWgC
fwGC
FwGc
fWgC
fWgc
fwGc
fwgC
fwgc
!
!
!
!
Representation of States
Farmer
Wolf
Goat
Cabbage
FWGC
FWGc
FWgC
fwGC
FwGc
fWgC
fWgc
fwGc
fwgC
fwgc
!
!
!
!
Representation of States
Farmer
Wolf
Goat
Cabbage
FWGC
FWGc
FWgC
fwGC
FwGc
fWgC
fWgc
fwGc
fwgC
fwgc
!
!
!
!
FWGC
fwgc
fWgC
Representation of States
Farmer
Wolf
Goat
Cabbage
FWGC
FWGc
FWgC
fwGC
FwGc
fWgC
fWgc
fwGc
fwgC
fwgc
!
!
!
!
Final State
Start State
fwGc
fWgc
fwgc
FWGC
fWgC
FWGc
FwGc
FWgC
FwGC
fwGc
fWgc
fwgc
FWGC
fWgC
FWGc
FwGc
FWgC
FwGC
farmer
cabbage
goat
wolf
fwgC
fwGc
fwgc
fWgc
FWGC
fWgC
FWGc
FwGc
FWgC
FwGC
Boat Solution
FwGC
fwgc
FwGc
fwGc
FWGc
fwgC
FWgC
fWgc
fWgC
FWGC
Traveling Salesman
Suppose you are given the following driving distances in
kms between the following capital cities.
Adelaide Brisbane Canberra
Darwin
Sydney
2053
1155
3017
1385
1080
3415
939
3940
285
Adelaide
Brisbane
2053
Canberra
1155
1080
Darwin
3017
3415
3940
Sydney
1385
939
285
3975
3975