Anda di halaman 1dari 2

# Tutorial 4

## FIT 1029 Algorithmic Problem Solving

20 March 2014 Prepared by Julian Garca
The objectives of this tutorial are:
To understand substrings.
To understand sequential search.
To understand brute force approach.
To learn how to generate subsets and permutations.
To understand how to use lists to represent possible solutions.
How many substrings of the form Str[k, .., j], such that k j are
there for Str = Algorithm.
Write an algorithm
1
that prints out all the substrings of the form
1
The rst part of this exercise is an
instance of the problem solved by the
algorithm you are required to write in
part 2
Str[k, .., j] for any Str.
Write an algorithm that generates all the possible subsets of a set of N elements.
How could you use this algorithm to apply the brute force approach to the Farmer, Wolf, Goat and
Cabbage problem?
Write an algorithm that nds all the possible positions that 4 Queens can be in on a 4 4 Chess board, so
that no two Queens are in the same row or column.
Consider a list of integers from 1 to N, and let each integer have a
direction (either left or right) associated with it. We will denote the
integer k directed leftwards by

## k , and directed rightwards by

k .
Also, we will say that a directed integer is mobile in the list, if it is
greater than its immediate neighbour in the direction it is pointing.
For example, in the list [

3 ,

1 ,

2 ] only 2 is mobile.
tutorial 4. fit 1029 algorithmic problem solving 2
Consider the following algorithm and determine what is printed
2
.
2
Remember the Johnson-Trotter Algo-
rithm, used to generate permutations
and briey discussed in the lecture.
L [

3 ,

1 ,

2 ]
index index of the largest mobile integer in L
print L
while(index = 1) do
{
Swap L[index]with the immediate neighbour it is poiting at.
Reverse the direction on all integers larger than L[index]
print L
index index of the largest mobile integer in L
}
Write down an algorithm to perform the step
3
:
3
Remember the idea of module?
Reverse the direction on all integers larger than L[index]
Consider the following types of items:
Type 1 2 3
Value \$4 \$13 \$11
Weight 3kg 4kg 7kg
Suppose you have a knapsack of capacity 7kg. Write down all the
possible sets of items you could carry, together with their total
value.
Write down an algorithm to print out all possible sets of items and
their total value, for any given knapsack problem
4
.
4
A instance of the knapsack problem
is given by a list of values, a list of
weights, and a maximum knapsack
capacity