Swe116 Tutorial Sheet I
Swe116 Tutorial Sheet I
Swe116 Tutorial Sheet I
COLLEGE OF TECHNOLOGY
TUTORIAL SHEET I
PROGRAM: HND
1. a) Write down driving directions for going from your school to your home
with the precision required by an algorithm.
b) Write down a recipe for cooking your favorite dish with the precision
required by an algorithm.
3. Prove that for every pair of positive integers m and n, the equality
gcd(m, n) = gcd(n, m mod n)
Note: This problem requires you to prove the correctness of the Euclid’s algorithm.
4. A farmer finds himself on a riverbank with a wolf, a goat, and a head of cabbage. He needs
to transport all three to the other side of the river in his boat. However, the boat has room
for only the peasant himself and one other item (either the wolf, the goat, or the cabbage).
In his absence, the wolf would eat the goat, and the goat would eat the cabbage.
Solve this problem for the peasant or prove it has no solution.
6. Describe the standard algorithm for finding the binary representation of a positive
decimal integer
a) in English.
b) in a pseudocode.
1
7. Describe the algorithm used by your favorite ATM machine in dispensing cash. (You may
give your description in either English or a pseudocode, whichever you find more
convenient.
8. Describe the algorithm used by a telephone to place a phone call. (You may give your
description in either English or a pseudocode, whichever you find more convenient.)
9. Königsberg bridges The Konigsberg bridge puzzle is universally accepted ¨ as the problem
that gave birth to graph theory. It was solved by the great Swiss-born mathematician
Leonhard Euler (1707–1783). The problem asked whether one could, in a single stroll, cross
all seven bridges of the city of Konigsberg exactly once and return to a starting point.
Following is a sketch ¨ of the river with its two islands and seven bridges:
a) Explain how we can use the graph-coloring problem to color the map so that no two
neighboring regions are colored the same.
b) Use your answer to part (a) to color the map with the smallest number of colors.
11. Consider the algorithm for the sorting problem that sorts an array by counting, for each of
its elements, the number of smaller elements and then uses this information to put the
element in its appropriate position
in the sorted array:
2
//Input: Array A[0..n - 1] of orderable values
//Output: Array S[0..n - 1] of A’s elements sorted in nondecreasing order
for i ← 0 to n - 1 do
Count[i] ← 0
for i ← 0 to n - 2 do
for j ← i + 1 to n - 1 do
if A[i] < A[j]
Count[j] ← Count[j] + 1
else Count[i] ← Count[i] + 1
for i ← 0 to n - 1 do
S[Count[i]] ← A[i]
a) Apply this algorithm to sorting the list 60, 35, 81, 98, 14, 47
b) Is this algorithm stable?
c) Is it in-place?
For further reading, see Section 2.1 Insertion Sort; Introduction to Algorithm.